18921c28c7333ad2b4d34f013904ad4737044f366John Hoford/*
28921c28c7333ad2b4d34f013904ad4737044f366John Hoford * Copyright (C) 2013 The Android Open Source Project
38921c28c7333ad2b4d34f013904ad4737044f366John Hoford *
48921c28c7333ad2b4d34f013904ad4737044f366John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
58921c28c7333ad2b4d34f013904ad4737044f366John Hoford * you may not use this file except in compliance with the License.
68921c28c7333ad2b4d34f013904ad4737044f366John Hoford * You may obtain a copy of the License at
78921c28c7333ad2b4d34f013904ad4737044f366John Hoford *
88921c28c7333ad2b4d34f013904ad4737044f366John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
98921c28c7333ad2b4d34f013904ad4737044f366John Hoford *
108921c28c7333ad2b4d34f013904ad4737044f366John Hoford * Unless required by applicable law or agreed to in writing, software
118921c28c7333ad2b4d34f013904ad4737044f366John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
128921c28c7333ad2b4d34f013904ad4737044f366John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138921c28c7333ad2b4d34f013904ad4737044f366John Hoford * See the License for the specific language governing permissions and
148921c28c7333ad2b4d34f013904ad4737044f366John Hoford * limitations under the License.
158921c28c7333ad2b4d34f013904ad4737044f366John Hoford */
168921c28c7333ad2b4d34f013904ad4737044f366John Hoford
178921c28c7333ad2b4d34f013904ad4737044f366John Hoford#include <math.h>
188921c28c7333ad2b4d34f013904ad4737044f366John Hoford#include "filters.h"
198921c28c7333ad2b4d34f013904ad4737044f366John Hoford
208921c28c7333ad2b4d34f013904ad4737044f366John Hofordvoid JNIFUNCF(ImageFilterHighlights, nativeApplyFilter, jobject bitmap,
218921c28c7333ad2b4d34f013904ad4737044f366John Hoford              jint width, jint height, jfloatArray luminanceMap){
228921c28c7333ad2b4d34f013904ad4737044f366John Hoford    char* destination = 0;
238921c28c7333ad2b4d34f013904ad4737044f366John Hoford    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
248921c28c7333ad2b4d34f013904ad4737044f366John Hoford    unsigned char * rgb = (unsigned char * )destination;
258921c28c7333ad2b4d34f013904ad4737044f366John Hoford    int i;
268921c28c7333ad2b4d34f013904ad4737044f366John Hoford    int len = width * height * 4;
278921c28c7333ad2b4d34f013904ad4737044f366John Hoford    jfloat* lum = (*env)->GetFloatArrayElements(env, luminanceMap,0);
288921c28c7333ad2b4d34f013904ad4737044f366John Hoford    unsigned short * hsv = (unsigned short *)malloc(3*sizeof(short));
298921c28c7333ad2b4d34f013904ad4737044f366John Hoford
308921c28c7333ad2b4d34f013904ad4737044f366John Hoford    for (i = 0; i < len; i+=4)
318921c28c7333ad2b4d34f013904ad4737044f366John Hoford    {
328921c28c7333ad2b4d34f013904ad4737044f366John Hoford        rgb2hsv(rgb,i,hsv,0);
338921c28c7333ad2b4d34f013904ad4737044f366John Hoford        int v = clampMax(hsv[0],4080);
348921c28c7333ad2b4d34f013904ad4737044f366John Hoford        hsv[0] = (unsigned short) clampMax(lum[((255*v)/4080)]*4080,4080);
358921c28c7333ad2b4d34f013904ad4737044f366John Hoford        hsv2rgb(hsv,0, rgb,i);
368921c28c7333ad2b4d34f013904ad4737044f366John Hoford    }
378921c28c7333ad2b4d34f013904ad4737044f366John Hoford
388921c28c7333ad2b4d34f013904ad4737044f366John Hoford    free(hsv);
398921c28c7333ad2b4d34f013904ad4737044f366John Hoford    AndroidBitmap_unlockPixels(env, bitmap);
408921c28c7333ad2b4d34f013904ad4737044f366John Hoford}
41