OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ui/gfx/image/image.h" | |
6 | |
7 #include <algorithm> | |
8 #include <set> | |
9 | |
10 #include "base/logging.h" | |
11 #include "base/memory/scoped_ptr.h" | |
12 #include "base/stl_util.h" | |
13 #include "third_party/skia/include/core/SkBitmap.h" | |
14 #include "ui/gfx/image/image_png_rep.h" | |
15 #include "ui/gfx/image/image_skia.h" | |
16 #include "ui/gfx/image/image_skia_source.h" | |
17 #include "ui/gfx/size.h" | |
18 | |
19 #if !defined(OS_IOS) | |
20 #include "ui/gfx/codec/png_codec.h" | |
21 #endif | |
22 | |
23 #if defined(OS_IOS) | |
24 #include "base/mac/foundation_util.h" | |
25 #include "ui/gfx/image/image_skia_util_ios.h" | |
26 #elif defined(OS_MACOSX) | |
27 #include "base/mac/mac_util.h" | |
28 #include "ui/gfx/image/image_skia_util_mac.h" | |
29 #endif | |
30 | |
31 namespace gfx { | |
32 | |
33 namespace internal { | |
34 | |
35 #if defined(OS_IOS) | |
36 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromUIImage( | |
37 UIImage* uiimage); | |
38 // Caller takes ownership of the returned UIImage. | |
39 UIImage* CreateUIImageFromPNG( | |
40 const std::vector<ImagePNGRep>& image_png_reps); | |
41 gfx::Size UIImageSize(UIImage* image); | |
42 #elif defined(OS_MACOSX) | |
43 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage( | |
44 NSImage* nsimage); | |
45 // Caller takes ownership of the returned NSImage. | |
46 NSImage* NSImageFromPNG(const std::vector<ImagePNGRep>& image_png_reps, | |
47 CGColorSpaceRef color_space); | |
48 gfx::Size NSImageSize(NSImage* image); | |
49 #endif // defined(OS_MACOSX) | |
50 | |
51 #if defined(OS_IOS) | |
52 ImageSkia* ImageSkiaFromPNG( | |
53 const std::vector<ImagePNGRep>& image_png_reps); | |
54 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | |
55 const ImageSkia* skia); | |
56 #else | |
57 // Returns a 16x16 red image to visually show error in decoding PNG. | |
58 // Caller takes ownership of returned ImageSkia. | |
59 ImageSkia* GetErrorImageSkia() { | |
60 SkBitmap bitmap; | |
61 bitmap.allocN32Pixels(16, 16); | |
62 bitmap.eraseARGB(0xff, 0xff, 0, 0); | |
63 return new ImageSkia(ImageSkiaRep(bitmap, 1.0f)); | |
64 } | |
65 | |
66 class PNGImageSource : public ImageSkiaSource { | |
67 public: | |
68 PNGImageSource() {} | |
69 ~PNGImageSource() override {} | |
70 | |
71 ImageSkiaRep GetImageForScale(float scale) override { | |
72 if (image_skia_reps_.empty()) | |
73 return ImageSkiaRep(); | |
74 | |
75 const ImageSkiaRep* rep = NULL; | |
76 // gfx::ImageSkia passes one of the resource scale factors. The source | |
77 // should return: | |
78 // 1) The ImageSkiaRep with the highest scale if all available | |
79 // scales are smaller than |scale|. | |
80 // 2) The ImageSkiaRep with the smallest one that is larger than |scale|. | |
81 for (ImageSkiaRepSet::const_iterator iter = image_skia_reps_.begin(); | |
82 iter != image_skia_reps_.end(); ++iter) { | |
83 if ((*iter).scale() == scale) | |
84 return (*iter); | |
85 if (!rep || rep->scale() < (*iter).scale()) | |
86 rep = &(*iter); | |
87 if (rep->scale() >= scale) | |
88 break; | |
89 } | |
90 return rep ? *rep : ImageSkiaRep(); | |
91 } | |
92 | |
93 const gfx::Size size() const { | |
94 return size_; | |
95 } | |
96 | |
97 bool AddPNGData(const ImagePNGRep& png_rep) { | |
98 const gfx::ImageSkiaRep rep = ToImageSkiaRep(png_rep); | |
99 if (rep.is_null()) | |
100 return false; | |
101 if (size_.IsEmpty()) | |
102 size_ = gfx::Size(rep.GetWidth(), rep.GetHeight()); | |
103 image_skia_reps_.insert(rep); | |
104 return true; | |
105 } | |
106 | |
107 static ImageSkiaRep ToImageSkiaRep(const ImagePNGRep& png_rep) { | |
108 scoped_refptr<base::RefCountedMemory> raw_data = png_rep.raw_data; | |
109 CHECK(raw_data.get()); | |
110 SkBitmap bitmap; | |
111 if (!PNGCodec::Decode(raw_data->front(), raw_data->size(), | |
112 &bitmap)) { | |
113 LOG(ERROR) << "Unable to decode PNG for " << png_rep.scale << "."; | |
114 return ImageSkiaRep(); | |
115 } | |
116 return ImageSkiaRep(bitmap, png_rep.scale); | |
117 } | |
118 | |
119 private: | |
120 struct Compare { | |
121 bool operator()(const ImageSkiaRep& rep1, const ImageSkiaRep& rep2) { | |
122 return rep1.scale() < rep2.scale(); | |
123 } | |
124 }; | |
125 | |
126 typedef std::set<ImageSkiaRep, Compare> ImageSkiaRepSet; | |
127 ImageSkiaRepSet image_skia_reps_; | |
128 gfx::Size size_; | |
129 | |
130 DISALLOW_COPY_AND_ASSIGN(PNGImageSource); | |
131 }; | |
132 | |
133 ImageSkia* ImageSkiaFromPNG( | |
134 const std::vector<ImagePNGRep>& image_png_reps) { | |
135 if (image_png_reps.empty()) | |
136 return GetErrorImageSkia(); | |
137 scoped_ptr<PNGImageSource> image_source(new PNGImageSource); | |
138 | |
139 for (size_t i = 0; i < image_png_reps.size(); ++i) { | |
140 if (!image_source->AddPNGData(image_png_reps[i])) | |
141 return GetErrorImageSkia(); | |
142 } | |
143 const gfx::Size& size = image_source->size(); | |
144 DCHECK(!size.IsEmpty()); | |
145 if (size.IsEmpty()) | |
146 return GetErrorImageSkia(); | |
147 return new ImageSkia(image_source.release(), size); | |
148 } | |
149 | |
150 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromImageSkia( | |
151 const ImageSkia* image_skia) { | |
152 ImageSkiaRep image_skia_rep = image_skia->GetRepresentation(1.0f); | |
153 | |
154 scoped_refptr<base::RefCountedBytes> png_bytes(new base::RefCountedBytes()); | |
155 if (image_skia_rep.scale() != 1.0f || | |
156 !PNGCodec::EncodeBGRASkBitmap(image_skia_rep.sk_bitmap(), false, | |
157 &png_bytes->data())) { | |
158 return NULL; | |
159 } | |
160 return png_bytes; | |
161 } | |
162 #endif | |
163 | |
164 class ImageRepPNG; | |
165 class ImageRepSkia; | |
166 class ImageRepCocoa; | |
167 class ImageRepCocoaTouch; | |
168 | |
169 // An ImageRep is the object that holds the backing memory for an Image. Each | |
170 // RepresentationType has an ImageRep subclass that is responsible for freeing | |
171 // the memory that the ImageRep holds. When an ImageRep is created, it expects | |
172 // to take ownership of the image, without having to retain it or increase its | |
173 // reference count. | |
174 class ImageRep { | |
175 public: | |
176 explicit ImageRep(Image::RepresentationType rep) : type_(rep) {} | |
177 | |
178 // Deletes the associated pixels of an ImageRep. | |
179 virtual ~ImageRep() {} | |
180 | |
181 // Cast helpers ("fake RTTI"). | |
182 ImageRepPNG* AsImageRepPNG() { | |
183 CHECK_EQ(type_, Image::kImageRepPNG); | |
184 return reinterpret_cast<ImageRepPNG*>(this); | |
185 } | |
186 | |
187 ImageRepSkia* AsImageRepSkia() { | |
188 CHECK_EQ(type_, Image::kImageRepSkia); | |
189 return reinterpret_cast<ImageRepSkia*>(this); | |
190 } | |
191 | |
192 #if defined(OS_IOS) | |
193 ImageRepCocoaTouch* AsImageRepCocoaTouch() { | |
194 CHECK_EQ(type_, Image::kImageRepCocoaTouch); | |
195 return reinterpret_cast<ImageRepCocoaTouch*>(this); | |
196 } | |
197 #elif defined(OS_MACOSX) | |
198 ImageRepCocoa* AsImageRepCocoa() { | |
199 CHECK_EQ(type_, Image::kImageRepCocoa); | |
200 return reinterpret_cast<ImageRepCocoa*>(this); | |
201 } | |
202 #endif | |
203 | |
204 Image::RepresentationType type() const { return type_; } | |
205 | |
206 virtual int Width() const = 0; | |
207 virtual int Height() const = 0; | |
208 virtual gfx::Size Size() const = 0; | |
209 | |
210 private: | |
211 Image::RepresentationType type_; | |
212 }; | |
213 | |
214 class ImageRepPNG : public ImageRep { | |
215 public: | |
216 ImageRepPNG() : ImageRep(Image::kImageRepPNG) { | |
217 } | |
218 | |
219 ImageRepPNG(const std::vector<ImagePNGRep>& image_png_reps) | |
220 : ImageRep(Image::kImageRepPNG), | |
221 image_png_reps_(image_png_reps) { | |
222 } | |
223 | |
224 ~ImageRepPNG() override {} | |
225 | |
226 int Width() const override { return Size().width(); } | |
227 | |
228 int Height() const override { return Size().height(); } | |
229 | |
230 gfx::Size Size() const override { | |
231 // Read the PNG data to get the image size, caching it. | |
232 if (!size_cache_) { | |
233 for (std::vector<ImagePNGRep>::const_iterator it = image_reps().begin(); | |
234 it != image_reps().end(); ++it) { | |
235 if (it->scale == 1.0f) { | |
236 size_cache_.reset(new gfx::Size(it->Size())); | |
237 return *size_cache_; | |
238 } | |
239 } | |
240 size_cache_.reset(new gfx::Size); | |
241 } | |
242 | |
243 return *size_cache_; | |
244 } | |
245 | |
246 const std::vector<ImagePNGRep>& image_reps() const { return image_png_reps_; } | |
247 | |
248 private: | |
249 std::vector<ImagePNGRep> image_png_reps_; | |
250 | |
251 // Cached to avoid having to parse the raw data multiple times. | |
252 mutable scoped_ptr<gfx::Size> size_cache_; | |
253 | |
254 DISALLOW_COPY_AND_ASSIGN(ImageRepPNG); | |
255 }; | |
256 | |
257 class ImageRepSkia : public ImageRep { | |
258 public: | |
259 // Takes ownership of |image|. | |
260 explicit ImageRepSkia(ImageSkia* image) | |
261 : ImageRep(Image::kImageRepSkia), | |
262 image_(image) { | |
263 } | |
264 | |
265 ~ImageRepSkia() override {} | |
266 | |
267 int Width() const override { return image_->width(); } | |
268 | |
269 int Height() const override { return image_->height(); } | |
270 | |
271 gfx::Size Size() const override { return image_->size(); } | |
272 | |
273 ImageSkia* image() { return image_.get(); } | |
274 | |
275 private: | |
276 scoped_ptr<ImageSkia> image_; | |
277 | |
278 DISALLOW_COPY_AND_ASSIGN(ImageRepSkia); | |
279 }; | |
280 | |
281 #if defined(OS_IOS) | |
282 class ImageRepCocoaTouch : public ImageRep { | |
283 public: | |
284 explicit ImageRepCocoaTouch(UIImage* image) | |
285 : ImageRep(Image::kImageRepCocoaTouch), | |
286 image_(image) { | |
287 CHECK(image); | |
288 } | |
289 | |
290 virtual ~ImageRepCocoaTouch() { | |
291 base::mac::NSObjectRelease(image_); | |
292 image_ = nil; | |
293 } | |
294 | |
295 virtual int Width() const override { | |
296 return Size().width(); | |
297 } | |
298 | |
299 virtual int Height() const override { | |
300 return Size().height(); | |
301 } | |
302 | |
303 virtual gfx::Size Size() const override { | |
304 return internal::UIImageSize(image_); | |
305 } | |
306 | |
307 UIImage* image() const { return image_; } | |
308 | |
309 private: | |
310 UIImage* image_; | |
311 | |
312 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoaTouch); | |
313 }; | |
314 #elif defined(OS_MACOSX) | |
315 class ImageRepCocoa : public ImageRep { | |
316 public: | |
317 explicit ImageRepCocoa(NSImage* image) | |
318 : ImageRep(Image::kImageRepCocoa), | |
319 image_(image) { | |
320 CHECK(image); | |
321 } | |
322 | |
323 ~ImageRepCocoa() override { | |
324 base::mac::NSObjectRelease(image_); | |
325 image_ = nil; | |
326 } | |
327 | |
328 int Width() const override { return Size().width(); } | |
329 | |
330 int Height() const override { return Size().height(); } | |
331 | |
332 gfx::Size Size() const override { return internal::NSImageSize(image_); } | |
333 | |
334 NSImage* image() const { return image_; } | |
335 | |
336 private: | |
337 NSImage* image_; | |
338 | |
339 DISALLOW_COPY_AND_ASSIGN(ImageRepCocoa); | |
340 }; | |
341 #endif // defined(OS_MACOSX) | |
342 | |
343 // The Storage class acts similarly to the pixels in a SkBitmap: the Image | |
344 // class holds a refptr instance of Storage, which in turn holds all the | |
345 // ImageReps. This way, the Image can be cheaply copied. | |
346 class ImageStorage : public base::RefCounted<ImageStorage> { | |
347 public: | |
348 ImageStorage(Image::RepresentationType default_type) | |
349 : default_representation_type_(default_type), | |
350 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
351 default_representation_color_space_( | |
352 base::mac::GetGenericRGBColorSpace()), | |
353 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
354 representations_deleter_(&representations_) { | |
355 } | |
356 | |
357 Image::RepresentationType default_representation_type() { | |
358 return default_representation_type_; | |
359 } | |
360 Image::RepresentationMap& representations() { return representations_; } | |
361 | |
362 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
363 void set_default_representation_color_space(CGColorSpaceRef color_space) { | |
364 default_representation_color_space_ = color_space; | |
365 } | |
366 CGColorSpaceRef default_representation_color_space() { | |
367 return default_representation_color_space_; | |
368 } | |
369 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
370 | |
371 private: | |
372 friend class base::RefCounted<ImageStorage>; | |
373 | |
374 ~ImageStorage() {} | |
375 | |
376 // The type of image that was passed to the constructor. This key will always | |
377 // exist in the |representations_| map. | |
378 Image::RepresentationType default_representation_type_; | |
379 | |
380 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
381 // The default representation's colorspace. This is used for converting to | |
382 // NSImage. This field exists to compensate for PNGCodec not writing or | |
383 // reading colorspace ancillary chunks. (sRGB, iCCP). | |
384 // Not owned. | |
385 CGColorSpaceRef default_representation_color_space_; | |
386 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
387 | |
388 // All the representations of an Image. Size will always be at least one, with | |
389 // more for any converted representations. | |
390 Image::RepresentationMap representations_; | |
391 | |
392 STLValueDeleter<Image::RepresentationMap> representations_deleter_; | |
393 | |
394 DISALLOW_COPY_AND_ASSIGN(ImageStorage); | |
395 }; | |
396 | |
397 } // namespace internal | |
398 | |
399 Image::Image() { | |
400 // |storage_| is NULL for empty Images. | |
401 } | |
402 | |
403 Image::Image(const std::vector<ImagePNGRep>& image_reps) { | |
404 // Do not store obviously invalid ImagePNGReps. | |
405 std::vector<ImagePNGRep> filtered; | |
406 for (size_t i = 0; i < image_reps.size(); ++i) { | |
407 if (image_reps[i].raw_data.get() && image_reps[i].raw_data->size()) | |
408 filtered.push_back(image_reps[i]); | |
409 } | |
410 | |
411 if (filtered.empty()) | |
412 return; | |
413 | |
414 storage_ = new internal::ImageStorage(Image::kImageRepPNG); | |
415 internal::ImageRepPNG* rep = new internal::ImageRepPNG(filtered); | |
416 AddRepresentation(rep); | |
417 } | |
418 | |
419 Image::Image(const ImageSkia& image) { | |
420 if (!image.isNull()) { | |
421 storage_ = new internal::ImageStorage(Image::kImageRepSkia); | |
422 internal::ImageRepSkia* rep = new internal::ImageRepSkia( | |
423 new ImageSkia(image)); | |
424 AddRepresentation(rep); | |
425 } | |
426 } | |
427 | |
428 #if defined(OS_IOS) | |
429 Image::Image(UIImage* image) | |
430 : storage_(new internal::ImageStorage(Image::kImageRepCocoaTouch)) { | |
431 if (image) { | |
432 internal::ImageRepCocoaTouch* rep = new internal::ImageRepCocoaTouch(image); | |
433 AddRepresentation(rep); | |
434 } | |
435 } | |
436 #elif defined(OS_MACOSX) | |
437 Image::Image(NSImage* image) { | |
438 if (image) { | |
439 storage_ = new internal::ImageStorage(Image::kImageRepCocoa); | |
440 internal::ImageRepCocoa* rep = new internal::ImageRepCocoa(image); | |
441 AddRepresentation(rep); | |
442 } | |
443 } | |
444 #endif | |
445 | |
446 Image::Image(const Image& other) : storage_(other.storage_) { | |
447 } | |
448 | |
449 Image& Image::operator=(const Image& other) { | |
450 storage_ = other.storage_; | |
451 return *this; | |
452 } | |
453 | |
454 Image::~Image() { | |
455 } | |
456 | |
457 // static | |
458 Image Image::CreateFrom1xBitmap(const SkBitmap& bitmap) { | |
459 return Image(ImageSkia::CreateFrom1xBitmap(bitmap)); | |
460 } | |
461 | |
462 // static | |
463 Image Image::CreateFrom1xPNGBytes(const unsigned char* input, | |
464 size_t input_size) { | |
465 if (input_size == 0u) | |
466 return Image(); | |
467 | |
468 scoped_refptr<base::RefCountedBytes> raw_data(new base::RefCountedBytes()); | |
469 raw_data->data().assign(input, input + input_size); | |
470 | |
471 return CreateFrom1xPNGBytes(raw_data); | |
472 } | |
473 | |
474 Image Image::CreateFrom1xPNGBytes( | |
475 const scoped_refptr<base::RefCountedMemory>& input) { | |
476 if (!input.get() || input->size() == 0u) | |
477 return Image(); | |
478 | |
479 std::vector<ImagePNGRep> image_reps; | |
480 image_reps.push_back(ImagePNGRep(input, 1.0f)); | |
481 return Image(image_reps); | |
482 } | |
483 | |
484 const SkBitmap* Image::ToSkBitmap() const { | |
485 // Possibly create and cache an intermediate ImageRepSkia. | |
486 return ToImageSkia()->bitmap(); | |
487 } | |
488 | |
489 const ImageSkia* Image::ToImageSkia() const { | |
490 internal::ImageRep* rep = GetRepresentation(kImageRepSkia, false); | |
491 if (!rep) { | |
492 switch (DefaultRepresentationType()) { | |
493 case kImageRepPNG: { | |
494 internal::ImageRepPNG* png_rep = | |
495 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG(); | |
496 rep = new internal::ImageRepSkia( | |
497 internal::ImageSkiaFromPNG(png_rep->image_reps())); | |
498 break; | |
499 } | |
500 #if defined(OS_IOS) | |
501 case kImageRepCocoaTouch: { | |
502 internal::ImageRepCocoaTouch* native_rep = | |
503 GetRepresentation(kImageRepCocoaTouch, true) | |
504 ->AsImageRepCocoaTouch(); | |
505 rep = new internal::ImageRepSkia(new ImageSkia( | |
506 ImageSkiaFromUIImage(native_rep->image()))); | |
507 break; | |
508 } | |
509 #elif defined(OS_MACOSX) | |
510 case kImageRepCocoa: { | |
511 internal::ImageRepCocoa* native_rep = | |
512 GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa(); | |
513 rep = new internal::ImageRepSkia(new ImageSkia( | |
514 ImageSkiaFromNSImage(native_rep->image()))); | |
515 break; | |
516 } | |
517 #endif | |
518 default: | |
519 NOTREACHED(); | |
520 } | |
521 CHECK(rep); | |
522 AddRepresentation(rep); | |
523 } | |
524 return rep->AsImageRepSkia()->image(); | |
525 } | |
526 | |
527 #if defined(OS_IOS) | |
528 UIImage* Image::ToUIImage() const { | |
529 internal::ImageRep* rep = GetRepresentation(kImageRepCocoaTouch, false); | |
530 if (!rep) { | |
531 switch (DefaultRepresentationType()) { | |
532 case kImageRepPNG: { | |
533 internal::ImageRepPNG* png_rep = | |
534 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG(); | |
535 rep = new internal::ImageRepCocoaTouch(internal::CreateUIImageFromPNG( | |
536 png_rep->image_reps())); | |
537 break; | |
538 } | |
539 case kImageRepSkia: { | |
540 internal::ImageRepSkia* skia_rep = | |
541 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); | |
542 UIImage* image = UIImageFromImageSkia(*skia_rep->image()); | |
543 base::mac::NSObjectRetain(image); | |
544 rep = new internal::ImageRepCocoaTouch(image); | |
545 break; | |
546 } | |
547 default: | |
548 NOTREACHED(); | |
549 } | |
550 CHECK(rep); | |
551 AddRepresentation(rep); | |
552 } | |
553 return rep->AsImageRepCocoaTouch()->image(); | |
554 } | |
555 #elif defined(OS_MACOSX) | |
556 NSImage* Image::ToNSImage() const { | |
557 internal::ImageRep* rep = GetRepresentation(kImageRepCocoa, false); | |
558 if (!rep) { | |
559 CGColorSpaceRef default_representation_color_space = | |
560 storage_->default_representation_color_space(); | |
561 | |
562 switch (DefaultRepresentationType()) { | |
563 case kImageRepPNG: { | |
564 internal::ImageRepPNG* png_rep = | |
565 GetRepresentation(kImageRepPNG, true)->AsImageRepPNG(); | |
566 rep = new internal::ImageRepCocoa(internal::NSImageFromPNG( | |
567 png_rep->image_reps(), default_representation_color_space)); | |
568 break; | |
569 } | |
570 case kImageRepSkia: { | |
571 internal::ImageRepSkia* skia_rep = | |
572 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); | |
573 NSImage* image = NSImageFromImageSkiaWithColorSpace(*skia_rep->image(), | |
574 default_representation_color_space); | |
575 base::mac::NSObjectRetain(image); | |
576 rep = new internal::ImageRepCocoa(image); | |
577 break; | |
578 } | |
579 default: | |
580 NOTREACHED(); | |
581 } | |
582 CHECK(rep); | |
583 AddRepresentation(rep); | |
584 } | |
585 return rep->AsImageRepCocoa()->image(); | |
586 } | |
587 #endif | |
588 | |
589 scoped_refptr<base::RefCountedMemory> Image::As1xPNGBytes() const { | |
590 if (IsEmpty()) | |
591 return new base::RefCountedBytes(); | |
592 | |
593 internal::ImageRep* rep = GetRepresentation(kImageRepPNG, false); | |
594 | |
595 if (rep) { | |
596 const std::vector<ImagePNGRep>& image_png_reps = | |
597 rep->AsImageRepPNG()->image_reps(); | |
598 for (size_t i = 0; i < image_png_reps.size(); ++i) { | |
599 if (image_png_reps[i].scale == 1.0f) | |
600 return image_png_reps[i].raw_data; | |
601 } | |
602 return new base::RefCountedBytes(); | |
603 } | |
604 | |
605 scoped_refptr<base::RefCountedMemory> png_bytes(NULL); | |
606 switch (DefaultRepresentationType()) { | |
607 #if defined(OS_IOS) | |
608 case kImageRepCocoaTouch: { | |
609 internal::ImageRepCocoaTouch* cocoa_touch_rep = | |
610 GetRepresentation(kImageRepCocoaTouch, true) | |
611 ->AsImageRepCocoaTouch(); | |
612 png_bytes = internal::Get1xPNGBytesFromUIImage( | |
613 cocoa_touch_rep->image()); | |
614 break; | |
615 } | |
616 #elif defined(OS_MACOSX) | |
617 case kImageRepCocoa: { | |
618 internal::ImageRepCocoa* cocoa_rep = | |
619 GetRepresentation(kImageRepCocoa, true)->AsImageRepCocoa(); | |
620 png_bytes = internal::Get1xPNGBytesFromNSImage(cocoa_rep->image()); | |
621 break; | |
622 } | |
623 #endif | |
624 case kImageRepSkia: { | |
625 internal::ImageRepSkia* skia_rep = | |
626 GetRepresentation(kImageRepSkia, true)->AsImageRepSkia(); | |
627 png_bytes = internal::Get1xPNGBytesFromImageSkia(skia_rep->image()); | |
628 break; | |
629 } | |
630 default: | |
631 NOTREACHED(); | |
632 } | |
633 if (!png_bytes.get() || !png_bytes->size()) { | |
634 // Add an ImageRepPNG with no data such that the conversion is not | |
635 // attempted each time we want the PNG bytes. | |
636 AddRepresentation(new internal::ImageRepPNG()); | |
637 return new base::RefCountedBytes(); | |
638 } | |
639 | |
640 // Do not insert representations for scale factors other than 1x even if | |
641 // they are available because: | |
642 // - Only the 1x PNG bytes can be accessed. | |
643 // - ImageRepPNG is not used as an intermediate type in converting to a | |
644 // final type eg (converting from ImageRepSkia to ImageRepPNG to get an | |
645 // ImageRepCocoa). | |
646 std::vector<ImagePNGRep> image_png_reps; | |
647 image_png_reps.push_back(ImagePNGRep(png_bytes, 1.0f)); | |
648 rep = new internal::ImageRepPNG(image_png_reps); | |
649 AddRepresentation(rep); | |
650 return png_bytes; | |
651 } | |
652 | |
653 SkBitmap Image::AsBitmap() const { | |
654 return IsEmpty() ? SkBitmap() : *ToSkBitmap(); | |
655 } | |
656 | |
657 ImageSkia Image::AsImageSkia() const { | |
658 return IsEmpty() ? ImageSkia() : *ToImageSkia(); | |
659 } | |
660 | |
661 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
662 NSImage* Image::AsNSImage() const { | |
663 return IsEmpty() ? nil : ToNSImage(); | |
664 } | |
665 #endif | |
666 | |
667 scoped_refptr<base::RefCountedMemory> Image::Copy1xPNGBytes() const { | |
668 scoped_refptr<base::RefCountedMemory> original = As1xPNGBytes(); | |
669 scoped_refptr<base::RefCountedBytes> copy(new base::RefCountedBytes()); | |
670 copy->data().assign(original->front(), original->front() + original->size()); | |
671 return copy; | |
672 } | |
673 | |
674 ImageSkia* Image::CopyImageSkia() const { | |
675 return new ImageSkia(*ToImageSkia()); | |
676 } | |
677 | |
678 SkBitmap* Image::CopySkBitmap() const { | |
679 return new SkBitmap(*ToSkBitmap()); | |
680 } | |
681 | |
682 #if defined(OS_IOS) | |
683 UIImage* Image::CopyUIImage() const { | |
684 UIImage* image = ToUIImage(); | |
685 base::mac::NSObjectRetain(image); | |
686 return image; | |
687 } | |
688 #elif defined(OS_MACOSX) | |
689 NSImage* Image::CopyNSImage() const { | |
690 NSImage* image = ToNSImage(); | |
691 base::mac::NSObjectRetain(image); | |
692 return image; | |
693 } | |
694 #endif | |
695 | |
696 bool Image::HasRepresentation(RepresentationType type) const { | |
697 return storage_.get() && storage_->representations().count(type) != 0; | |
698 } | |
699 | |
700 size_t Image::RepresentationCount() const { | |
701 if (!storage_.get()) | |
702 return 0; | |
703 | |
704 return storage_->representations().size(); | |
705 } | |
706 | |
707 bool Image::IsEmpty() const { | |
708 return RepresentationCount() == 0; | |
709 } | |
710 | |
711 int Image::Width() const { | |
712 if (IsEmpty()) | |
713 return 0; | |
714 return GetRepresentation(DefaultRepresentationType(), true)->Width(); | |
715 } | |
716 | |
717 int Image::Height() const { | |
718 if (IsEmpty()) | |
719 return 0; | |
720 return GetRepresentation(DefaultRepresentationType(), true)->Height(); | |
721 } | |
722 | |
723 gfx::Size Image::Size() const { | |
724 if (IsEmpty()) | |
725 return gfx::Size(); | |
726 return GetRepresentation(DefaultRepresentationType(), true)->Size(); | |
727 } | |
728 | |
729 void Image::SwapRepresentations(Image* other) { | |
730 storage_.swap(other->storage_); | |
731 } | |
732 | |
733 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
734 void Image::SetSourceColorSpace(CGColorSpaceRef color_space) { | |
735 if (storage_.get()) | |
736 storage_->set_default_representation_color_space(color_space); | |
737 } | |
738 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
739 | |
740 Image::RepresentationType Image::DefaultRepresentationType() const { | |
741 CHECK(storage_.get()); | |
742 return storage_->default_representation_type(); | |
743 } | |
744 | |
745 internal::ImageRep* Image::GetRepresentation( | |
746 RepresentationType rep_type, bool must_exist) const { | |
747 CHECK(storage_.get()); | |
748 RepresentationMap::iterator it = storage_->representations().find(rep_type); | |
749 if (it == storage_->representations().end()) { | |
750 CHECK(!must_exist); | |
751 return NULL; | |
752 } | |
753 return it->second; | |
754 } | |
755 | |
756 void Image::AddRepresentation(internal::ImageRep* rep) const { | |
757 CHECK(storage_.get()); | |
758 storage_->representations().insert(std::make_pair(rep->type(), rep)); | |
759 } | |
760 | |
761 } // namespace gfx | |
OLD | NEW |