summaryrefslogtreecommitdiffstats
path: root/jni/image_util_jni.cpp
blob: 2297f9164702a331f221be5b4af9181cebd53283 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright (c) 2016, The Linux Foundation. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above
      copyright notice, this list of conditions and the following
      disclaimer in the documentation and/or other materials provided
      with the distribution.
    * Neither the name of The Linux Foundation nor the names of its
      contributors may be used to endorse or promote products derived
      from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <jni.h>
#include <assert.h>
#include <stdlib.h>

#ifdef __ANDROID__
#include "android/log.h"
#define printf(...) __android_log_print( ANDROID_LOG_ERROR, "ImageUtil", __VA_ARGS__ )
#endif

#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_com_android_camera_imageprocessor_FrameProcessor_nativeRotateNV21
        (JNIEnv* env, jobject thiz, jobjectArray inBuf,
         jint imageWidth, jint imageHeight, jint degree, jobjectArray outBuf);
JNIEXPORT jint JNICALL Java_com_android_camera_imageprocessor_FrameProcessor_nativeNV21toRgb(
        JNIEnv *env, jobject thiz, jobjectArray yvuBuf, jobjectArray rgbBuf, jint width, jint height);
#ifdef __cplusplus
}
#endif

typedef unsigned char uint8_t;

void rotateBufAndMerge(uint8_t *in_buf, jint imageWidth, jint imageHeight, jint degree, uint8_t *out_buf)
{
    if(degree == 90) {
        int i = 0;
        for (int x = 0; x < imageWidth; x++) {
            for (int y = imageHeight - 1; y >= 0; y--) {
                int offset = y * imageWidth + x;
                out_buf[i] = in_buf[offset];
                i++;
            }
        }
        i = imageWidth * imageHeight;
        for (int x = 0; x < imageWidth; x += 2) {
            for (int y = imageHeight / 2 - 1; y >= 0; y--) {
                int offset = imageWidth*imageHeight + y * imageWidth + x;
                out_buf[i] = in_buf[offset];
                i++;
                out_buf[i] = in_buf[offset + 1];
                i++;
            }
        }
    } else if(degree == 270) {
        int i = 0;
        for (int x = imageWidth - 1; x >= 0; x--) {
            for (int y = 0; y < imageHeight; y++) {
                int offset = y * imageWidth + x;
                out_buf[i] = in_buf[offset];
                i++;
            }
        }
        i = imageWidth * imageHeight;
        for (int x = imageWidth - 2; x >= 0; x-=2) {
            for (int y = 0; y < imageHeight/2; y++) {
                int offset = imageWidth*imageHeight + y * imageWidth + x;
                out_buf[i] = in_buf[offset];
                i++;
                out_buf[i] = in_buf[offset + 1];
                i++;
            }
        }
    } else if(degree == 180) {
        int i = 0;
        for (int y = imageHeight - 1; y >= 0; y--) {
            for (int x = imageWidth - 1; x >= 0 ; x--) {
                int offset = y * imageWidth + x;
                out_buf[i] = in_buf[offset];
                i++;
            }
        }
        i = imageWidth * imageHeight;
        for (int y = imageHeight/2 - 1; y >= 0; y--) {
            for (int x = imageWidth - 2; x >= 0 ; x-=2) {
                int offset = imageWidth*imageHeight + y * imageWidth + x;
                out_buf[i] = in_buf[offset];
                i++;
                out_buf[i] = in_buf[offset + 1];
                i++;
            }
        }
    }
}

jint JNICALL Java_com_android_camera_imageprocessor_FrameProcessor_nativeRotateNV21(
        JNIEnv* env, jobject thiz, jobjectArray inBuf,
        jint imageWidth, jint imageHeight, jint degree, jobjectArray outBuf)
{
    uint8_t *in_buf = (uint8_t *)env->GetDirectBufferAddress(inBuf);
    uint8_t *out_buf = (uint8_t *)env->GetDirectBufferAddress(outBuf);
    rotateBufAndMerge(in_buf, imageWidth, imageHeight, degree, out_buf);

    return 0;
}

jint JNICALL Java_com_android_camera_imageprocessor_FrameProcessor_nativeNV21toRgb(
        JNIEnv* env, jobject thiz, jobjectArray yvuBuf, jobjectArray rgbBuf, jint width, jint height)
{
    uint8_t *in_buf = (uint8_t *)env->GetDirectBufferAddress(yvuBuf);
    uint8_t *rgb_buf = (uint8_t *)env->GetDirectBufferAddress(rgbBuf);
    int ysize = width * height;
    int y_value;
    int i, v, u, r, g, b;
    for(int x=0; x < width; x++) {
        for(int y=0; y < height; y++) {
            y_value = (in_buf[y*width+x] & 0xFF);
            i = ysize + (x/2*2) + ((y/2) * width);
            v = (in_buf[i] & 0xFF) - 128;
            u = (in_buf[i + 1] & 0xFF) - 128;
            r = (int)(1.164f * y_value + 1.596f * v);
            g = (int)(1.164f * y_value - 0.813f * v - 0.391f * u);
            b = (int)(1.164f * y_value + 2.018f * u);
            r = r > 255 ? 255 : r < 0 ? 0 : r;
            g = g > 255 ? 255 : g < 0 ? 0 : g;
            b = b > 255 ? 255 : b < 0 ? 0 : b;
            rgb_buf[(y*width + x) * 4 + 3] = (uint8_t)(0xFF);
            rgb_buf[(y*width + x) * 4 + 2] = (uint8_t)(b & 0xFF);
            rgb_buf[(y*width + x) * 4 + 1] = (uint8_t)(g & 0xFF);
            rgb_buf[(y*width + x) * 4 + 0] = (uint8_t)(r & 0xFF);
        }
    }
    return 0;
}