1 /*
2 * Copyright (C) 2013 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 brightness from image (handed down as ByteBuffer).
18
19 #include "brightness.h"
20
21 #include <math.h>
22 #include <string.h>
23 #include <jni.h>
24 #include <unistd.h>
25 #include <android/log.h>
26
27 jfloat
Java_androidx_media_filterfw_samples_simplecamera_AvgBrightnessFilter_brightnessOperator(JNIEnv * env,jclass clazz,jint width,jint height,jobject imageBuffer)28 Java_androidx_media_filterfw_samples_simplecamera_AvgBrightnessFilter_brightnessOperator(
29 JNIEnv* env, jclass clazz, jint width, jint height, jobject imageBuffer) {
30
31 if (imageBuffer == 0) {
32 return 0.0f;
33 }
34 float pixelTotals[] = { 0.0f, 0.0f, 0.0f };
35 const int numPixels = width * height;
36 unsigned char* srcPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer));
37 for (int i = 0; i < numPixels; i++) {
38 pixelTotals[0] += *(srcPtr + 4 * i);
39 pixelTotals[1] += *(srcPtr + 4 * i + 1);
40 pixelTotals[2] += *(srcPtr + 4 * i + 2);
41 }
42 float avgPixels[] = { 0.0f, 0.0f, 0.0f };
43
44 avgPixels[0] = pixelTotals[0] / numPixels;
45 avgPixels[1] = pixelTotals[1] / numPixels;
46 avgPixels[2] = pixelTotals[2] / numPixels;
47 float returnValue = sqrt(0.241f * avgPixels[0] * avgPixels[0] +
48 0.691f * avgPixels[1] * avgPixels[1] +
49 0.068f * avgPixels[2] * avgPixels[2]);
50
51 return returnValue / 255;
52 }
53