Skip to content

Instantly share code, notes, and snippets.

@vetional
Forked from anonymous/percent.c
Last active August 24, 2016 16:00
Show Gist options
  • Save vetional/2962ffc4e4a81a903c32dad34c72eed4 to your computer and use it in GitHub Desktop.
Save vetional/2962ffc4e4a81a903c32dad34c72eed4 to your computer and use it in GitHub Desktop.
#ifdef __ANDROID__
#include "jni.h"
#include "android/log.h"
#define LOG_TAG "psx"
#define PRINTMSGS(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#endif
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
JNIEXPORT jfloat JNICALL
Java_com_mofirst_launcher_Launcher_getPercentage(JNIEnv *env, jclass obj, jintArray arr, jint w, jint h){
jsize len = (*env)->GetArrayLength(env, arr);
int i = 0, colorPixel = 0, right_corner_pixel =0, left_corner_pixel = 0;
float percentage = 0.0f;
clock_t start, end;
int left = 100, right = 0, top = 100, bottom = 0;
start = clock();
jint *body = (*env)->GetIntArrayElements(env, arr, 0);
for (i=0; i<len; i++) {
if((body[i] >> 24) != 0){
colorPixel++;
if(top >= i/h) top = i/h;
if(bottom <= i/h) bottom = i/h;
if(left >= i%h) left=i%h;
if(right <= i%h) right=i%h;
}
}
int shape = 0;
int dy = abs(bottom-top);
int dx = abs(right-left);
//PRINTMSGS("%d,%d \n", dy,dx);
float ratio = (float)dy / dx;
float dxy = dy*dx;
if( ratio > 0.9f && ratio < 1.1f) shape = 100000;
percentage = (float)colorPixel/dxy;
(*env)->ReleaseIntArrayElements(env, arr, body, 0);
end = clock();
int boxWeight = floor((dxy / (w * h))* 100) * 100;
//PRINTMSGS("boxWegight: %d, percentage: %f, shape:%d \n", boxWeight, percentage, shape);
return (float)percentage + shape + boxWeight;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment