| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 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 | 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 "gfx/codec/png_codec.h" | 5 #include "gfx/codec/png_codec.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
| 9 #include "third_party/skia/include/core/SkBitmap.h" | 9 #include "third_party/skia/include/core/SkBitmap.h" |
| 10 #include "third_party/skia/include/core/SkUnPreMultiply.h" | 10 #include "third_party/skia/include/core/SkUnPreMultiply.h" |
| (...skipping 512 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 523 // Called by libpng to flush its internal buffer to ours. | 523 // Called by libpng to flush its internal buffer to ours. |
| 524 void EncoderWriteCallback(png_structp png, png_bytep data, png_size_t size) { | 524 void EncoderWriteCallback(png_structp png, png_bytep data, png_size_t size) { |
| 525 PngEncoderState* state = static_cast<PngEncoderState*>(png_get_io_ptr(png)); | 525 PngEncoderState* state = static_cast<PngEncoderState*>(png_get_io_ptr(png)); |
| 526 DCHECK(state->out); | 526 DCHECK(state->out); |
| 527 | 527 |
| 528 size_t old_size = state->out->size(); | 528 size_t old_size = state->out->size(); |
| 529 state->out->resize(old_size + size); | 529 state->out->resize(old_size + size); |
| 530 memcpy(&(*state->out)[old_size], data, size); | 530 memcpy(&(*state->out)[old_size], data, size); |
| 531 } | 531 } |
| 532 | 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 |
| 533 void ConvertBGRAtoRGB(const unsigned char* bgra, int pixel_width, | 538 void ConvertBGRAtoRGB(const unsigned char* bgra, int pixel_width, |
| 534 unsigned char* rgb, bool* is_opaque) { | 539 unsigned char* rgb, bool* is_opaque) { |
| 535 for (int x = 0; x < pixel_width; x++) { | 540 for (int x = 0; x < pixel_width; x++) { |
| 536 const unsigned char* pixel_in = &bgra[x * 4]; | 541 const unsigned char* pixel_in = &bgra[x * 4]; |
| 537 unsigned char* pixel_out = &rgb[x * 3]; | 542 unsigned char* pixel_out = &rgb[x * 3]; |
| 538 pixel_out[0] = pixel_in[2]; | 543 pixel_out[0] = pixel_in[2]; |
| 539 pixel_out[1] = pixel_in[1]; | 544 pixel_out[1] = pixel_in[1]; |
| 540 pixel_out[2] = pixel_in[0]; | 545 pixel_out[2] = pixel_in[0]; |
| 541 } | 546 } |
| 542 } | 547 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 555 const unsigned char* input, | 560 const unsigned char* input, |
| 556 int png_output_color_type, int output_color_components, | 561 int png_output_color_type, int output_color_components, |
| 557 FormatConverter converter) { | 562 FormatConverter converter) { |
| 558 // Make sure to not declare any locals here -- locals in the presence | 563 // Make sure to not declare any locals here -- locals in the presence |
| 559 // of setjmp() in C++ code makes gcc complain. | 564 // of setjmp() in C++ code makes gcc complain. |
| 560 | 565 |
| 561 if (setjmp(png_jmpbuf(png_ptr))) | 566 if (setjmp(png_jmpbuf(png_ptr))) |
| 562 return false; | 567 return false; |
| 563 | 568 |
| 564 // Set our callback for libpng to give us the data. | 569 // Set our callback for libpng to give us the data. |
| 565 png_set_write_fn(png_ptr, state, EncoderWriteCallback, NULL); | 570 png_set_write_fn(png_ptr, state, EncoderWriteCallback, FakeFlushCallback); |
| 566 | 571 |
| 567 png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_output_color_type, | 572 png_set_IHDR(png_ptr, info_ptr, width, height, 8, png_output_color_type, |
| 568 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, | 573 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, |
| 569 PNG_FILTER_TYPE_DEFAULT); | 574 PNG_FILTER_TYPE_DEFAULT); |
| 570 png_write_info(png_ptr, info_ptr); | 575 png_write_info(png_ptr, info_ptr); |
| 571 | 576 |
| 572 if (!converter) { | 577 if (!converter) { |
| 573 // No conversion needed, give the data directly to libpng. | 578 // No conversion needed, give the data directly to libpng. |
| 574 for (int y = 0; y < height; y ++) { | 579 for (int y = 0; y < height; y ++) { |
| 575 png_write_row(png_ptr, | 580 png_write_row(png_ptr, |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 | 690 |
| 686 SkAutoLockPixels lock_input(input); | 691 SkAutoLockPixels lock_input(input); |
| 687 DCHECK(input.empty() || input.bytesPerPixel() == bbp); | 692 DCHECK(input.empty() || input.bytesPerPixel() == bbp); |
| 688 | 693 |
| 689 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), | 694 return Encode(reinterpret_cast<unsigned char*>(input.getAddr32(0, 0)), |
| 690 FORMAT_SkBitmap, input.width(), input.height(), | 695 FORMAT_SkBitmap, input.width(), input.height(), |
| 691 input.width() * bbp, discard_transparency, output); | 696 input.width() * bbp, discard_transparency, output); |
| 692 } | 697 } |
| 693 | 698 |
| 694 } // namespace gfx | 699 } // namespace gfx |
| OLD | NEW |