Home | History | Annotate | Download | only in cameraanalyzer
      1 /*
      2  * Copyright (C) 2011 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 #define LOG_NDEBUG 0
     17 
     18 #define LOG_TAG "CameraTestsJNI"
     19 #include <utils/Log.h>
     20 #include "com_android_cts_verifier_camera_analyzer_CameraTests.h"
     21 #include "android/bitmap.h"
     22 #include "testingimage.h"
     23 #include "imagetesthandler.h"
     24 
     25 #include <string.h>
     26 
     27 jlong Java_com_android_cts_verifier_camera_analyzer_CameraTests_findNative(
     28         JNIEnv*      env,
     29         jobject      thiz,
     30         jobject      inputBitmap) {
     31 
     32     ALOGV("JNI findNative starts!");
     33 
     34     // Verify that we can handle the input bitmap
     35     AndroidBitmapInfo inputInfo;
     36     AndroidBitmap_getInfo(env, inputBitmap, &inputInfo);
     37     if (inputInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 &&
     38         inputInfo.format != ANDROID_BITMAP_FORMAT_RGB_565) {
     39         ALOGE("Only RGBA_8888 and RGB_565 bitmaps are supported, type was %d.",
     40              inputInfo.format);
     41     }
     42 
     43     // Get some references to the fields and class type of ColorChecker
     44     jclass thizCls = env->GetObjectClass(thiz);
     45     ALOGV("ColorChecker field and classes reference finished!");
     46 
     47     // Get raw inputs and outputs ready
     48     uint8_t *inputBuffer = NULL;
     49     int result = AndroidBitmap_lockPixels(
     50             env,
     51             inputBitmap,
     52             reinterpret_cast<void**>(&inputBuffer));
     53 
     54     if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
     55         ALOGE("Unable to lock input bitmap");
     56     }
     57 
     58     uint8_t *outputImage = NULL;
     59     int outputWidth, outputHeight;
     60 
     61     ALOGV("Input and output images created!");
     62 
     63     // Find the color checker
     64     bool success;
     65     uint8_t *inputBufferRGBA = NULL;
     66     int inputStride;
     67     bool freeInputRGBA = false;
     68     switch (inputInfo.format) {
     69         case ANDROID_BITMAP_FORMAT_RGB_565: {
     70             // First convert to RGBA_8888
     71             inputBufferRGBA = new uint8_t[inputInfo.width *
     72                                           inputInfo.height *
     73                                           4];
     74             inputStride = inputInfo.width * 4;
     75             uint8_t *outP = inputBufferRGBA;
     76             for (int y = 0; y < inputInfo.height; y++ ) {
     77                 uint16_t *inP = (uint16_t*)(&inputBuffer[y * inputInfo.stride]);
     78                 for (int x = 0; x < inputInfo.width; x++) {
     79                     *(outP++) = ( ((*inP) >> 0) & 0x001F) << 3;
     80                     *(outP++) = ( ((*inP) >> 5) & 0x003F) << 2;
     81                     *(outP++) = ( ((*inP) >> 11) & 0x001F) << 3;
     82                     outP++;
     83                     inP++;
     84                 }
     85             }
     86             freeInputRGBA = true;
     87 
     88             ALOGV("RGB_565 Format with width, height and stride as %d, %d, %d",
     89                  inputInfo.width, inputInfo.height, inputStride);
     90             break;
     91         }
     92         case ANDROID_BITMAP_FORMAT_RGBA_8888: {
     93             // Already in RGBA
     94             inputBufferRGBA = inputBuffer;
     95             inputStride = inputInfo.stride;
     96             ALOGV("RGB_8888 Format with width, height and stride as %d, %d, %d",
     97                  inputInfo.width, inputInfo.height, inputStride);
     98             break;
     99         }
    100     }
    101 
    102     TestingImage *input_testing_image =
    103             new TestingImage(inputBufferRGBA, inputInfo.height, inputInfo.width,
    104                              4, inputStride, 120, 160);
    105 
    106     long lp = (long)input_testing_image;
    107 
    108     result = AndroidBitmap_unlockPixels(env, inputBitmap);
    109     if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
    110         ALOGE("Unable to unlock input bitmap");
    111     }
    112 
    113     if (freeInputRGBA) {
    114         ALOGV("Deleteing inputbufferRGBA");
    115         delete[] inputBufferRGBA;
    116     }
    117 
    118     return lp;
    119     ALOGV("Input format switched!");
    120 }
    121 
    122 jlong Java_com_android_cts_verifier_camera_analyzer_CameraTests_createImageTestHandler(
    123         JNIEnv*      env,
    124         jobject      thiz,
    125         jint         debugHeight,
    126         jint         debugWidth) {
    127 
    128     ImageTestHandler* testHandler =
    129             new ImageTestHandler(debugHeight, debugWidth);
    130     long handlerAddress = (long)testHandler;
    131     return handlerAddress;
    132 }
    133 
    134 void Java_com_android_cts_verifier_camera_analyzer_CameraTests_cleanUpHandler(
    135         JNIEnv*      env,
    136         jobject      thiz,
    137         jlong        inputHandlerAddress) {
    138 
    139     ImageTestHandler* testHandler = (ImageTestHandler*) (long) inputHandlerAddress;
    140     delete testHandler;
    141 }
    142 
    143 void Java_com_android_cts_verifier_camera_analyzer_CameraTests_displayHandlerDebugOutput(
    144         JNIEnv*      env,
    145         jobject      thiz,
    146         jlong        inputHandlerAddress) {
    147 
    148     jclass thizCls = env->GetObjectClass(thiz);
    149     jfieldID outputId = env->GetFieldID(thizCls, "mDebugOutput",
    150                                         "Landroid/graphics/Bitmap;");
    151 
    152     ImageTestHandler* testHandler = (ImageTestHandler*) (long) inputHandlerAddress;
    153     uint8_t *outputImage =  new uint8_t[testHandler->getDebugHeight() *
    154                                         testHandler->getDebugWidth() * 4];
    155 
    156     const unsigned char *debugoutput = testHandler->debug_output();
    157     memcpy(outputImage, debugoutput, testHandler->getDebugHeight() *
    158             testHandler->getDebugWidth() * 4);
    159 
    160     int outputWidth = testHandler->getDebugWidth();
    161     int outputHeight = testHandler->getDebugHeight();
    162     bool success = false;
    163 
    164     if (outputImage == NULL) {
    165         ALOGV("output Image is null!");
    166     } else {
    167         ALOGV("output Image is ready!");
    168     }
    169 
    170     // Create debug bitmap from output image data
    171     if (outputImage != NULL) {
    172         // Get method handles, create inputs to createBitmap
    173         jclass bitmapClass =
    174                 env->FindClass("android/graphics/Bitmap");
    175         jclass bitmapConfigClass =
    176                 env->FindClass("android/graphics/Bitmap$Config");
    177 
    178         jmethodID createBitmap = env->GetStaticMethodID(
    179             bitmapClass, "createBitmap",
    180             "(IILandroid/graphics/Bitmap$Config;)Landroid/graphics/Bitmap;");
    181 
    182         jmethodID getConfig = env->GetStaticMethodID(
    183             bitmapConfigClass, "valueOf",
    184             "(Ljava/lang/String;)Landroid/graphics/Bitmap$Config;");
    185 
    186         // Create bitmap config and bitmap
    187         jstring bitmapConfigValue = env->NewStringUTF("ARGB_8888");
    188         jobject rgbaConfig = env->CallStaticObjectMethod(bitmapConfigClass,
    189                                                          getConfig,
    190                                                          bitmapConfigValue);
    191         jobject outputBitmap = env->CallStaticObjectMethod(bitmapClass,
    192                                                            createBitmap,
    193                                                            outputWidth,
    194                                                            outputHeight,
    195                                                            rgbaConfig);
    196         // Copy output image into it
    197         uint8_t *outputBuffer;
    198         int result = AndroidBitmap_lockPixels(
    199                 env,
    200                 outputBitmap,
    201                 reinterpret_cast<void**>(&outputBuffer) );
    202 
    203         if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
    204             ALOGE("Unable to lock output bitmap");
    205         }
    206 
    207         memcpy(outputBuffer, outputImage, outputWidth * outputHeight * 4);
    208 
    209         result = AndroidBitmap_unlockPixels(env, outputBitmap);
    210         if (result != ANDROID_BITMAP_RESULT_SUCCESS) {
    211             ALOGE("Unable to unlock output bitmap");
    212         }
    213 
    214         // Write new Bitmap reference into mDebugOutput class member
    215         env->SetObjectField(thiz, outputId, outputBitmap);
    216         ALOGV("Copied to outputBitmap");
    217         delete [] outputImage;
    218         env->DeleteLocalRef(outputBitmap);
    219         env->DeleteLocalRef(rgbaConfig);
    220         env->DeleteLocalRef(bitmapClass);
    221         env->DeleteLocalRef(bitmapConfigClass);
    222     }
    223 }
    224