Remove chromeos==0 blacklist for test_isolation_mode.
[chromium-blink-merge.git] / ui / gfx / codec / jpeg_codec.cc
blob8a08fe0291afcc8a61c6b4f560396a4a50057da8
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "ui/gfx/codec/jpeg_codec.h"
7 #include <setjmp.h>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "third_party/skia/include/core/SkBitmap.h"
12 #include "third_party/skia/include/core/SkColorPriv.h"
14 extern "C" {
15 #if defined(USE_SYSTEM_LIBJPEG)
16 #include <jpeglib.h>
17 #elif defined(USE_LIBJPEG_TURBO)
18 #include "third_party/libjpeg_turbo/jpeglib.h"
19 #else
20 #include "third_party/libjpeg/jpeglib.h"
21 #endif
24 namespace gfx {
26 // Encoder/decoder shared stuff ------------------------------------------------
28 namespace {
30 // used to pass error info through the JPEG library
31 struct CoderErrorMgr {
32 jpeg_error_mgr pub;
33 jmp_buf setjmp_buffer;
36 void ErrorExit(jpeg_common_struct* cinfo) {
37 CoderErrorMgr *err = reinterpret_cast<CoderErrorMgr*>(cinfo->err);
39 // Return control to the setjmp point.
40 longjmp(err->setjmp_buffer, false);
43 } // namespace
45 // This method helps identify at run time which library chromium is using.
46 JPEGCodec::LibraryVariant JPEGCodec::JpegLibraryVariant() {
47 #if defined(USE_SYSTEM_LIBJPEG)
48 return SYSTEM_LIBJPEG;
49 #elif defined(USE_LIBJPEG_TURBO)
50 return LIBJPEG_TURBO;
51 #else
52 return IJG_LIBJPEG;
53 #endif
56 // Encoder ---------------------------------------------------------------------
58 // This code is based on nsJPEGEncoder from Mozilla.
59 // Copyright 2005 Google Inc. (Brett Wilson, contributor)
61 namespace {
63 // Initial size for the output buffer in the JpegEncoderState below.
64 static const int initial_output_buffer_size = 8192;
66 struct JpegEncoderState {
67 explicit JpegEncoderState(std::vector<unsigned char>* o)
68 : out(o),
69 image_buffer_used(0) {
72 // Output buffer, of which 'image_buffer_used' bytes are actually used (this
73 // will often be less than the actual size of the vector because we size it
74 // so that libjpeg can write directly into it.
75 std::vector<unsigned char>* out;
77 // Number of bytes in the 'out' buffer that are actually used (see above).
78 size_t image_buffer_used;
81 // Initializes the JpegEncoderState for encoding, and tells libjpeg about where
82 // the output buffer is.
84 // From the JPEG library:
85 // "Initialize destination. This is called by jpeg_start_compress() before
86 // any data is actually written. It must initialize next_output_byte and
87 // free_in_buffer. free_in_buffer must be initialized to a positive value."
88 void InitDestination(jpeg_compress_struct* cinfo) {
89 JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data);
90 DCHECK(state->image_buffer_used == 0) << "initializing after use";
92 state->out->resize(initial_output_buffer_size);
93 state->image_buffer_used = 0;
95 cinfo->dest->next_output_byte = &(*state->out)[0];
96 cinfo->dest->free_in_buffer = initial_output_buffer_size;
99 // Resize the buffer that we give to libjpeg and update our and its state.
101 // From the JPEG library:
102 // "Callback used by libjpeg whenever the buffer has filled (free_in_buffer
103 // reaches zero). In typical applications, it should write out the *entire*
104 // buffer (use the saved start address and buffer length; ignore the current
105 // state of next_output_byte and free_in_buffer). Then reset the pointer &
106 // count to the start of the buffer, and return TRUE indicating that the
107 // buffer has been dumped. free_in_buffer must be set to a positive value
108 // when TRUE is returned. A FALSE return should only be used when I/O
109 // suspension is desired (this operating mode is discussed in the next
110 // section)."
111 boolean EmptyOutputBuffer(jpeg_compress_struct* cinfo) {
112 JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data);
114 // note the new size, the buffer is full
115 state->image_buffer_used = state->out->size();
117 // expand buffer, just double size each time
118 state->out->resize(state->out->size() * 2);
120 // tell libjpeg where to write the next data
121 cinfo->dest->next_output_byte = &(*state->out)[state->image_buffer_used];
122 cinfo->dest->free_in_buffer = state->out->size() - state->image_buffer_used;
123 return 1;
126 // Cleans up the JpegEncoderState to prepare for returning in the final form.
128 // From the JPEG library:
129 // "Terminate destination --- called by jpeg_finish_compress() after all data
130 // has been written. In most applications, this must flush any data
131 // remaining in the buffer. Use either next_output_byte or free_in_buffer to
132 // determine how much data is in the buffer."
133 void TermDestination(jpeg_compress_struct* cinfo) {
134 JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data);
135 DCHECK(state->out->size() >= state->image_buffer_used);
137 // update the used byte based on the next byte libjpeg would write to
138 state->image_buffer_used = cinfo->dest->next_output_byte - &(*state->out)[0];
139 DCHECK(state->image_buffer_used < state->out->size()) <<
140 "JPEG library busted, got a bad image buffer size";
142 // update our buffer so that it exactly encompases the desired data
143 state->out->resize(state->image_buffer_used);
146 #if !defined(JCS_EXTENSIONS)
147 // Converts RGBA to RGB (removing the alpha values) to prepare to send data to
148 // libjpeg. This converts one row of data in rgba with the given width in
149 // pixels the the given rgb destination buffer (which should have enough space
150 // reserved for the final data).
151 void StripAlpha(const unsigned char* rgba, int pixel_width, unsigned char* rgb)
153 for (int x = 0; x < pixel_width; x++)
154 memcpy(&rgb[x * 3], &rgba[x * 4], 3);
157 // Converts BGRA to RGB by reordering the color components and dropping the
158 // alpha. This converts one row of data in rgba with the given width in
159 // pixels the the given rgb destination buffer (which should have enough space
160 // reserved for the final data).
161 void BGRAtoRGB(const unsigned char* bgra, int pixel_width, unsigned char* rgb)
163 for (int x = 0; x < pixel_width; x++) {
164 const unsigned char* pixel_in = &bgra[x * 4];
165 unsigned char* pixel_out = &rgb[x * 3];
166 pixel_out[0] = pixel_in[2];
167 pixel_out[1] = pixel_in[1];
168 pixel_out[2] = pixel_in[0];
171 #endif // !defined(JCS_EXTENSIONS)
173 // This class destroys the given jpeg_compress object when it goes out of
174 // scope. It simplifies the error handling in Encode (and even applies to the
175 // success case).
176 class CompressDestroyer {
177 public:
178 CompressDestroyer() : cinfo_(NULL) {
180 ~CompressDestroyer() {
181 DestroyManagedObject();
183 void SetManagedObject(jpeg_compress_struct* ci) {
184 DestroyManagedObject();
185 cinfo_ = ci;
187 void DestroyManagedObject() {
188 if (cinfo_) {
189 jpeg_destroy_compress(cinfo_);
190 cinfo_ = NULL;
193 private:
194 jpeg_compress_struct* cinfo_;
197 } // namespace
199 bool JPEGCodec::Encode(const unsigned char* input, ColorFormat format,
200 int w, int h, int row_byte_width,
201 int quality, std::vector<unsigned char>* output) {
202 jpeg_compress_struct cinfo;
203 CompressDestroyer destroyer;
204 destroyer.SetManagedObject(&cinfo);
205 output->clear();
206 #if !defined(JCS_EXTENSIONS)
207 unsigned char* row_buffer = NULL;
208 #endif
210 // We set up the normal JPEG error routines, then override error_exit.
211 // This must be done before the call to create_compress.
212 CoderErrorMgr errmgr;
213 cinfo.err = jpeg_std_error(&errmgr.pub);
214 errmgr.pub.error_exit = ErrorExit;
216 // Establish the setjmp return context for ErrorExit to use.
217 if (setjmp(errmgr.setjmp_buffer)) {
218 // If we get here, the JPEG code has signaled an error.
219 // MSDN notes: "if you intend your code to be portable, do not rely on
220 // correct destruction of frame-based objects when executing a nonlocal
221 // goto using a call to longjmp." So we delete the CompressDestroyer's
222 // object manually instead.
223 destroyer.DestroyManagedObject();
224 #if !defined(JCS_EXTENSIONS)
225 delete[] row_buffer;
226 #endif
227 return false;
230 // The destroyer will destroy() cinfo on exit.
231 jpeg_create_compress(&cinfo);
233 cinfo.image_width = w;
234 cinfo.image_height = h;
235 cinfo.input_components = 3;
236 #ifdef JCS_EXTENSIONS
237 // Choose an input colorspace and return if it is an unsupported one. Since
238 // libjpeg-turbo supports all input formats used by Chromium (i.e. RGB, RGBA,
239 // and BGRA), we just map the input parameters to a colorspace used by
240 // libjpeg-turbo.
241 if (format == FORMAT_RGB) {
242 cinfo.input_components = 3;
243 cinfo.in_color_space = JCS_RGB;
244 } else if (format == FORMAT_RGBA ||
245 (format == FORMAT_SkBitmap && SK_R32_SHIFT == 0)) {
246 cinfo.input_components = 4;
247 cinfo.in_color_space = JCS_EXT_RGBX;
248 } else if (format == FORMAT_BGRA ||
249 (format == FORMAT_SkBitmap && SK_B32_SHIFT == 0)) {
250 cinfo.input_components = 4;
251 cinfo.in_color_space = JCS_EXT_BGRX;
252 } else {
253 // We can exit this function without calling jpeg_destroy_compress() because
254 // CompressDestroyer automaticaly calls it.
255 NOTREACHED() << "Invalid pixel format";
256 return false;
258 #else
259 cinfo.in_color_space = JCS_RGB;
260 #endif
261 cinfo.data_precision = 8;
263 jpeg_set_defaults(&cinfo);
264 jpeg_set_quality(&cinfo, quality, 1); // quality here is 0-100
266 // set up the destination manager
267 jpeg_destination_mgr destmgr;
268 destmgr.init_destination = InitDestination;
269 destmgr.empty_output_buffer = EmptyOutputBuffer;
270 destmgr.term_destination = TermDestination;
271 cinfo.dest = &destmgr;
273 JpegEncoderState state(output);
274 cinfo.client_data = &state;
276 jpeg_start_compress(&cinfo, 1);
278 // feed it the rows, doing necessary conversions for the color format
279 #ifdef JCS_EXTENSIONS
280 // This function already returns when the input format is not supported by
281 // libjpeg-turbo and needs conversion. Therefore, we just encode lines without
282 // conversions.
283 while (cinfo.next_scanline < cinfo.image_height) {
284 const unsigned char* row = &input[cinfo.next_scanline * row_byte_width];
285 jpeg_write_scanlines(&cinfo, const_cast<unsigned char**>(&row), 1);
287 #else
288 if (format == FORMAT_RGB) {
289 // no conversion necessary
290 while (cinfo.next_scanline < cinfo.image_height) {
291 const unsigned char* row = &input[cinfo.next_scanline * row_byte_width];
292 jpeg_write_scanlines(&cinfo, const_cast<unsigned char**>(&row), 1);
294 } else {
295 // get the correct format converter
296 void (*converter)(const unsigned char* in, int w, unsigned char* rgb);
297 if (format == FORMAT_RGBA ||
298 (format == FORMAT_SkBitmap && SK_R32_SHIFT == 0)) {
299 converter = StripAlpha;
300 } else if (format == FORMAT_BGRA ||
301 (format == FORMAT_SkBitmap && SK_B32_SHIFT == 0)) {
302 converter = BGRAtoRGB;
303 } else {
304 NOTREACHED() << "Invalid pixel format";
305 return false;
308 // output row after converting
309 row_buffer = new unsigned char[w * 3];
311 while (cinfo.next_scanline < cinfo.image_height) {
312 converter(&input[cinfo.next_scanline * row_byte_width], w, row_buffer);
313 jpeg_write_scanlines(&cinfo, &row_buffer, 1);
315 delete[] row_buffer;
317 #endif
319 jpeg_finish_compress(&cinfo);
320 return true;
323 // Decoder --------------------------------------------------------------------
325 namespace {
327 struct JpegDecoderState {
328 JpegDecoderState(const unsigned char* in, size_t len)
329 : input_buffer(in), input_buffer_length(len) {
332 const unsigned char* input_buffer;
333 size_t input_buffer_length;
336 // Callback to initialize the source.
338 // From the JPEG library:
339 // "Initialize source. This is called by jpeg_read_header() before any data is
340 // actually read. May leave bytes_in_buffer set to 0 (in which case a
341 // fill_input_buffer() call will occur immediately)."
342 void InitSource(j_decompress_ptr cinfo) {
343 JpegDecoderState* state = static_cast<JpegDecoderState*>(cinfo->client_data);
344 cinfo->src->next_input_byte = state->input_buffer;
345 cinfo->src->bytes_in_buffer = state->input_buffer_length;
348 // Callback to fill the buffer. Since our buffer already contains all the data,
349 // we should never need to provide more data. If libjpeg thinks it needs more
350 // data, our input is probably corrupt.
352 // From the JPEG library:
353 // "This is called whenever bytes_in_buffer has reached zero and more data is
354 // wanted. In typical applications, it should read fresh data into the buffer
355 // (ignoring the current state of next_input_byte and bytes_in_buffer), reset
356 // the pointer & count to the start of the buffer, and return TRUE indicating
357 // that the buffer has been reloaded. It is not necessary to fill the buffer
358 // entirely, only to obtain at least one more byte. bytes_in_buffer MUST be
359 // set to a positive value if TRUE is returned. A FALSE return should only
360 // be used when I/O suspension is desired."
361 boolean FillInputBuffer(j_decompress_ptr cinfo) {
362 return false;
365 // Skip data in the buffer. Since we have all the data at once, this operation
366 // is easy. It is not clear if this ever gets called because the JPEG library
367 // should be able to do the skip itself (it has all the data).
369 // From the JPEG library:
370 // "Skip num_bytes worth of data. The buffer pointer and count should be
371 // advanced over num_bytes input bytes, refilling the buffer as needed. This
372 // is used to skip over a potentially large amount of uninteresting data
373 // (such as an APPn marker). In some applications it may be possible to
374 // optimize away the reading of the skipped data, but it's not clear that
375 // being smart is worth much trouble; large skips are uncommon.
376 // bytes_in_buffer may be zero on return. A zero or negative skip count
377 // should be treated as a no-op."
378 void SkipInputData(j_decompress_ptr cinfo, long num_bytes) {
379 if (num_bytes > static_cast<long>(cinfo->src->bytes_in_buffer)) {
380 // Since all our data should be in the buffer, trying to skip beyond it
381 // means that there is some kind of error or corrupt input data. A 0 for
382 // bytes left means it will call FillInputBuffer which will then fail.
383 cinfo->src->next_input_byte += cinfo->src->bytes_in_buffer;
384 cinfo->src->bytes_in_buffer = 0;
385 } else if (num_bytes > 0) {
386 cinfo->src->bytes_in_buffer -= static_cast<size_t>(num_bytes);
387 cinfo->src->next_input_byte += num_bytes;
391 // Our source doesn't need any cleanup, so this is a NOP.
393 // From the JPEG library:
394 // "Terminate source --- called by jpeg_finish_decompress() after all data has
395 // been read to clean up JPEG source manager. NOT called by jpeg_abort() or
396 // jpeg_destroy()."
397 void TermSource(j_decompress_ptr cinfo) {
400 #if !defined(JCS_EXTENSIONS)
401 // Converts one row of rgb data to rgba data by adding a fully-opaque alpha
402 // value.
403 void AddAlpha(const unsigned char* rgb, int pixel_width, unsigned char* rgba) {
404 for (int x = 0; x < pixel_width; x++) {
405 memcpy(&rgba[x * 4], &rgb[x * 3], 3);
406 rgba[x * 4 + 3] = 0xff;
410 // Converts one row of RGB data to BGRA by reordering the color components and
411 // adding alpha values of 0xff.
412 void RGBtoBGRA(const unsigned char* bgra, int pixel_width, unsigned char* rgb)
414 for (int x = 0; x < pixel_width; x++) {
415 const unsigned char* pixel_in = &bgra[x * 3];
416 unsigned char* pixel_out = &rgb[x * 4];
417 pixel_out[0] = pixel_in[2];
418 pixel_out[1] = pixel_in[1];
419 pixel_out[2] = pixel_in[0];
420 pixel_out[3] = 0xff;
423 #endif // !defined(JCS_EXTENSIONS)
425 // This class destroys the given jpeg_decompress object when it goes out of
426 // scope. It simplifies the error handling in Decode (and even applies to the
427 // success case).
428 class DecompressDestroyer {
429 public:
430 DecompressDestroyer() : cinfo_(NULL) {
432 ~DecompressDestroyer() {
433 DestroyManagedObject();
435 void SetManagedObject(jpeg_decompress_struct* ci) {
436 DestroyManagedObject();
437 cinfo_ = ci;
439 void DestroyManagedObject() {
440 if (cinfo_) {
441 jpeg_destroy_decompress(cinfo_);
442 cinfo_ = NULL;
445 private:
446 jpeg_decompress_struct* cinfo_;
449 } // namespace
451 bool JPEGCodec::Decode(const unsigned char* input, size_t input_size,
452 ColorFormat format, std::vector<unsigned char>* output,
453 int* w, int* h) {
454 jpeg_decompress_struct cinfo;
455 DecompressDestroyer destroyer;
456 destroyer.SetManagedObject(&cinfo);
457 output->clear();
459 // We set up the normal JPEG error routines, then override error_exit.
460 // This must be done before the call to create_decompress.
461 CoderErrorMgr errmgr;
462 cinfo.err = jpeg_std_error(&errmgr.pub);
463 errmgr.pub.error_exit = ErrorExit;
464 // Establish the setjmp return context for ErrorExit to use.
465 if (setjmp(errmgr.setjmp_buffer)) {
466 // If we get here, the JPEG code has signaled an error.
467 // See note in JPEGCodec::Encode() for why we need to destroy the cinfo
468 // manually here.
469 destroyer.DestroyManagedObject();
470 return false;
473 // The destroyer will destroy() cinfo on exit. We don't want to set the
474 // destroyer's object until cinfo is initialized.
475 jpeg_create_decompress(&cinfo);
477 // set up the source manager
478 jpeg_source_mgr srcmgr;
479 srcmgr.init_source = InitSource;
480 srcmgr.fill_input_buffer = FillInputBuffer;
481 srcmgr.skip_input_data = SkipInputData;
482 srcmgr.resync_to_restart = jpeg_resync_to_restart; // use default routine
483 srcmgr.term_source = TermSource;
484 cinfo.src = &srcmgr;
486 JpegDecoderState state(input, input_size);
487 cinfo.client_data = &state;
489 // fill the file metadata into our buffer
490 if (jpeg_read_header(&cinfo, true) != JPEG_HEADER_OK)
491 return false;
493 // we want to always get RGB data out
494 switch (cinfo.jpeg_color_space) {
495 case JCS_GRAYSCALE:
496 case JCS_RGB:
497 case JCS_YCbCr:
498 #ifdef JCS_EXTENSIONS
499 // Choose an output colorspace and return if it is an unsupported one.
500 // Same as JPEGCodec::Encode(), libjpeg-turbo supports all input formats
501 // used by Chromium (i.e. RGB, RGBA, and BGRA) and we just map the input
502 // parameters to a colorspace.
503 if (format == FORMAT_RGB) {
504 cinfo.out_color_space = JCS_RGB;
505 cinfo.output_components = 3;
506 } else if (format == FORMAT_RGBA ||
507 (format == FORMAT_SkBitmap && SK_R32_SHIFT == 0)) {
508 cinfo.out_color_space = JCS_EXT_RGBX;
509 cinfo.output_components = 4;
510 } else if (format == FORMAT_BGRA ||
511 (format == FORMAT_SkBitmap && SK_B32_SHIFT == 0)) {
512 cinfo.out_color_space = JCS_EXT_BGRX;
513 cinfo.output_components = 4;
514 } else {
515 // We can exit this function without calling jpeg_destroy_decompress()
516 // because DecompressDestroyer automaticaly calls it.
517 NOTREACHED() << "Invalid pixel format";
518 return false;
520 #else
521 cinfo.out_color_space = JCS_RGB;
522 #endif
523 break;
524 case JCS_CMYK:
525 case JCS_YCCK:
526 default:
527 // Mozilla errors out on these color spaces, so I presume that the jpeg
528 // library can't do automatic color space conversion for them. We don't
529 // care about these anyway.
530 return false;
532 #ifndef JCS_EXTENSIONS
533 cinfo.output_components = 3;
534 #endif
536 jpeg_calc_output_dimensions(&cinfo);
537 *w = cinfo.output_width;
538 *h = cinfo.output_height;
540 jpeg_start_decompress(&cinfo);
542 // FIXME(brettw) we may want to allow the capability for callers to request
543 // how to align row lengths as we do for the compressor.
544 int row_read_stride = cinfo.output_width * cinfo.output_components;
546 #ifdef JCS_EXTENSIONS
547 // Create memory for a decoded image and write decoded lines to the memory
548 // without conversions same as JPEGCodec::Encode().
549 int row_write_stride = row_read_stride;
550 output->resize(row_write_stride * cinfo.output_height);
552 for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) {
553 unsigned char* rowptr = &(*output)[row * row_write_stride];
554 if (!jpeg_read_scanlines(&cinfo, &rowptr, 1))
555 return false;
557 #else
558 if (format == FORMAT_RGB) {
559 // easy case, row needs no conversion
560 int row_write_stride = row_read_stride;
561 output->resize(row_write_stride * cinfo.output_height);
563 for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) {
564 unsigned char* rowptr = &(*output)[row * row_write_stride];
565 if (!jpeg_read_scanlines(&cinfo, &rowptr, 1))
566 return false;
568 } else {
569 // Rows need conversion to output format: read into a temporary buffer and
570 // expand to the final one. Performance: we could avoid the extra
571 // allocation by doing the expansion in-place.
572 int row_write_stride;
573 void (*converter)(const unsigned char* rgb, int w, unsigned char* out);
574 if (format == FORMAT_RGBA ||
575 (format == FORMAT_SkBitmap && SK_R32_SHIFT == 0)) {
576 row_write_stride = cinfo.output_width * 4;
577 converter = AddAlpha;
578 } else if (format == FORMAT_BGRA ||
579 (format == FORMAT_SkBitmap && SK_B32_SHIFT == 0)) {
580 row_write_stride = cinfo.output_width * 4;
581 converter = RGBtoBGRA;
582 } else {
583 NOTREACHED() << "Invalid pixel format";
584 jpeg_destroy_decompress(&cinfo);
585 return false;
588 output->resize(row_write_stride * cinfo.output_height);
590 scoped_ptr<unsigned char[]> row_data(new unsigned char[row_read_stride]);
591 unsigned char* rowptr = row_data.get();
592 for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) {
593 if (!jpeg_read_scanlines(&cinfo, &rowptr, 1))
594 return false;
595 converter(rowptr, *w, &(*output)[row * row_write_stride]);
598 #endif
600 jpeg_finish_decompress(&cinfo);
601 jpeg_destroy_decompress(&cinfo);
602 return true;
605 // static
606 SkBitmap* JPEGCodec::Decode(const unsigned char* input, size_t input_size) {
607 int w, h;
608 std::vector<unsigned char> data_vector;
609 if (!Decode(input, input_size, FORMAT_SkBitmap, &data_vector, &w, &h))
610 return NULL;
612 // Skia only handles 32 bit images.
613 int data_length = w * h * 4;
615 SkBitmap* bitmap = new SkBitmap();
616 bitmap->allocN32Pixels(w, h);
617 memcpy(bitmap->getAddr32(0, 0), &data_vector[0], data_length);
619 return bitmap;
622 } // namespace gfx