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