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