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