Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(196)

Side by Side Diff: src/codec/SkJpegCodec.cpp

Issue 1076923002: SkJpegCodec (Closed) Base URL: https://skia.googlesource.com/skia.git@gif-real
Patch Set: SkJpegCodec Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * Copyright 2015 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "SkJpegCodec.h"
9
10 #include "SkCodec.h"
11 #include "SkJpegAutoClean.h"
12 #include "SkJpegCodec.h"
13 #include "SkJpegUtility.h"
14 #include "SkCodecPriv.h"
15 #include "SkColorPriv.h"
16 #include "SkStream.h"
17 #include "SkTemplates.h"
18 #include "SkTypes.h"
19
20 #include <stdio.h>
21 extern "C" {
22 #include "jerror.h"
23 #include "jpegint.h"
24 #include "jpeglib.h"
25 }
26
27 // ANDROID_RGB
28 // If this is defined in the jpeg headers it indicates that jpeg offers
29 // support for two additional formats: JCS_RGBA_8888 and JCS_RGB_565.
30
31 /*
32 * Dummy error reporting functions for when the client wants to suppress message s
scroggo 2015/04/10 17:19:06 Instead of using these, use SkCodecPrintf. Then th
msarett 2015/04/13 20:54:05 This actually isn't fine. When SK_PRINT_CODEC_MES
scroggo 2015/04/14 13:10:32 No, jpeglib will print messages. Just always use t
33 */
34 static void do_nothing_emit_message(jpeg_common_struct*, int) {
35 // Do nothing
36 }
37 static void do_nothing_output_message(j_common_ptr) {
38 // Do nothing
39 }
40
41 /*
42 * Choose the size of the memory buffer on Android
43 */
44 static void overwrite_mem_buffer_size(jpeg_decompress_struct* jpegInfo) {
45 #ifdef SK_BUILD_FOR_ANDROID
46
47 // Use 30 MB for devices with a large amount of system memory and 5MB otherwise
48 // TODO: (msarett) This matches SkImageDecoder. Why were these values chosen?
scroggo 2015/04/10 17:19:06 Good question. I am not sure why these values were
msarett 2015/04/13 20:54:04 I'm going to investigate this bug further after su
49 #ifdef ANDROID_LARGE_MEMORY_DEVICE
50 jpegInfo->mem->max_memory_to_use = 30 * 1024 * 1024;
51 #else
52 jpegInfo->mem->max_memory_to_use = 5 * 1024 * 1024;
53 #endif
54
55 #endif // SK_BUILD_FOR_ANDROID
56 }
57
58 /*
59 * Print informative error messages
60 */
61 static void print_jpeg_decoder_errors(const jpeg_decompress_struct& jpegInfo, in t width,
62 int height, const char caller[]) {
63 char buffer[JMSG_LENGTH_MAX];
64 jpegInfo.err->format_message((const j_common_ptr) &jpegInfo, buffer);
65 SkCodecPrintf("libjpeg error %d <%s> from %s [%d %d]\n",
66 jpegInfo.err->msg_code, buffer, caller, width, height);
67 }
68
69 static bool return_false(const jpeg_decompress_struct& jpegInfo, const char call er[]) {
70 print_jpeg_decoder_errors(jpegInfo, jpegInfo.output_width, jpegInfo.output_h eight, caller);
71 return false;
72 }
73
74 static SkCodec::Result return_failure(const jpeg_decompress_struct& jpegInfo, co nst char caller[],
75 SkCodec::Result result) {
76 print_jpeg_decoder_errors(jpegInfo, jpegInfo.output_width, jpegInfo.output_h eight, caller);
77 return result;
78 }
79
80 /*
81 * Convert a row of CMYK samples to RGBX in place.
82 * Note that this method moves the row pointer in its processing.
scroggo 2015/04/10 17:19:05 Could you specify what width means? It appears th
msarett 2015/04/13 20:54:05 I meant to have a TODO here. I removed the commen
83 */
84 static void convert_CMYK_to_RGB(uint8_t* row, unsigned int width) {
85 for (uint32_t x = 0; x < width; x++, row += 4) {
86 row[0] = SkMulDiv255Round(row[0], row[3]);
87 row[1] = SkMulDiv255Round(row[1], row[3]);
88 row[2] = SkMulDiv255Round(row[2], row[3]);
89 row[3] = 0xFF;
90 }
91 }
92
93 /*
94 * Common code for turning off upsampling and smoothing.
95 * Turning these off helps performance without showing noticable
96 * differences in the resulting bitmap.
97 */
98 static void turn_off_visual_optimizations(jpeg_decompress_struct* jpegInfo) {
99 SkASSERT(jpegInfo != NULL);
100 // This gives about 30% performance improvement in SkImageDecoder. In theory , it may
101 // reduce the visual quality. In practice it's hard to see a difference.
scroggo 2015/04/10 17:19:06 It does. See https://codereview.chromium.org/97356
msarett 2015/04/13 20:54:05 Done.
102 // TODO (msarett): Measure performance gains for SkCodec.
103 jpegInfo->do_fancy_upsampling = 0;
104
105 // This gives another few percents in SkCodec.
106 // TODO (msarett): Measure performance gains for SkCodec.
107 jpegInfo->do_block_smoothing = 0;
108 }
109
110 /*
111 * Common code for setting the dct method.
112 */
113 static void set_dct_method(jpeg_decompress_struct* jpegInfo) {
scroggo 2015/04/10 17:19:06 Not sure this needs to be its own method, given th
msarett 2015/04/13 20:54:05 Done.
114 SkASSERT(jpegInfo != NULL);
115 #ifdef DCT_IFAST_SUPPORTED
116 jpegInfo->dct_method = JDCT_IFAST;
117 #else
118 jpegInfo->dct_method = JDCT_ISLOW;
119 #endif
120 }
121
122 /*
123 * Get the preferred color type to decode to based on the properties of the jpeg .
124 * Also, inform libjpeg what format to decode to.
125 */
126 SkColorType get_color_type(jpeg_decompress_struct* jpegInfo) {
127 SkASSERT(jpegInfo != NULL);
128
129 switch (jpegInfo->jpeg_color_space) {
130 case JCS_CMYK:
131 case JCS_YCCK:
132 // libjpeg cannot convert from CMYK or YCCK to RGB.
133 // Here, we ask libjpeg to give us CMYK samples back and
134 // we will later manually convert them to RGB.
135 jpegInfo->out_color_space = JCS_CMYK;
136 return kN32_SkColorType;
137 case JCS_GRAYSCALE:
138 jpegInfo->out_color_space = JCS_GRAYSCALE;
139 return kGray_8_SkColorType;
140 case JCS_RGB:
scroggo 2015/04/10 17:19:06 Why did you make this explicit only to fall throug
msarett 2015/04/13 20:54:05 You're right this is unnecessary.
141 default:
142 #ifdef ANDROID_RGB
143 jpegInfo->dither_mode = JDITHER_NONE;
144 jpegInfo->out_color_space = JCS_RGBA_8888;
145 #else
146 jpegInfo->out_color_space = JCS_RGB;
147 #endif
148 // TODO (msarett): SkImageDecoder uses dithering when decoding to 56 5.
149 // Will we want to dither when we enable decodes to 565?
scroggo 2015/04/10 17:19:06 No. We do not want to do dithering. If the image w
msarett 2015/04/13 20:54:05 Hmm ok. Looking at libjpeg, it appears that the d
scroggo 2015/04/14 13:10:32 Oh, I meant we would not do dithering after the fa
msarett 2015/04/14 19:30:36 The default dithering for all destinations is Floy
scroggo 2015/04/15 00:31:05 If Chromium always uses FS, that seems like a good
150 return kN32_SkColorType;
151 }
152 }
153
154 /*
155 * Get the config and bytes per pixel of the source data.
156 * Return whether the data is supported.
157 */
158 static SkSwizzler::SrcConfig get_src_config(const jpeg_decompress_struct* jpegIn fo) {
159 if (JCS_CMYK == jpegInfo->out_color_space) {
160 // In this case we will manually convert the CMYK values to RGBX.
161 return SkSwizzler::kRGBX;
162 } else if (3 == jpegInfo->out_color_components && JCS_RGB == jpegInfo->out_c olor_space) {
scroggo 2015/04/10 17:19:06 nit: these do not need to be else statements, sinc
msarett 2015/04/13 20:54:05 Done.
163 return SkSwizzler::kRGB;
164 #ifdef ANDROID_RGB
165 } else if (JCS_RGBA_8888 == jpegInfo->out_color_space) {
166 return SkSwizzler::kRGBX;
167 } else if (JCS_RGB_565 == jpegInfo->out_color_space) {
168 return SkSwizzler::kRGB_565;
169 #endif
170 } else if (1 == jpegInfo->out_color_components && JCS_GRAYSCALE == jpegInfo- >out_color_space) {
171 return SkSwizzler::kGray;
172 } else {
173 return SkSwizzler::kUnknown;
174 }
175 }
176
177 bool SkJpegCodec::IsJpeg(SkStream* stream) {
178 static const uint8_t jpegSig[] = { 0xFF, 0xD8, 0xFF };
179 char buffer[sizeof(jpegSig)];
180 return stream->read(buffer, sizeof(jpegSig)) == sizeof(jpegSig) &&
181 !memcmp(buffer, jpegSig, sizeof(jpegSig));
182 }
183
184 bool SkJpegCodec::ReadHeader(SkStream* stream, SkCodec** codecOut,
185 jpeg_decompress_struct** jpegInfoOut) {
186 // Initialize a struct that will contain all of the jpeg info as we prepare to decode
187 // Store the struct in a container that automatically frees the memory used
188 JpegAutoClean jpegInfo(
189 (jpeg_decompress_struct*) SkNEW(jpeg_decompress_struct), false);
190
191 // The source manager handles the input stream for libjpeg
192 SkAutoTDelete<skjpeg_source_mgr> srcMgr(SkNEW_ARGS(skjpeg_source_mgr, (strea m)));
193 SkAutoTDeleteArray<uint8_t> buffer(SkNEW_ARRAY(uint8_t, srcMgr->kBufferSize) );
194 srcMgr->fBuffer = buffer.get();
scroggo 2015/04/10 17:19:05 This seems dangerous. Why did you not make fBuffer
msarett 2015/04/13 20:54:04 The old code allocated fBuffer on the stack, which
scroggo 2015/04/14 13:10:32 Let's discuss this further in person, so I can bet
195
196 // Set up the error manager. This must be set before the call to jpeg_creat e_decompress
197 // in case there is a failure in initializing memory in libjpeg.
198 SkAutoTDelete<skjpeg_error_mgr> errorMgr(SkNEW(skjpeg_error_mgr));
199 jpegInfo.get()->err = jpeg_std_error(errorMgr.get());
200 errorMgr->error_exit = skjpeg_err_exit;
201
202 // All objects need to be instantiated before this setjmp call so that
203 // they will be cleaned up properly if an error occurs.
204 if (setjmp(errorMgr.get()->fJmpBuf)) {
205 return return_false(*(jpegInfo.get()), "setjmp");
206 }
207
208 // Initialize the jpeg info struct to prepare to decode
209 jpeg_create_decompress(jpegInfo.get());
210 overwrite_mem_buffer_size(jpegInfo.get());
211 // Set ownership of the source manager and error manager to jpegInfo
212 jpegInfo.get()->src = srcMgr.detach();
213 buffer.detach();
214 errorMgr.detach();
215 jpegInfo.setInit(true);
scroggo 2015/04/10 17:19:06 setInit seems like an odd function to me. It does
msarett 2015/04/13 20:54:05 Let's take it further. I think the new version is
216
217 // Set the warning and error messages if necessary
218 #ifndef SK_PRINT_CODEC_MESSAGES
219 jpegInfo.get()->err->emit_message = &do_nothing_emit_message;
220 jpegInfo.get()->err->output_message = &do_nothing_output_message;
221 #endif
222
223 // Read the jpeg header
224 int status = jpeg_read_header(jpegInfo.get(), true);
225 if (status != JPEG_HEADER_OK) {
226 return return_false(*(jpegInfo.get()), "read_header");
227 }
228
229 // Choose a method of performing the discrete cosine transform
230 set_dct_method(jpegInfo.get());
231
232 // Optimize for performance over visual quality
233 turn_off_visual_optimizations(jpegInfo.get());
234
235 if (NULL != codecOut) {
236 // Recommend the color type to decode to
237 const SkColorType colorType = get_color_type(jpegInfo.get());
238
239 // Create image info object and the codec
240 const SkImageInfo& imageInfo = SkImageInfo::Make(jpegInfo.get()->image_w idth,
241 jpegInfo.get()->image_height, colorType, kOpaque_SkAlphaType);
242 *codecOut = SkNEW_ARGS(SkJpegCodec, (imageInfo, stream, jpegInfo.detach( )));
243 } else {
244 SkASSERT(NULL != jpegInfoOut);
245 *jpegInfoOut = jpegInfo.detach();
246 }
247 return true;
248 }
249
250 SkCodec* SkJpegCodec::NewFromStream(SkStream* stream) {
251 SkAutoTDelete<SkStream> streamDeleter(stream);
252 SkCodec* codec = NULL;
253 if (ReadHeader(stream, &codec, NULL)) {
254 // Codec has taken ownership of the stream, we do not need to delete it
255 SkASSERT(codec);
256 streamDeleter.detach();
257 return codec;
258 }
259 return NULL;
260 }
261
262 SkJpegCodec::SkJpegCodec(const SkImageInfo& srcInfo, SkStream* stream,
263 jpeg_decompress_struct* jpegInfo)
264 : INHERITED(srcInfo, stream)
265 , fJpegInfo(jpegInfo, true)
266 {}
267
268 /*
269 * Return a valid set of output dimensions for this decoder, given an input scal e
270 */
271 SkISize SkJpegCodec::onGetScaledDimensions(float desiredScale) const {
272 #ifdef IDCT_SCALING_SUPPORTED
273 // libjpeg supports scaling by 1/1, 1/2, 1/4, and 1/8, so we will support th ese as well
274 long scale;
275 if (desiredScale > 0.75) {
scroggo 2015/04/10 17:19:06 nit: .75f
msarett 2015/04/13 20:54:05 Done.
276 scale = 1;
277 } else if (desiredScale > 0.375) {
278 scale = 2;
279 } else if (desiredScale > 0.1875) {
280 scale = 4;
281 } else {
282 scale = 8;
283 }
284
285 // Return the calculated output dimensions for the given scale.
286 // This code is risky because it may become incorrect if libjpeg changes how it calculates
scroggo 2015/04/10 17:19:06 I tend to think libjpeg will not change how it mak
msarett 2015/04/13 20:54:05 The compiler complains if I change scale_denom at
scroggo 2015/04/14 13:10:32 Yeah, it might just depend on what the client want
287 // output dimensions. The ideal way to get the output dimensions would be:
288 // fJpegInfo.get()->scale_denom = scale;
289 // jpeg_calc_output_dimensions(fJpegInfo.get());
290 // However, we cannot do this because this function is const.
291 return SkISize::Make(jdiv_round_up((long) fJpegInfo.get()->image_width, scal e),
292 jdiv_round_up((long) fJpegInfo.get()->image_height, sca le));
293 #else
294 return this->getInfo().dimensions();
295 #endif
296 }
297
298 /*
299 * Checks if the conversion between the input image and the requested output
300 * image has been implemented
301 */
302 static bool conversion_possible(const SkImageInfo& dst,
303 const SkImageInfo& src) {
304 // Ensure that the profile type is unchanged
305 if (dst.profileType() != src.profileType()) {
306 return false;
307 }
308
309 // Ensure that the alpha type is opaque
310 if (kOpaque_SkAlphaType != dst.alphaType()) {
311 return false;
312 }
313
314 // Always allow kN32 as the color type
315 if (kN32_SkColorType == dst.colorType()) {
316 return true;
317 }
318
319 // Otherwise require that the destination color type match our recommendatio n
320 return dst.colorType() == src.colorType();
321 }
322
323 /*
324 * Initiates the jpeg decode
325 */
326 SkCodec::Result SkJpegCodec::onGetPixels(const SkImageInfo& dstInfo,
327 void* dst, size_t dstRowBytes,
328 const Options& options, SkPMColor*, int*) {
329 // Set a new jump location for libjpeg errors
330 skjpeg_error_mgr* errorMgr = (skjpeg_error_mgr*) fJpegInfo.get()->err;
331 if (setjmp(errorMgr->fJmpBuf)) {
332 return return_failure(*(fJpegInfo.get()), "setjmp", kIncompleteInput);
333 }
334
335 // Rewind the stream if needed
336 SkCodec::RewindState rewindState = this->rewindIfNeeded();
337 if (rewindState == kCouldNotRewind_RewindState) {
338 return kCouldNotRewind;
339 } else if (rewindState == kRewound_RewindState) {
340 jpeg_decompress_struct* jpegInfo = NULL;
341 if (!ReadHeader(this->stream(), NULL, &jpegInfo)) {
342 return kCouldNotRewind;
343 }
344 SkASSERT(NULL != jpegInfo);
345 fJpegInfo.reset(jpegInfo);
346 }
347
348 // Check if we can decode to the requested destination
349 if (!conversion_possible(dstInfo, this->getInfo())) {
350 return return_failure(*(fJpegInfo.get()), "conversion_possible", kInvali dConversion);
351 }
352
353 // Check if we can scale to the requested dimensions
354 // libjpeg can scale to 1/1, 1/2, 1/4, and 1/8
355 SkASSERT(1 == fJpegInfo.get()->scale_num);
scroggo 2015/04/10 17:19:06 If you implement operator->, you can just use fJp
msarett 2015/04/13 20:54:05 Done.
356 SkASSERT(1 == fJpegInfo.get()->scale_denom);
357 jpeg_calc_output_dimensions(fJpegInfo.get());
358 const uint32_t dstWidth = dstInfo.width();
359 const uint32_t dstHeight = dstInfo.height();
360 while (fJpegInfo.get()->output_width != dstWidth ||
361 fJpegInfo.get()->output_height != dstHeight) {
362
363 // Return a failure if we have tried all of the possible scales
364 if (8 == fJpegInfo.get()->scale_denom) {
scroggo 2015/04/10 17:19:06 It seems like we could also early exit (as a failu
msarett 2015/04/13 20:54:04 Yeah this is something I thought about and ultimat
365 return return_failure(*(fJpegInfo.get()), "cannot scale to requested dimensions",
366 kInvalidScale);
367 }
368
369 // Try the next scale
370 fJpegInfo.get()->scale_denom *= 2;
371 jpeg_calc_output_dimensions(fJpegInfo.get());
372 }
373
374 // Now, given valid output dimensions, we can start the decompress
375 if (!jpeg_start_decompress(fJpegInfo.get())) {
376 return return_failure(*(fJpegInfo.get()), "start_decompress", kInvalidIn put);
377 }
378
379 // Create the swizzler
380 SkSwizzler::SrcConfig srcConfig = get_src_config(fJpegInfo.get());
scroggo 2015/04/10 17:19:06 I think this and the following several local varia
msarett 2015/04/13 20:54:05 Done.
381 SkAutoTDelete<SkSwizzler> swizzler(SkSwizzler::CreateSwizzler(srcConfig, NUL L, dstInfo,
382 dst, dstRowBytes, options.fZeroInitialized));
383 if (NULL == swizzler.get()) {
384 return return_failure(*(fJpegInfo.get()), "CreateSwizzler", kInvalidInpu t);
385 }
386 uint32_t srcBytesPerPixel = SkSwizzler::BytesPerPixel(srcConfig);
387
388 // This is usually 1, but can also be 2 or 4.
389 // If we wanted to always read one row at a time, we could, but we will save space and time
390 // by using the recommendation from libjpeg.
391 uint32_t rowsPerDecode = fJpegInfo.get()->rec_outbuf_height;
scroggo 2015/04/10 17:19:06 We should also implement scanline decoding, althou
msarett 2015/04/13 20:54:05 Will do.
392 SkASSERT(rowsPerDecode <= 4);
393
394 // Create a buffer to contain decoded rows (libjpeg requires a 2D array)
395 uint32_t srcRowBytes = srcBytesPerPixel * dstWidth;
396 SkAutoTDeleteArray<uint8_t> srcBuffer(SkNEW_ARRAY(uint8_t, srcRowBytes * row sPerDecode));
397 JSAMPLE* srcRows[4];
398 uint8_t* srcPtr = srcBuffer.get();
399 for (uint8_t i = 0; i < rowsPerDecode; i++) {
400 srcRows[i] = (JSAMPLE*) srcPtr;
401 srcPtr += srcRowBytes;
402 }
403
404 // Ensure that we loop enough times to decode all of the rows
405 // libjpeg will prevent us from reading past the bottom of the image
406 for (uint32_t y = 0; y < dstHeight + rowsPerDecode - 1; y += rowsPerDecode) {
407 // Read rows of the image
408 uint32_t rowsDecoded = jpeg_read_scanlines(fJpegInfo.get(), srcRows, row sPerDecode);
409
410 // If we cannot read enough rows, assume the input is incomplete
411 if (rowsDecoded < rowsPerDecode && y + rowsDecoded < dstHeight) {
scroggo 2015/04/10 17:19:06 extra space here between && and y?
msarett 2015/04/13 20:54:05 Done.
412 // Convert and swizzle the rows that we were able to read
413 if (JCS_CMYK == fJpegInfo.get()->out_color_space) {
414 convert_CMYK_to_RGB(srcRows[0], dstWidth * rowsDecoded);
415 }
416 for (uint32_t i = 0; i < rowsDecoded; i++) {
417 swizzler->next(srcRows[i]);
418 }
419
420 // Fill the remainder of the image with white.
421 // This error handling behavior is unspecified but matches the behav ior of
422 // SkImageDecoder.
423 SkSwizzler::Fill(dst, dstInfo, dstRowBytes, y + rowsDecoded, SK_Colo rWHITE, NULL);
scroggo 2015/04/10 17:19:06 Don't we use a different color for other images? I
msarett 2015/04/13 20:54:05 We will consistently fill all opaque images with b
scroggo 2015/04/14 13:10:32 If we want to enforce using the same values everyw
424
425 // Finish the decode and indicate that the input was incomplete.
426 fJpegInfo.get()->output_scanline = dstHeight;
scroggo 2015/04/10 17:19:06 What does this do?
msarett 2015/04/13 20:54:05 This suppresses a warning message from libjpeg. "
427 jpeg_finish_decompress(fJpegInfo.get());
428 return return_failure(*(fJpegInfo.get()), "Incomplete image data", k IncompleteInput);
429 }
430
431 // Convert to RGB if necessary
432 if (JCS_CMYK == fJpegInfo.get()->out_color_space) {
scroggo 2015/04/10 17:19:06 Isn't this code the same whether we reached the en
msarett 2015/04/13 20:54:05 Done.
433 convert_CMYK_to_RGB(srcRows[0], dstWidth * rowsDecoded);
434 }
435
436 // Swizzle to output destination
437 for (uint32_t i = 0; i < rowsDecoded; i++) {
438 swizzler->next(srcRows[i]);
439 }
440 }
441 jpeg_finish_decompress(fJpegInfo.get());
442
443 return kSuccess;
444 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698