1d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford/*
2d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * Copyright (C) 2012 The Android Open Source Project
3d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *
4d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * Licensed under the Apache License, Version 2.0 (the "License");
5d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * you may not use this file except in compliance with the License.
6d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * You may obtain a copy of the License at
7d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *
8d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *      http://www.apache.org/licenses/LICENSE-2.0
9d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford *
10d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * Unless required by applicable law or agreed to in writing, software
11d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * distributed under the License is distributed on an "AS IS" BASIS,
12d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * See the License for the specific language governing permissions and
14d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford * limitations under the License.
15d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford */
16d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
17d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford#include "filters.h"
18d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
19d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hofordvoid JNIFUNCF(ImageFilterHue, nativeApplyFilter, jobject bitmap, jint width, jint height, jfloatArray matrix)
20d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford{
21d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    char* destination = 0;
22d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    AndroidBitmap_lockPixels(env, bitmap, (void**) &destination);
23d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    unsigned char * rgb = (unsigned char * )destination;
24d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    int i;
25d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    int len = width * height * 4;
26d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    jfloat* mat = (*env)->GetFloatArrayElements(env, matrix,0);
27d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
28d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    for (i = 0; i < len; i+=4)
29d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    {
30d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      int r = rgb[RED];
31d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      int g = rgb[GREEN];
32d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      int b = rgb[BLUE];
33d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
34d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      float rf = r*mat[0] + g*mat[4] +  b*mat[8] + mat[12];
35d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      float gf = r*mat[1] + g*mat[5] +  b*mat[9] + mat[13];
36d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      float bf = r*mat[2] + g*mat[6] +  b*mat[10] + mat[14];
37d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
38d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      rgb[RED]   = clamp((int)rf);
39d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      rgb[GREEN] = clamp((int)gf);
40d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford      rgb[BLUE]  = clamp((int)bf);
41d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    }
42d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
43d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    (*env)->ReleaseFloatArrayElements(env, matrix, mat, 0);
44d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford    AndroidBitmap_unlockPixels(env, bitmap);
45d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford}
46d42f6c69c5980110c16cd679f914c4e4e7caa29dJohn Hoford
47