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 exposure from image (handed down as ByteBuffer).
18
19#include "exposure.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
28jfloat
29Java_androidx_media_filterfw_samples_simplecamera_ExposureFilter_overExposureOperator(
30    JNIEnv* env, jclass clazz, jint width, jint height, jobject imageBuffer) {
31    if (imageBuffer == 0) {
32        return 0.0f;
33    }
34    const int numPixels = width * height;
35    unsigned char* srcPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer));
36    int output = 0;
37    float tempLuminance = 0.0f;
38
39    for (int i = 0; i < numPixels; i++) {
40        tempLuminance = (0.2126f * *(srcPtr + 4 * i) +
41                        0.7152f * *(srcPtr + 4 * i + 1) +
42                        0.0722f * *(srcPtr + 4 * i + 2));
43        if (tempLuminance + 5 >= 255) {
44            output++;
45        }
46    }
47    return (static_cast<float>(output)) / numPixels;
48}
49
50jfloat
51Java_androidx_media_filterfw_samples_simplecamera_ExposureFilter_underExposureOperator(
52    JNIEnv* env, jclass clazz, jint width, jint height, jobject imageBuffer) {
53    if (imageBuffer == 0) {
54        return 0.0f;
55    }
56    const int numPixels = width * height;
57    unsigned char* srcPtr = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer));
58    int output = 0;
59    float tempLuminance = 0.0f;
60
61    for (int i = 0; i < numPixels; i++) {
62        tempLuminance = (0.2126f * *(srcPtr + 4 * i) +
63                        0.7152f * *(srcPtr + 4 * i + 1) +
64                        0.0722f * *(srcPtr + 4 * i + 2));
65        if (tempLuminance - 5 <= 0) {
66            output++;
67        }
68    }
69    return (static_cast<float>(output)) / numPixels;
70}
71