diff options
author | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-13 02:19:17 +0000 |
---|---|---|
committer | pkotwicz@chromium.org <pkotwicz@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-12-13 02:19:17 +0000 |
commit | 023f5e1d03c90150b1f47e952e5a0159ac5597bc (patch) | |
tree | a757f30f490d8cb8485644ec6ec7137f0fbb277f /ui | |
parent | 35d2edd2e2def89bafe671d5ff212e2e97241c1b (diff) | |
download | chromium_src-023f5e1d03c90150b1f47e952e5a0159ac5597bc.zip chromium_src-023f5e1d03c90150b1f47e952e5a0159ac5597bc.tar.gz chromium_src-023f5e1d03c90150b1f47e952e5a0159ac5597bc.tar.bz2 |
Removes Image(const unsigned char* png, size_t input_size) in favor of using gfx::Image::CreateFrom1xPNGBytes()
Also converts gfx::Image png API to use base::RefCountedMemory instead of base::RefCountedBytes
BUG=160632
Test=None
R=rsesek
TBR=sky
Review URL: https://chromiumcodereview.appspot.com/11415055
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@172779 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'ui')
-rw-r--r-- | ui/gfx/image/image.cc | 54 | ||||
-rw-r--r-- | ui/gfx/image/image.h | 17 | ||||
-rw-r--r-- | ui/gfx/image/image_ios.mm | 6 | ||||
-rw-r--r-- | ui/gfx/image/image_mac.mm | 4 | ||||
-rw-r--r-- | ui/gfx/image/image_mac_unittest.mm | 4 | ||||
-rw-r--r-- | ui/gfx/image/image_png_rep.cc | 2 | ||||
-rw-r--r-- | ui/gfx/image/image_png_rep.h | 4 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest.cc | 23 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest_util.cc | 2 | ||||
-rw-r--r-- | ui/gfx/image/image_unittest_util.h | 2 |
10 files changed, 55 insertions, 63 deletions
diff --git a/ui/gfx/image/image.cc b/ui/gfx/image/image.cc index f32498c..b09b66f 100644 --- a/ui/gfx/image/image.cc +++ b/ui/gfx/image/image.cc @@ -63,7 +63,7 @@ GdkPixbuf* GetErrorPixbuf() { GdkPixbuf* GdkPixbufFromPNG( const std::vector<gfx::ImagePNGRep>& image_png_reps) { - scoped_refptr<base::RefCountedBytes> png_bytes(NULL); + scoped_refptr<base::RefCountedMemory> png_bytes(NULL); for (size_t i = 0; i < image_png_reps.size(); ++i) { if (image_png_reps[i].scale_factor == ui::SCALE_FACTOR_100P) png_bytes = image_png_reps[i].raw_data; @@ -97,7 +97,7 @@ GdkPixbuf* GdkPixbufFromPNG( return pixbuf; } -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromPixbuf( +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromPixbuf( GdkPixbuf* pixbuf) { gchar* image = NULL; gsize image_size; @@ -114,12 +114,14 @@ scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromPixbuf( #endif // defined(TOOLKIT_GTK) #if defined(OS_IOS) -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromUIImage(UIImage* uiimage); +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( + UIImage* uiimage); // Caller takes ownership of the returned UIImage. UIImage* CreateUIImageFromPNG( const std::vector<gfx::ImagePNGRep>& image_png_reps); #elif defined(OS_MACOSX) -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromNSImage(NSImage* nsimage); +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage( + NSImage* nsimage); // Caller takes ownership of the returned NSImage. NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps); #endif // defined(OS_MACOSX) @@ -127,7 +129,7 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps); #if defined(OS_IOS) ImageSkia* ImageSkiaFromPNG( const std::vector<gfx::ImagePNGRep>& image_png_reps); -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromImageSkia( +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( const ImageSkia* skia); #else // Returns a 16x16 red image to visually show error in decoding PNG. @@ -147,7 +149,7 @@ ImageSkia* ImageSkiaFromPNG( scoped_ptr<gfx::ImageSkia> image_skia(new ImageSkia()); for (size_t i = 0; i < image_png_reps.size(); ++i) { - scoped_refptr<base::RefCountedBytes> raw_data = + scoped_refptr<base::RefCountedMemory> raw_data = image_png_reps[i].raw_data; CHECK(raw_data.get()); SkBitmap bitmap; @@ -164,7 +166,7 @@ ImageSkia* ImageSkiaFromPNG( return image_skia.release(); } -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromImageSkia( +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( const ImageSkia* image_skia) { ImageSkiaRep image_skia_rep = image_skia->GetRepresentation( ui::SCALE_FACTOR_100P); @@ -409,20 +411,6 @@ Image::Image() { // |storage_| is NULL for empty Images. } -Image::Image(const unsigned char* png, size_t input_size) { - if (input_size == 0u) - return; - - scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes()); - raw_data->data().assign(png, png + input_size); - std::vector<gfx::ImagePNGRep> image_reps; - image_reps.push_back(ImagePNGRep(raw_data, ui::SCALE_FACTOR_100P)); - - storage_ = new internal::ImageStorage(Image::kImageRepPNG); - internal::ImageRepPNG* rep = new internal::ImageRepPNG(image_reps); - AddRepresentation(rep); -} - Image::Image(const std::vector<ImagePNGRep>& image_reps) { // Do not store obviously invalid ImagePNGReps. std::vector<ImagePNGRep> filtered; @@ -497,9 +485,16 @@ Image::~Image() { } // static -Image Image::CreateFrom1xPNGEncodedData(const unsigned char* input, - size_t input_size) { - return Image(input, input_size); +Image Image::CreateFrom1xPNGBytes(const unsigned char* input, + size_t input_size) { + if (input_size == 0u) + return gfx::Image(); + + scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes()); + raw_data->data().assign(input, input + input_size); + std::vector<gfx::ImagePNGRep> image_reps; + image_reps.push_back(ImagePNGRep(raw_data, ui::SCALE_FACTOR_100P)); + return gfx::Image(image_reps); } const SkBitmap* Image::ToSkBitmap() const { @@ -652,7 +647,7 @@ NSImage* Image::ToNSImage() const { } #endif -scoped_refptr<base::RefCountedBytes> Image::As1xPNGBytes() const { +scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const { if (IsEmpty()) return new base::RefCountedBytes(); @@ -668,7 +663,7 @@ scoped_refptr<base::RefCountedBytes> Image::As1xPNGBytes() const { return new base::RefCountedBytes(); } - scoped_refptr<base::RefCountedBytes> png_bytes(NULL); + scoped_refptr<base::RefCountedMemory> png_bytes(NULL); switch (DefaultRepresentationType()) { #if defined(TOOLKIT_GTK) case kImageRepGdk: { @@ -738,8 +733,11 @@ NSImage* Image::AsNSImage() const { } #endif -scoped_refptr<base::RefCountedBytes> Image::Copy1xPNGBytes() const { - return new base::RefCountedBytes(As1xPNGBytes()->data()); +scoped_refptr<base::RefCountedMemory> Image::Copy1xPNGBytes() const { + scoped_refptr<base::RefCountedMemory> original = As1xPNGBytes(); + scoped_refptr<base::RefCountedBytes> copy(new base::RefCountedBytes()); + copy->data().assign(original->front(), original->front() + original->size()); + return copy; } ImageSkia* Image::CopyImageSkia() const { diff --git a/ui/gfx/image/image.h b/ui/gfx/image/image.h index 3bd53a9..b4dceb4 100644 --- a/ui/gfx/image/image.h +++ b/ui/gfx/image/image.h @@ -64,13 +64,6 @@ class UI_EXPORT Image { // Creates an empty image with no representations. Image(); - // Creates a new image by copying the PNG-encoded input for use as the default - // representation. For example (from an std::vector): - // std::vector<unsigned char> png = ...; - // gfx::Image image(&png.front(), png.size()); - // TODO(pkotwicz): Get rid of this constructor. - Image(const unsigned char* png, size_t input_size); - // Creates a new image by copying the raw PNG-encoded input for use as the // default representation. explicit Image(const std::vector<ImagePNGRep>& image_reps); @@ -111,9 +104,9 @@ class UI_EXPORT Image { // For example (from an std::vector): // std::vector<unsigned char> png = ...; // gfx::Image image = - // Image::CreateFrom1xPNGEncodedData(&png.front(), png.size()); - static Image CreateFrom1xPNGEncodedData(const unsigned char* input, - size_t input_size); + // Image::CreateFrom1xPNGBytes(&png.front(), png.size()); + static Image CreateFrom1xPNGBytes(const unsigned char* input, + size_t input_size); // Converts the Image to the desired representation and stores it internally. // The returned result is a weak pointer owned by and scoped to the life of @@ -133,7 +126,7 @@ class UI_EXPORT Image { // unavailable, either because the image has no data for 1x or because it is // empty, an empty RefCountedBytes object is returned. NULL is never // returned. - scoped_refptr<base::RefCountedBytes> As1xPNGBytes() const; + scoped_refptr<base::RefCountedMemory> As1xPNGBytes() const; // Same as ToSkBitmap(), but returns a null SkBitmap if this image is empty. SkBitmap AsBitmap() const; @@ -153,7 +146,7 @@ class UI_EXPORT Image { // backing pixels are shared amongst all copies (a fact of each of the // converted representations, rather than a limitation imposed by Image) and // so the result should be considered immutable. - scoped_refptr<base::RefCountedBytes> Copy1xPNGBytes() const; + scoped_refptr<base::RefCountedMemory> Copy1xPNGBytes() const; ImageSkia* CopyImageSkia() const; SkBitmap* CopySkBitmap() const; #if defined(TOOLKIT_GTK) diff --git a/ui/gfx/image/image_ios.mm b/ui/gfx/image/image_ios.mm index 0815d79..2f91893 100644 --- a/ui/gfx/image/image_ios.mm +++ b/ui/gfx/image/image_ios.mm @@ -49,7 +49,7 @@ UIImage* CreateErrorUIImage(float scale) { // Converts from ImagePNGRep to UIImage. UIImage* CreateUIImageFromImagePNGRep(const gfx::ImagePNGRep& image_png_rep) { float scale = ui::GetScaleFactorScale(image_png_rep.scale_factor); - scoped_refptr<base::RefCountedBytes> png = image_png_rep.raw_data; + scoped_refptr<base::RefCountedMemory> png = image_png_rep.raw_data; CHECK(png.get()); NSData* data = [NSData dataWithBytes:png->front() length:png->size()]; UIImage* image = [[UIImage alloc] initWithData:data scale:scale]; @@ -58,7 +58,7 @@ UIImage* CreateUIImageFromImagePNGRep(const gfx::ImagePNGRep& image_png_rep) { } // namespace -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromUIImage( +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( UIImage* uiimage) { NSData* data = UIImagePNGRepresentation(uiimage); @@ -95,7 +95,7 @@ UIImage* CreateUIImageFromPNG( return CreateUIImageFromImagePNGRep(image_png_reps[closest_index]); } -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromImageSkia( +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( const ImageSkia* skia) { // iOS does not expose libpng, so conversion from ImageSkia to PNG must go // through UIImage. diff --git a/ui/gfx/image/image_mac.mm b/ui/gfx/image/image_mac.mm index 197c549..bd4f8b3 100644 --- a/ui/gfx/image/image_mac.mm +++ b/ui/gfx/image/image_mac.mm @@ -30,7 +30,7 @@ NSImage* GetErrorNSImage() { } // namespace -scoped_refptr<base::RefCountedBytes> Get1xPNGBytesFromNSImage( +scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage( NSImage* nsimage) { CGImageRef cg_image = [nsimage CGImageForProposedRect:NULL context:nil @@ -55,7 +55,7 @@ NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps) { scoped_nsobject<NSImage> image; for (size_t i = 0; i < image_png_reps.size(); ++i) { - scoped_refptr<base::RefCountedBytes> png = image_png_reps[i].raw_data; + scoped_refptr<base::RefCountedMemory> png = image_png_reps[i].raw_data; CHECK(png.get()); scoped_nsobject<NSData> ns_data( [[NSData alloc] initWithBytes:png->front() length:png->size()]); diff --git a/ui/gfx/image/image_mac_unittest.mm b/ui/gfx/image/image_mac_unittest.mm index 862d329..4943c81 100644 --- a/ui/gfx/image/image_mac_unittest.mm +++ b/ui/gfx/image/image_mac_unittest.mm @@ -185,8 +185,8 @@ TEST_F(ImageMacTest, MultiResolutionPNGToNSImage) { const int kSize1x = 25; const int kSize2x = 50; - scoped_refptr<base::RefCountedBytes> bytes1x = gt::CreatePNGBytes(kSize1x); - scoped_refptr<base::RefCountedBytes> bytes2x = gt::CreatePNGBytes(kSize2x); + scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); + scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); std::vector<gfx::ImagePNGRep> image_png_reps; image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); diff --git a/ui/gfx/image/image_png_rep.cc b/ui/gfx/image/image_png_rep.cc index 19dba3a6..a0a26e4 100644 --- a/ui/gfx/image/image_png_rep.cc +++ b/ui/gfx/image/image_png_rep.cc @@ -11,7 +11,7 @@ ImagePNGRep::ImagePNGRep() scale_factor(ui::SCALE_FACTOR_NONE) { } -ImagePNGRep::ImagePNGRep(const scoped_refptr<base::RefCountedBytes>& data, +ImagePNGRep::ImagePNGRep(const scoped_refptr<base::RefCountedMemory>& data, ui::ScaleFactor data_scale_factor) : raw_data(data), scale_factor(data_scale_factor) { diff --git a/ui/gfx/image/image_png_rep.h b/ui/gfx/image/image_png_rep.h index 6d69eff..9306e73 100644 --- a/ui/gfx/image/image_png_rep.h +++ b/ui/gfx/image/image_png_rep.h @@ -15,11 +15,11 @@ namespace gfx { // was intended for. struct UI_EXPORT ImagePNGRep { ImagePNGRep(); - ImagePNGRep(const scoped_refptr<base::RefCountedBytes>& data, + ImagePNGRep(const scoped_refptr<base::RefCountedMemory>& data, ui::ScaleFactor data_scale_factor); ~ImagePNGRep(); - scoped_refptr<base::RefCountedBytes> raw_data; + scoped_refptr<base::RefCountedMemory> raw_data; ui::ScaleFactor scale_factor; }; diff --git a/ui/gfx/image/image_unittest.cc b/ui/gfx/image/image_unittest.cc index faf8361..0f495ce 100644 --- a/ui/gfx/image/image_unittest.cc +++ b/ui/gfx/image/image_unittest.cc @@ -146,7 +146,7 @@ TEST_F(ImageTest, SkiaToSkiaRef) { TEST_F(ImageTest, EmptyImageToPNG) { gfx::Image image; - scoped_refptr<base::RefCountedBytes> png_bytes = image.As1xPNGBytes(); + scoped_refptr<base::RefCountedMemory> png_bytes = image.As1xPNGBytes(); EXPECT_TRUE(png_bytes.get()); EXPECT_FALSE(png_bytes->size()); } @@ -160,7 +160,7 @@ TEST_F(ImageTest, ImageNo1xToPNG) { image_skia.AddRepresentation(gfx::ImageSkiaRep(gt::CreateBitmap( kSize2x, kSize2x), ui::SCALE_FACTOR_200P)); gfx::Image image1(image_skia); - scoped_refptr<base::RefCountedBytes> png_bytes1 = image1.As1xPNGBytes(); + scoped_refptr<base::RefCountedMemory> png_bytes1 = image1.As1xPNGBytes(); EXPECT_TRUE(png_bytes1.get()); EXPECT_FALSE(png_bytes1->size()); @@ -168,7 +168,7 @@ TEST_F(ImageTest, ImageNo1xToPNG) { image_png_reps.push_back(gfx::ImagePNGRep( gt::CreatePNGBytes(kSize2x), ui::SCALE_FACTOR_200P)); gfx::Image image2(image_png_reps); - scoped_refptr<base::RefCountedBytes> png_bytes2 = image2.As1xPNGBytes(); + scoped_refptr<base::RefCountedMemory> png_bytes2 = image2.As1xPNGBytes(); EXPECT_TRUE(png_bytes2.get()); EXPECT_FALSE(png_bytes2->size()); } @@ -179,7 +179,7 @@ TEST_F(ImageTest, CreateExtractPNGBytes) { const int kSize1x = 25; const int kSize2x = 50; - scoped_refptr<base::RefCountedBytes> bytes1x = gt::CreatePNGBytes(kSize1x); + scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); std::vector<gfx::ImagePNGRep> image_png_reps; image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); image_png_reps.push_back(gfx::ImagePNGRep( @@ -211,8 +211,8 @@ TEST_F(ImageTest, MultiResolutionPNGToImageSkia) { const int kSize1x = 25; const int kSize2x = 50; - scoped_refptr<base::RefCountedBytes> bytes1x = gt::CreatePNGBytes(kSize1x); - scoped_refptr<base::RefCountedBytes> bytes2x = gt::CreatePNGBytes(kSize2x); + scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); + scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); std::vector<gfx::ImagePNGRep> image_png_reps; image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); @@ -235,8 +235,8 @@ TEST_F(ImageTest, MultiResolutionPNGToPlatform) { const int kSize1x = 25; const int kSize2x = 50; - scoped_refptr<base::RefCountedBytes> bytes1x = gt::CreatePNGBytes(kSize1x); - scoped_refptr<base::RefCountedBytes> bytes2x = gt::CreatePNGBytes(kSize2x); + scoped_refptr<base::RefCountedMemory> bytes1x = gt::CreatePNGBytes(kSize1x); + scoped_refptr<base::RefCountedMemory> bytes2x = gt::CreatePNGBytes(kSize2x); std::vector<gfx::ImagePNGRep> image_png_reps; image_png_reps.push_back(gfx::ImagePNGRep(bytes1x, ui::SCALE_FACTOR_100P)); image_png_reps.push_back(gfx::ImagePNGRep(bytes2x, ui::SCALE_FACTOR_200P)); @@ -261,7 +261,7 @@ TEST_F(ImageTest, MultiResolutionPNGToPlatform) { TEST_F(ImageTest, PlatformToPNGEncodeAndDecode) { gfx::Image image(gt::CreatePlatformImage()); - scoped_refptr<base::RefCountedBytes> png_data = image.As1xPNGBytes(); + scoped_refptr<base::RefCountedMemory> png_data = image.As1xPNGBytes(); EXPECT_TRUE(png_data.get()); EXPECT_TRUE(png_data->size()); EXPECT_TRUE(image.HasRepresentation(gfx::Image::kImageRepPNG)); @@ -281,7 +281,7 @@ TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) { ui::ScaleFactor ideal_scale_factor = ui::GetScaleFactorFromScale(1.0f); gfx::Image from_skia(gt::CreateBitmap(25, 25)); - scoped_refptr<base::RefCountedBytes> png_bytes = + scoped_refptr<base::RefCountedMemory> png_bytes = from_skia.As1xPNGBytes(); std::vector<gfx::ImagePNGRep> image_png_reps; @@ -297,7 +297,8 @@ TEST_F(ImageTest, PNGEncodeFromSkiaDecodeToPlatform) { TEST_F(ImageTest, PNGEncodeFromPlatformDecodeToSkia) { // Force the conversion sequence platform_type to png to skia. gfx::Image from_platform(gt::CreatePlatformImage()); - scoped_refptr<base::RefCountedBytes> png_bytes = from_platform.As1xPNGBytes(); + scoped_refptr<base::RefCountedMemory> png_bytes = + from_platform.As1xPNGBytes(); std::vector<gfx::ImagePNGRep> image_png_reps; image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, ui::SCALE_FACTOR_100P)); gfx::Image from_png(image_png_reps); diff --git a/ui/gfx/image/image_unittest_util.cc b/ui/gfx/image/image_unittest_util.cc index 4ef1c0c..2441821 100644 --- a/ui/gfx/image/image_unittest_util.cc +++ b/ui/gfx/image/image_unittest_util.cc @@ -43,7 +43,7 @@ const SkBitmap CreateBitmap(int width, int height) { return bitmap; } -scoped_refptr<base::RefCountedBytes> CreatePNGBytes(int edge_size) { +scoped_refptr<base::RefCountedMemory> CreatePNGBytes(int edge_size) { SkBitmap bitmap = CreateBitmap(edge_size, edge_size); scoped_refptr<base::RefCountedBytes> bytes(new base::RefCountedBytes()); PNGCodec::EncodeBGRASkBitmap(bitmap, false, &bytes->data()); diff --git a/ui/gfx/image/image_unittest_util.h b/ui/gfx/image/image_unittest_util.h index 06fc823..756dbdd 100644 --- a/ui/gfx/image/image_unittest_util.h +++ b/ui/gfx/image/image_unittest_util.h @@ -30,7 +30,7 @@ void SetSupportedScaleFactorsTo1xAnd2x(); const SkBitmap CreateBitmap(int width, int height); // Returns PNG encoded bytes for a bitmap of |edge_size|x|edge_size|. -scoped_refptr<base::RefCountedBytes> CreatePNGBytes(int edge_size); +scoped_refptr<base::RefCountedMemory> CreatePNGBytes(int edge_size); // TODO(rohitrao): Remove the no-argument version of CreateImage(). gfx::Image CreateImage(); |