OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/image/image.h" | 5 #include "ui/gfx/image/image.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
(...skipping 25 matching lines...) Expand all Loading... |
36 | 36 |
37 namespace gfx { | 37 namespace gfx { |
38 | 38 |
39 namespace internal { | 39 namespace internal { |
40 | 40 |
41 #if defined(TOOLKIT_GTK) | 41 #if defined(TOOLKIT_GTK) |
42 const ImageSkia ImageSkiaFromGdkPixbuf(GdkPixbuf* pixbuf) { | 42 const ImageSkia ImageSkiaFromGdkPixbuf(GdkPixbuf* pixbuf) { |
43 CHECK(pixbuf); | 43 CHECK(pixbuf); |
44 gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf), | 44 gfx::Canvas canvas(gfx::Size(gdk_pixbuf_get_width(pixbuf), |
45 gdk_pixbuf_get_height(pixbuf)), | 45 gdk_pixbuf_get_height(pixbuf)), |
46 ui::SCALE_FACTOR_100P, | 46 1.0f, |
47 false); | 47 false); |
48 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); | 48 skia::ScopedPlatformPaint scoped_platform_paint(canvas.sk_canvas()); |
49 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); | 49 cairo_t* cr = scoped_platform_paint.GetPlatformSurface(); |
50 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); | 50 gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0); |
51 cairo_paint(cr); | 51 cairo_paint(cr); |
52 return ImageSkia(canvas.ExtractImageRep()); | 52 return ImageSkia(canvas.ExtractImageRep()); |
53 } | 53 } |
54 | 54 |
55 // Returns a 16x16 red pixbuf to visually show error in decoding PNG. | 55 // Returns a 16x16 red pixbuf to visually show error in decoding PNG. |
56 // Also logs error to console. | 56 // Also logs error to console. |
57 GdkPixbuf* GetErrorPixbuf() { | 57 GdkPixbuf* GetErrorPixbuf() { |
58 LOG(ERROR) << "Unable to decode PNG."; | 58 LOG(ERROR) << "Unable to decode PNG."; |
59 GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 16, 16); | 59 GdkPixbuf* pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, 16, 16); |
60 gdk_pixbuf_fill(pixbuf, 0xff0000ff); | 60 gdk_pixbuf_fill(pixbuf, 0xff0000ff); |
61 return pixbuf; | 61 return pixbuf; |
62 } | 62 } |
63 | 63 |
64 GdkPixbuf* GdkPixbufFromPNG( | 64 GdkPixbuf* GdkPixbufFromPNG( |
65 const std::vector<gfx::ImagePNGRep>& image_png_reps) { | 65 const std::vector<gfx::ImagePNGRep>& image_png_reps) { |
66 scoped_refptr<base::RefCountedMemory> png_bytes(NULL); | 66 scoped_refptr<base::RefCountedMemory> png_bytes(NULL); |
67 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 67 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
68 if (image_png_reps[i].scale_factor == ui::SCALE_FACTOR_100P) | 68 if (image_png_reps[i].scale == 1.0f) |
69 png_bytes = image_png_reps[i].raw_data; | 69 png_bytes = image_png_reps[i].raw_data; |
70 } | 70 } |
71 | 71 |
72 if (!png_bytes.get()) | 72 if (!png_bytes.get()) |
73 return GetErrorPixbuf(); | 73 return GetErrorPixbuf(); |
74 | 74 |
75 GdkPixbuf* pixbuf = NULL; | 75 GdkPixbuf* pixbuf = NULL; |
76 ui::ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); | 76 ui::ScopedGObject<GdkPixbufLoader>::Type loader(gdk_pixbuf_loader_new()); |
77 | 77 |
78 bool ok = gdk_pixbuf_loader_write(loader.get(), | 78 bool ok = gdk_pixbuf_loader_write(loader.get(), |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | 135 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( |
136 const ImageSkia* skia); | 136 const ImageSkia* skia); |
137 #else | 137 #else |
138 // Returns a 16x16 red image to visually show error in decoding PNG. | 138 // Returns a 16x16 red image to visually show error in decoding PNG. |
139 // Caller takes ownership of returned ImageSkia. | 139 // Caller takes ownership of returned ImageSkia. |
140 ImageSkia* GetErrorImageSkia() { | 140 ImageSkia* GetErrorImageSkia() { |
141 SkBitmap bitmap; | 141 SkBitmap bitmap; |
142 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); | 142 bitmap.setConfig(SkBitmap::kARGB_8888_Config, 16, 16); |
143 bitmap.allocPixels(); | 143 bitmap.allocPixels(); |
144 bitmap.eraseRGB(0xff, 0, 0); | 144 bitmap.eraseRGB(0xff, 0, 0); |
145 return new gfx::ImageSkia(gfx::ImageSkiaRep(bitmap, ui::SCALE_FACTOR_100P)); | 145 return new gfx::ImageSkia(gfx::ImageSkiaRep(bitmap, 1.0f)); |
146 } | 146 } |
147 | 147 |
148 ImageSkia* ImageSkiaFromPNG( | 148 ImageSkia* ImageSkiaFromPNG( |
149 const std::vector<gfx::ImagePNGRep>& image_png_reps) { | 149 const std::vector<gfx::ImagePNGRep>& image_png_reps) { |
150 if (image_png_reps.empty()) | 150 if (image_png_reps.empty()) |
151 return GetErrorImageSkia(); | 151 return GetErrorImageSkia(); |
152 | 152 |
153 scoped_ptr<gfx::ImageSkia> image_skia(new ImageSkia()); | 153 scoped_ptr<gfx::ImageSkia> image_skia(new ImageSkia()); |
154 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 154 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
155 scoped_refptr<base::RefCountedMemory> raw_data = | 155 scoped_refptr<base::RefCountedMemory> raw_data = |
156 image_png_reps[i].raw_data; | 156 image_png_reps[i].raw_data; |
157 CHECK(raw_data.get()); | 157 CHECK(raw_data.get()); |
158 SkBitmap bitmap; | 158 SkBitmap bitmap; |
159 if (!gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), | 159 if (!gfx::PNGCodec::Decode(raw_data->front(), raw_data->size(), |
160 &bitmap)) { | 160 &bitmap)) { |
161 LOG(ERROR) << "Unable to decode PNG for " | 161 LOG(ERROR) << "Unable to decode PNG for " |
162 << ui::GetScaleFactorScale(image_png_reps[i].scale_factor) | 162 << image_png_reps[i].scale |
163 << "."; | 163 << "."; |
164 return GetErrorImageSkia(); | 164 return GetErrorImageSkia(); |
165 } | 165 } |
166 image_skia->AddRepresentation(gfx::ImageSkiaRep( | 166 image_skia->AddRepresentation(gfx::ImageSkiaRep( |
167 bitmap, image_png_reps[i].scale_factor)); | 167 bitmap, image_png_reps[i].scale)); |
168 } | 168 } |
169 return image_skia.release(); | 169 return image_skia.release(); |
170 } | 170 } |
171 | 171 |
172 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | 172 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( |
173 const ImageSkia* image_skia) { | 173 const ImageSkia* image_skia) { |
174 ImageSkiaRep image_skia_rep = image_skia->GetRepresentation( | 174 ImageSkiaRep image_skia_rep = image_skia->GetRepresentation(1.0f); |
175 ui::SCALE_FACTOR_100P); | |
176 | 175 |
177 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); | 176 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); |
178 if (image_skia_rep.scale_factor() != ui::SCALE_FACTOR_100P || | 177 if (image_skia_rep.scale() != 1.0f || |
179 !gfx::PNGCodec::EncodeBGRASkBitmap(image_skia_rep.sk_bitmap(), false, | 178 !gfx::PNGCodec::EncodeBGRASkBitmap(image_skia_rep.sk_bitmap(), false, |
180 &png_bytes->data())) { | 179 &png_bytes->data())) { |
181 return NULL; | 180 return NULL; |
182 } | 181 } |
183 return png_bytes; | 182 return png_bytes; |
184 } | 183 } |
185 #endif | 184 #endif |
186 | 185 |
187 class ImageRepPNG; | 186 class ImageRepPNG; |
188 class ImageRepSkia; | 187 class ImageRepSkia; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
267 | 266 |
268 virtual int Height() const OVERRIDE { | 267 virtual int Height() const OVERRIDE { |
269 return Size().height(); | 268 return Size().height(); |
270 } | 269 } |
271 | 270 |
272 virtual gfx::Size Size() const OVERRIDE { | 271 virtual gfx::Size Size() const OVERRIDE { |
273 // Read the PNG data to get the image size, caching it. | 272 // Read the PNG data to get the image size, caching it. |
274 if (!size_cache_) { | 273 if (!size_cache_) { |
275 for (std::vector<ImagePNGRep>::const_iterator it = image_reps().begin(); | 274 for (std::vector<ImagePNGRep>::const_iterator it = image_reps().begin(); |
276 it != image_reps().end(); ++it) { | 275 it != image_reps().end(); ++it) { |
277 if (it->scale_factor == ui::SCALE_FACTOR_100P) { | 276 if (it->scale == 1.0f) { |
278 size_cache_.reset(new gfx::Size(it->Size())); | 277 size_cache_.reset(new gfx::Size(it->Size())); |
279 return *size_cache_; | 278 return *size_cache_; |
280 } | 279 } |
281 } | 280 } |
282 size_cache_.reset(new gfx::Size); | 281 size_cache_.reset(new gfx::Size); |
283 } | 282 } |
284 | 283 |
285 return *size_cache_; | 284 return *size_cache_; |
286 } | 285 } |
287 | 286 |
(...skipping 309 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
597 | 596 |
598 // static | 597 // static |
599 Image Image::CreateFrom1xPNGBytes(const unsigned char* input, | 598 Image Image::CreateFrom1xPNGBytes(const unsigned char* input, |
600 size_t input_size) { | 599 size_t input_size) { |
601 if (input_size == 0u) | 600 if (input_size == 0u) |
602 return gfx::Image(); | 601 return gfx::Image(); |
603 | 602 |
604 scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes()); | 603 scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes()); |
605 raw_data->data().assign(input, input + input_size); | 604 raw_data->data().assign(input, input + input_size); |
606 std::vector<gfx::ImagePNGRep> image_reps; | 605 std::vector<gfx::ImagePNGRep> image_reps; |
607 image_reps.push_back(ImagePNGRep(raw_data, ui::SCALE_FACTOR_100P)); | 606 image_reps.push_back(ImagePNGRep(raw_data, 1.0f)); |
608 return gfx::Image(image_reps); | 607 return gfx::Image(image_reps); |
609 } | 608 } |
610 | 609 |
611 const SkBitmap* Image::ToSkBitmap() const { | 610 const SkBitmap* Image::ToSkBitmap() const { |
612 // Possibly create and cache an intermediate ImageRepSkia. | 611 // Possibly create and cache an intermediate ImageRepSkia. |
613 return ToImageSkia()->bitmap(); | 612 return ToImageSkia()->bitmap(); |
614 } | 613 } |
615 | 614 |
616 const ImageSkia* Image::ToImageSkia() const { | 615 const ImageSkia* Image::ToImageSkia() const { |
617 internal::ImageRep* rep = GetRepresentation(kImageRepSkia, false); | 616 internal::ImageRep* rep = GetRepresentation(kImageRepSkia, false); |
(...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
765 scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const { | 764 scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const { |
766 if (IsEmpty()) | 765 if (IsEmpty()) |
767 return new base::RefCountedBytes(); | 766 return new base::RefCountedBytes(); |
768 | 767 |
769 internal::ImageRep* rep = GetRepresentation(kImageRepPNG, false); | 768 internal::ImageRep* rep = GetRepresentation(kImageRepPNG, false); |
770 | 769 |
771 if (rep) { | 770 if (rep) { |
772 const std::vector<gfx::ImagePNGRep>& image_png_reps = | 771 const std::vector<gfx::ImagePNGRep>& image_png_reps = |
773 rep->AsImageRepPNG()->image_reps(); | 772 rep->AsImageRepPNG()->image_reps(); |
774 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 773 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
775 if (image_png_reps[i].scale_factor == ui::SCALE_FACTOR_100P) | 774 if (image_png_reps[i].scale == 1.0f) |
776 return image_png_reps[i].raw_data; | 775 return image_png_reps[i].raw_data; |
777 } | 776 } |
778 return new base::RefCountedBytes(); | 777 return new base::RefCountedBytes(); |
779 } | 778 } |
780 | 779 |
781 scoped_refptr<base::RefCountedMemory> png_bytes(NULL); | 780 scoped_refptr<base::RefCountedMemory> png_bytes(NULL); |
782 switch (DefaultRepresentationType()) { | 781 switch (DefaultRepresentationType()) { |
783 #if defined(TOOLKIT_GTK) | 782 #if defined(TOOLKIT_GTK) |
784 case kImageRepGdk: { | 783 case kImageRepGdk: { |
785 internal::ImageRepGdk* gdk_rep = | 784 internal::ImageRepGdk* gdk_rep = |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
820 return new base::RefCountedBytes(); | 819 return new base::RefCountedBytes(); |
821 } | 820 } |
822 | 821 |
823 // Do not insert representations for scale factors other than 1x even if | 822 // Do not insert representations for scale factors other than 1x even if |
824 // they are available because: | 823 // they are available because: |
825 // - Only the 1x PNG bytes can be accessed. | 824 // - Only the 1x PNG bytes can be accessed. |
826 // - ImageRepPNG is not used as an intermediate type in converting to a | 825 // - ImageRepPNG is not used as an intermediate type in converting to a |
827 // final type eg (converting from ImageRepSkia to ImageRepPNG to get an | 826 // final type eg (converting from ImageRepSkia to ImageRepPNG to get an |
828 // ImageRepCocoa). | 827 // ImageRepCocoa). |
829 std::vector<ImagePNGRep> image_png_reps; | 828 std::vector<ImagePNGRep> image_png_reps; |
830 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, | 829 image_png_reps.push_back(gfx::ImagePNGRep(png_bytes, 1.0f)); |
831 ui::SCALE_FACTOR_100P)); | |
832 rep = new internal::ImageRepPNG(image_png_reps); | 830 rep = new internal::ImageRepPNG(image_png_reps); |
833 AddRepresentation(rep); | 831 AddRepresentation(rep); |
834 return png_bytes; | 832 return png_bytes; |
835 } | 833 } |
836 | 834 |
837 SkBitmap Image::AsBitmap() const { | 835 SkBitmap Image::AsBitmap() const { |
838 return IsEmpty() ? SkBitmap() : *ToSkBitmap(); | 836 return IsEmpty() ? SkBitmap() : *ToSkBitmap(); |
839 } | 837 } |
840 | 838 |
841 ImageSkia Image::AsImageSkia() const { | 839 ImageSkia Image::AsImageSkia() const { |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
948 } | 946 } |
949 return it->second; | 947 return it->second; |
950 } | 948 } |
951 | 949 |
952 void Image::AddRepresentation(internal::ImageRep* rep) const { | 950 void Image::AddRepresentation(internal::ImageRep* rep) const { |
953 CHECK(storage_.get()); | 951 CHECK(storage_.get()); |
954 storage_->representations().insert(std::make_pair(rep->type(), rep)); | 952 storage_->representations().insert(std::make_pair(rep->type(), rep)); |
955 } | 953 } |
956 | 954 |
957 } // namespace gfx | 955 } // namespace gfx |
OLD | NEW |