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 #import <AppKit/AppKit.h> | 7 #import <AppKit/AppKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_nsobject.h" | 10 #include "base/memory/scoped_nsobject.h" |
(...skipping 30 matching lines...) Expand all Loading... |
41 NSData* ns_data = [ns_bitmap representationUsingType:NSPNGFileType | 41 NSData* ns_data = [ns_bitmap representationUsingType:NSPNGFileType |
42 properties:nil]; | 42 properties:nil]; |
43 const unsigned char* bytes = | 43 const unsigned char* bytes = |
44 static_cast<const unsigned char*>([ns_data bytes]); | 44 static_cast<const unsigned char*>([ns_data bytes]); |
45 scoped_refptr<base::RefCountedBytes> refcounted_bytes( | 45 scoped_refptr<base::RefCountedBytes> refcounted_bytes( |
46 new base::RefCountedBytes()); | 46 new base::RefCountedBytes()); |
47 refcounted_bytes->data().assign(bytes, bytes + [ns_data length]); | 47 refcounted_bytes->data().assign(bytes, bytes + [ns_data length]); |
48 return refcounted_bytes; | 48 return refcounted_bytes; |
49 } | 49 } |
50 | 50 |
51 NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps) { | 51 NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps, |
| 52 CGColorSpaceRef color_space) { |
52 if (image_png_reps.empty()) { | 53 if (image_png_reps.empty()) { |
53 LOG(ERROR) << "Unable to decode PNG."; | 54 LOG(ERROR) << "Unable to decode PNG."; |
54 return GetErrorNSImage(); | 55 return GetErrorNSImage(); |
55 } | 56 } |
56 | 57 |
57 scoped_nsobject<NSImage> image; | 58 scoped_nsobject<NSImage> image; |
58 for (size_t i = 0; i < image_png_reps.size(); ++i) { | 59 for (size_t i = 0; i < image_png_reps.size(); ++i) { |
59 scoped_refptr<base::RefCountedMemory> png = image_png_reps[i].raw_data; | 60 scoped_refptr<base::RefCountedMemory> png = image_png_reps[i].raw_data; |
60 CHECK(png.get()); | 61 CHECK(png.get()); |
61 scoped_nsobject<NSData> ns_data( | 62 scoped_nsobject<NSData> ns_data( |
62 [[NSData alloc] initWithBytes:png->front() length:png->size()]); | 63 [[NSData alloc] initWithBytes:png->front() length:png->size()]); |
63 scoped_nsobject<NSBitmapImageRep> ns_image_rep( | 64 scoped_nsobject<NSBitmapImageRep> ns_image_rep( |
64 [[NSBitmapImageRep alloc] initWithData:ns_data]); | 65 [[NSBitmapImageRep alloc] initWithData:ns_data]); |
65 if (!ns_image_rep) { | 66 if (!ns_image_rep) { |
66 LOG(ERROR) << "Unable to decode PNG at " | 67 LOG(ERROR) << "Unable to decode PNG at " |
67 << ui::GetScaleFactorScale(image_png_reps[i].scale_factor) | 68 << ui::GetScaleFactorScale(image_png_reps[i].scale_factor) |
68 << "."; | 69 << "."; |
69 return GetErrorNSImage(); | 70 return GetErrorNSImage(); |
70 } | 71 } |
71 | 72 |
| 73 // PNGCodec ignores colorspace related ancillary chunks (sRGB, iCCP). Ignore |
| 74 // colorspace information when decoding directly from PNG to an NSImage so |
| 75 // that the conversions: PNG -> SkBitmap -> NSImage and PNG -> NSImage |
| 76 // produce visually similar results. |
| 77 CGColorSpaceModel decoded_color_space_model = CGColorSpaceGetModel( |
| 78 [[ns_image_rep colorSpace] CGColorSpace]); |
| 79 CGColorSpaceModel color_space_model = CGColorSpaceGetModel(color_space); |
| 80 if (decoded_color_space_model == color_space_model) { |
| 81 scoped_nsobject<NSColorSpace> ns_color_space( |
| 82 [[NSColorSpace alloc] initWithCGColorSpace:color_space]); |
| 83 NSBitmapImageRep* ns_retagged_image_rep = |
| 84 [ns_image_rep |
| 85 bitmapImageRepByRetaggingWithColorSpace:ns_color_space]; |
| 86 if (ns_retagged_image_rep && ns_retagged_image_rep != ns_image_rep) |
| 87 ns_image_rep.reset([ns_retagged_image_rep retain]); |
| 88 } |
| 89 |
72 if (!image.get()) { | 90 if (!image.get()) { |
73 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor); | 91 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor); |
74 NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale, | 92 NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale, |
75 [ns_image_rep pixelsHigh] / scale); | 93 [ns_image_rep pixelsHigh] / scale); |
76 image.reset([[NSImage alloc] initWithSize:image_size]); | 94 image.reset([[NSImage alloc] initWithSize:image_size]); |
77 } | 95 } |
78 [image addRepresentation:ns_image_rep]; | 96 [image addRepresentation:ns_image_rep]; |
79 } | 97 } |
80 | 98 |
81 return image.release(); | 99 return image.release(); |
82 } | 100 } |
83 | 101 |
84 gfx::Size NSImageSize(NSImage* image) { | 102 gfx::Size NSImageSize(NSImage* image) { |
85 NSSize size = [image size]; | 103 NSSize size = [image size]; |
86 int width = static_cast<int>(size.width); | 104 int width = static_cast<int>(size.width); |
87 int height = static_cast<int>(size.height); | 105 int height = static_cast<int>(size.height); |
88 return gfx::Size(width, height); | 106 return gfx::Size(width, height); |
89 } | 107 } |
90 | 108 |
91 } // namespace internal | 109 } // namespace internal |
92 } // namespace gfx | 110 } // namespace gfx |
93 | 111 |
OLD | NEW |