1 /*
2  * Copyright (C) 2014 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 #include <jni.h>
18 #include <math.h>
19 #include <android/bitmap.h>
20 
21 #include "jpegutil.h"
22 
23 /**
24  * @param env the JNI environment
25  * @param yBuf the buffer containing the Y component of the image
26  * @param yPStride the stride between adjacent pixels in the same row in yBuf
27  * @param yRStride the stride between adjacent rows in yBuf
28  * @param cbBuf the buffer containing the Cb component of the image
29  * @param cbPStride the stride between adjacent pixels in the same row in cbBuf
30  * @param cbRStride the stride between adjacent rows in cbBuf
31  * @param crBuf the buffer containing the Cr component of the image
32  * @param crPStride the stride between adjacent pixels in the same row in crBuf
33  * @param crRStride the stride between adjacent rows in crBuf
34  */
35 extern "C" JNIEXPORT jint JNICALL
Java_com_android_camera_util_JpegUtilNative_compressJpegFromYUV420pNative(JNIEnv * env,jclass clazz,jint width,jint height,jobject yBuf,jint yPStride,jint yRStride,jobject cbBuf,jint cbPStride,jint cbRStride,jobject crBuf,jint crPStride,jint crRStride,jobject outBuf,jint outBufCapacity,jint quality)36     Java_com_android_camera_util_JpegUtilNative_compressJpegFromYUV420pNative(
37         JNIEnv* env, jclass clazz, jint width, jint height, jobject yBuf,
38         jint yPStride, jint yRStride, jobject cbBuf, jint cbPStride,
39         jint cbRStride, jobject crBuf, jint crPStride, jint crRStride,
40         jobject outBuf, jint outBufCapacity, jint quality) {
41   jbyte* y = (jbyte*)env->GetDirectBufferAddress(yBuf);
42   jbyte* cb = (jbyte*)env->GetDirectBufferAddress(cbBuf);
43   jbyte* cr = (jbyte*)env->GetDirectBufferAddress(crBuf);
44   jbyte* out = (jbyte*)env->GetDirectBufferAddress(outBuf);
45 
46   jpegutil::Plane yP(width, height, width, height, (unsigned char*)y, yPStride,
47                      yRStride);
48   jpegutil::Plane cbP(width, height, width / 2, height / 2, (unsigned char*)cb,
49                       cbPStride, cbRStride);
50   jpegutil::Plane crP(width, height, width / 2, height / 2, (unsigned char*)cr,
51                       crPStride, crRStride);
52 
53   auto flush = [](size_t numBytes) {
54     // do nothing
55   };
56 
57   return jpegutil::compress(yP, cbP, crP, (unsigned char*)out, outBufCapacity,
58                             flush, quality);
59 }
60