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