Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 1 | #undef LOG_TAG |
| 2 | #define LOG_TAG "YuvToJpegEncoder" |
| 3 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 4 | #include "CreateJavaOutputStreamAdaptor.h" |
Matt Sarett | 79ad7be | 2016-03-25 14:28:40 -0400 | [diff] [blame] | 5 | #include "SkJPEGWriteUtility.h" |
Kevin Lubick | 1175dc0 | 2022-02-28 12:41:27 -0500 | [diff] [blame] | 6 | #include "SkStream.h" |
| 7 | #include "SkTypes.h" |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 8 | #include "YuvToJpegEncoder.h" |
Mathias Agopian | 8f2423e | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 9 | #include <ui/PixelFormat.h> |
| 10 | #include <hardware/hardware.h> |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 11 | |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 12 | #include "graphics_jni_helpers.h" |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 13 | |
Kevin Lubick | ba25360 | 2022-08-09 21:13:39 +0000 | [diff] [blame] | 14 | #include <csetjmp> |
| 15 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 16 | YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) { |
Mathias Agopian | a696f5d | 2010-02-17 17:53:09 -0800 | [diff] [blame] | 17 | // Only ImageFormat.NV21 and ImageFormat.YUY2 are supported |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 18 | // for now. |
Mathias Agopian | a696f5d | 2010-02-17 17:53:09 -0800 | [diff] [blame] | 19 | if (format == HAL_PIXEL_FORMAT_YCrCb_420_SP) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 20 | return new Yuv420SpToJpegEncoder(strides); |
Mathias Agopian | 8f2423e | 2010-02-16 17:33:37 -0800 | [diff] [blame] | 21 | } else if (format == HAL_PIXEL_FORMAT_YCbCr_422_I) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 22 | return new Yuv422IToJpegEncoder(strides); |
| 23 | } else { |
| 24 | return NULL; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | YuvToJpegEncoder::YuvToJpegEncoder(int* strides) : fStrides(strides) { |
| 29 | } |
| 30 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 31 | struct ErrorMgr { |
| 32 | struct jpeg_error_mgr pub; |
| 33 | jmp_buf jmp; |
| 34 | }; |
| 35 | |
| 36 | void error_exit(j_common_ptr cinfo) { |
| 37 | ErrorMgr* err = (ErrorMgr*) cinfo->err; |
| 38 | (*cinfo->err->output_message) (cinfo); |
| 39 | longjmp(err->jmp, 1); |
| 40 | } |
| 41 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 42 | bool YuvToJpegEncoder::encode(SkWStream* stream, void* inYuv, int width, |
| 43 | int height, int* offsets, int jpegQuality) { |
| 44 | jpeg_compress_struct cinfo; |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 45 | ErrorMgr err; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 46 | skjpeg_destination_mgr sk_wstream(stream); |
| 47 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 48 | cinfo.err = jpeg_std_error(&err.pub); |
| 49 | err.pub.error_exit = error_exit; |
| 50 | |
| 51 | if (setjmp(err.jmp)) { |
| 52 | jpeg_destroy_compress(&cinfo); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | jpeg_create_compress(&cinfo); |
| 56 | |
| 57 | cinfo.dest = &sk_wstream; |
| 58 | |
| 59 | setJpegCompressStruct(&cinfo, width, height, jpegQuality); |
| 60 | |
| 61 | jpeg_start_compress(&cinfo, TRUE); |
| 62 | |
| 63 | compress(&cinfo, (uint8_t*) inYuv, offsets); |
| 64 | |
| 65 | jpeg_finish_compress(&cinfo); |
| 66 | |
Leon Scroggins III | 1c3ded39 | 2018-02-28 13:23:32 -0500 | [diff] [blame] | 67 | jpeg_destroy_compress(&cinfo); |
| 68 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
| 72 | void YuvToJpegEncoder::setJpegCompressStruct(jpeg_compress_struct* cinfo, |
| 73 | int width, int height, int quality) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 74 | cinfo->image_width = width; |
| 75 | cinfo->image_height = height; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 76 | cinfo->input_components = 3; |
| 77 | cinfo->in_color_space = JCS_YCbCr; |
| 78 | jpeg_set_defaults(cinfo); |
Chia-chi Yeh | aa86859 | 2010-03-10 17:08:58 +0800 | [diff] [blame] | 79 | |
| 80 | jpeg_set_quality(cinfo, quality, TRUE); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 81 | jpeg_set_colorspace(cinfo, JCS_YCbCr); |
| 82 | cinfo->raw_data_in = TRUE; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 83 | cinfo->dct_method = JDCT_IFAST; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 84 | configSamplingFactors(cinfo); |
| 85 | } |
| 86 | |
| 87 | /////////////////////////////////////////////////////////////////// |
| 88 | Yuv420SpToJpegEncoder::Yuv420SpToJpegEncoder(int* strides) : |
| 89 | YuvToJpegEncoder(strides) { |
| 90 | fNumPlanes = 2; |
| 91 | } |
| 92 | |
| 93 | void Yuv420SpToJpegEncoder::compress(jpeg_compress_struct* cinfo, |
| 94 | uint8_t* yuv, int* offsets) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 95 | ALOGD("onFlyCompress"); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 96 | JSAMPROW y[16]; |
| 97 | JSAMPROW cb[8]; |
| 98 | JSAMPROW cr[8]; |
| 99 | JSAMPARRAY planes[3]; |
| 100 | planes[0] = y; |
| 101 | planes[1] = cb; |
| 102 | planes[2] = cr; |
| 103 | |
| 104 | int width = cinfo->image_width; |
| 105 | int height = cinfo->image_height; |
| 106 | uint8_t* yPlanar = yuv + offsets[0]; |
| 107 | uint8_t* vuPlanar = yuv + offsets[1]; //width * height; |
| 108 | uint8_t* uRows = new uint8_t [8 * (width >> 1)]; |
| 109 | uint8_t* vRows = new uint8_t [8 * (width >> 1)]; |
| 110 | |
| 111 | |
| 112 | // process 16 lines of Y and 8 lines of U/V each time. |
| 113 | while (cinfo->next_scanline < cinfo->image_height) { |
| 114 | //deitnerleave u and v |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 115 | deinterleave(vuPlanar, uRows, vRows, cinfo->next_scanline, width, height); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 116 | |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 117 | // Jpeg library ignores the rows whose indices are greater than height. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 118 | for (int i = 0; i < 16; i++) { |
| 119 | // y row |
| 120 | y[i] = yPlanar + (cinfo->next_scanline + i) * fStrides[0]; |
| 121 | |
| 122 | // construct u row and v row |
| 123 | if ((i & 1) == 0) { |
| 124 | // height and width are both halved because of downsampling |
| 125 | int offset = (i >> 1) * (width >> 1); |
| 126 | cb[i/2] = uRows + offset; |
| 127 | cr[i/2] = vRows + offset; |
| 128 | } |
| 129 | } |
| 130 | jpeg_write_raw_data(cinfo, planes, 16); |
| 131 | } |
| 132 | delete [] uRows; |
| 133 | delete [] vRows; |
| 134 | |
| 135 | } |
| 136 | |
| 137 | void Yuv420SpToJpegEncoder::deinterleave(uint8_t* vuPlanar, uint8_t* uRows, |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 138 | uint8_t* vRows, int rowIndex, int width, int height) { |
| 139 | int numRows = (height - rowIndex) / 2; |
| 140 | if (numRows > 8) numRows = 8; |
| 141 | for (int row = 0; row < numRows; ++row) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 142 | int offset = ((rowIndex >> 1) + row) * fStrides[1]; |
| 143 | uint8_t* vu = vuPlanar + offset; |
| 144 | for (int i = 0; i < (width >> 1); ++i) { |
| 145 | int index = row * (width >> 1) + i; |
| 146 | uRows[index] = vu[1]; |
| 147 | vRows[index] = vu[0]; |
| 148 | vu += 2; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void Yuv420SpToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { |
| 154 | // cb and cr are horizontally downsampled and vertically downsampled as well. |
| 155 | cinfo->comp_info[0].h_samp_factor = 2; |
| 156 | cinfo->comp_info[0].v_samp_factor = 2; |
| 157 | cinfo->comp_info[1].h_samp_factor = 1; |
| 158 | cinfo->comp_info[1].v_samp_factor = 1; |
| 159 | cinfo->comp_info[2].h_samp_factor = 1; |
| 160 | cinfo->comp_info[2].v_samp_factor = 1; |
| 161 | } |
| 162 | |
| 163 | /////////////////////////////////////////////////////////////////////////////// |
| 164 | Yuv422IToJpegEncoder::Yuv422IToJpegEncoder(int* strides) : |
| 165 | YuvToJpegEncoder(strides) { |
| 166 | fNumPlanes = 1; |
| 167 | } |
| 168 | |
| 169 | void Yuv422IToJpegEncoder::compress(jpeg_compress_struct* cinfo, |
| 170 | uint8_t* yuv, int* offsets) { |
Sally Qi | 7e3f93b | 2021-07-15 00:00:54 +0000 | [diff] [blame] | 171 | ALOGD("onFlyCompress_422"); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 172 | JSAMPROW y[16]; |
| 173 | JSAMPROW cb[16]; |
| 174 | JSAMPROW cr[16]; |
| 175 | JSAMPARRAY planes[3]; |
| 176 | planes[0] = y; |
| 177 | planes[1] = cb; |
| 178 | planes[2] = cr; |
| 179 | |
| 180 | int width = cinfo->image_width; |
| 181 | int height = cinfo->image_height; |
| 182 | uint8_t* yRows = new uint8_t [16 * width]; |
| 183 | uint8_t* uRows = new uint8_t [16 * (width >> 1)]; |
| 184 | uint8_t* vRows = new uint8_t [16 * (width >> 1)]; |
| 185 | |
| 186 | uint8_t* yuvOffset = yuv + offsets[0]; |
| 187 | |
| 188 | // process 16 lines of Y and 16 lines of U/V each time. |
| 189 | while (cinfo->next_scanline < cinfo->image_height) { |
| 190 | deinterleave(yuvOffset, yRows, uRows, vRows, cinfo->next_scanline, width, height); |
| 191 | |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 192 | // Jpeg library ignores the rows whose indices are greater than height. |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 193 | for (int i = 0; i < 16; i++) { |
| 194 | // y row |
| 195 | y[i] = yRows + i * width; |
| 196 | |
| 197 | // construct u row and v row |
| 198 | // width is halved because of downsampling |
| 199 | int offset = i * (width >> 1); |
| 200 | cb[i] = uRows + offset; |
| 201 | cr[i] = vRows + offset; |
| 202 | } |
| 203 | |
| 204 | jpeg_write_raw_data(cinfo, planes, 16); |
| 205 | } |
| 206 | delete [] yRows; |
| 207 | delete [] uRows; |
| 208 | delete [] vRows; |
| 209 | } |
| 210 | |
| 211 | |
| 212 | void Yuv422IToJpegEncoder::deinterleave(uint8_t* yuv, uint8_t* yRows, uint8_t* uRows, |
| 213 | uint8_t* vRows, int rowIndex, int width, int height) { |
Wu-cheng Li | 4b63f14 | 2012-10-16 23:40:03 +0800 | [diff] [blame] | 214 | int numRows = height - rowIndex; |
| 215 | if (numRows > 16) numRows = 16; |
| 216 | for (int row = 0; row < numRows; ++row) { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 217 | uint8_t* yuvSeg = yuv + (rowIndex + row) * fStrides[0]; |
| 218 | for (int i = 0; i < (width >> 1); ++i) { |
| 219 | int indexY = row * width + (i << 1); |
| 220 | int indexU = row * (width >> 1) + i; |
| 221 | yRows[indexY] = yuvSeg[0]; |
| 222 | yRows[indexY + 1] = yuvSeg[2]; |
| 223 | uRows[indexU] = yuvSeg[1]; |
| 224 | vRows[indexU] = yuvSeg[3]; |
| 225 | yuvSeg += 4; |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | void Yuv422IToJpegEncoder::configSamplingFactors(jpeg_compress_struct* cinfo) { |
| 231 | // cb and cr are horizontally downsampled and vertically downsampled as well. |
| 232 | cinfo->comp_info[0].h_samp_factor = 2; |
| 233 | cinfo->comp_info[0].v_samp_factor = 2; |
| 234 | cinfo->comp_info[1].h_samp_factor = 1; |
| 235 | cinfo->comp_info[1].v_samp_factor = 2; |
| 236 | cinfo->comp_info[2].h_samp_factor = 1; |
| 237 | cinfo->comp_info[2].v_samp_factor = 2; |
| 238 | } |
| 239 | /////////////////////////////////////////////////////////////////////////////// |
| 240 | |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 241 | using namespace android::recoverymap; |
| 242 | |
| 243 | jpegr_color_gamut P010Yuv420ToJpegREncoder::findColorGamut(JNIEnv* env, int aDataSpace) { |
| 244 | switch (aDataSpace & ADataSpace::STANDARD_MASK) { |
| 245 | case ADataSpace::STANDARD_BT709: |
| 246 | return jpegr_color_gamut::JPEGR_COLORGAMUT_BT709; |
| 247 | case ADataSpace::STANDARD_DCI_P3: |
| 248 | return jpegr_color_gamut::JPEGR_COLORGAMUT_P3; |
| 249 | case ADataSpace::STANDARD_BT2020: |
| 250 | return jpegr_color_gamut::JPEGR_COLORGAMUT_BT2100; |
| 251 | default: |
| 252 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 253 | env->ThrowNew(IllegalArgumentException, |
| 254 | "The requested color gamut is not supported by JPEG/R."); |
| 255 | } |
| 256 | |
| 257 | return jpegr_color_gamut::JPEGR_COLORGAMUT_UNSPECIFIED; |
| 258 | } |
| 259 | |
| 260 | jpegr_transfer_function P010Yuv420ToJpegREncoder::findHdrTransferFunction(JNIEnv* env, |
| 261 | int aDataSpace) { |
| 262 | switch (aDataSpace & ADataSpace::TRANSFER_MASK) { |
| 263 | case ADataSpace::TRANSFER_ST2084: |
| 264 | return jpegr_transfer_function::JPEGR_TF_PQ; |
| 265 | case ADataSpace::TRANSFER_HLG: |
| 266 | return jpegr_transfer_function::JPEGR_TF_HLG; |
| 267 | default: |
| 268 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 269 | env->ThrowNew(IllegalArgumentException, |
| 270 | "The requested HDR transfer function is not supported by JPEG/R."); |
| 271 | } |
| 272 | |
| 273 | return jpegr_transfer_function::JPEGR_TF_UNSPECIFIED; |
| 274 | } |
| 275 | |
| 276 | bool P010Yuv420ToJpegREncoder::encode(JNIEnv* env, |
| 277 | SkWStream* stream, void* hdr, int hdrColorSpace, void* sdr, int sdrColorSpace, |
| 278 | int width, int height, int jpegQuality) { |
| 279 | // Check SDR color space. Now we only support SRGB transfer function |
| 280 | if ((sdrColorSpace & ADataSpace::TRANSFER_MASK) != ADataSpace::TRANSFER_SRGB) { |
| 281 | jclass IllegalArgumentException = env->FindClass("java/lang/IllegalArgumentException"); |
| 282 | env->ThrowNew(IllegalArgumentException, |
| 283 | "The requested SDR color space is not supported. Transfer function must be SRGB"); |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | jpegr_color_gamut hdrColorGamut = findColorGamut(env, hdrColorSpace); |
| 288 | jpegr_color_gamut sdrColorGamut = findColorGamut(env, sdrColorSpace); |
| 289 | jpegr_transfer_function hdrTransferFunction = findHdrTransferFunction(env, hdrColorSpace); |
| 290 | |
| 291 | if (hdrColorGamut == jpegr_color_gamut::JPEGR_COLORGAMUT_UNSPECIFIED |
| 292 | || sdrColorGamut == jpegr_color_gamut::JPEGR_COLORGAMUT_UNSPECIFIED |
| 293 | || hdrTransferFunction == jpegr_transfer_function::JPEGR_TF_UNSPECIFIED) { |
| 294 | return false; |
| 295 | } |
| 296 | |
Dichen Zhang | f84ed40 | 2023-02-10 22:41:46 +0000 | [diff] [blame^] | 297 | JpegR jpegREncoder; |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 298 | |
| 299 | jpegr_uncompressed_struct p010; |
| 300 | p010.data = hdr; |
| 301 | p010.width = width; |
| 302 | p010.height = height; |
| 303 | p010.colorGamut = hdrColorGamut; |
| 304 | |
| 305 | jpegr_uncompressed_struct yuv420; |
| 306 | yuv420.data = sdr; |
| 307 | yuv420.width = width; |
| 308 | yuv420.height = height; |
| 309 | yuv420.colorGamut = sdrColorGamut; |
| 310 | |
| 311 | jpegr_compressed_struct jpegR; |
| 312 | jpegR.maxLength = width * height * sizeof(uint8_t); |
| 313 | |
| 314 | std::unique_ptr<uint8_t[]> jpegr_data = std::make_unique<uint8_t[]>(jpegR.maxLength); |
| 315 | jpegR.data = jpegr_data.get(); |
| 316 | |
Dichen Zhang | f84ed40 | 2023-02-10 22:41:46 +0000 | [diff] [blame^] | 317 | if (int success = jpegREncoder.encodeJPEGR(&p010, &yuv420, |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 318 | hdrTransferFunction, |
| 319 | &jpegR, jpegQuality, nullptr); success != android::OK) { |
| 320 | ALOGW("Encode JPEG/R failed, error code: %d.", success); |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | if (!stream->write(jpegR.data, jpegR.length)) { |
| 325 | ALOGW("Writing JPEG/R to stream failed."); |
| 326 | return false; |
| 327 | } |
| 328 | |
| 329 | return true; |
| 330 | } |
| 331 | |
| 332 | /////////////////////////////////////////////////////////////////////////////// |
| 333 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 334 | static jboolean YuvImage_compressToJpeg(JNIEnv* env, jobject, jbyteArray inYuv, |
Ashok Bhat | 39029b2 | 2014-01-10 16:24:38 +0000 | [diff] [blame] | 335 | jint format, jint width, jint height, jintArray offsets, |
| 336 | jintArray strides, jint jpegQuality, jobject jstream, |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 337 | jbyteArray jstorage) { |
| 338 | jbyte* yuv = env->GetByteArrayElements(inYuv, NULL); |
| 339 | SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); |
| 340 | |
| 341 | jint* imgOffsets = env->GetIntArrayElements(offsets, NULL); |
| 342 | jint* imgStrides = env->GetIntArrayElements(strides, NULL); |
| 343 | YuvToJpegEncoder* encoder = YuvToJpegEncoder::create(format, imgStrides); |
Pawel Augustyn | 7c68a407 | 2012-07-16 11:23:54 +0200 | [diff] [blame] | 344 | jboolean result = JNI_FALSE; |
| 345 | if (encoder != NULL) { |
| 346 | encoder->encode(strm, yuv, width, height, imgOffsets, jpegQuality); |
| 347 | delete encoder; |
| 348 | result = JNI_TRUE; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 349 | } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 350 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 351 | env->ReleaseByteArrayElements(inYuv, yuv, 0); |
| 352 | env->ReleaseIntArrayElements(offsets, imgOffsets, 0); |
| 353 | env->ReleaseIntArrayElements(strides, imgStrides, 0); |
Martin Wallgren | d865900 | 2014-08-20 14:58:58 +0200 | [diff] [blame] | 354 | delete strm; |
Pawel Augustyn | 7c68a407 | 2012-07-16 11:23:54 +0200 | [diff] [blame] | 355 | return result; |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 356 | } |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 357 | |
| 358 | static jboolean YuvImage_compressToJpegR(JNIEnv* env, jobject, jbyteArray inHdr, |
| 359 | jint hdrColorSpace, jbyteArray inSdr, jint sdrColorSpace, |
| 360 | jint width, jint height, jint quality, jobject jstream, |
| 361 | jbyteArray jstorage) { |
| 362 | jbyte* hdr = env->GetByteArrayElements(inHdr, NULL); |
| 363 | jbyte* sdr = env->GetByteArrayElements(inSdr, NULL); |
| 364 | SkWStream* strm = CreateJavaOutputStreamAdaptor(env, jstream, jstorage); |
| 365 | P010Yuv420ToJpegREncoder encoder; |
| 366 | |
| 367 | jboolean result = JNI_FALSE; |
| 368 | if (encoder.encode(env, strm, hdr, hdrColorSpace, sdr, sdrColorSpace, |
| 369 | width, height, quality)) { |
| 370 | result = JNI_TRUE; |
| 371 | } |
| 372 | |
| 373 | env->ReleaseByteArrayElements(inHdr, hdr, 0); |
| 374 | env->ReleaseByteArrayElements(inSdr, sdr, 0); |
| 375 | delete strm; |
| 376 | return result; |
| 377 | } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 378 | /////////////////////////////////////////////////////////////////////////////// |
| 379 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 380 | static const JNINativeMethod gYuvImageMethods[] = { |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 381 | { "nativeCompressToJpeg", "([BIII[I[IILjava/io/OutputStream;[B)Z", |
Dichen Zhang | 3b2c0ce | 2022-12-14 19:58:55 +0000 | [diff] [blame] | 382 | (void*)YuvImage_compressToJpeg }, |
| 383 | { "nativeCompressToJpegR", "([BI[BIIIILjava/io/OutputStream;[B)Z", |
| 384 | (void*)YuvImage_compressToJpegR } |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 385 | }; |
| 386 | |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 387 | int register_android_graphics_YuvImage(JNIEnv* env) |
| 388 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 389 | return android::RegisterMethodsOrDie(env, "android/graphics/YuvImage", gYuvImageMethods, |
| 390 | NELEM(gYuvImageMethods)); |
Wei-Ta Chen | bca2d61 | 2009-11-30 17:52:05 +0800 | [diff] [blame] | 391 | } |