1 /*
2  * Copyright (C) 2012 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 // Native function to extract histogram from image (handed down as ByteBuffer).
18 
19 #include "frametovalues.h"
20 
21 #include <string.h>
22 #include <jni.h>
23 #include <unistd.h>
24 #include <android/log.h>
25 
26 #include "imgprocutil.h"
27 
Java_androidx_media_filterpacks_image_ToGrayValuesFilter_toGrayValues(JNIEnv * env,jclass clazz,jobject imageBuffer,jobject grayBuffer)28 jboolean Java_androidx_media_filterpacks_image_ToGrayValuesFilter_toGrayValues(
29     JNIEnv* env, jclass clazz, jobject imageBuffer, jobject grayBuffer )
30 {
31     unsigned char* pixelPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer));
32     unsigned char* grayPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(grayBuffer));
33 
34     if (pixelPtr == 0 || grayPtr == 0) {
35       return JNI_FALSE;
36     }
37 
38     int numPixels  = env->GetDirectBufferCapacity(imageBuffer) / 4;
39 
40     // TODO: the current implementation is focused on the correctness not performance.
41     // If performance becomes an issue, it is better to increment pixelPtr directly.
42     int disp = 0;
43     for(int idx = 0; idx < numPixels; idx++, disp+=4) {
44       int R = *(pixelPtr + disp);
45       int G = *(pixelPtr + disp + 1);
46       int B = *(pixelPtr + disp + 2);
47       int gray = getIntensityFast(R, G, B);
48       *(grayPtr+idx) = static_cast<unsigned char>(gray);
49     }
50 
51     return JNI_TRUE;
52 }
53 
Java_androidx_media_filterpacks_image_ToRgbValuesFilter_toRgbValues(JNIEnv * env,jclass clazz,jobject imageBuffer,jobject rgbBuffer)54 jboolean Java_androidx_media_filterpacks_image_ToRgbValuesFilter_toRgbValues(
55     JNIEnv* env, jclass clazz, jobject imageBuffer, jobject rgbBuffer )
56 {
57     unsigned char* pixelPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer));
58     unsigned char* rgbPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(rgbBuffer));
59 
60     if (pixelPtr == 0 || rgbPtr == 0) {
61       return JNI_FALSE;
62     }
63 
64     int numPixels  = env->GetDirectBufferCapacity(imageBuffer) / 4;
65 
66     // TODO: this code could be revised to improve the performance as the TODO above.
67     int pixelDisp = 0;
68     int rgbDisp = 0;
69     for(int idx = 0; idx < numPixels; idx++, pixelDisp += 4, rgbDisp += 3) {
70       for (int c = 0; c < 3; ++c) {
71         *(rgbPtr + rgbDisp + c) = *(pixelPtr + pixelDisp + c);
72       }
73     }
74     return JNI_TRUE;
75 }
76 
77