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