1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "ITS-StatsImage-JNI"
18 // #define LOG_NDEBUG 0
19 #include <android/log.h>
20 
21 #include <jni.h>
22 #include <stdio.h>
23 #include <sys/types.h>
24 #include <sys/stat.h>
25 #include <inttypes.h>
26 #include <string.h>
27 
com_android_cts_verifier_camera_its_computeStatsImage(JNIEnv * env,jobject thiz,jbyteArray img,jint width,jint height,jint aax,jint aay,jint aaw,jint aah,jint gridWidth,jint gridHeight)28 jfloatArray com_android_cts_verifier_camera_its_computeStatsImage(JNIEnv* env, jobject thiz,
29         // The full pixel array read off the sensor.
30         jbyteArray img,
31         jint width, jint height,
32         // The active array crop region.
33         jint aax, jint aay, jint aaw, jint aah,
34         // The size of each grid cell to use when computing stats from the active array.
35         jint gridWidth, jint gridHeight)
36 {
37     int bufSize = (int)(env->GetArrayLength(img));
38     unsigned char *buf = (unsigned char*)env->GetByteArrayElements(img, /*is_copy*/NULL);
39 
40     // Size of the full raw image pixel array.
41     const int paw = width;
42     const int pah = height;
43     // Size of each grid cell.
44     const int gw = gridWidth;
45     const int gh = gridHeight;
46     // Number of grid cells (rounding down to full cells only at right+bottom edges).
47     const int ngx = aaw / gw;
48     const int ngy = aah / gh;
49 
50     float *mean = new float[ngy*ngx*4];
51     float *var = new float[ngy*ngx*4];
52     // For each grid cell ...
53     for (int gy = 0; gy < ngy; gy++) {
54         for (int gx = 0; gx < ngx; gx++) {
55             float sum[4] = {0};
56             float sumSq[4] = {0};
57             int count[4] = {0};
58             // Iterate over pixels in the grid cell
59             for (int y = aay+gy*gh; y < aay+(gy+1)*gh; y++) {
60                 int chnOffset = (y & 0x1) * 2;
61                 unsigned char *pbuf = buf + 2*y*paw + 2*gx*gw + 2*aax;
62                 for (int x = aax+gx*gw; x < aax+(gx+1)*gw; x++) {
63                     // input is RAW16
64                     int byte0 = *pbuf++;
65                     int byte1 = *pbuf++;
66                     int pixelValue = (byte1 << 8) | byte0;
67                     int ch = chnOffset + (x & 1);
68                     sum[ch] += pixelValue;
69                     sumSq[ch] += pixelValue * pixelValue;
70                     count[ch] += 1;
71                 }
72             }
73             for (int ch = 0; ch < 4; ch++) {
74                 float m = (float)sum[ch] / count[ch];
75                 float mSq = (float)sumSq[ch] / count[ch];
76                 mean[gy*ngx*4 + gx*4 + ch] = m;
77                 var[gy*ngx*4 + gx*4 + ch] = mSq - m*m;
78             }
79         }
80     }
81 
82     jfloatArray ret = env->NewFloatArray(ngx*ngy*4*2);
83     env->SetFloatArrayRegion(ret, 0, ngx*ngy*4, (float*)mean);
84     env->SetFloatArrayRegion(ret, ngx*ngy*4, ngx*ngy*4, (float*)var);
85     delete [] mean;
86     delete [] var;
87     return ret;
88 }
89 
90 static JNINativeMethod gMethods[] = {
91     {  "computeStatsImage", "([BIIIIIIII)[F",
92             (void *) com_android_cts_verifier_camera_its_computeStatsImage  },
93 };
94 
register_com_android_cts_verifier_camera_its_StatsImage(JNIEnv * env)95 int register_com_android_cts_verifier_camera_its_StatsImage(JNIEnv* env)
96 {
97     jclass clazz = env->FindClass("com/android/cts/verifier/camera/its/StatsImage");
98 
99     return env->RegisterNatives(clazz, gMethods,
100             sizeof(gMethods) / sizeof(JNINativeMethod));
101 }
102