Change next_proto member type.
[chromium-blink-merge.git] / tools / imagediff / image_diff_png.cc
blob7524026c192038c964824bb7da7380385253c3fe
1 // Copyright 2013 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 "tools/imagediff/image_diff_png.h"
7 #include <stdlib.h>
8 #include <string.h>
10 #include "base/logging.h"
11 #include "build/build_config.h"
12 #include "third_party/libpng/png.h"
13 #include "third_party/zlib/zlib.h"
15 namespace image_diff_png {
17 // This is a duplicate of ui/gfx/codec/png_codec.cc, after removing code related
18 // to Skia, that we can use when running layout tests with minimal dependencies.
19 namespace {
21 enum ColorFormat {
22 // 3 bytes per pixel (packed), in RGB order regardless of endianness.
23 // This is the native JPEG format.
24 FORMAT_RGB,
26 // 4 bytes per pixel, in RGBA order in memory regardless of endianness.
27 FORMAT_RGBA,
29 // 4 bytes per pixel, in BGRA order in memory regardless of endianness.
30 // This is the default Windows DIB order.
31 FORMAT_BGRA,
33 // 4 bytes per pixel, in pre-multiplied kARGB_8888_Config format. For use
34 // with directly writing to a skia bitmap.
35 FORMAT_SkBitmap
38 // Represents a comment in the tEXt ancillary chunk of the png.
39 struct Comment {
40 std::string key;
41 std::string text;
44 // Converts BGRA->RGBA and RGBA->BGRA.
45 void ConvertBetweenBGRAandRGBA(const unsigned char* input, int pixel_width,
46 unsigned char* output, bool* is_opaque) {
47 for (int x = 0; x < pixel_width; x++) {
48 const unsigned char* pixel_in = &input[x * 4];
49 unsigned char* pixel_out = &output[x * 4];
50 pixel_out[0] = pixel_in[2];
51 pixel_out[1] = pixel_in[1];
52 pixel_out[2] = pixel_in[0];
53 pixel_out[3] = pixel_in[3];
57 void ConvertRGBAtoRGB(const unsigned char* rgba, int pixel_width,
58 unsigned char* rgb, bool* is_opaque) {
59 for (int x = 0; x < pixel_width; x++) {
60 const unsigned char* pixel_in = &rgba[x * 4];
61 unsigned char* pixel_out = &rgb[x * 3];
62 pixel_out[0] = pixel_in[0];
63 pixel_out[1] = pixel_in[1];
64 pixel_out[2] = pixel_in[2];
68 } // namespace
70 // Decoder --------------------------------------------------------------------
72 // This code is based on WebKit libpng interface (PNGImageDecoder), which is
73 // in turn based on the Mozilla png decoder.
75 namespace {
77 // Gamma constants: We assume we're on Windows which uses a gamma of 2.2.
78 const double kMaxGamma = 21474.83; // Maximum gamma accepted by png library.
79 const double kDefaultGamma = 2.2;
80 const double kInverseGamma = 1.0 / kDefaultGamma;
82 class PngDecoderState {
83 public:
84 // Output is a vector<unsigned char>.
85 PngDecoderState(ColorFormat ofmt, std::vector<unsigned char>* o)
86 : output_format(ofmt),
87 output_channels(0),
88 is_opaque(true),
89 output(o),
90 row_converter(NULL),
91 width(0),
92 height(0),
93 done(false) {
96 ColorFormat output_format;
97 int output_channels;
99 // Used during the reading of an SkBitmap. Defaults to true until we see a
100 // pixel with anything other than an alpha of 255.
101 bool is_opaque;
103 // An intermediary buffer for decode output.
104 std::vector<unsigned char>* output;
106 // Called to convert a row from the library to the correct output format.
107 // When NULL, no conversion is necessary.
108 void (*row_converter)(const unsigned char* in, int w, unsigned char* out,
109 bool* is_opaque);
111 // Size of the image, set in the info callback.
112 int width;
113 int height;
115 // Set to true when we've found the end of the data.
116 bool done;
119 void ConvertRGBtoRGBA(const unsigned char* rgb, int pixel_width,
120 unsigned char* rgba, bool* is_opaque) {
121 for (int x = 0; x < pixel_width; x++) {
122 const unsigned char* pixel_in = &rgb[x * 3];
123 unsigned char* pixel_out = &rgba[x * 4];
124 pixel_out[0] = pixel_in[0];
125 pixel_out[1] = pixel_in[1];
126 pixel_out[2] = pixel_in[2];
127 pixel_out[3] = 0xff;
131 void ConvertRGBtoBGRA(const unsigned char* rgb, int pixel_width,
132 unsigned char* bgra, bool* is_opaque) {
133 for (int x = 0; x < pixel_width; x++) {
134 const unsigned char* pixel_in = &rgb[x * 3];
135 unsigned char* pixel_out = &bgra[x * 4];
136 pixel_out[0] = pixel_in[2];
137 pixel_out[1] = pixel_in[1];
138 pixel_out[2] = pixel_in[0];
139 pixel_out[3] = 0xff;
143 // Called when the png header has been read. This code is based on the WebKit
144 // PNGImageDecoder
145 void DecodeInfoCallback(png_struct* png_ptr, png_info* info_ptr) {
146 PngDecoderState* state = static_cast<PngDecoderState*>(
147 png_get_progressive_ptr(png_ptr));
149 int bit_depth, color_type, interlace_type, compression_type;
150 int filter_type, channels;
151 png_uint_32 w, h;
152 png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
153 &interlace_type, &compression_type, &filter_type);
155 // Bounds check. When the image is unreasonably big, we'll error out and
156 // end up back at the setjmp call when we set up decoding. "Unreasonably big"
157 // means "big enough that w * h * 32bpp might overflow an int"; we choose this
158 // threshold to match WebKit and because a number of places in code assume
159 // that an image's size (in bytes) fits in a (signed) int.
160 unsigned long long total_size =
161 static_cast<unsigned long long>(w) * static_cast<unsigned long long>(h);
162 if (total_size > ((1 << 29) - 1))
163 longjmp(png_jmpbuf(png_ptr), 1);
164 state->width = static_cast<int>(w);
165 state->height = static_cast<int>(h);
167 // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
168 if (color_type == PNG_COLOR_TYPE_PALETTE ||
169 (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8))
170 png_set_expand(png_ptr);
172 // Transparency for paletted images.
173 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
174 png_set_expand(png_ptr);
176 // Convert 16-bit to 8-bit.
177 if (bit_depth == 16)
178 png_set_strip_16(png_ptr);
180 // Expand grayscale to RGB.
181 if (color_type == PNG_COLOR_TYPE_GRAY ||
182 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
183 png_set_gray_to_rgb(png_ptr);
185 // Deal with gamma and keep it under our control.
186 double gamma;
187 if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
188 if (gamma <= 0.0 || gamma > kMaxGamma) {
189 gamma = kInverseGamma;
190 png_set_gAMA(png_ptr, info_ptr, gamma);
192 png_set_gamma(png_ptr, kDefaultGamma, gamma);
193 } else {
194 png_set_gamma(png_ptr, kDefaultGamma, kInverseGamma);
197 // Tell libpng to send us rows for interlaced pngs.
198 if (interlace_type == PNG_INTERLACE_ADAM7)
199 png_set_interlace_handling(png_ptr);
201 // Update our info now
202 png_read_update_info(png_ptr, info_ptr);
203 channels = png_get_channels(png_ptr, info_ptr);
205 // Pick our row format converter necessary for this data.
206 if (channels == 3) {
207 switch (state->output_format) {
208 case FORMAT_RGB:
209 state->row_converter = NULL; // no conversion necessary
210 state->output_channels = 3;
211 break;
212 case FORMAT_RGBA:
213 state->row_converter = &ConvertRGBtoRGBA;
214 state->output_channels = 4;
215 break;
216 case FORMAT_BGRA:
217 state->row_converter = &ConvertRGBtoBGRA;
218 state->output_channels = 4;
219 break;
220 default:
221 NOTREACHED() << "Unknown output format";
222 break;
224 } else if (channels == 4) {
225 switch (state->output_format) {
226 case FORMAT_RGB:
227 state->row_converter = &ConvertRGBAtoRGB;
228 state->output_channels = 3;
229 break;
230 case FORMAT_RGBA:
231 state->row_converter = NULL; // no conversion necessary
232 state->output_channels = 4;
233 break;
234 case FORMAT_BGRA:
235 state->row_converter = &ConvertBetweenBGRAandRGBA;
236 state->output_channels = 4;
237 break;
238 default:
239 NOTREACHED() << "Unknown output format";
240 break;
242 } else {
243 NOTREACHED() << "Unknown input channels";
244 longjmp(png_jmpbuf(png_ptr), 1);
247 state->output->resize(
248 state->width * state->output_channels * state->height);
251 void DecodeRowCallback(png_struct* png_ptr, png_byte* new_row,
252 png_uint_32 row_num, int pass) {
253 PngDecoderState* state = static_cast<PngDecoderState*>(
254 png_get_progressive_ptr(png_ptr));
256 DCHECK(pass == 0);
257 if (static_cast<int>(row_num) > state->height) {
258 NOTREACHED() << "Invalid row";
259 return;
262 unsigned char* base = NULL;
263 base = &state->output->front();
265 unsigned char* dest = &base[state->width * state->output_channels * row_num];
266 if (state->row_converter)
267 state->row_converter(new_row, state->width, dest, &state->is_opaque);
268 else
269 memcpy(dest, new_row, state->width * state->output_channels);
272 void DecodeEndCallback(png_struct* png_ptr, png_info* info) {
273 PngDecoderState* state = static_cast<PngDecoderState*>(
274 png_get_progressive_ptr(png_ptr));
276 // Mark the image as complete, this will tell the Decode function that we
277 // have successfully found the end of the data.
278 state->done = true;
281 // Automatically destroys the given read structs on destruction to make
282 // cleanup and error handling code cleaner.
283 class PngReadStructDestroyer {
284 public:
285 PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) {
287 ~PngReadStructDestroyer() {
288 png_destroy_read_struct(ps_, pi_, NULL);
290 private:
291 png_struct** ps_;
292 png_info** pi_;
295 bool BuildPNGStruct(const unsigned char* input, size_t input_size,
296 png_struct** png_ptr, png_info** info_ptr) {
297 if (input_size < 8)
298 return false; // Input data too small to be a png
300 // Have libpng check the signature, it likes the first 8 bytes.
301 if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0)
302 return false;
304 *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
305 if (!*png_ptr)
306 return false;
308 *info_ptr = png_create_info_struct(*png_ptr);
309 if (!*info_ptr) {
310 png_destroy_read_struct(png_ptr, NULL, NULL);
311 return false;
314 return true;
317 } // namespace
319 // static
320 bool Decode(const unsigned char* input, size_t input_size,
321 ColorFormat format, std::vector<unsigned char>* output,
322 int* w, int* h) {
323 png_struct* png_ptr = NULL;
324 png_info* info_ptr = NULL;
325 if (!BuildPNGStruct(input, input_size, &png_ptr, &info_ptr))
326 return false;
328 PngReadStructDestroyer destroyer(&png_ptr, &info_ptr);
329 if (setjmp(png_jmpbuf(png_ptr))) {
330 // The destroyer will ensure that the structures are cleaned up in this
331 // case, even though we may get here as a jump from random parts of the
332 // PNG library called below.
333 return false;
336 PngDecoderState state(format, output);
338 png_set_progressive_read_fn(png_ptr, &state, &DecodeInfoCallback,
339 &DecodeRowCallback, &DecodeEndCallback);
340 png_process_data(png_ptr,
341 info_ptr,
342 const_cast<unsigned char*>(input),
343 input_size);
345 if (!state.done) {
346 // Fed it all the data but the library didn't think we got all the data, so
347 // this file must be truncated.
348 output->clear();
349 return false;
352 *w = state.width;
353 *h = state.height;
354 return true;
357 // Encoder --------------------------------------------------------------------
359 // This section of the code is based on nsPNGEncoder.cpp in Mozilla
360 // (Copyright 2005 Google Inc.)
362 namespace {
364 // Passed around as the io_ptr in the png structs so our callbacks know where
365 // to write data.
366 struct PngEncoderState {
367 explicit PngEncoderState(std::vector<unsigned char>* o) : out(o) {}
368 std::vector<unsigned char>* out;
371 // Called by libpng to flush its internal buffer to ours.
372 void EncoderWriteCallback(png_structp png, png_bytep data, png_size_t size) {
373 PngEncoderState* state = static_cast<PngEncoderState*>(png_get_io_ptr(png));
374 DCHECK(state->out);
376 size_t old_size = state->out->size();
377 state->out->resize(old_size + size);
378 memcpy(&(*state->out)[old_size], data, size);
381 void FakeFlushCallback(png_structp png) {
382 // We don't need to perform any flushing since we aren't doing real IO, but
383 // we're required to provide this function by libpng.
386 void ConvertBGRAtoRGB(const unsigned char* bgra, int pixel_width,
387 unsigned char* rgb, bool* is_opaque) {
388 for (int x = 0; x < pixel_width; x++) {
389 const unsigned char* pixel_in = &bgra[x * 4];
390 unsigned char* pixel_out = &rgb[x * 3];
391 pixel_out[0] = pixel_in[2];
392 pixel_out[1] = pixel_in[1];
393 pixel_out[2] = pixel_in[0];
397 #ifdef PNG_TEXT_SUPPORTED
399 inline char* strdup(const char* str) {
400 #if defined(OS_WIN)
401 return _strdup(str);
402 #else
403 return ::strdup(str);
404 #endif
407 class CommentWriter {
408 public:
409 explicit CommentWriter(const std::vector<Comment>& comments)
410 : comments_(comments),
411 png_text_(new png_text[comments.size()]) {
412 for (size_t i = 0; i < comments.size(); ++i)
413 AddComment(i, comments[i]);
416 ~CommentWriter() {
417 for (size_t i = 0; i < comments_.size(); ++i) {
418 free(png_text_[i].key);
419 free(png_text_[i].text);
421 delete [] png_text_;
424 bool HasComments() {
425 return !comments_.empty();
428 png_text* get_png_text() {
429 return png_text_;
432 int size() {
433 return static_cast<int>(comments_.size());
436 private:
437 void AddComment(size_t pos, const Comment& comment) {
438 png_text_[pos].compression = PNG_TEXT_COMPRESSION_NONE;
439 // A PNG comment's key can only be 79 characters long.
440 DCHECK(comment.key.length() < 79);
441 png_text_[pos].key = strdup(comment.key.substr(0, 78).c_str());
442 png_text_[pos].text = strdup(comment.text.c_str());
443 png_text_[pos].text_length = comment.text.length();
444 #ifdef PNG_iTXt_SUPPORTED
445 png_text_[pos].itxt_length = 0;
446 png_text_[pos].lang = 0;
447 png_text_[pos].lang_key = 0;
448 #endif
451 const std::vector<Comment> comments_;
452 png_text* png_text_;
454 #endif // PNG_TEXT_SUPPORTED
456 // The type of functions usable for converting between pixel formats.
457 typedef void (*FormatConverter)(const unsigned char* in, int w,
458 unsigned char* out, bool* is_opaque);
460 // libpng uses a wacky setjmp-based API, which makes the compiler nervous.
461 // We constrain all of the calls we make to libpng where the setjmp() is in
462 // place to this function.
463 // Returns true on success.
464 bool DoLibpngWrite(png_struct* png_ptr, png_info* info_ptr,
465 PngEncoderState* state,
466 int width, int height, int row_byte_width,
467 const unsigned char* input, int compression_level,
468 int png_output_color_type, int output_color_components,
469 FormatConverter converter,
470 const std::vector<Comment>& comments) {
471 #ifdef PNG_TEXT_SUPPORTED
472 CommentWriter comment_writer(comments);
473 #endif
474 unsigned char* row_buffer = NULL;
476 // Make sure to not declare any locals here -- locals in the presence
477 // of setjmp() in C++ code makes gcc complain.
479 if (setjmp(png_jmpbuf(png_ptr))) {
480 delete[] row_buffer;
481 return false;
484 png_set_compression_level(png_ptr, compression_level);
486 // Set our callback for libpng to give us the data.
487 png_set_write_fn(png_ptr, state, EncoderWriteCallback, FakeFlushCallback);
489 png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_output_color_type,
490 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
491 PNG_FILTER_TYPE_DEFAULT);
493 #ifdef PNG_TEXT_SUPPORTED
494 if (comment_writer.HasComments()) {
495 png_set_text(png_ptr, info_ptr, comment_writer.get_png_text(),
496 comment_writer.size());
498 #endif
500 png_write_info(png_ptr, info_ptr);
502 if (!converter) {
503 // No conversion needed, give the data directly to libpng.
504 for (int y = 0; y < height; y ++) {
505 png_write_row(png_ptr,
506 const_cast<unsigned char*>(&input[y * row_byte_width]));
508 } else {
509 // Needs conversion using a separate buffer.
510 row_buffer = new unsigned char[width * output_color_components];
511 for (int y = 0; y < height; y ++) {
512 converter(&input[y * row_byte_width], width, row_buffer, NULL);
513 png_write_row(png_ptr, row_buffer);
515 delete[] row_buffer;
518 png_write_end(png_ptr, info_ptr);
519 return true;
522 } // namespace
524 // static
525 bool EncodeWithCompressionLevel(const unsigned char* input, ColorFormat format,
526 const int width, const int height,
527 int row_byte_width,
528 bool discard_transparency,
529 const std::vector<Comment>& comments,
530 int compression_level,
531 std::vector<unsigned char>* output) {
532 // Run to convert an input row into the output row format, NULL means no
533 // conversion is necessary.
534 FormatConverter converter = NULL;
536 int input_color_components, output_color_components;
537 int png_output_color_type;
538 switch (format) {
539 case FORMAT_RGB:
540 input_color_components = 3;
541 output_color_components = 3;
542 png_output_color_type = PNG_COLOR_TYPE_RGB;
543 discard_transparency = false;
544 break;
546 case FORMAT_RGBA:
547 input_color_components = 4;
548 if (discard_transparency) {
549 output_color_components = 3;
550 png_output_color_type = PNG_COLOR_TYPE_RGB;
551 converter = ConvertRGBAtoRGB;
552 } else {
553 output_color_components = 4;
554 png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
555 converter = NULL;
557 break;
559 case FORMAT_BGRA:
560 input_color_components = 4;
561 if (discard_transparency) {
562 output_color_components = 3;
563 png_output_color_type = PNG_COLOR_TYPE_RGB;
564 converter = ConvertBGRAtoRGB;
565 } else {
566 output_color_components = 4;
567 png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
568 converter = ConvertBetweenBGRAandRGBA;
570 break;
572 default:
573 NOTREACHED() << "Unknown pixel format";
574 return false;
577 // Row stride should be at least as long as the length of the data.
578 DCHECK(input_color_components * width <= row_byte_width);
580 png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
581 NULL, NULL, NULL);
582 if (!png_ptr)
583 return false;
584 png_info* info_ptr = png_create_info_struct(png_ptr);
585 if (!info_ptr) {
586 png_destroy_write_struct(&png_ptr, NULL);
587 return false;
590 PngEncoderState state(output);
591 bool success = DoLibpngWrite(png_ptr, info_ptr, &state,
592 width, height, row_byte_width,
593 input, compression_level, png_output_color_type,
594 output_color_components, converter, comments);
595 png_destroy_write_struct(&png_ptr, &info_ptr);
597 return success;
600 // static
601 bool Encode(const unsigned char* input, ColorFormat format,
602 const int width, const int height, int row_byte_width,
603 bool discard_transparency,
604 const std::vector<Comment>& comments,
605 std::vector<unsigned char>* output) {
606 return EncodeWithCompressionLevel(input, format, width, height,
607 row_byte_width,
608 discard_transparency,
609 comments, Z_DEFAULT_COMPRESSION,
610 output);
613 // Decode a PNG into an RGBA pixel array.
614 bool DecodePNG(const unsigned char* input, size_t input_size,
615 std::vector<unsigned char>* output,
616 int* width, int* height) {
617 return Decode(input, input_size, FORMAT_RGBA, output, width, height);
620 // Encode an RGBA pixel array into a PNG.
621 bool EncodeRGBAPNG(const unsigned char* input,
622 int width,
623 int height,
624 int row_byte_width,
625 std::vector<unsigned char>* output) {
626 return Encode(input, FORMAT_RGBA,
627 width, height, row_byte_width, false,
628 std::vector<Comment>(), output);
631 // Encode an BGRA pixel array into a PNG.
632 bool EncodeBGRAPNG(const unsigned char* input,
633 int width,
634 int height,
635 int row_byte_width,
636 bool discard_transparency,
637 std::vector<unsigned char>* output) {
638 return Encode(input, FORMAT_BGRA,
639 width, height, row_byte_width, discard_transparency,
640 std::vector<Comment>(), output);
643 } // image_diff_png