From 65e7a79bade71acadf8aee3dd96f93f71080503a Mon Sep 17 00:00:00 2001 From: "estade@chromium.org" Date: Tue, 27 Jan 2009 01:04:56 +0000 Subject: Move jpeg_codec.* to base/gfx Review URL: http://codereview.chromium.org/18772 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8692 0039d316-1c4b-4281-b951-d872f2087c98 --- base/base.xcodeproj/project.pbxproj | 41 ++ base/base_unittests.scons | 2 + base/build/base_gfx.vcproj | 8 + base/build/base_gfx.vsprops | 2 +- base/build/base_unittests.vcproj | 4 + base/gfx/base_gfx.scons | 3 + base/gfx/jpeg_codec.cc | 522 +++++++++++++++++++++ base/gfx/jpeg_codec.h | 60 +++ base/gfx/jpeg_codec_unittest.cc | 147 ++++++ chrome/browser/base_history_model.cc | 2 +- .../history/expire_history_backend_unittest.cc | 2 +- chrome/browser/history/history_backend_unittest.cc | 2 +- chrome/browser/history/history_unittest.cc | 2 +- chrome/browser/history/thumbnail_database.cc | 2 +- .../browser/history/thumbnail_database_unittest.cc | 2 +- chrome/chrome.xcodeproj/project.pbxproj | 10 - chrome/common/common.scons | 2 - chrome/common/common.vcproj | 8 - chrome/common/common.vsprops | 2 +- chrome/common/jpeg_codec.cc | 521 -------------------- chrome/common/jpeg_codec.h | 60 --- chrome/common/jpeg_codec_unittest.cc | 147 ------ chrome/test/unit/unittests.vcproj | 4 - chrome/tools/profiles/generate_profile.cc | 2 +- 24 files changed, 796 insertions(+), 761 deletions(-) create mode 100644 base/gfx/jpeg_codec.cc create mode 100644 base/gfx/jpeg_codec.h create mode 100644 base/gfx/jpeg_codec_unittest.cc delete mode 100644 chrome/common/jpeg_codec.cc delete mode 100644 chrome/common/jpeg_codec.h delete mode 100644 chrome/common/jpeg_codec_unittest.cc diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index e5ef83a..ece0285 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -37,6 +37,9 @@ /* Begin PBXBuildFile section */ 141593B80EA63EBE00E32418 /* thread_collision_warner_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 146C6A6E0EA63D970029E7B6 /* thread_collision_warner_unittest.cc */; }; 146C6A6B0EA63D4F0029E7B6 /* thread_collision_warner.cc in Sources */ = {isa = PBXBuildFile; fileRef = 146C6A620EA63CAE0029E7B6 /* thread_collision_warner.cc */; }; + 32AC71B80F2E5321002BDDC8 /* jpeg_codec_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 32AC71B50F2E52FC002BDDC8 /* jpeg_codec_unittest.cc */; }; + 32AC71BF0F2E5421002BDDC8 /* jpeg_codec.cc in Sources */ = {isa = PBXBuildFile; fileRef = 32AC71B60F2E530F002BDDC8 /* jpeg_codec.cc */; }; + 32AC72300F2E64F7002BDDC8 /* libjpeg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 32AC718E0F2E4F62002BDDC8 /* libjpeg.a */; }; 4D11B59A0E91730200EF7617 /* rand_util.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D11B5940E9172F800EF7617 /* rand_util.cc */; }; 4D11B59B0E91730200EF7617 /* rand_util_posix.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D11B5960E9172F800EF7617 /* rand_util_posix.cc */; }; 4D11B59C0E91730500EF7617 /* rand_util_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D11B5970E9172F800EF7617 /* rand_util_unittest.cc */; }; @@ -194,6 +197,13 @@ /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ + 32AC718D0F2E4F62002BDDC8 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 32AC71890F2E4F62002BDDC8 /* libjpeg.xcodeproj */; + proxyType = 2; + remoteGlobalIDString = D2AAC046055464E500DB518D /* libjpeg.a */; + remoteInfo = libjpeg; + }; 7B165D2F0E55081000185273 /* PBXContainerItemProxy */ = { isa = PBXContainerItemProxy; containerPortal = E45A2C680E47AEFF00DB1196 /* gtest.xcodeproj */; @@ -382,6 +392,10 @@ 146C6A610EA63C9F0029E7B6 /* thread_collision_warner.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = thread_collision_warner.h; sourceTree = ""; }; 146C6A620EA63CAE0029E7B6 /* thread_collision_warner.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_collision_warner.cc; sourceTree = ""; }; 146C6A6E0EA63D970029E7B6 /* thread_collision_warner_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = thread_collision_warner_unittest.cc; sourceTree = ""; }; + 32AC71890F2E4F62002BDDC8 /* libjpeg.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = libjpeg.xcodeproj; path = third_party/libjpeg/libjpeg.xcodeproj; sourceTree = ""; }; + 32AC71B50F2E52FC002BDDC8 /* jpeg_codec_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jpeg_codec_unittest.cc; sourceTree = ""; }; + 32AC71B60F2E530F002BDDC8 /* jpeg_codec.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jpeg_codec.cc; sourceTree = ""; }; + 32AC71B70F2E530F002BDDC8 /* jpeg_codec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jpeg_codec.h; sourceTree = ""; }; 38246046390EAED65BB7C380 /* waitable_event_watcher_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = waitable_event_watcher_posix.cc; sourceTree = ""; }; 4D11B5940E9172F800EF7617 /* rand_util.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = rand_util.cc; sourceTree = ""; }; 4D11B5950E9172F800EF7617 /* rand_util.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = rand_util.h; sourceTree = ""; }; @@ -704,6 +718,7 @@ 7B836E180E55CE5B00F6AD31 /* libicudata.a in Frameworks */, 7B836ADE0E55097000F6AD31 /* libicui18n.a in Frameworks */, 7B836ADD0E55097000F6AD31 /* libicuuc.a in Frameworks */, + 32AC72300F2E64F7002BDDC8 /* libjpeg.a in Frameworks */, 7B85062A0E5B556900730B43 /* libpng.a in Frameworks */, 7B4DF5350E5B6A66004D7619 /* libskia.a in Frameworks */, 7B85062F0E5B559A00730B43 /* libzlib.a in Frameworks */, @@ -713,6 +728,14 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ + 32AC718A0F2E4F62002BDDC8 /* Products */ = { + isa = PBXGroup; + children = ( + 32AC718E0F2E4F62002BDDC8 /* libjpeg.a */, + ); + name = Products; + sourceTree = ""; + }; 7B165D2C0E55081000185273 /* Products */ = { isa = PBXGroup; children = ( @@ -773,6 +796,7 @@ E45A2C680E47AEFF00DB1196 /* gtest.xcodeproj */, E4562AF30E27E428005E4685 /* icu.xcodeproj */, 7B26301F0E82F1E6001CE27F /* libevent.xcodeproj */, + 32AC71890F2E4F62002BDDC8 /* libjpeg.xcodeproj */, E4562A2A0E27CA2F005E4685 /* libpng.xcodeproj */, 829E30D60DBFD8ED00819EBF /* skia.xcodeproj */, E4562A3B0E27CAB6005E4685 /* zlib.xcodeproj */, @@ -1087,6 +1111,9 @@ 825403B40D92D2EC0006B936 /* gfx */ = { isa = PBXGroup; children = ( + 32AC71B60F2E530F002BDDC8 /* jpeg_codec.cc */, + 32AC71B70F2E530F002BDDC8 /* jpeg_codec.h */, + 32AC71B50F2E52FC002BDDC8 /* jpeg_codec_unittest.cc */, 825403C80D92D31D0006B936 /* native_theme.cc */, 825403C90D92D31D0006B936 /* native_theme.h */, 7B8505A20E5B3FBE00730B43 /* native_theme_unittest.cc */, @@ -1204,6 +1231,10 @@ ProjectRef = 7B26301F0E82F1E6001CE27F /* libevent.xcodeproj */; }, { + ProductGroup = 32AC718A0F2E4F62002BDDC8 /* Products */; + ProjectRef = 32AC71890F2E4F62002BDDC8 /* libjpeg.xcodeproj */; + }, + { ProductGroup = 7B165D5A0E55081400185273 /* Products */; ProjectRef = E4562A2A0E27CA2F005E4685 /* libpng.xcodeproj */; }, @@ -1228,6 +1259,13 @@ /* End PBXProject section */ /* Begin PBXReferenceProxy section */ + 32AC718E0F2E4F62002BDDC8 /* libjpeg.a */ = { + isa = PBXReferenceProxy; + fileType = archive.ar; + path = libjpeg.a; + remoteRef = 32AC718D0F2E4F62002BDDC8 /* PBXContainerItemProxy */; + sourceTree = BUILT_PRODUCTS_DIR; + }; 7B165D300E55081000185273 /* libgtest.a */ = { isa = PBXReferenceProxy; fileType = archive.ar; @@ -1442,6 +1480,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 32AC71BF0F2E5421002BDDC8 /* jpeg_codec.cc in Sources */, 825403EE0D92D31D0006B936 /* png_decoder.cc in Sources */, 825403F00D92D31D0006B936 /* png_encoder.cc in Sources */, 825403F20D92D31D0006B936 /* point.cc in Sources */, @@ -1466,6 +1505,7 @@ 93611B1A0E5A878400F9405D /* histogram_unittest.cc in Sources */, 7BAE392B0E6F4EF200C3F750 /* hmac_unittest.cc in Sources */, B57E4D780E9C26340090055D /* idletimer_unittest.cc in Sources */, + 32AC71B80F2E5321002BDDC8 /* jpeg_codec_unittest.cc in Sources */, 7B78D3920E54FE0100609465 /* json_reader_unittest.cc in Sources */, 7B78D3930E54FE0100609465 /* json_writer_unittest.cc in Sources */, 7BF892E00E758883000BAF8A /* lazy_instance_unittest.cc in Sources */, @@ -1637,6 +1677,7 @@ .., ../skia/include, ../skia/include/corecg, + ../third_party/libjpeg, ../third_party/libpng, ); PRODUCT_NAME = base_gfx; diff --git a/base/base_unittests.scons b/base/base_unittests.scons index a7ea02f..48eaef8 100644 --- a/base/base_unittests.scons +++ b/base/base_unittests.scons @@ -15,6 +15,7 @@ env.ApplySConscript([ '$BASE_DIR/gfx/using_base_gfx.scons', '$GTEST_DIR/../using_gtest.scons', '$ICU38_DIR/using_icu38.scons', + '$LIBJPEG_DIR/using_libjpeg.scons', '$LIBPNG_DIR/using_libpng.scons', '$SKIA_DIR/using_skia.scons', '$ZLIB_DIR/using_zlib.scons', @@ -120,6 +121,7 @@ input_files = ChromeFileList([ ]), MSVSFilter('gfx_tests', [ + 'gfx/jpeg_codec_unittest.cc', 'gfx/png_codec_unittest.cc', ]), ]) diff --git a/base/build/base_gfx.vcproj b/base/build/base_gfx.vcproj index 70875a9..b19945a 100644 --- a/base/build/base_gfx.vcproj +++ b/base/build/base_gfx.vcproj @@ -122,6 +122,14 @@ + + + + diff --git a/base/build/base_gfx.vsprops b/base/build/base_gfx.vsprops index 9dc5341..2ddeb0a 100644 --- a/base/build/base_gfx.vsprops +++ b/base/build/base_gfx.vsprops @@ -3,6 +3,6 @@ ProjectType="Visual C++" Version="8.00" Name="base_gfx" - InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\third_party\icu38\build\using_icu.vsprops;$(SolutionDir)..\third_party\libpng\using_libpng.vsprops;$(SolutionDir)..\third_party\zlib\using_zlib.vsprops;..\..\skia\using_skia.vsprops" + InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\third_party\icu38\build\using_icu.vsprops;$(SolutionDir)..\third_party\libjpeg\using_libjpeg.vsprops;$(SolutionDir)..\third_party\libpng\using_libpng.vsprops;$(SolutionDir)..\third_party\zlib\using_zlib.vsprops;..\..\skia\using_skia.vsprops" > diff --git a/base/build/base_unittests.vcproj b/base/build/base_unittests.vcproj index a35fcc4..22df2c8 100644 --- a/base/build/base_unittests.vcproj +++ b/base/build/base_unittests.vcproj @@ -412,6 +412,10 @@ Name="gfx_tests" > + + diff --git a/base/gfx/base_gfx.scons b/base/gfx/base_gfx.scons index ceb663b..4c93190 100644 --- a/base/gfx/base_gfx.scons +++ b/base/gfx/base_gfx.scons @@ -12,6 +12,7 @@ env = env.Clone() env.ApplySConscript([ '$ICU38_DIR/using_icu38.scons', + '$LIBJPEG_DIR/using_libjpeg.scons', '$LIBPNG_DIR/using_libpng.scons', '$SKIA_DIR/using_skia.scons', '$ZLIB_DIR/using_zlib.scons', @@ -25,6 +26,8 @@ if env.Bit('windows'): ) input_files = ChromeFileList([ + 'jpeg_codec.cc', + 'jpeg_codec.h', 'gdi_util.cc', 'gdi_util.h', 'native_theme.cc', diff --git a/base/gfx/jpeg_codec.cc b/base/gfx/jpeg_codec.cc new file mode 100644 index 0000000..f78d5af --- /dev/null +++ b/base/gfx/jpeg_codec.cc @@ -0,0 +1,522 @@ +// 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 "base/gfx/jpeg_codec.h" + +#include + +#include "base/logging.h" +#include "base/scoped_ptr.h" +#include "skia/include/SkBitmap.h" + +extern "C" { +#include "jpeglib.h" +} + +// 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(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* 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* 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(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(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(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* output) { + jpeg_compress_struct cinfo; + CompressDestroyer destroyer; + 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); + destroyer.SetManagedObject(&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(&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(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(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(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* output, + int* w, int* h) { + jpeg_decompress_struct cinfo; + DecompressDestroyer destroyer; + 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); + destroyer.SetManagedObject(&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(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 row_data(new unsigned char[row_read_stride]); + unsigned char* rowptr = row_data.get(); + for (int row = 0; row < static_cast(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 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; +} + diff --git a/base/gfx/jpeg_codec.h b/base/gfx/jpeg_codec.h new file mode 100644 index 0000000..141c00e --- /dev/null +++ b/base/gfx/jpeg_codec.h @@ -0,0 +1,60 @@ +// 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 BASE_GFX_JPEG_CODEC_H_ +#define BASE_GFX_JPEG_CODEC_H_ + +#include + +class SkBitmap; + +// 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* 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* 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); +}; + +#endif // BASE_GFX_JPEG_CODEC_H_ + diff --git a/base/gfx/jpeg_codec_unittest.cc b/base/gfx/jpeg_codec_unittest.cc new file mode 100644 index 0000000..0a58630 --- /dev/null +++ b/base/gfx/jpeg_codec_unittest.cc @@ -0,0 +1,147 @@ +// 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 + +#include "base/gfx/jpeg_codec.h" +#include "testing/gtest/include/gtest/gtest.h" + +// 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& a, + const std::vector& 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(a[i]) - static_cast(b[i])); + + return acc / static_cast(a.size()); +} + +static void MakeRGBImage(int w, int h, std::vector* 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 original; + MakeRGBImage(w, h, &original); + + // encode, making sure it was compressed some + std::vector 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 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 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 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 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 original; + MakeRGBImage(w, h, &original); + + // it should fail when given non-JPEG compressed data + std::vector output; + int outw, outh; + ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(), + JPEGCodec::FORMAT_RGB, &output, + &outw, &outh)); + + // make some compressed data + std::vector 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)); +} diff --git a/chrome/browser/base_history_model.cc b/chrome/browser/base_history_model.cc index cce4923..f1aa980 100644 --- a/chrome/browser/base_history_model.cc +++ b/chrome/browser/base_history_model.cc @@ -4,10 +4,10 @@ #include "chrome/browser/base_history_model.h" +#include "base/gfx/jpeg_codec.h" #include "base/gfx/png_decoder.h" #include "chrome/app/theme/theme_resources.h" #include "chrome/browser/profile.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/resource_bundle.h" #include "SkBitmap.h" diff --git a/chrome/browser/history/expire_history_backend_unittest.cc b/chrome/browser/history/expire_history_backend_unittest.cc index f3e6a8b..305a094 100644 --- a/chrome/browser/history/expire_history_backend_unittest.cc +++ b/chrome/browser/history/expire_history_backend_unittest.cc @@ -5,6 +5,7 @@ #include "base/basictypes.h" #include "base/compiler_specific.h" #include "base/file_util.h" +#include "base/gfx/jpeg_codec.h" #include "base/path_service.h" #include "base/scoped_ptr.h" #include "chrome/browser/bookmarks/bookmark_model.h" @@ -13,7 +14,6 @@ #include "chrome/browser/history/history_database.h" #include "chrome/browser/history/text_database_manager.h" #include "chrome/browser/history/thumbnail_database.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/notification_service.h" #include "chrome/common/thumbnail_score.h" #include "chrome/tools/profiles/thumbnail-inl.h" diff --git a/chrome/browser/history/history_backend_unittest.cc b/chrome/browser/history/history_backend_unittest.cc index ea86d2e..c8aa28c 100644 --- a/chrome/browser/history/history_backend_unittest.cc +++ b/chrome/browser/history/history_backend_unittest.cc @@ -3,13 +3,13 @@ // found in the LICENSE file. #include "base/file_util.h" +#include "base/gfx/jpeg_codec.h" #include "base/path_service.h" #include "base/scoped_ptr.h" #include "chrome/browser/bookmarks/bookmark_model.h" #include "chrome/browser/history/history_backend.h" #include "chrome/browser/history/in_memory_history_backend.h" #include "chrome/browser/history/in_memory_database.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/thumbnail_score.h" #include "chrome/tools/profiles/thumbnail-inl.h" #include "googleurl/src/gurl.h" diff --git a/chrome/browser/history/history_unittest.cc b/chrome/browser/history/history_unittest.cc index 2f10adb..3018f25 100644 --- a/chrome/browser/history/history_unittest.cc +++ b/chrome/browser/history/history_unittest.cc @@ -22,6 +22,7 @@ #include "base/basictypes.h" #include "base/file_util.h" +#include "base/gfx/jpeg_codec.h" #include "base/message_loop.h" #include "base/path_service.h" #include "base/string_util.h" @@ -35,7 +36,6 @@ #include "chrome/browser/history/in_memory_history_backend.h" #include "chrome/browser/history/page_usage_data.h" #include "chrome/common/chrome_paths.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/notification_service.h" #include "chrome/common/sqlite_utils.h" #include "chrome/common/scoped_vector.h" diff --git a/chrome/browser/history/thumbnail_database.cc b/chrome/browser/history/thumbnail_database.cc index d493b56..3f60561 100644 --- a/chrome/browser/history/thumbnail_database.cc +++ b/chrome/browser/history/thumbnail_database.cc @@ -5,11 +5,11 @@ #include "chrome/browser/history/thumbnail_database.h" #include "base/file_util.h" +#include "base/gfx/jpeg_codec.h" #include "base/time.h" #include "base/string_util.h" #include "chrome/browser/history/history_publisher.h" #include "chrome/browser/history/url_database.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/sqlite_utils.h" #include "chrome/common/thumbnail_score.h" #include "skia/include/SkBitmap.h" diff --git a/chrome/browser/history/thumbnail_database_unittest.cc b/chrome/browser/history/thumbnail_database_unittest.cc index c8ad456..cc51477 100644 --- a/chrome/browser/history/thumbnail_database_unittest.cc +++ b/chrome/browser/history/thumbnail_database_unittest.cc @@ -4,10 +4,10 @@ #include "base/basictypes.h" #include "base/file_util.h" +#include "base/gfx/jpeg_codec.h" #include "base/path_service.h" #include "chrome/browser/history/thumbnail_database.h" #include "chrome/common/chrome_paths.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/thumbnail_score.h" #include "chrome/tools/profiles/thumbnail-inl.h" #include "googleurl/src/gurl.h" diff --git a/chrome/chrome.xcodeproj/project.pbxproj b/chrome/chrome.xcodeproj/project.pbxproj index 499b205..309ae19 100644 --- a/chrome/chrome.xcodeproj/project.pbxproj +++ b/chrome/chrome.xcodeproj/project.pbxproj @@ -148,7 +148,6 @@ 4D7BFC380E9D4CFF009A6919 /* ipc_message.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBB00E9D4C9F009A6919 /* ipc_message.cc */; }; 4D7BFC4B0E9D4D06009A6919 /* json_value_serializer.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBC30E9D4C9F009A6919 /* json_value_serializer.cc */; }; 4D7BFC540E9D4D09009A6919 /* libxml_utils.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBCC0E9D4C9F009A6919 /* libxml_utils.cc */; }; - 4D7BFC580E9D4D0E009A6919 /* jpeg_codec.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBC00E9D4C9F009A6919 /* jpeg_codec.cc */; }; 4D7BFC5C0E9D4D24009A6919 /* notification_service.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBD90E9D4C9F009A6919 /* notification_service.cc */; }; 4D7BFC710E9D4D28009A6919 /* pref_member.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBE50E9D4C9F009A6919 /* pref_member.cc */; }; 4D7BFC7A0E9D4D2B009A6919 /* pref_names.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBE80E9D4C9F009A6919 /* pref_names.cc */; }; @@ -163,7 +162,6 @@ 4D7BFCCE0E9D4D7A009A6919 /* cookie_monster_sqlite.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFCBF0E9D4D6B009A6919 /* cookie_monster_sqlite.cc */; }; 4D7BFCDF0E9D4DC4009A6919 /* animation_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFB870E9D4C9F009A6919 /* animation_unittest.cc */; }; 4D7BFCE10E9D4DC8009A6919 /* bzip2_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFB880E9D4C9F009A6919 /* bzip2_unittest.cc */; }; - 4D7BFCE30E9D4DCE009A6919 /* jpeg_codec_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBC20E9D4C9F009A6919 /* jpeg_codec_unittest.cc */; }; 4D7BFCE70E9D4DD4009A6919 /* json_value_serializer_unittest.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFBC60E9D4C9F009A6919 /* json_value_serializer_unittest.cc */; }; 4D7BFCF30E9D4E07009A6919 /* run_all_unittests.cc in Sources */ = {isa = PBXBuildFile; fileRef = 4D7BFCEF0E9D4DFE009A6919 /* run_all_unittests.cc */; }; 4D7BFD960E9D5223009A6919 /* libbrowser.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4D7BF3060E9D477E009A6919 /* libbrowser.a */; }; @@ -1540,9 +1538,6 @@ 4D7BFBBD0E9D4C9F009A6919 /* ipc_sync_message_unittest.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ipc_sync_message_unittest.h; sourceTree = ""; }; 4D7BFBBE0E9D4C9F009A6919 /* ipc_tests.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ipc_tests.cc; sourceTree = ""; }; 4D7BFBBF0E9D4C9F009A6919 /* ipc_tests.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ipc_tests.h; sourceTree = ""; }; - 4D7BFBC00E9D4C9F009A6919 /* jpeg_codec.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jpeg_codec.cc; sourceTree = ""; }; - 4D7BFBC10E9D4C9F009A6919 /* jpeg_codec.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = jpeg_codec.h; sourceTree = ""; }; - 4D7BFBC20E9D4C9F009A6919 /* jpeg_codec_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = jpeg_codec_unittest.cc; sourceTree = ""; }; 4D7BFBC30E9D4C9F009A6919 /* json_value_serializer.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = json_value_serializer.cc; sourceTree = ""; }; 4D7BFBC40E9D4C9F009A6919 /* json_value_serializer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = json_value_serializer.h; sourceTree = ""; }; 4D7BFBC50E9D4C9F009A6919 /* json_value_serializer_perftest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = json_value_serializer_perftest.cc; sourceTree = ""; }; @@ -2770,9 +2765,6 @@ 4D7BFBBD0E9D4C9F009A6919 /* ipc_sync_message_unittest.h */, 4D7BFBBE0E9D4C9F009A6919 /* ipc_tests.cc */, 4D7BFBBF0E9D4C9F009A6919 /* ipc_tests.h */, - 4D7BFBC00E9D4C9F009A6919 /* jpeg_codec.cc */, - 4D7BFBC10E9D4C9F009A6919 /* jpeg_codec.h */, - 4D7BFBC20E9D4C9F009A6919 /* jpeg_codec_unittest.cc */, 4D7BFBC30E9D4C9F009A6919 /* json_value_serializer.cc */, 4D7BFBC40E9D4C9F009A6919 /* json_value_serializer.h */, 4D7BFBC50E9D4C9F009A6919 /* json_value_serializer_perftest.cc */, @@ -4200,7 +4192,6 @@ B503E0FC0F01764800547DC6 /* user_script_slave_unittest.cc in Sources */, 4D7BFB3C0E9D4C25009A6919 /* history_types_unittest.cc in Sources */, B507AC440F004B610060FEE8 /* ipc_sync_message_unittest.cc in Sources */, - 4D7BFCE30E9D4DCE009A6919 /* jpeg_codec_unittest.cc in Sources */, 4D7BFCE70E9D4DD4009A6919 /* json_value_serializer_unittest.cc in Sources */, B52E29BE0F0AA333008AD1C8 /* l10n_util_unittest.cc in Sources */, E450775F0F154036003BE099 /* mru_cache_unittest.cc in Sources */, @@ -4241,7 +4232,6 @@ 4D7BFC380E9D4CFF009A6919 /* ipc_message.cc in Sources */, E4F3257D0EE83679002533CE /* ipc_message_utils.cc in Sources */, B507AC1F0F0048E10060FEE8 /* ipc_sync_message.cc in Sources */, - 4D7BFC580E9D4D0E009A6919 /* jpeg_codec.cc in Sources */, 4D7BFC4B0E9D4D06009A6919 /* json_value_serializer.cc in Sources */, B502DA520F098888005BE90C /* l10n_util.cc in Sources */, 4D7BFC540E9D4D09009A6919 /* libxml_utils.cc in Sources */, diff --git a/chrome/common/common.scons b/chrome/common/common.scons index aa65c66..3965ebb 100644 --- a/chrome/common/common.scons +++ b/chrome/common/common.scons @@ -129,8 +129,6 @@ input_files = ChromeFileList([ 'env_vars.h', 'filter_policy.h', 'gears_api.h', - 'jpeg_codec.cc', - 'jpeg_codec.h', 'json_value_serializer.cc', 'json_value_serializer.h', 'jstemplate_builder.cc', diff --git a/chrome/common/common.vcproj b/chrome/common/common.vcproj index c41b135..48fe00b 100644 --- a/chrome/common/common.vcproj +++ b/chrome/common/common.vcproj @@ -438,14 +438,6 @@ > - - - - diff --git a/chrome/common/common.vsprops b/chrome/common/common.vsprops index 3e611fc..faa2e6c 100644 --- a/chrome/common/common.vsprops +++ b/chrome/common/common.vsprops @@ -3,6 +3,6 @@ ProjectType="Visual C++" Version="8.00" Name="common (chrome)" - InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\third_party\icu38\build\using_icu.vsprops;$(SolutionDir)..\third_party\libjpeg\using_libjpeg.vsprops;$(SolutionDir)..\third_party\zlib\using_zlib.vsprops;$(SolutionDir)..\third_party\libpng\using_libpng.vsprops;$(SolutionDir)..\skia\using_skia.vsprops;$(SolutionDir)..\tools\grit\build\using_generated_resources.vsprops;$(SolutionDir)..\third_party\libxml\build\using_libxml.vsprops;$(SolutionDir)..\third_party\npapi\using_npapi.vsprops;$(SolutionDir)..\chrome\third_party\wtl\using_wtl.vsprops" + InheritedPropertySheets="$(SolutionDir)..\build\common.vsprops;$(SolutionDir)..\third_party\icu38\build\using_icu.vsprops;$(SolutionDir)..\third_party\zlib\using_zlib.vsprops;$(SolutionDir)..\third_party\libpng\using_libpng.vsprops;$(SolutionDir)..\skia\using_skia.vsprops;$(SolutionDir)..\tools\grit\build\using_generated_resources.vsprops;$(SolutionDir)..\third_party\libxml\build\using_libxml.vsprops;$(SolutionDir)..\third_party\npapi\using_npapi.vsprops;$(SolutionDir)..\chrome\third_party\wtl\using_wtl.vsprops" > diff --git a/chrome/common/jpeg_codec.cc b/chrome/common/jpeg_codec.cc deleted file mode 100644 index 08d5d45..0000000 --- a/chrome/common/jpeg_codec.cc +++ /dev/null @@ -1,521 +0,0 @@ -// 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 - -#include "chrome/common/jpeg_codec.h" -#include "base/logging.h" -#include "base/scoped_ptr.h" -#include "SkBitmap.h" - -extern "C" { -#include "jpeglib.h" -} - -// 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(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* 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* 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(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(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(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* output) { - jpeg_compress_struct cinfo; - CompressDestroyer destroyer; - 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); - destroyer.SetManagedObject(&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(&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(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(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(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* output, - int* w, int* h) { - jpeg_decompress_struct cinfo; - DecompressDestroyer destroyer; - 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); - destroyer.SetManagedObject(&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(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 row_data(new unsigned char[row_read_stride]); - unsigned char* rowptr = row_data.get(); - for (int row = 0; row < static_cast(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 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; -} - diff --git a/chrome/common/jpeg_codec.h b/chrome/common/jpeg_codec.h deleted file mode 100644 index 0ced106..0000000 --- a/chrome/common/jpeg_codec.h +++ /dev/null @@ -1,60 +0,0 @@ -// 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 CHROME_COMMON_JPEG_CODEC_H__ -#define CHROME_COMMON_JPEG_CODEC_H__ - -#include - -class SkBitmap; - -// 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* 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* 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); -}; - -#endif // CHROME_COMMON_JPEG_CODEC_H__ - diff --git a/chrome/common/jpeg_codec_unittest.cc b/chrome/common/jpeg_codec_unittest.cc deleted file mode 100644 index 35beed8..0000000 --- a/chrome/common/jpeg_codec_unittest.cc +++ /dev/null @@ -1,147 +0,0 @@ -// 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 - -#include "chrome/common/jpeg_codec.h" -#include "testing/gtest/include/gtest/gtest.h" - -// 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& a, - const std::vector& 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(a[i]) - static_cast(b[i])); - - return acc / static_cast(a.size()); -} - -static void MakeRGBImage(int w, int h, std::vector* 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 original; - MakeRGBImage(w, h, &original); - - // encode, making sure it was compressed some - std::vector 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 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 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 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 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 original; - MakeRGBImage(w, h, &original); - - // it should fail when given non-JPEG compressed data - std::vector output; - int outw, outh; - ASSERT_FALSE(JPEGCodec::Decode(&original[0], original.size(), - JPEGCodec::FORMAT_RGB, &output, - &outw, &outh)); - - // make some compressed data - std::vector 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)); -} diff --git a/chrome/test/unit/unittests.vcproj b/chrome/test/unit/unittests.vcproj index 817c1c7..048cdf1 100644 --- a/chrome/test/unit/unittests.vcproj +++ b/chrome/test/unit/unittests.vcproj @@ -843,10 +843,6 @@ > - - diff --git a/chrome/tools/profiles/generate_profile.cc b/chrome/tools/profiles/generate_profile.cc index 42685a9..0971879 100644 --- a/chrome/tools/profiles/generate_profile.cc +++ b/chrome/tools/profiles/generate_profile.cc @@ -8,6 +8,7 @@ #include "chrome/tools/profiles/thumbnail-inl.h" #include "base/at_exit.h" +#include "base/gfx/jpeg_codec.h" #include "base/icu_util.h" #include "base/message_loop.h" #include "base/path_service.h" @@ -15,7 +16,6 @@ #include "base/string_util.h" #include "base/time.h" #include "chrome/browser/history/history.h" -#include "chrome/common/jpeg_codec.h" #include "chrome/common/thumbnail_score.h" #include "SkBitmap.h" -- cgit v1.1