2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "jpeg_reader.h"
18 #include "error_codes.h"
19 #include "jpeg_hook.h"
23 JpegReader::JpegReader() :
29 mScanlineUnformattedBuflen(0),
30 mScanlineBytesRemaining(0),
35 JpegReader::~JpegReader() {
36 if (reset() != J_SUCCESS) {
37 LOGE("Failed to destroy compress object, JpegReader may leak memory.");
41 int32_t JpegReader::setup(JNIEnv *env, jobject in, int32_t* width, int32_t* height,
42 Jpeg_Config::Format format) {
43 if (mFinished || mSetup) {
46 if (env->ExceptionCheck()) {
50 // Setup error handler
51 SetupErrMgr(reinterpret_cast<j_common_ptr>(&mInfo), &mErrorManager);
52 // Set jump address for error handling
53 if (setjmp(mErrorManager.setjmp_buf)) {
58 jpeg_create_decompress(&mInfo);
60 // Setup our data source object, this allocates java global references
61 int32_t flags = MakeSrc(&mInfo, env, in);
62 if (flags != J_SUCCESS) {
63 LOGE("Failed to make source with error code: %d ", flags);
67 // Reads jpeg file header
68 jpeg_read_header(&mInfo, TRUE);
69 jpeg_calc_output_dimensions(&mInfo);
71 const int components = (static_cast<int>(format) & 0xff);
73 // Do setup for input format
76 mInfo.out_color_space = JCS_GRAYSCALE;
77 mScanlineUnformattedBuflen = mInfo.output_width;
81 mScanlineUnformattedBuflen = mInfo.output_width * components;
82 if (mInfo.jpeg_color_space == JCS_CMYK
83 || mInfo.jpeg_color_space == JCS_YCCK) {
84 // Always use cmyk for output in a 4 channel jpeg.
85 // libjpeg has a builtin cmyk->rgb decoder.
86 mScanlineUnformattedBuflen = mInfo.output_width * 4;
87 mInfo.out_color_space = JCS_CMYK;
89 mInfo.out_color_space = JCS_RGB;
93 return J_ERROR_BAD_ARGS;
96 mScanlineBuflen = mInfo.output_width * components;
97 mScanlineBytesRemaining = mScanlineBuflen;
98 mScanlineBuf = (JSAMPLE *) (mInfo.mem->alloc_small)(
99 reinterpret_cast<j_common_ptr>(&mInfo), JPOOL_PERMANENT,
100 mScanlineUnformattedBuflen * sizeof(JSAMPLE));
101 mScanlineIter = mScanlineBuf;
102 jpeg_start_decompress(&mInfo);
104 // Output image dimensions
106 *width = mInfo.output_width;
108 if (height != NULL) {
109 *height = mInfo.output_height;
117 int32_t JpegReader::read(int8_t* bytes, int32_t offset, int32_t count) {
119 return J_ERROR_FATAL;
124 // Set jump address for error handling
125 if (setjmp(mErrorManager.setjmp_buf)) {
126 return J_ERROR_FATAL;
129 return J_ERROR_BAD_ARGS;
131 int32_t total_length = count;
132 while (mInfo.output_scanline < mInfo.output_height) {
133 if (count < mScanlineBytesRemaining) {
134 // read partial scanline and return
136 // Treat NULL bytes as a skip
137 memcpy((void*) (bytes + offset), (void*) mScanlineIter,
138 count * sizeof(int8_t));
140 mScanlineBytesRemaining -= count;
141 mScanlineIter += count;
143 } else if (count > 0) {
144 // read full scanline
146 // Treat NULL bytes as a skip
147 memcpy((void*) (bytes + offset), (void*) mScanlineIter,
148 mScanlineBytesRemaining * sizeof(int8_t));
149 bytes += mScanlineBytesRemaining;
151 count -= mScanlineBytesRemaining;
152 mScanlineBytesRemaining = 0;
154 // Scanline buffer exhausted, read next scanline
155 if (jpeg_read_scanlines(&mInfo, &mScanlineBuf, 1) != 1) {
156 // Always read full scanline, no IO suspension
157 return J_ERROR_FATAL;
159 // Do in-place pixel formatting
160 formatPixels(static_cast<uint8_t*>(mScanlineBuf),
161 mScanlineUnformattedBuflen);
164 mScanlineIter = mScanlineBuf;
165 mScanlineBytesRemaining = mScanlineBuflen;
168 // Read all of the scanlines
169 jpeg_finish_decompress(&mInfo);
171 return total_length - count;
174 void JpegReader::updateEnv(JNIEnv *env) {
175 UpdateSrcEnv(&mInfo, env);
178 // Does in-place pixel formatting
179 void JpegReader::formatPixels(uint8_t* buf, int32_t len) {
182 // Do cmyk->rgb conversion if necessary
183 switch (mInfo.out_color_space) {
185 // Convert CMYK to RGB
186 int r, g, b, c, m, y, k;
187 for (int i = 0; i < len; i += 4) {
192 // Handle fmt for weird photoshop markers
193 if (mInfo.saw_Adobe_marker) {
198 r = (255 - k) * (255 - c) / 255;
199 g = (255 - k) * (255 - m) / 255;
200 b = (255 - k) * (255 - y) / 255;
208 iter += (len * 3 / 4);
215 // Do endianness and alpha for output format
216 if (mFormat == Jpeg_Config::FORMAT_RGBA) {
218 for (int i = len - 1; i >= 0; i -= 4) {
220 buf[i - 1] = *--iter;
221 buf[i - 2] = *--iter;
222 buf[i - 3] = *--iter;
224 } else if (mFormat == Jpeg_Config::FORMAT_ABGR) {
225 // Reverse endianness and set alphas to 255
227 for (int i = len - 1; i >= 0; i -= 4) {
239 int32_t JpegReader::reset() {
240 // Set jump address for error handling
241 if (setjmp(mErrorManager.setjmp_buf)) {
242 return J_ERROR_FATAL;
244 // Clean up global java references
246 // Wipe decompress struct, free memory pools
247 jpeg_destroy_decompress(&mInfo);