Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1134)

Side by Side Diff: gfx/codec/png_codec.cc

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

Powered by Google App Engine
This is Rietveld 408576698