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