summaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-03 03:12:20 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-10-03 03:12:20 +0000
commit468fec96cd171682405d30b266f71f1766396a07 (patch)
treee3dc28717fa79c7149303d1c9ec6bb8437edfbcc /app
parentdb1faced81ea15604ca67135c5597d35269ff84a (diff)
downloadchromium_src-468fec96cd171682405d30b266f71f1766396a07.zip
chromium_src-468fec96cd171682405d30b266f71f1766396a07.tar.gz
chromium_src-468fec96cd171682405d30b266f71f1766396a07.tar.bz2
Move the JPEG and PNG codecs from base/gfx to app/gfx/codec. Move the classes
into the gfx namespace. Combine the PNGEncoder and PNGDecoder. There were separate when we had different executables for the browser and renderer, and linked the encoder only in one of them (which saved us some space used by libpng). This hasn't been the case for years, so combining them (again) makes sense. TEST=none BUG=none Review URL: http://codereview.chromium.org/243076 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@27930 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'app')
-rw-r--r--app/app.gyp12
-rw-r--r--app/gfx/codec/DEPS5
-rw-r--r--app/gfx/codec/jpeg_codec.cc531
-rw-r--r--app/gfx/codec/jpeg_codec.h63
-rw-r--r--app/gfx/codec/jpeg_codec_unittest.cc151
-rw-r--r--app/gfx/codec/png_codec.cc577
-rw-r--r--app/gfx/codec/png_codec.h96
-rw-r--r--app/gfx/codec/png_codec_unittest.cc251
-rw-r--r--app/resource_bundle.cc14
9 files changed, 1693 insertions, 7 deletions
diff --git a/app/app.gyp b/app/app.gyp
index 73d65ba..12e3121 100644
--- a/app/app.gyp
+++ b/app/app.gyp
@@ -49,7 +49,10 @@
'../skia/skia.gyp:skia',
'../third_party/icu/icu.gyp:icui18n',
'../third_party/icu/icu.gyp:icuuc',
+ '../third_party/libjpeg/libjpeg.gyp:libjpeg',
+ '../third_party/libpng/libpng.gyp:libpng',
'../third_party/sqlite/sqlite.gyp:sqlite',
+ '../third_party/zlib/zlib.gyp:zlib',
],
'include_dirs': [
'..',
@@ -74,6 +77,10 @@
'gfx/canvas_linux.cc',
'gfx/canvas_mac.mm',
'gfx/canvas_win.cc',
+ 'gfx/codec/jpeg_codec.cc',
+ 'gfx/codec/jpeg_codec.h',
+ 'gfx/codec/png_codec.cc',
+ 'gfx/codec/png_codec.h',
'gfx/font.h',
'gfx/font_gtk.cc',
'gfx/font_mac.mm',
@@ -199,10 +206,15 @@
'../testing/gtest.gyp:gtest',
'../third_party/icu/icu.gyp:icui18n',
'../third_party/icu/icu.gyp:icuuc',
+ '../third_party/libjpeg/libjpeg.gyp:libjpeg',
+ '../third_party/libpng/libpng.gyp:libpng',
'../third_party/libxml/libxml.gyp:libxml',
+ '../third_party/zlib/zlib.gyp:zlib',
],
'sources': [
'animation_unittest.cc',
+ 'gfx/codec/jpeg_codec_unittest.cc',
+ 'gfx/codec/png_codec_unittest.cc',
'gfx/color_utils_unittest.cc',
'gfx/font_unittest.cc',
'gfx/icon_util_unittest.cc',
diff --git a/app/gfx/codec/DEPS b/app/gfx/codec/DEPS
new file mode 100644
index 0000000..e4907a6c
--- /dev/null
+++ b/app/gfx/codec/DEPS
@@ -0,0 +1,5 @@
+include_rules = [
+ "+skia",
+ "+third_party/libjpeg",
+ "+third_party/libpng",
+]
diff --git a/app/gfx/codec/jpeg_codec.cc b/app/gfx/codec/jpeg_codec.cc
new file mode 100644
index 0000000..75ae384
--- /dev/null
+++ b/app/gfx/codec/jpeg_codec.cc
@@ -0,0 +1,531 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/gfx/codec/jpeg_codec.h"
+
+#include <setjmp.h>
+
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+extern "C" {
+#if defined(USE_SYSTEM_LIBJPEG)
+#include <jpeglib.h>
+#else
+#include "third_party/libjpeg/jpeglib.h"
+#endif
+}
+
+namespace gfx {
+
+// Encoder/decoder shared stuff ------------------------------------------------
+
+namespace {
+
+// used to pass error info through the JPEG library
+struct CoderErrorMgr {
+ jpeg_error_mgr pub;
+ jmp_buf setjmp_buffer;
+};
+
+void ErrorExit(jpeg_common_struct* cinfo) {
+ CoderErrorMgr *err = reinterpret_cast<CoderErrorMgr*>(cinfo->err);
+
+ // Return control to the setjmp point.
+ longjmp(err->setjmp_buffer, false);
+}
+
+} // namespace
+
+// Encoder ---------------------------------------------------------------------
+//
+// This code is based on nsJPEGEncoder from Mozilla.
+// Copyright 2005 Google Inc. (Brett Wilson, contributor)
+
+namespace {
+
+// Initial size for the output buffer in the JpegEncoderState below.
+const static int initial_output_buffer_size = 8192;
+
+struct JpegEncoderState {
+ JpegEncoderState(std::vector<unsigned char>* o)
+ : out(o),
+ image_buffer_used(0) {
+ }
+
+ // Output buffer, of which 'image_buffer_used' bytes are actually used (this
+ // will often be less than the actual size of the vector because we size it
+ // so that libjpeg can write directly into it.
+ std::vector<unsigned char>* out;
+
+ // Number of bytes in the 'out' buffer that are actually used (see above).
+ size_t image_buffer_used;
+};
+
+// Initializes the JpegEncoderState for encoding, and tells libjpeg about where
+// the output buffer is.
+//
+// From the JPEG library:
+// "Initialize destination. This is called by jpeg_start_compress() before
+// any data is actually written. It must initialize next_output_byte and
+// free_in_buffer. free_in_buffer must be initialized to a positive value."
+void InitDestination(jpeg_compress_struct* cinfo) {
+ JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data);
+ DCHECK(state->image_buffer_used == 0) << "initializing after use";
+
+ state->out->resize(initial_output_buffer_size);
+ state->image_buffer_used = 0;
+
+ cinfo->dest->next_output_byte = &(*state->out)[0];
+ cinfo->dest->free_in_buffer = initial_output_buffer_size;
+}
+
+// Resize the buffer that we give to libjpeg and update our and its state.
+//
+// From the JPEG library:
+// "Callback used by libjpeg whenever the buffer has filled (free_in_buffer
+// reaches zero). In typical applications, it should write out the *entire*
+// buffer (use the saved start address and buffer length; ignore the current
+// state of next_output_byte and free_in_buffer). Then reset the pointer &
+// count to the start of the buffer, and return TRUE indicating that the
+// buffer has been dumped. free_in_buffer must be set to a positive value
+// when TRUE is returned. A FALSE return should only be used when I/O
+// suspension is desired (this operating mode is discussed in the next
+// section)."
+boolean EmptyOutputBuffer(jpeg_compress_struct* cinfo) {
+ JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data);
+
+ // note the new size, the buffer is full
+ state->image_buffer_used = state->out->size();
+
+ // expand buffer, just double size each time
+ state->out->resize(state->out->size() * 2);
+
+ // tell libjpeg where to write the next data
+ cinfo->dest->next_output_byte = &(*state->out)[state->image_buffer_used];
+ cinfo->dest->free_in_buffer = state->out->size() - state->image_buffer_used;
+ return 1;
+}
+
+// Cleans up the JpegEncoderState to prepare for returning in the final form.
+//
+// From the JPEG library:
+// "Terminate destination --- called by jpeg_finish_compress() after all data
+// has been written. In most applications, this must flush any data
+// remaining in the buffer. Use either next_output_byte or free_in_buffer to
+// determine how much data is in the buffer."
+void TermDestination(jpeg_compress_struct* cinfo) {
+ JpegEncoderState* state = static_cast<JpegEncoderState*>(cinfo->client_data);
+ DCHECK(state->out->size() >= state->image_buffer_used);
+
+ // update the used byte based on the next byte libjpeg would write to
+ state->image_buffer_used = cinfo->dest->next_output_byte - &(*state->out)[0];
+ DCHECK(state->image_buffer_used < state->out->size()) <<
+ "JPEG library busted, got a bad image buffer size";
+
+ // update our buffer so that it exactly encompases the desired data
+ state->out->resize(state->image_buffer_used);
+}
+
+// Converts RGBA to RGB (removing the alpha values) to prepare to send data to
+// libjpeg. This converts one row of data in rgba with the given width in
+// pixels the the given rgb destination buffer (which should have enough space
+// reserved for the final data).
+void StripAlpha(const unsigned char* rgba, int pixel_width, unsigned char* rgb)
+{
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgba[x * 4];
+ unsigned char* pixel_out = &rgb[x * 3];
+ pixel_out[0] = pixel_in[0];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[2];
+ }
+}
+
+// Converts BGRA to RGB by reordering the color components and dropping the
+// alpha. This converts one row of data in rgba with the given width in
+// pixels the the given rgb destination buffer (which should have enough space
+// reserved for the final data).
+void BGRAtoRGB(const unsigned char* bgra, int pixel_width, unsigned char* rgb)
+{
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &bgra[x * 4];
+ unsigned char* pixel_out = &rgb[x * 3];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ }
+}
+
+// This class destroys the given jpeg_compress object when it goes out of
+// scope. It simplifies the error handling in Encode (and even applies to the
+// success case).
+class CompressDestroyer {
+ public:
+ CompressDestroyer() : cinfo_(NULL) {
+ }
+ ~CompressDestroyer() {
+ DestroyManagedObject();
+ }
+ void SetManagedObject(jpeg_compress_struct* ci) {
+ DestroyManagedObject();
+ cinfo_ = ci;
+ }
+ void DestroyManagedObject() {
+ if (cinfo_) {
+ jpeg_destroy_compress(cinfo_);
+ cinfo_ = NULL;
+ }
+ }
+ private:
+ jpeg_compress_struct* cinfo_;
+};
+
+} // namespace
+
+bool JPEGCodec::Encode(const unsigned char* input, ColorFormat format,
+ int w, int h, int row_byte_width,
+ int quality, std::vector<unsigned char>* output) {
+ jpeg_compress_struct cinfo;
+ CompressDestroyer destroyer;
+ destroyer.SetManagedObject(&cinfo);
+ output->clear();
+
+ // We set up the normal JPEG error routines, then override error_exit.
+ // This must be done before the call to create_compress.
+ CoderErrorMgr errmgr;
+ cinfo.err = jpeg_std_error(&errmgr.pub);
+ errmgr.pub.error_exit = ErrorExit;
+ // Establish the setjmp return context for ErrorExit to use.
+ if (setjmp(errmgr.setjmp_buffer)) {
+ // If we get here, the JPEG code has signaled an error.
+ // MSDN notes: "if you intend your code to be portable, do not rely on
+ // correct destruction of frame-based objects when executing a nonlocal
+ // goto using a call to longjmp." So we delete the CompressDestroyer's
+ // object manually instead.
+ destroyer.DestroyManagedObject();
+ return false;
+ }
+
+ // The destroyer will destroy() cinfo on exit.
+ jpeg_create_compress(&cinfo);
+
+ cinfo.image_width = w;
+ cinfo.image_height = h;
+ cinfo.input_components = 3;
+ cinfo.in_color_space = JCS_RGB;
+ cinfo.data_precision = 8;
+
+ jpeg_set_defaults(&cinfo);
+ jpeg_set_quality(&cinfo, quality, 1); // quality here is 0-100
+
+ // set up the destination manager
+ jpeg_destination_mgr destmgr;
+ destmgr.init_destination = InitDestination;
+ destmgr.empty_output_buffer = EmptyOutputBuffer;
+ destmgr.term_destination = TermDestination;
+ cinfo.dest = &destmgr;
+
+ JpegEncoderState state(output);
+ cinfo.client_data = &state;
+
+ jpeg_start_compress(&cinfo, 1);
+
+ // feed it the rows, doing necessary conversions for the color format
+ if (format == FORMAT_RGB) {
+ // no conversion necessary
+ while (cinfo.next_scanline < cinfo.image_height) {
+ const unsigned char* row = &input[cinfo.next_scanline * row_byte_width];
+ jpeg_write_scanlines(&cinfo, const_cast<unsigned char**>(&row), 1);
+ }
+ } else {
+ // get the correct format converter
+ void (*converter)(const unsigned char* in, int w, unsigned char* rgb);
+ if (format == FORMAT_RGBA) {
+ converter = StripAlpha;
+ } else if (format == FORMAT_BGRA) {
+ converter = BGRAtoRGB;
+ } else {
+ NOTREACHED() << "Invalid pixel format";
+ return false;
+ }
+
+ // output row after converting
+ unsigned char* row = new unsigned char[w * 3];
+
+ while (cinfo.next_scanline < cinfo.image_height) {
+ converter(&input[cinfo.next_scanline * row_byte_width], w, row);
+ jpeg_write_scanlines(&cinfo, &row, 1);
+ }
+ delete[] row;
+ }
+
+ jpeg_finish_compress(&cinfo);
+ return true;
+}
+
+// Decoder --------------------------------------------------------------------
+
+namespace {
+
+struct JpegDecoderState {
+ JpegDecoderState(const unsigned char* in, size_t len)
+ : input_buffer(in), input_buffer_length(len) {
+ }
+
+ const unsigned char* input_buffer;
+ size_t input_buffer_length;
+};
+
+// Callback to initialize the source.
+//
+// From the JPEG library:
+// "Initialize source. This is called by jpeg_read_header() before any data is
+// actually read. May leave bytes_in_buffer set to 0 (in which case a
+// fill_input_buffer() call will occur immediately)."
+void InitSource(j_decompress_ptr cinfo) {
+ JpegDecoderState* state = static_cast<JpegDecoderState*>(cinfo->client_data);
+ cinfo->src->next_input_byte = state->input_buffer;
+ cinfo->src->bytes_in_buffer = state->input_buffer_length;
+}
+
+// Callback to fill the buffer. Since our buffer already contains all the data,
+// we should never need to provide more data. If libjpeg thinks it needs more
+// data, our input is probably corrupt.
+//
+// From the JPEG library:
+// "This is called whenever bytes_in_buffer has reached zero and more data is
+// wanted. In typical applications, it should read fresh data into the buffer
+// (ignoring the current state of next_input_byte and bytes_in_buffer), reset
+// the pointer & count to the start of the buffer, and return TRUE indicating
+// that the buffer has been reloaded. It is not necessary to fill the buffer
+// entirely, only to obtain at least one more byte. bytes_in_buffer MUST be
+// set to a positive value if TRUE is returned. A FALSE return should only
+// be used when I/O suspension is desired."
+boolean FillInputBuffer(j_decompress_ptr cinfo) {
+ return false;
+}
+
+// Skip data in the buffer. Since we have all the data at once, this operation
+// is easy. It is not clear if this ever gets called because the JPEG library
+// should be able to do the skip itself (it has all the data).
+//
+// From the JPEG library:
+// "Skip num_bytes worth of data. The buffer pointer and count should be
+// advanced over num_bytes input bytes, refilling the buffer as needed. This
+// is used to skip over a potentially large amount of uninteresting data
+// (such as an APPn marker). In some applications it may be possible to
+// optimize away the reading of the skipped data, but it's not clear that
+// being smart is worth much trouble; large skips are uncommon.
+// bytes_in_buffer may be zero on return. A zero or negative skip count
+// should be treated as a no-op."
+void SkipInputData(j_decompress_ptr cinfo, long num_bytes) {
+ if (num_bytes > static_cast<long>(cinfo->src->bytes_in_buffer)) {
+ // Since all our data should be in the buffer, trying to skip beyond it
+ // means that there is some kind of error or corrupt input data. A 0 for
+ // bytes left means it will call FillInputBuffer which will then fail.
+ cinfo->src->next_input_byte += cinfo->src->bytes_in_buffer;
+ cinfo->src->bytes_in_buffer = 0;
+ } else if (num_bytes > 0) {
+ cinfo->src->bytes_in_buffer -= static_cast<size_t>(num_bytes);
+ cinfo->src->next_input_byte += num_bytes;
+ }
+}
+
+// Our source doesn't need any cleanup, so this is a NOP.
+//
+// From the JPEG library:
+// "Terminate source --- called by jpeg_finish_decompress() after all data has
+// been read to clean up JPEG source manager. NOT called by jpeg_abort() or
+// jpeg_destroy()."
+void TermSource(j_decompress_ptr cinfo) {
+}
+
+// Converts one row of rgb data to rgba data by adding a fully-opaque alpha
+// value.
+void AddAlpha(const unsigned char* rgb, int pixel_width, unsigned char* rgba) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgb[x * 3];
+ unsigned char* pixel_out = &rgba[x * 4];
+ pixel_out[0] = pixel_in[0];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[2];
+ pixel_out[3] = 0xff;
+ }
+}
+
+// Converts one row of RGB data to BGRA by reordering the color components and
+// adding alpha values of 0xff.
+void RGBtoBGRA(const unsigned char* bgra, int pixel_width, unsigned char* rgb)
+{
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &bgra[x * 3];
+ unsigned char* pixel_out = &rgb[x * 4];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ pixel_out[3] = 0xff;
+ }
+}
+
+// This class destroys the given jpeg_decompress object when it goes out of
+// scope. It simplifies the error handling in Decode (and even applies to the
+// success case).
+class DecompressDestroyer {
+ public:
+ DecompressDestroyer() : cinfo_(NULL) {
+ }
+ ~DecompressDestroyer() {
+ DestroyManagedObject();
+ }
+ void SetManagedObject(jpeg_decompress_struct* ci) {
+ DestroyManagedObject();
+ cinfo_ = ci;
+ }
+ void DestroyManagedObject() {
+ if (cinfo_) {
+ jpeg_destroy_decompress(cinfo_);
+ cinfo_ = NULL;
+ }
+ }
+ private:
+ jpeg_decompress_struct* cinfo_;
+};
+
+} // namespace
+
+bool JPEGCodec::Decode(const unsigned char* input, size_t input_size,
+ ColorFormat format, std::vector<unsigned char>* output,
+ int* w, int* h) {
+ jpeg_decompress_struct cinfo;
+ DecompressDestroyer destroyer;
+ destroyer.SetManagedObject(&cinfo);
+ output->clear();
+
+ // We set up the normal JPEG error routines, then override error_exit.
+ // This must be done before the call to create_decompress.
+ CoderErrorMgr errmgr;
+ cinfo.err = jpeg_std_error(&errmgr.pub);
+ errmgr.pub.error_exit = ErrorExit;
+ // Establish the setjmp return context for ErrorExit to use.
+ if (setjmp(errmgr.setjmp_buffer)) {
+ // If we get here, the JPEG code has signaled an error.
+ // See note in JPEGCodec::Encode() for why we need to destroy the cinfo
+ // manually here.
+ destroyer.DestroyManagedObject();
+ return false;
+ }
+
+ // The destroyer will destroy() cinfo on exit. We don't want to set the
+ // destroyer's object until cinfo is initialized.
+ jpeg_create_decompress(&cinfo);
+
+ // set up the source manager
+ jpeg_source_mgr srcmgr;
+ srcmgr.init_source = InitSource;
+ srcmgr.fill_input_buffer = FillInputBuffer;
+ srcmgr.skip_input_data = SkipInputData;
+ srcmgr.resync_to_restart = jpeg_resync_to_restart; // use default routine
+ srcmgr.term_source = TermSource;
+ cinfo.src = &srcmgr;
+
+ JpegDecoderState state(input, input_size);
+ cinfo.client_data = &state;
+
+ // fill the file metadata into our buffer
+ if (jpeg_read_header(&cinfo, true) != JPEG_HEADER_OK)
+ return false;
+
+ // we want to always get RGB data out
+ switch (cinfo.jpeg_color_space) {
+ case JCS_GRAYSCALE:
+ case JCS_RGB:
+ case JCS_YCbCr:
+ cinfo.out_color_space = JCS_RGB;
+ break;
+ case JCS_CMYK:
+ case JCS_YCCK:
+ default:
+ // Mozilla errors out on these color spaces, so I presume that the jpeg
+ // library can't do automatic color space conversion for them. We don't
+ // care about these anyway.
+ return false;
+ }
+ cinfo.output_components = 3;
+
+ jpeg_calc_output_dimensions(&cinfo);
+ *w = cinfo.output_width;
+ *h = cinfo.output_height;
+
+ jpeg_start_decompress(&cinfo);
+
+ // FIXME(brettw) we may want to allow the capability for callers to request
+ // how to align row lengths as we do for the compressor.
+ int row_read_stride = cinfo.output_width * cinfo.output_components;
+
+ if (format == FORMAT_RGB) {
+ // easy case, row needs no conversion
+ int row_write_stride = row_read_stride;
+ output->resize(row_write_stride * cinfo.output_height);
+
+ for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) {
+ unsigned char* rowptr = &(*output)[row * row_write_stride];
+ if (!jpeg_read_scanlines(&cinfo, &rowptr, 1))
+ return false;
+ }
+ } else {
+ // Rows need conversion to output format: read into a temporary buffer and
+ // expand to the final one. Performance: we could avoid the extra
+ // allocation by doing the expansion in-place.
+ int row_write_stride;
+ void (*converter)(const unsigned char* rgb, int w, unsigned char* out);
+ if (format == FORMAT_RGBA) {
+ row_write_stride = cinfo.output_width * 4;
+ converter = AddAlpha;
+ } else if (format == FORMAT_BGRA) {
+ row_write_stride = cinfo.output_width * 4;
+ converter = RGBtoBGRA;
+ } else {
+ NOTREACHED() << "Invalid pixel format";
+ jpeg_destroy_decompress(&cinfo);
+ return false;
+ }
+
+ output->resize(row_write_stride * cinfo.output_height);
+
+ scoped_array<unsigned char> row_data(new unsigned char[row_read_stride]);
+ unsigned char* rowptr = row_data.get();
+ for (int row = 0; row < static_cast<int>(cinfo.output_height); row++) {
+ if (!jpeg_read_scanlines(&cinfo, &rowptr, 1))
+ return false;
+ converter(rowptr, *w, &(*output)[row * row_write_stride]);
+ }
+ }
+
+ jpeg_finish_decompress(&cinfo);
+ jpeg_destroy_decompress(&cinfo);
+ return true;
+}
+
+// static
+SkBitmap* JPEGCodec::Decode(const unsigned char* input, size_t input_size) {
+ int w, h;
+ std::vector<unsigned char> data_vector;
+ // Use FORMAT_BGRA as that maps to Skia's 32 bit (kARGB_8888_Config) format.
+ if (!Decode(input, input_size, FORMAT_BGRA, &data_vector, &w, &h))
+ return NULL;
+
+ // Skia only handles 32 bit images.
+ int data_length = w * h * 4;
+
+ SkBitmap* bitmap = new SkBitmap();
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, w, h);
+ bitmap->allocPixels();
+ memcpy(bitmap->getAddr32(0, 0), &data_vector[0], data_length);
+
+ return bitmap;
+}
+
+} // namespace gfx
diff --git a/app/gfx/codec/jpeg_codec.h b/app/gfx/codec/jpeg_codec.h
new file mode 100644
index 0000000..8f58ecd
--- /dev/null
+++ b/app/gfx/codec/jpeg_codec.h
@@ -0,0 +1,63 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef APP_GFX_CODEC_JPEG_CODEC_H_
+#define APP_GFX_CODEC_JPEG_CODEC_H_
+
+#include <vector>
+
+class SkBitmap;
+
+namespace gfx {
+
+// Interface for encoding/decoding JPEG data. This is a wrapper around libjpeg,
+// which has an inconvenient interface for callers. This is only used for UI
+// elements, WebKit has its own more complicated JPEG decoder which handles,
+// among other things, partially downloaded data.
+class JPEGCodec {
+ public:
+ enum ColorFormat {
+ // 3 bytes per pixel (packed), in RGB order regardless of endianness.
+ // This is the native JPEG format.
+ FORMAT_RGB,
+
+ // 4 bytes per pixel, in RGBA order in mem regardless of endianness.
+ FORMAT_RGBA,
+
+ // 4 bytes per pixel, in BGRA order in mem regardless of endianness.
+ // This is the default Windows DIB order.
+ FORMAT_BGRA
+ };
+
+ // Encodes the given raw 'input' data, with each pixel being represented as
+ // given in 'format'. The encoded JPEG data will be written into the supplied
+ // vector and true will be returned on success. On failure (false), the
+ // contents of the output buffer are undefined.
+ //
+ // w, h: dimensions of the image
+ // row_byte_width: the width in bytes of each row. This may be greater than
+ // w * bytes_per_pixel if there is extra padding at the end of each row
+ // (often, each row is padded to the next machine word).
+ // quality: an integer in the range 0-100, where 100 is the highest quality.
+ static bool Encode(const unsigned char* input, ColorFormat format,
+ int w, int h, int row_byte_width,
+ int quality, std::vector<unsigned char>* output);
+
+ // Decodes the JPEG data contained in input of length input_size. The
+ // decoded data will be placed in *output with the dimensions in *w and *h
+ // on success (returns true). This data will be written in the'format'
+ // format. On failure, the values of these output variables is undefined.
+ static bool Decode(const unsigned char* input, size_t input_size,
+ ColorFormat format, std::vector<unsigned char>* output,
+ int* w, int* h);
+
+ // Decodes the JPEG data contained in input of length input_size. If
+ // successful, a SkBitmap is created and returned. It is up to the caller
+ // to delete the returned bitmap.
+ static SkBitmap* Decode(const unsigned char* input, size_t input_size);
+};
+
+} // namespace gfx
+
+#endif // APP_GFX_CODEC_JPEG_CODEC_H_
diff --git a/app/gfx/codec/jpeg_codec_unittest.cc b/app/gfx/codec/jpeg_codec_unittest.cc
new file mode 100644
index 0000000..bf8eaab
--- /dev/null
+++ b/app/gfx/codec/jpeg_codec_unittest.cc
@@ -0,0 +1,151 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <math.h>
+
+#include "app/gfx/codec/jpeg_codec.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace gfx {
+
+// out of 100, this indicates how compressed it will be, this should be changed
+// with jpeg equality threshold
+// static int jpeg_quality = 75; // FIXME(brettw)
+static int jpeg_quality = 100;
+
+// The threshold of average color differences where we consider two images
+// equal. This number was picked to be a little above the observed difference
+// using the above quality.
+static double jpeg_equality_threshold = 1.0;
+
+// Computes the average difference between each value in a and b. A and b
+// should be the same size. Used to see if two images are approximately equal
+// in the presence of compression.
+static double AveragePixelDelta(const std::vector<unsigned char>& a,
+ const std::vector<unsigned char>& b) {
+ // if the sizes are different, say the average difference is the maximum
+ if (a.size() != b.size())
+ return 255.0;
+ if (a.size() == 0)
+ return 0; // prevent divide by 0 below
+
+ double acc = 0.0;
+ for (size_t i = 0; i < a.size(); i++)
+ acc += fabs(static_cast<double>(a[i]) - static_cast<double>(b[i]));
+
+ return acc / static_cast<double>(a.size());
+}
+
+static void MakeRGBImage(int w, int h, std::vector<unsigned char>* dat) {
+ dat->resize(w * h * 3);
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ unsigned char* org_px = &(*dat)[(y * w + x) * 3];
+ org_px[0] = x * 3; // r
+ org_px[1] = x * 3 + 1; // g
+ org_px[2] = x * 3 + 2; // b
+ }
+ }
+}
+
+TEST(JPEGCodec, EncodeDecodeRGB) {
+ int w = 20, h = 20;
+
+ // create an image with known values
+ std::vector<unsigned char> original;
+ MakeRGBImage(w, h, &original);
+
+ // encode, making sure it was compressed some
+ std::vector<unsigned char> encoded;
+ EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGB, w, h,
+ w * 3, jpeg_quality, &encoded));
+ EXPECT_GT(original.size(), encoded.size());
+
+ // decode, it should have the same size as the original
+ std::vector<unsigned char> decoded;
+ int outw, outh;
+ EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(),
+ JPEGCodec::FORMAT_RGB, &decoded,
+ &outw, &outh));
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original.size(), decoded.size());
+
+ // Images must be approximately equal (compression will have introduced some
+ // minor artifacts).
+ ASSERT_GE(jpeg_equality_threshold, AveragePixelDelta(original, decoded));
+}
+
+TEST(JPEGCodec, EncodeDecodeRGBA) {
+ int w = 20, h = 20;
+
+ // create an image with known values, a must be opaque because it will be
+ // lost during compression
+ std::vector<unsigned char> original;
+ original.resize(w * h * 4);
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ unsigned char* org_px = &original[(y * w + x) * 4];
+ org_px[0] = x * 3; // r
+ org_px[1] = x * 3 + 1; // g
+ org_px[2] = x * 3 + 2; // b
+ org_px[3] = 0xFF; // a (opaque)
+ }
+ }
+
+ // encode, making sure it was compressed some
+ std::vector<unsigned char> encoded;
+ EXPECT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGBA, w, h,
+ w * 4, jpeg_quality, &encoded));
+ EXPECT_GT(original.size(), encoded.size());
+
+ // decode, it should have the same size as the original
+ std::vector<unsigned char> decoded;
+ int outw, outh;
+ EXPECT_TRUE(JPEGCodec::Decode(&encoded[0], encoded.size(),
+ JPEGCodec::FORMAT_RGBA, &decoded,
+ &outw, &outh));
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original.size(), decoded.size());
+
+ // Images must be approximately equal (compression will have introduced some
+ // minor artifacts).
+ ASSERT_GE(jpeg_equality_threshold, AveragePixelDelta(original, decoded));
+}
+
+// Test that corrupted data decompression causes failures.
+TEST(JPEGCodec, DecodeCorrupted) {
+ int w = 20, h = 20;
+
+ // some random data (an uncompressed image)
+ std::vector<unsigned char> original;
+ MakeRGBImage(w, h, &original);
+
+ // it should fail when given non-JPEG compressed data
+ std::vector<unsigned char> output;
+ int outw, outh;
+ ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(),
+ JPEGCodec::FORMAT_RGB, &output,
+ &outw, &outh));
+
+ // make some compressed data
+ std::vector<unsigned char> compressed;
+ ASSERT_TRUE(JPEGCodec::Encode(&original[0], JPEGCodec::FORMAT_RGB, w, h,
+ w * 3, jpeg_quality, &compressed));
+
+ // try decompressing a truncated version
+ ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size() / 2,
+ JPEGCodec::FORMAT_RGB, &output,
+ &outw, &outh));
+
+ // corrupt it and try decompressing that
+ for (int i = 10; i < 30; i++)
+ compressed[i] = i;
+ ASSERT_FALSE(JPEGCodec::Decode(&compressed[0], compressed.size(),
+ JPEGCodec::FORMAT_RGB, &output,
+ &outw, &outh));
+}
+
+} // namespace gfx
diff --git a/app/gfx/codec/png_codec.cc b/app/gfx/codec/png_codec.cc
new file mode 100644
index 0000000..41057a0
--- /dev/null
+++ b/app/gfx/codec/png_codec.cc
@@ -0,0 +1,577 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "app/gfx/codec/png_codec.h"
+
+#include "base/logging.h"
+#include "base/scoped_ptr.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+#include "third_party/skia/include/core/SkUnPreMultiply.h"
+
+extern "C" {
+#if defined(USE_SYSTEM_LIBPNG)
+#include <png.h>
+#else
+#include "third_party/libpng/png.h"
+#endif
+}
+
+namespace gfx {
+
+namespace {
+
+// Converts BGRA->RGBA and RGBA->BGRA.
+void ConvertBetweenBGRAandRGBA(const unsigned char* input, int pixel_width,
+ unsigned char* output) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &input[x * 4];
+ unsigned char* pixel_out = &output[x * 4];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ pixel_out[3] = pixel_in[3];
+ }
+}
+
+void ConvertRGBAtoRGB(const unsigned char* rgba, int pixel_width,
+ unsigned char* rgb) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgba[x * 4];
+ unsigned char* pixel_out = &rgb[x * 3];
+ pixel_out[0] = pixel_in[0];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[2];
+ }
+}
+
+} // namespace
+
+// Decoder --------------------------------------------------------------------
+//
+// This code is based on WebKit libpng interface (PNGImageDecoder), which is
+// in turn based on the Mozilla png decoder.
+
+namespace {
+
+// Gamma constants: We assume we're on Windows which uses a gamma of 2.2.
+const double kMaxGamma = 21474.83; // Maximum gamma accepted by png library.
+const double kDefaultGamma = 2.2;
+const double kInverseGamma = 1.0 / kDefaultGamma;
+
+class PngDecoderState {
+ public:
+ PngDecoderState(PNGCodec::ColorFormat ofmt, std::vector<unsigned char>* o)
+ : output_format(ofmt),
+ output_channels(0),
+ output(o),
+ row_converter(NULL),
+ width(0),
+ height(0),
+ done(false) {
+ }
+
+ PNGCodec::ColorFormat output_format;
+ int output_channels;
+
+ std::vector<unsigned char>* output;
+
+ // Called to convert a row from the library to the correct output format.
+ // When NULL, no conversion is necessary.
+ void (*row_converter)(const unsigned char* in, int w, unsigned char* out);
+
+ // Size of the image, set in the info callback.
+ int width;
+ int height;
+
+ // Set to true when we've found the end of the data.
+ bool done;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PngDecoderState);
+};
+
+void ConvertRGBtoRGBA(const unsigned char* rgb, int pixel_width,
+ unsigned char* rgba) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgb[x * 3];
+ unsigned char* pixel_out = &rgba[x * 4];
+ pixel_out[0] = pixel_in[0];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[2];
+ pixel_out[3] = 0xff;
+ }
+}
+
+void ConvertRGBtoBGRA(const unsigned char* rgb, int pixel_width,
+ unsigned char* bgra) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &rgb[x * 3];
+ unsigned char* pixel_out = &bgra[x * 4];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ pixel_out[3] = 0xff;
+ }
+}
+
+// Called when the png header has been read. This code is based on the WebKit
+// PNGImageDecoder
+void DecodeInfoCallback(png_struct* png_ptr, png_info* info_ptr) {
+ PngDecoderState* state = static_cast<PngDecoderState*>(
+ png_get_progressive_ptr(png_ptr));
+
+ int bit_depth, color_type, interlace_type, compression_type;
+ int filter_type, channels;
+ png_uint_32 w, h;
+ png_get_IHDR(png_ptr, info_ptr, &w, &h, &bit_depth, &color_type,
+ &interlace_type, &compression_type, &filter_type);
+
+ // Bounds check. When the image is unreasonably big, we'll error out and
+ // end up back at the setjmp call when we set up decoding. "Unreasonably big"
+ // means "big enough that w * h * 32bpp might overflow an int"; we choose this
+ // threshold to match WebKit and because a number of places in code assume
+ // that an image's size (in bytes) fits in a (signed) int.
+ unsigned long long total_size =
+ static_cast<unsigned long long>(w) * static_cast<unsigned long long>(h);
+ if (total_size > ((1 << 29) - 1))
+ longjmp(png_ptr->jmpbuf, 1);
+ state->width = static_cast<int>(w);
+ state->height = static_cast<int>(h);
+
+ // Expand to ensure we use 24-bit for RGB and 32-bit for RGBA.
+ if (color_type == PNG_COLOR_TYPE_PALETTE ||
+ (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8))
+ png_set_expand(png_ptr);
+
+ // Transparency for paletted images.
+ if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
+ png_set_expand(png_ptr);
+
+ // Convert 16-bit to 8-bit.
+ if (bit_depth == 16)
+ png_set_strip_16(png_ptr);
+
+ // Expand grayscale to RGB.
+ if (color_type == PNG_COLOR_TYPE_GRAY ||
+ color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+ png_set_gray_to_rgb(png_ptr);
+
+ // Deal with gamma and keep it under our control.
+ double gamma;
+ if (png_get_gAMA(png_ptr, info_ptr, &gamma)) {
+ if (gamma <= 0.0 || gamma > kMaxGamma) {
+ gamma = kInverseGamma;
+ png_set_gAMA(png_ptr, info_ptr, gamma);
+ }
+ png_set_gamma(png_ptr, kDefaultGamma, gamma);
+ } else {
+ png_set_gamma(png_ptr, kDefaultGamma, kInverseGamma);
+ }
+
+ // Tell libpng to send us rows for interlaced pngs.
+ if (interlace_type == PNG_INTERLACE_ADAM7)
+ png_set_interlace_handling(png_ptr);
+
+ // Update our info now
+ png_read_update_info(png_ptr, info_ptr);
+ channels = png_get_channels(png_ptr, info_ptr);
+
+ // Pick our row format converter necessary for this data.
+ if (channels == 3) {
+ switch (state->output_format) {
+ case PNGCodec::FORMAT_RGB:
+ state->row_converter = NULL; // no conversion necessary
+ state->output_channels = 3;
+ break;
+ case PNGCodec::FORMAT_RGBA:
+ state->row_converter = &ConvertRGBtoRGBA;
+ state->output_channels = 4;
+ break;
+ case PNGCodec::FORMAT_BGRA:
+ state->row_converter = &ConvertRGBtoBGRA;
+ state->output_channels = 4;
+ break;
+ default:
+ NOTREACHED() << "Unknown output format";
+ break;
+ }
+ } else if (channels == 4) {
+ switch (state->output_format) {
+ case PNGCodec::FORMAT_RGB:
+ state->row_converter = &ConvertRGBAtoRGB;
+ state->output_channels = 3;
+ break;
+ case PNGCodec::FORMAT_RGBA:
+ state->row_converter = NULL; // no conversion necessary
+ state->output_channels = 4;
+ break;
+ case PNGCodec::FORMAT_BGRA:
+ state->row_converter = &ConvertBetweenBGRAandRGBA;
+ state->output_channels = 4;
+ break;
+ default:
+ NOTREACHED() << "Unknown output format";
+ break;
+ }
+ } else {
+ NOTREACHED() << "Unknown input channels";
+ longjmp(png_ptr->jmpbuf, 1);
+ }
+
+ state->output->resize(state->width * state->output_channels * state->height);
+}
+
+void DecodeRowCallback(png_struct* png_ptr, png_byte* new_row,
+ png_uint_32 row_num, int pass) {
+ PngDecoderState* state = static_cast<PngDecoderState*>(
+ png_get_progressive_ptr(png_ptr));
+
+ DCHECK(pass == 0) << "We didn't turn on interlace handling, but libpng is "
+ "giving us interlaced data.";
+ if (static_cast<int>(row_num) > state->height) {
+ NOTREACHED() << "Invalid row";
+ return;
+ }
+
+ unsigned char* dest = &(*state->output)[
+ state->width * state->output_channels * row_num];
+ if (state->row_converter)
+ state->row_converter(new_row, state->width, dest);
+ else
+ memcpy(dest, new_row, state->width * state->output_channels);
+}
+
+void DecodeEndCallback(png_struct* png_ptr, png_info* info) {
+ PngDecoderState* state = static_cast<PngDecoderState*>(
+ png_get_progressive_ptr(png_ptr));
+
+ // Mark the image as complete, this will tell the Decode function that we
+ // have successfully found the end of the data.
+ state->done = true;
+}
+
+// Automatically destroys the given read structs on destruction to make
+// cleanup and error handling code cleaner.
+class PngReadStructDestroyer {
+ public:
+ PngReadStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) {
+ }
+ ~PngReadStructDestroyer() {
+ png_destroy_read_struct(ps_, pi_, NULL);
+ }
+ private:
+ png_struct** ps_;
+ png_info** pi_;
+};
+
+} // namespace
+
+// static
+bool PNGCodec::Decode(const unsigned char* input, size_t input_size,
+ ColorFormat format, std::vector<unsigned char>* output,
+ int* w, int* h) {
+ if (input_size < 8)
+ return false; // Input data too small to be a png
+
+ // Have libpng check the signature, it likes the first 8 bytes.
+ if (png_sig_cmp(const_cast<unsigned char*>(input), 0, 8) != 0)
+ return false;
+
+ png_struct* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
+ png_voidp_NULL,
+ png_error_ptr_NULL,
+ png_error_ptr_NULL);
+ if (!png_ptr)
+ return false;
+
+ png_info* info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ png_destroy_read_struct(&png_ptr, NULL, NULL);
+ return false;
+ }
+
+ PngReadStructDestroyer destroyer(&png_ptr, &info_ptr);
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ // The destroyer will ensure that the structures are cleaned up in this
+ // case, even though we may get here as a jump from random parts of the
+ // PNG library called below.
+ return false;
+ }
+
+ PngDecoderState state(format, output);
+
+ png_set_progressive_read_fn(png_ptr, &state, &DecodeInfoCallback,
+ &DecodeRowCallback, &DecodeEndCallback);
+ png_process_data(png_ptr,
+ info_ptr,
+ const_cast<unsigned char*>(input),
+ input_size);
+
+ if (!state.done) {
+ // Fed it all the data but the library didn't think we got all the data, so
+ // this file must be truncated.
+ output->clear();
+ return false;
+ }
+
+ *w = state.width;
+ *h = state.height;
+ return true;
+}
+
+// static
+bool PNGCodec::Decode(const std::vector<unsigned char>* data,
+ SkBitmap* bitmap) {
+ DCHECK(bitmap);
+ if (!data || data->empty())
+ return false;
+ int width, height;
+ std::vector<unsigned char> decoded_data;
+ if (PNGCodec::Decode(&data->front(), data->size(), PNGCodec::FORMAT_BGRA,
+ &decoded_data, &width, &height)) {
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
+ bitmap->allocPixels();
+ unsigned char* bitmap_data =
+ reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0));
+ for (int i = width * height * 4 - 4; i >= 0; i -= 4) {
+ unsigned char alpha = decoded_data[i + 3];
+ if (alpha != 0 && alpha != 255) {
+ SkColor premultiplied = SkPreMultiplyARGB(alpha,
+ decoded_data[i], decoded_data[i + 1], decoded_data[i + 2]);
+ bitmap_data[i + 3] = alpha;
+ bitmap_data[i] = SkColorGetR(premultiplied);
+ bitmap_data[i + 1] = SkColorGetG(premultiplied);
+ bitmap_data[i + 2] = SkColorGetB(premultiplied);
+ } else {
+ bitmap_data[i + 3] = alpha;
+ bitmap_data[i] = decoded_data[i];
+ bitmap_data[i + 1] = decoded_data[i + 1];
+ bitmap_data[i + 2] = decoded_data[i + 2];
+ }
+ }
+ return true;
+ }
+ return false;
+}
+
+// static
+SkBitmap* PNGCodec::CreateSkBitmapFromBGRAFormat(
+ std::vector<unsigned char>& bgra, int width, int height) {
+ SkBitmap* bitmap = new SkBitmap();
+ bitmap->setConfig(SkBitmap::kARGB_8888_Config, width, height);
+ bitmap->allocPixels();
+
+ bool opaque = false;
+ unsigned char* bitmap_data =
+ reinterpret_cast<unsigned char*>(bitmap->getAddr32(0, 0));
+ for (int i = width * height * 4 - 4; i >= 0; i -= 4) {
+ unsigned char alpha = bgra[i + 3];
+ if (!opaque && alpha != 255) {
+ opaque = false;
+ }
+ bitmap_data[i + 3] = alpha;
+ bitmap_data[i] = (bgra[i] * alpha) >> 8;
+ bitmap_data[i + 1] = (bgra[i + 1] * alpha) >> 8;
+ bitmap_data[i + 2] = (bgra[i + 2] * alpha) >> 8;
+ }
+
+ bitmap->setIsOpaque(opaque);
+ return bitmap;
+}
+
+// Encoder --------------------------------------------------------------------
+//
+// This section of the code is based on nsPNGEncoder.cpp in Mozilla
+// (Copyright 2005 Google Inc.)
+
+namespace {
+
+// Passed around as the io_ptr in the png structs so our callbacks know where
+// to write data.
+struct PngEncoderState {
+ PngEncoderState(std::vector<unsigned char>* o) : out(o) {}
+ std::vector<unsigned char>* out;
+};
+
+// Called by libpng to flush its internal buffer to ours.
+void EncoderWriteCallback(png_structp png, png_bytep data, png_size_t size) {
+ PngEncoderState* state = static_cast<PngEncoderState*>(png_get_io_ptr(png));
+ DCHECK(state->out);
+
+ size_t old_size = state->out->size();
+ state->out->resize(old_size + size);
+ memcpy(&(*state->out)[old_size], data, size);
+}
+
+void ConvertBGRAtoRGB(const unsigned char* bgra, int pixel_width,
+ unsigned char* rgb) {
+ for (int x = 0; x < pixel_width; x++) {
+ const unsigned char* pixel_in = &bgra[x * 4];
+ unsigned char* pixel_out = &rgb[x * 3];
+ pixel_out[0] = pixel_in[2];
+ pixel_out[1] = pixel_in[1];
+ pixel_out[2] = pixel_in[0];
+ }
+}
+
+// Automatically destroys the given write structs on destruction to make
+// cleanup and error handling code cleaner.
+class PngWriteStructDestroyer {
+ public:
+ PngWriteStructDestroyer(png_struct** ps, png_info** pi) : ps_(ps), pi_(pi) {
+ }
+ ~PngWriteStructDestroyer() {
+ png_destroy_write_struct(ps_, pi_);
+ }
+ private:
+ png_struct** ps_;
+ png_info** pi_;
+
+ DISALLOW_EVIL_CONSTRUCTORS(PngWriteStructDestroyer);
+};
+
+} // namespace
+
+// static
+bool PNGCodec::Encode(const unsigned char* input, ColorFormat format,
+ int w, int h, int row_byte_width,
+ bool discard_transparency,
+ std::vector<unsigned char>* output) {
+ // Run to convert an input row into the output row format, NULL means no
+ // conversion is necessary.
+ void (*converter)(const unsigned char* in, int w, unsigned char* out) = NULL;
+
+ int input_color_components, output_color_components;
+ int png_output_color_type;
+ switch (format) {
+ case FORMAT_RGB:
+ input_color_components = 3;
+ output_color_components = 3;
+ png_output_color_type = PNG_COLOR_TYPE_RGB;
+ discard_transparency = false;
+ break;
+
+ case FORMAT_RGBA:
+ input_color_components = 4;
+ if (discard_transparency) {
+ output_color_components = 3;
+ png_output_color_type = PNG_COLOR_TYPE_RGB;
+ converter = ConvertRGBAtoRGB;
+ } else {
+ output_color_components = 4;
+ png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ converter = NULL;
+ }
+ break;
+
+ case FORMAT_BGRA:
+ input_color_components = 4;
+ if (discard_transparency) {
+ output_color_components = 3;
+ png_output_color_type = PNG_COLOR_TYPE_RGB;
+ converter = ConvertBGRAtoRGB;
+ } else {
+ output_color_components = 4;
+ png_output_color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+ converter = ConvertBetweenBGRAandRGBA;
+ }
+ break;
+
+ default:
+ NOTREACHED() << "Unknown pixel format";
+ return false;
+ }
+
+ // Row stride should be at least as long as the length of the data.
+ DCHECK(input_color_components * w <= row_byte_width);
+
+ png_struct* png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
+ png_voidp_NULL,
+ png_error_ptr_NULL,
+ png_error_ptr_NULL);
+ if (!png_ptr)
+ return false;
+ png_info* info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ png_destroy_write_struct(&png_ptr, NULL);
+ return false;
+ }
+ PngWriteStructDestroyer destroyer(&png_ptr, &info_ptr);
+
+ if (setjmp(png_jmpbuf(png_ptr))) {
+ // The destroyer will ensure that the structures are cleaned up in this
+ // case, even though we may get here as a jump from random parts of the
+ // PNG library called below.
+ return false;
+ }
+
+ // Set our callback for libpng to give us the data.
+ PngEncoderState state(output);
+ png_set_write_fn(png_ptr, &state, EncoderWriteCallback, NULL);
+
+ png_set_IHDR(png_ptr, info_ptr, w, h, 8, png_output_color_type,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
+ PNG_FILTER_TYPE_DEFAULT);
+ png_write_info(png_ptr, info_ptr);
+
+ if (!converter) {
+ // No conversion needed, give the data directly to libpng.
+ for (int y = 0; y < h; y ++)
+ png_write_row(png_ptr,
+ const_cast<unsigned char*>(&input[y * row_byte_width]));
+ } else {
+ // Needs conversion using a separate buffer.
+ unsigned char* row = new unsigned char[w * output_color_components];
+ for (int y = 0; y < h; y ++) {
+ converter(&input[y * row_byte_width], w, row);
+ png_write_row(png_ptr, row);
+ }
+ delete[] row;
+ }
+
+ png_write_end(png_ptr, info_ptr);
+ return true;
+}
+
+// static
+bool PNGCodec::EncodeBGRASkBitmap(const SkBitmap& input,
+ bool discard_transparency,
+ std::vector<unsigned char>* output) {
+ static const int bbp = 4;
+
+ SkAutoLockPixels lock_input(input);
+ DCHECK(input.empty() || input.bytesPerPixel() == bbp);
+
+ // SkBitmaps are premultiplied, we need to unpremultiply them.
+ scoped_array<unsigned char> divided(
+ new unsigned char[input.width() * input.height() * bbp]);
+
+ int i = 0;
+ for (int y = 0; y < input.height(); y++) {
+ for (int x = 0; x < input.width(); x++) {
+ uint32 pixel = input.getAddr32(0, y)[x];
+
+ int alpha = SkColorGetA(pixel);
+ if (alpha != 0 && alpha != 255) {
+ SkColor unmultiplied = SkUnPreMultiply::PMColorToColor(pixel);
+ divided[i + 0] = SkColorGetR(unmultiplied);
+ divided[i + 1] = SkColorGetG(unmultiplied);
+ divided[i + 2] = SkColorGetB(unmultiplied);
+ divided[i + 3] = alpha;
+ } else {
+ divided[i + 0] = SkColorGetR(pixel);
+ divided[i + 1] = SkColorGetG(pixel);
+ divided[i + 2] = SkColorGetB(pixel);
+ divided[i + 3] = alpha;
+ }
+ i += bbp;
+ }
+ }
+
+ return Encode(divided.get(),
+ PNGCodec::FORMAT_RGBA, input.width(), input.height(),
+ input.width() * bbp, discard_transparency, output);
+}
+
+} // namespace gfx
diff --git a/app/gfx/codec/png_codec.h b/app/gfx/codec/png_codec.h
new file mode 100644
index 0000000..1e07632
--- /dev/null
+++ b/app/gfx/codec/png_codec.h
@@ -0,0 +1,96 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef APP_GFX_CODEC_PNG_CODEC_H_
+#define APP_GFX_CODEC_PNG_CODEC_H_
+
+#include <vector>
+
+#include "base/basictypes.h"
+
+class SkBitmap;
+
+namespace gfx {
+
+// Interface for encoding and decoding PNG data. This is a wrapper around
+// libpng, which has an inconvenient interface for callers. This is currently
+// designed for use in tests only (where we control the files), so the handling
+// isn't as robust as would be required for a browser (see Decode() for more).
+// WebKit has its own more complicated PNG decoder which handles, among other
+// things, partially downloaded data.
+class PNGCodec {
+ public:
+ enum ColorFormat {
+ // 3 bytes per pixel (packed), in RGB order regardless of endianness.
+ // This is the native JPEG format.
+ FORMAT_RGB,
+
+ // 4 bytes per pixel, in RGBA order in memory regardless of endianness.
+ FORMAT_RGBA,
+
+ // 4 bytes per pixel, in BGRA order in memory regardless of endianness.
+ // This is the default Windows DIB order.
+ FORMAT_BGRA
+ };
+
+ // Encodes the given raw 'input' data, with each pixel being represented as
+ // given in 'format'. The encoded PNG data will be written into the supplied
+ // vector and true will be returned on success. On failure (false), the
+ // contents of the output buffer are undefined.
+ //
+ // When writing alpha values, the input colors are assumed to be post
+ // multiplied.
+ //
+ // w, h: dimensions of the image
+ // row_byte_width: the width in bytes of each row. This may be greater than
+ // w * bytes_per_pixel if there is extra padding at the end of each row
+ // (often, each row is padded to the next machine word).
+ // discard_transparency: when true, and when the input data format includes
+ // alpha values, these alpha values will be discarded and only RGB will be
+ // written to the resulting file. Otherwise, alpha values in the input
+ // will be preserved.
+ static bool Encode(const unsigned char* input, ColorFormat format,
+ int w, int h, int row_byte_width,
+ bool discard_transparency,
+ std::vector<unsigned char>* output);
+
+ // Call PNGCodec::Encode on the supplied SkBitmap |input|, which is assumed
+ // to be BGRA, 32 bits per pixel. The params |discard_transparency| and
+ // |output| are passed directly to Encode; refer to Encode for more
+ // information. During the call, an SkAutoLockPixels lock is held on |input|.
+ static bool EncodeBGRASkBitmap(const SkBitmap& input,
+ bool discard_transparency,
+ std::vector<unsigned char>* output);
+
+ // Decodes the PNG data contained in input of length input_size. The
+ // decoded data will be placed in *output with the dimensions in *w and *h
+ // on success (returns true). This data will be written in the 'format'
+ // format. On failure, the values of these output variables are undefined.
+ //
+ // This function may not support all PNG types, and it hasn't been tested
+ // with a large number of images, so assume a new format may not work. It's
+ // really designed to be able to read in something written by Encode() above.
+ static bool Decode(const unsigned char* input, size_t input_size,
+ ColorFormat format, std::vector<unsigned char>* output,
+ int* w, int* h);
+
+ // A convenience function for decoding PNGs as previously encoded by the PNG
+ // encoder. Chrome encodes png in the format PNGCodec::FORMAT_BGRA.
+ //
+ // Returns true if data is non-null and can be decoded as a png, false
+ // otherwise.
+ static bool Decode(const std::vector<unsigned char>* data, SkBitmap* icon);
+
+ // Create a SkBitmap from a decoded BGRA DIB. The caller owns the returned
+ // SkBitmap.
+ static SkBitmap* CreateSkBitmapFromBGRAFormat(
+ std::vector<unsigned char>& bgra, int width, int height);
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(PNGCodec);
+};
+
+} // namespace gfx
+
+#endif // APP_GFX_CODEC_PNG_CODEC_H_
diff --git a/app/gfx/codec/png_codec_unittest.cc b/app/gfx/codec/png_codec_unittest.cc
new file mode 100644
index 0000000..d16f519
--- /dev/null
+++ b/app/gfx/codec/png_codec_unittest.cc
@@ -0,0 +1,251 @@
+// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include <math.h>
+
+#include "app/gfx/codec/png_codec.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/skia/include/core/SkBitmap.h"
+
+namespace gfx {
+
+static void MakeRGBImage(int w, int h, std::vector<unsigned char>* dat) {
+ dat->resize(w * h * 3);
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ unsigned char* org_px = &(*dat)[(y * w + x) * 3];
+ org_px[0] = x * 3; // r
+ org_px[1] = x * 3 + 1; // g
+ org_px[2] = x * 3 + 2; // b
+ }
+ }
+}
+
+// Set use_transparency to write data into the alpha channel, otherwise it will
+// be filled with 0xff. With the alpha channel stripped, this should yield the
+// same image as MakeRGBImage above, so the code below can make reference
+// images for conversion testing.
+static void MakeRGBAImage(int w, int h, bool use_transparency,
+ std::vector<unsigned char>* dat) {
+ dat->resize(w * h * 4);
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ unsigned char* org_px = &(*dat)[(y * w + x) * 4];
+ org_px[0] = x * 3; // r
+ org_px[1] = x * 3 + 1; // g
+ org_px[2] = x * 3 + 2; // b
+ if (use_transparency)
+ org_px[3] = x*3 + 3; // a
+ else
+ org_px[3] = 0xFF; // a (opaque)
+ }
+ }
+}
+
+// Returns true if each channel of the given two colors are "close." This is
+// used for comparing colors where rounding errors may cause off-by-one.
+bool ColorsClose(uint32_t a, uint32_t b) {
+ return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 &&
+ abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2;
+}
+
+void MakeTestSkBitmap(int w, int h, SkBitmap* bmp) {
+ bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h);
+ bmp->allocPixels();
+
+ uint32_t* src_data = bmp->getAddr32(0, 0);
+ for (int i = 0; i < w * h; i++) {
+ src_data[i] = SkPreMultiplyARGB(i % 255, i % 250, i % 245, i % 240);
+ }
+}
+
+TEST(PNGCodec, EncodeDecodeRGB) {
+ const int w = 20, h = 20;
+
+ // create an image with known values
+ std::vector<unsigned char> original;
+ MakeRGBImage(w, h, &original);
+
+ // encode
+ std::vector<unsigned char> encoded;
+ EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, w, h,
+ w * 3, false, &encoded));
+
+ // decode, it should have the same size as the original
+ std::vector<unsigned char> decoded;
+ int outw, outh;
+ EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(),
+ PNGCodec::FORMAT_RGB, &decoded,
+ &outw, &outh));
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original.size(), decoded.size());
+
+ // Images must be equal
+ ASSERT_TRUE(original == decoded);
+}
+
+TEST(PNGCodec, EncodeDecodeRGBA) {
+ const int w = 20, h = 20;
+
+ // create an image with known values, a must be opaque because it will be
+ // lost during encoding
+ std::vector<unsigned char> original;
+ MakeRGBAImage(w, h, true, &original);
+
+ // encode
+ std::vector<unsigned char> encoded;
+ EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGBA, w, h,
+ w * 4, false, &encoded));
+
+ // decode, it should have the same size as the original
+ std::vector<unsigned char> decoded;
+ int outw, outh;
+ EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(),
+ PNGCodec::FORMAT_RGBA, &decoded,
+ &outw, &outh));
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original.size(), decoded.size());
+
+ // Images must be exactly equal
+ ASSERT_TRUE(original == decoded);
+}
+
+// Test that corrupted data decompression causes failures.
+TEST(PNGCodec, DecodeCorrupted) {
+ int w = 20, h = 20;
+
+ // Make some random data (an uncompressed image).
+ std::vector<unsigned char> original;
+ MakeRGBImage(w, h, &original);
+
+ // It should fail when given non-JPEG compressed data.
+ std::vector<unsigned char> output;
+ int outw, outh;
+ EXPECT_FALSE(PNGCodec::Decode(&original[0], original.size(),
+ PNGCodec::FORMAT_RGB, &output,
+ &outw, &outh));
+
+ // Make some compressed data.
+ std::vector<unsigned char> compressed;
+ EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_RGB, w, h,
+ w * 3, false, &compressed));
+
+ // Try decompressing a truncated version.
+ EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size() / 2,
+ PNGCodec::FORMAT_RGB, &output,
+ &outw, &outh));
+
+ // Corrupt it and try decompressing that.
+ for (int i = 10; i < 30; i++)
+ compressed[i] = i;
+ EXPECT_FALSE(PNGCodec::Decode(&compressed[0], compressed.size(),
+ PNGCodec::FORMAT_RGB, &output,
+ &outw, &outh));
+}
+
+TEST(PNGCodec, EncodeDecodeBGRA) {
+ const int w = 20, h = 20;
+
+ // Create an image with known values, alpha must be opaque because it will be
+ // lost during encoding.
+ std::vector<unsigned char> original;
+ MakeRGBAImage(w, h, true, &original);
+
+ // Encode.
+ std::vector<unsigned char> encoded;
+ EXPECT_TRUE(PNGCodec::Encode(&original[0], PNGCodec::FORMAT_BGRA, w, h,
+ w * 4, false, &encoded));
+
+ // Decode, it should have the same size as the original.
+ std::vector<unsigned char> decoded;
+ int outw, outh;
+ EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(),
+ PNGCodec::FORMAT_BGRA, &decoded,
+ &outw, &outh));
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original.size(), decoded.size());
+
+ // Images must be exactly equal.
+ ASSERT_TRUE(original == decoded);
+}
+
+TEST(PNGCodec, StripAddAlpha) {
+ const int w = 20, h = 20;
+
+ // These should be the same except one has a 0xff alpha channel.
+ std::vector<unsigned char> original_rgb;
+ MakeRGBImage(w, h, &original_rgb);
+ std::vector<unsigned char> original_rgba;
+ MakeRGBAImage(w, h, false, &original_rgba);
+
+ // Encode RGBA data as RGB.
+ std::vector<unsigned char> encoded;
+ EXPECT_TRUE(PNGCodec::Encode(&original_rgba[0],
+ PNGCodec::FORMAT_RGBA,
+ w, h,
+ w * 4, true, &encoded));
+
+ // Decode the RGB to RGBA.
+ std::vector<unsigned char> decoded;
+ int outw, outh;
+ EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(),
+ PNGCodec::FORMAT_RGBA, &decoded,
+ &outw, &outh));
+
+ // Decoded and reference should be the same (opaque alpha).
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original_rgba.size(), decoded.size());
+ ASSERT_TRUE(original_rgba == decoded);
+
+ // Encode RGBA to RGBA.
+ EXPECT_TRUE(PNGCodec::Encode(&original_rgba[0],
+ PNGCodec::FORMAT_RGBA,
+ w, h,
+ w * 4, false, &encoded));
+
+ // Decode the RGBA to RGB.
+ EXPECT_TRUE(PNGCodec::Decode(&encoded[0], encoded.size(),
+ PNGCodec::FORMAT_RGB, &decoded,
+ &outw, &outh));
+
+ // It should be the same as our non-alpha-channel reference.
+ ASSERT_EQ(w, outw);
+ ASSERT_EQ(h, outh);
+ ASSERT_EQ(original_rgb.size(), decoded.size());
+ ASSERT_TRUE(original_rgb == decoded);
+}
+
+TEST(PNGCodec, EncodeBGRASkBitmap) {
+ const int w = 20, h = 20;
+
+ SkBitmap original_bitmap;
+ MakeTestSkBitmap(w, h, &original_bitmap);
+
+ // Encode the bitmap.
+ std::vector<unsigned char> encoded;
+ PNGCodec::EncodeBGRASkBitmap(original_bitmap, false, &encoded);
+
+ // Decode the encoded string.
+ SkBitmap decoded_bitmap;
+ EXPECT_TRUE(PNGCodec::Decode(&encoded, &decoded_bitmap));
+
+ // Compare the original bitmap and the output bitmap. We use ColorsClose
+ // as SkBitmaps are considered to be pre-multiplied, the unpremultiplication
+ // (in Encode) and repremultiplication (in Decode) can be lossy.
+ for (int x = 0; x < w; x++) {
+ for (int y = 0; y < h; y++) {
+ uint32_t original_pixel = original_bitmap.getAddr32(0, y)[x];
+ uint32_t decoded_pixel = decoded_bitmap.getAddr32(0, y)[x];
+ EXPECT_TRUE(ColorsClose(original_pixel, decoded_pixel));
+ }
+ }
+}
+
+} // namespace gfx
diff --git a/app/resource_bundle.cc b/app/resource_bundle.cc
index c930fa3..a108660 100644
--- a/app/resource_bundle.cc
+++ b/app/resource_bundle.cc
@@ -4,8 +4,8 @@
#include "app/resource_bundle.h"
+#include "app/gfx/codec/png_codec.h"
#include "app/gfx/font.h"
-#include "base/gfx/png_decoder.h"
#include "base/logging.h"
#include "base/string_piece.h"
#include "net/base/file_stream.h"
@@ -80,16 +80,16 @@ SkBitmap* ResourceBundle::LoadBitmap(DataHandle data_handle, int resource_id) {
// Decode the PNG.
int image_width;
int image_height;
- if (!PNGDecoder::Decode(&raw_data.front(), raw_data.size(),
- PNGDecoder::FORMAT_BGRA,
- &png_data, &image_width, &image_height)) {
+ if (!gfx::PNGCodec::Decode(&raw_data.front(), raw_data.size(),
+ gfx::PNGCodec::FORMAT_BGRA,
+ &png_data, &image_width, &image_height)) {
NOTREACHED() << "Unable to decode image resource " << resource_id;
return NULL;
}
- return PNGDecoder::CreateSkBitmapFromBGRAFormat(png_data,
- image_width,
- image_height);
+ return gfx::PNGCodec::CreateSkBitmapFromBGRAFormat(png_data,
+ image_width,
+ image_height);
}
std::string ResourceBundle::GetDataResource(int resource_id) {