| 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 | // Stats (mean and stdev) scoring in the native. |
| 18 | |
| 19 | #include "stats_scorer.h" |
| 20 | |
| 21 | #include <jni.h> |
| 22 | #include <math.h> |
| 23 | |
| 24 | void Java_androidx_media_filterpacks_numeric_StatsFilter_score( |
| 25 | JNIEnv* env, jobject thiz, jobject imageBuffer, jfloatArray statsArray) |
| 26 | { |
| 27 | unsigned char* pImg = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer)); |
| 28 | int numPixels = env->GetDirectBufferCapacity(imageBuffer); // 1 byte per pixel |
| 29 | float sum = 0.0; |
| 30 | float sumSquares = 0.0; |
| 31 | |
| 32 | for (int i = 0; i < numPixels; ++i) { |
| 33 | float val = static_cast<float>(pImg[i]); |
| 34 | sum += val; |
| 35 | sumSquares += val * val; |
| 36 | } |
| 37 | jfloat result[2]; |
| 38 | result[0] = sum / numPixels; // mean |
| 39 | result[1] = sqrt((sumSquares - numPixels * result[0] * result[0]) / (numPixels - 1)); // stdev. |
| 40 | env->SetFloatArrayRegion(statsArray, 0, 2, result); |
| 41 | } |
| 42 | |
| 43 | void Java_androidx_media_filterpacks_numeric_StatsFilter_regionscore( |
| 44 | JNIEnv* env, jobject thiz, jobject imageBuffer, jint width, jint height, |
| 45 | jfloat left, jfloat top, jfloat right, jfloat bottom, jfloatArray statsArray) |
| 46 | { |
| 47 | unsigned char* pImg = static_cast<unsigned char*>(env->GetDirectBufferAddress(imageBuffer)); |
| 48 | int xStart = static_cast<int>(width * left); |
| 49 | int xEnd = static_cast<int>(width * right); |
| 50 | int yStart = static_cast<int>(height * top); |
| 51 | int yEnd = static_cast<int>(height * bottom); |
| 52 | int numPixels = (xEnd - xStart) * (yEnd - yStart); |
| 53 | float sum = 0.0; |
| 54 | float sumSquares = 0.0; |
| 55 | |
| 56 | for (int y = yStart; y < yEnd; y++) { |
| 57 | int disp = width * y; |
| 58 | for (int x = xStart; x < xEnd; ++x) { |
| 59 | float val = static_cast<float>(*(pImg + disp + x)); |
| 60 | sum += val; |
| 61 | sumSquares += val * val; |
| 62 | } |
| 63 | } |
| 64 | jfloat result[2]; |
| 65 | result[0] = sum / numPixels; // mean |
| 66 | result[1] = (numPixels == 1) ? |
| 67 | 0 : sqrt((sumSquares - numPixels * result[0] * result[0]) / (numPixels - 1)); // stdev. |
| 68 | env->SetFloatArrayRegion(statsArray, 0, 2, result); |
| 69 | } |
| 70 |