OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 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. | |
4 | |
5 #include <setjmp.h> | |
6 | |
7 #include "chrome/common/jpeg_codec.h" | |
8 #include "base/logging.h" | |
9 #include "base/scoped_ptr.h" | |
10 #include "SkBitmap.h" | |
11 | |
12 extern "C" { | |
13 #include "jpeglib.h" | |
14 } | |
15 | |
16 // Encoder/decoder shared stuff ------------------------------------------------ | |
17 | |
18 namespace { | |
19 | |
20 // used to pass error info through the JPEG library | |
21 struct CoderErrorMgr { | |
22 jpeg_error_mgr pub; | |
23 jmp_buf setjmp_buffer; | |
24 }; | |
25 | |
26 void ErrorExit(jpeg_common_struct* cinfo) { | |
27 CoderErrorMgr *err = reinterpret_cast<CoderErrorMgr*>(cinfo->err); | |
28 | |
29 // Return control to the setjmp point. | |
30 longjmp(err->setjmp_buffer, false); | |
31 } | |
32 | |
33 } // namespace | |
34 | |
35 // Encoder --------------------------------------------------------------------- | |
36 // | |
37 // This code is based on nsJPEGEncoder from Mozilla. | |
38 // Copyright 2005 Google Inc. (Brett Wilson, contributor) | |
39 | |
40 namespace { | |
41 | |
42 // Initial size for the output buffer in the JpegEncoderState below. | |
43 const static int initial_output_buffer_size = 8192; | |
44 | |
45 struct JpegEncoderState { | |
46 JpegEncoderState(std::vector<unsigned char>* o) : out(o), image_buffer_used(0)
{ | |
47 } | |
48 | |
49 // Output buffer, of which 'image_buffer_used' bytes are actually used (this | |
50 // will often be less than the actual size of the vector because we size it | |
51 // so that libjpeg can write directly into it. | |
52 std::vector<unsigned char>* out; | |
53 | |
54 // Number of bytes in the 'out' buffer that are actually used (see above). | |
55 size_t image_buffer_used; | |
56 }; | |
57 | |
58 // Initializes the JpegEncoderState for encoding, and tells libjpeg about where | |
59 // the output buffer is. | |
60 // | |
61 // From the JPEG library: | |
62 // "Initialize destination. This is called by jpeg_start_compress() before | |
63 // any data is actually written. It must initialize next_output_byte and | |
64 // free_in_buffer. free_in_buffer must be initialized to a positive value." | |
65 void InitDestination(jpeg_compress_struct* cinfo) { | |
66 JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data); | |
67 DCHECK(state->image_buffer_used == 0) << "initializing after use"; | |
68 | |
69 state->out->resize(initial_output_buffer_size); | |
70 state->image_buffer_used = 0; | |
71 | |
72 cinfo->dest->next_output_byte = &(*state->out)[0]; | |
73 cinfo->dest->free_in_buffer = initial_output_buffer_size; | |
74 } | |
75 | |
76 // Resize the buffer that we give to libjpeg and update our and its state. | |
77 // | |
78 // From the JPEG library: | |
79 // "Callback used by libjpeg whenever the buffer has filled (free_in_buffer | |
80 // reaches zero). In typical applications, it should write out the *entire* | |
81 // buffer (use the saved start address and buffer length; ignore the current | |
82 // state of next_output_byte and free_in_buffer). Then reset the pointer & | |
83 // count to the start of the buffer, and return TRUE indicating that the | |
84 // buffer has been dumped. free_in_buffer must be set to a positive value | |
85 // when TRUE is returned. A FALSE return should only be used when I/O | |
86 // suspension is desired (this operating mode is discussed in the next | |
87 // section)." | |
88 boolean EmptyOutputBuffer(jpeg_compress_struct* cinfo) { | |
89 JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data); | |
90 | |
91 // note the new size, the buffer is full | |
92 state->image_buffer_used = state->out->size(); | |
93 | |
94 // expand buffer, just double size each time | |
95 state->out->resize(state->out->size() * 2); | |
96 | |
97 // tell libjpeg where to write the next data | |
98 cinfo->dest->next_output_byte = &(*state->out)[state->image_buffer_used]; | |
99 cinfo->dest->free_in_buffer = state->out->size() - state->image_buffer_used; | |
100 return 1; | |
101 } | |
102 | |
103 // Cleans up the JpegEncoderState to prepare for returning in the final form. | |
104 // | |
105 // From the JPEG library: | |
106 // "Terminate destination --- called by jpeg_finish_compress() after all data | |
107 // has been written. In most applications, this must flush any data | |
108 // remaining in the buffer. Use either next_output_byte or free_in_buffer to | |
109 // determine how much data is in the buffer." | |
110 void TermDestination(jpeg_compress_struct* cinfo) { | |
111 JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data); | |
112 DCHECK(state->out->size() >= state->image_buffer_used); | |
113 | |
114 // update the used byte based on the next byte libjpeg would write to | |
115 state->image_buffer_used = cinfo->dest->next_output_byte - &(*state->out)[0]; | |
116 DCHECK(state->image_buffer_used < state->out->size()) << | |
117 "JPEG library busted, got a bad image buffer size"; | |
118 | |
119 // update our buffer so that it exactly encompases the desired data | |
120 state->out->resize(state->image_buffer_used); | |
121 } | |
122 | |
123 // Converts RGBA to RGB (removing the alpha values) to prepare to send data to | |
124 // libjpeg. This converts one row of data in rgba with the given width in | |
125 // pixels the the given rgb destination buffer (which should have enough space | |
126 // reserved for the final data). | |
127 void StripAlpha(const unsigned char* rgba, int pixel_width, unsigned char* rgb) | |
128 { | |
129 for (int x = 0; x < pixel_width; x++) { | |
130 const unsigned char* pixel_in = &rgba[x * 4]; | |
131 unsigned char* pixel_out = &rgb[x * 3]; | |
132 pixel_out[0] = pixel_in[0]; | |
133 pixel_out[1] = pixel_in[1]; | |
134 pixel_out[2] = pixel_in[2]; | |
135 } | |
136 } | |
137 | |
138 // Converts BGRA to RGB by reordering the color components and dropping the | |
139 // alpha. This converts one row of data in rgba with the given width in | |
140 // pixels the the given rgb destination buffer (which should have enough space | |
141 // reserved for the final data). | |
142 void BGRAtoRGB(const unsigned char* bgra, int pixel_width, unsigned char* rgb) | |
143 { | |
144 for (int x = 0; x < pixel_width; x++) { | |
145 const unsigned char* pixel_in = &bgra[x * 4]; | |
146 unsigned char* pixel_out = &rgb[x * 3]; | |
147 pixel_out[0] = pixel_in[2]; | |
148 pixel_out[1] = pixel_in[1]; | |
149 pixel_out[2] = pixel_in[0]; | |
150 } | |
151 } | |
152 | |
153 // This class destroys the given jpeg_compress object when it goes out of | |
154 // scope. It simplifies the error handling in Encode (and even applies to the | |
155 // success case). | |
156 class CompressDestroyer { | |
157 public: | |
158 CompressDestroyer() : cinfo_(NULL) { | |
159 } | |
160 ~CompressDestroyer() { | |
161 DestroyManagedObject(); | |
162 } | |
163 void SetManagedObject(jpeg_compress_struct* ci) { | |
164 DestroyManagedObject(); | |
165 cinfo_ = ci; | |
166 } | |
167 void DestroyManagedObject() { | |
168 if (cinfo_) { | |
169 jpeg_destroy_compress(cinfo_); | |
170 cinfo_ = NULL; | |
171 } | |
172 } | |
173 private: | |
174 jpeg_compress_struct* cinfo_; | |
175 }; | |
176 | |
177 } // namespace | |
178 | |
179 bool JPEGCodec::Encode(const unsigned char* input, ColorFormat format, | |
180 int w, int h, int row_byte_width, | |
181 int quality, std::vector<unsigned char>* output) { | |
182 jpeg_compress_struct cinfo; | |
183 CompressDestroyer destroyer; | |
184 output->clear(); | |
185 | |
186 // We set up the normal JPEG error routines, then override error_exit. | |
187 // This must be done before the call to create_compress. | |
188 CoderErrorMgr errmgr; | |
189 cinfo.err = jpeg_std_error(&errmgr.pub); | |
190 errmgr.pub.error_exit = ErrorExit; | |
191 // Establish the setjmp return context for ErrorExit to use. | |
192 if (setjmp(errmgr.setjmp_buffer)) { | |
193 // If we get here, the JPEG code has signaled an error. | |
194 // MSDN notes: "if you intend your code to be portable, do not rely on | |
195 // correct destruction of frame-based objects when executing a nonlocal | |
196 // goto using a call to longjmp." So we delete the CompressDestroyer's | |
197 // object manually instead. | |
198 destroyer.DestroyManagedObject(); | |
199 return false; | |
200 } | |
201 | |
202 // The destroyer will destroy() cinfo on exit. | |
203 jpeg_create_compress(&cinfo); | |
204 destroyer.SetManagedObject(&cinfo); | |
205 | |
206 cinfo.image_width = w; | |
207 cinfo.image_height = h; | |
208 cinfo.input_components = 3; | |
209 cinfo.in_color_space = JCS_RGB; | |
210 cinfo.data_precision = 8; | |
211 | |
212 jpeg_set_defaults(&cinfo); | |
213 jpeg_set_quality(&cinfo, quality, 1); // quality here is 0-100 | |
214 | |
215 // set up the destination manager | |
216 jpeg_destination_mgr destmgr; | |
217 destmgr.init_destination = InitDestination; | |
218 destmgr.empty_output_buffer = EmptyOutputBuffer; | |
219 destmgr.term_destination = TermDestination; | |
220 cinfo.dest = &destmgr; | |
221 | |
222 JpegEncoderState state(output); | |
223 cinfo.client_data = &state; | |
224 | |
225 jpeg_start_compress(&cinfo, 1); | |
226 | |
227 // feed it the rows, doing necessary conversions for the color format | |
228 if (format == FORMAT_RGB) { | |
229 // no conversion necessary | |
230 while (cinfo.next_scanline < cinfo.image_height) { | |
231 const unsigned char* row = &input[cinfo.next_scanline * row_byte_width]; | |
232 jpeg_write_scanlines(&cinfo, const_cast<unsigned char**>(&row), 1); | |
233 } | |
234 } else { | |
235 // get the correct format converter | |
236 void (*converter)(const unsigned char* in, int w, unsigned char* rgb); | |
237 if (format == FORMAT_RGBA) { | |
238 converter = StripAlpha; | |
239 } else if (format == FORMAT_BGRA) { | |
240 converter = BGRAtoRGB; | |
241 } else { | |
242 NOTREACHED() << "Invalid pixel format"; | |
243 return false; | |
244 } | |
245 | |
246 // output row after converting | |
247 unsigned char* row = new unsigned char[w * 3]; | |
248 | |
249 while (cinfo.next_scanline < cinfo.image_height) { | |
250 converter(&input[cinfo.next_scanline * row_byte_width], w, row); | |
251 jpeg_write_scanlines(&cinfo, &row, 1); | |
252 } | |
253 delete[] row; | |
254 } | |
255 | |
256 jpeg_finish_compress(&cinfo); | |
257 return true; | |
258 } | |
259 | |
260 // Decoder -------------------------------------------------------------------- | |
261 | |
262 namespace { | |
263 | |
264 struct JpegDecoderState { | |
265 JpegDecoderState(const unsigned char* in, size_t len) | |
266 : input_buffer(in), input_buffer_length(len) { | |
267 } | |
268 | |
269 const unsigned char* input_buffer; | |
270 size_t input_buffer_length; | |
271 }; | |
272 | |
273 // Callback to initialize the source. | |
274 // | |
275 // From the JPEG library: | |
276 // "Initialize source. This is called by jpeg_read_header() before any data is | |
277 // actually read. May leave bytes_in_buffer set to 0 (in which case a | |
278 // fill_input_buffer() call will occur immediately)." | |
279 void InitSource(j_decompress_ptr cinfo) { | |
280 JpegDecoderState* state = static_cast<JpegDecoderState*>(cinfo->client_data); | |
281 cinfo->src->next_input_byte = state->input_buffer; | |
282 cinfo->src->bytes_in_buffer = state->input_buffer_length; | |
283 } | |
284 | |
285 // Callback to fill the buffer. Since our buffer already contains all the data, | |
286 // we should never need to provide more data. If libjpeg thinks it needs more | |
287 // data, our input is probably corrupt. | |
288 // | |
289 // From the JPEG library: | |
290 // "This is called whenever bytes_in_buffer has reached zero and more data is | |
291 // wanted. In typical applications, it should read fresh data into the buffer | |
292 // (ignoring the current state of next_input_byte and bytes_in_buffer), reset | |
293 // the pointer & count to the start of the buffer, and return TRUE indicating | |
294 // that the buffer has been reloaded. It is not necessary to fill the buffer | |
295 // entirely, only to obtain at least one more byte. bytes_in_buffer MUST be | |
296 // set to a positive value if TRUE is returned. A FALSE return should only | |
297 // be used when I/O suspension is desired." | |
298 boolean FillInputBuffer(j_decompress_ptr cinfo) { | |
299 return false; | |
300 } | |
301 | |
302 // Skip data in the buffer. Since we have all the data at once, this operation | |
303 // is easy. It is not clear if this ever gets called because the JPEG library | |
304 // should be able to do the skip itself (it has all the data). | |
305 // | |
306 // From the JPEG library: | |
307 // "Skip num_bytes worth of data. The buffer pointer and count should be | |
308 // advanced over num_bytes input bytes, refilling the buffer as needed. This | |
309 // is used to skip over a potentially large amount of uninteresting data | |
310 // (such as an APPn marker). In some applications it may be possible to | |
311 // optimize away the reading of the skipped data, but it's not clear that | |
312 // being smart is worth much trouble; large skips are uncommon. | |
313 // bytes_in_buffer may be zero on return. A zero or negative skip count | |
314 // should be treated as a no-op." | |
315 void SkipInputData(j_decompress_ptr cinfo, long num_bytes) { | |
316 if (num_bytes > static_cast<long>(cinfo->src->bytes_in_buffer)) { | |
317 // Since all our data should be in the buffer, trying to skip beyond it | |
318 // means that there is some kind of error or corrupt input data. A 0 for | |
319 // bytes left means it will call FillInputBuffer which will then fail. | |
320 cinfo->src->next_input_byte += cinfo->src->bytes_in_buffer; | |
321 cinfo->src->bytes_in_buffer = 0; | |
322 } else if (num_bytes > 0) { | |
323 cinfo->src->bytes_in_buffer -= static_cast<size_t>(num_bytes); | |
324 cinfo->src->next_input_byte += num_bytes; | |
325 } | |
326 } | |
327 | |
328 // Our source doesn't need any cleanup, so this is a NOP. | |
329 // | |
330 // From the JPEG library: | |
331 // "Terminate source --- called by jpeg_finish_decompress() after all data has | |
332 // been read to clean up JPEG source manager. NOT called by jpeg_abort() or | |
333 // jpeg_destroy()." | |
334 void TermSource(j_decompress_ptr cinfo) { | |
335 } | |
336 | |
337 // Converts one row of rgb data to rgba data by adding a fully-opaque alpha | |
338 // value. | |
339 void AddAlpha(const unsigned char* rgb, int pixel_width, unsigned char* rgba) { | |
340 for (int x = 0; x < pixel_width; x++) { | |
341 const unsigned char* pixel_in = &rgb[x * 3]; | |
342 unsigned char* pixel_out = &rgba[x * 4]; | |
343 pixel_out[0] = pixel_in[0]; | |
344 pixel_out[1] = pixel_in[1]; | |
345 pixel_out[2] = pixel_in[2]; | |
346 pixel_out[3] = 0xff; | |
347 } | |
348 } | |
349 | |
350 // Converts one row of RGB data to BGRA by reordering the color components and | |
351 // adding alpha values of 0xff. | |
352 void RGBtoBGRA(const unsigned char* bgra, int pixel_width, unsigned char* rgb) | |
353 { | |
354 for (int x = 0; x < pixel_width; x++) { | |
355 const unsigned char* pixel_in = &bgra[x * 3]; | |
356 unsigned char* pixel_out = &rgb[x * 4]; | |
357 pixel_out[0] = pixel_in[2]; | |
358 pixel_out[1] = pixel_in[1]; | |
359 pixel_out[2] = pixel_in[0]; | |
360 pixel_out[3] = 0xff; | |
361 } | |
362 } | |
363 | |
364 // This class destroys the given jpeg_decompress object when it goes out of | |
365 // scope. It simplifies the error handling in Decode (and even applies to the | |
366 // success case). | |
367 class DecompressDestroyer { | |
368 public: | |
369 DecompressDestroyer() : cinfo_(NULL) { | |
370 } | |
371 ~DecompressDestroyer() { | |
372 DestroyManagedObject(); | |
373 } | |
374 void SetManagedObject(jpeg_decompress_struct* ci) { | |
375 DestroyManagedObject(); | |
376 cinfo_ = ci; | |
377 } | |
378 void DestroyManagedObject() { | |
379 if (cinfo_) { | |
380 jpeg_destroy_decompress(cinfo_); | |
381 cinfo_ = NULL; | |
382 } | |
383 } | |
384 private: | |
385 jpeg_decompress_struct* cinfo_; | |
386 }; | |
387 | |
388 } // namespace | |
389 | |
390 bool JPEGCodec::Decode(const unsigned char* input, size_t input_size, | |
391 ColorFormat format, std::vector<unsigned char>* output, | |
392 int* w, int* h) { | |
393 jpeg_decompress_struct cinfo; | |
394 DecompressDestroyer destroyer; | |
395 output->clear(); | |
396 | |
397 // We set up the normal JPEG error routines, then override error_exit. | |
398 // This must be done before the call to create_decompress. | |
399 CoderErrorMgr errmgr; | |
400 cinfo.err = jpeg_std_error(&errmgr.pub); | |
401 errmgr.pub.error_exit = ErrorExit; | |
402 // Establish the setjmp return context for ErrorExit to use. | |
403 if (setjmp(errmgr.setjmp_buffer)) { | |
404 // If we get here, the JPEG code has signaled an error. | |
405 // See note in JPEGCodec::Encode() for why we need to destroy the cinfo | |
406 // manually here. | |
407 destroyer.DestroyManagedObject(); | |
408 return false; | |
409 } | |
410 | |
411 // The destroyer will destroy() cinfo on exit. We don't want to set the | |
412 // destroyer's object until cinfo is initialized. | |
413 jpeg_create_decompress(&cinfo); | |
414 destroyer.SetManagedObject(&cinfo); | |
415 | |
416 // set up the source manager | |
417 jpeg_source_mgr srcmgr; | |
418 srcmgr.init_source = InitSource; | |
419 srcmgr.fill_input_buffer = FillInputBuffer; | |
420 srcmgr.skip_input_data = SkipInputData; | |
421 srcmgr.resync_to_restart = jpeg_resync_to_restart; // use default routine | |
422 srcmgr.term_source = TermSource; | |
423 cinfo.src = &srcmgr; | |
424 | |
425 JpegDecoderState state(input, input_size); | |
426 cinfo.client_data = &state; | |
427 | |
428 // fill the file metadata into our buffer | |
429 if (jpeg_read_header(&cinfo, true) != JPEG_HEADER_OK) | |
430 return false; | |
431 | |
432 // we want to always get RGB data out | |
433 switch (cinfo.jpeg_color_space) { | |
434 case JCS_GRAYSCALE: | |
435 case JCS_RGB: | |
436 case JCS_YCbCr: | |
437 cinfo.out_color_space = JCS_RGB; | |
438 break; | |
439 case JCS_CMYK: | |
440 case JCS_YCCK: | |
441 default: | |
442 // Mozilla errors out on these color spaces, so I presume that the jpeg | |
443 // library can't do automatic color space conversion for them. We don't | |
444 // care about these anyway. | |
445 return false; | |
446 } | |
447 cinfo.output_components = 3; | |
448 | |
449 jpeg_calc_output_dimensions(&cinfo); | |
450 *w = cinfo.output_width; | |
451 *h = cinfo.output_height; | |
452 | |
453 jpeg_start_decompress(&cinfo); | |
454 | |
455 // FIXME(brettw) we may want to allow the capability for callers to request | |
456 // how to align row lengths as we do for the compressor. | |
457 int row_read_stride = cinfo.output_width * cinfo.output_components; | |
458 | |
459 if (format == FORMAT_RGB) { | |
460 // easy case, row needs no conversion | |
461 int row_write_stride = row_read_stride; | |
462 output->resize(row_write_stride * cinfo.output_height); | |
463 | |
464 for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) { | |
465 unsigned char* rowptr = &(*output)[row * row_write_stride]; | |
466 if (!jpeg_read_scanlines(&cinfo, &rowptr, 1)) | |
467 return false; | |
468 } | |
469 } else { | |
470 // Rows need conversion to output format: read into a temporary buffer and | |
471 // expand to the final one. Performance: we could avoid the extra | |
472 // allocation by doing the expansion in-place. | |
473 int row_write_stride; | |
474 void (*converter)(const unsigned char* rgb, int w, unsigned char* out); | |
475 if (format == FORMAT_RGBA) { | |
476 row_write_stride = cinfo.output_width * 4; | |
477 converter = AddAlpha; | |
478 } else if (format == FORMAT_BGRA) { | |
479 row_write_stride = cinfo.output_width * 4; | |
480 converter = RGBtoBGRA; | |
481 } else { | |
482 NOTREACHED() << "Invalid pixel format"; | |
483 jpeg_destroy_decompress(&cinfo); | |
484 return false; | |
485 } | |
486 | |
487 output->resize(row_write_stride * cinfo.output_height); | |
488 | |
489 scoped_array<unsigned char> row_data(new unsigned char[row_read_stride]); | |
490 unsigned char* rowptr = row_data.get(); | |
491 for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) { | |
492 if (!jpeg_read_scanlines(&cinfo, &rowptr, 1)) | |
493 return false; | |
494 converter(rowptr, *w, &(*output)[row * row_write_stride]); | |
495 } | |
496 } | |
497 | |
498 jpeg_finish_decompress(&cinfo); | |
499 jpeg_destroy_decompress(&cinfo); | |
500 return true; | |
501 } | |
502 | |
503 // static | |
504 SkBitmap* JPEGCodec::Decode(const unsigned char* input, size_t input_size) { | |
505 int w, h; | |
506 std::vector<unsigned char> data_vector; | |
507 // Use FORMAT_BGRA as that maps to Skia's 32 bit (kARGB_8888_Config) format. | |
508 if (!Decode(input, input_size, FORMAT_BGRA, &data_vector, &w, &h)) | |
509 return NULL; | |
510 | |
511 // Skia only handles 32 bit images. | |
512 int data_length = w * h * 4; | |
513 | |
514 SkBitmap* bitmap = new SkBitmap(); | |
515 bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h); | |
516 bitmap->allocPixels(); | |
517 memcpy(bitmap->getAddr32(0, 0), &data_vector[0], data_length); | |
518 | |
519 return bitmap; | |
520 } | |
521 | |
OLD | NEW |