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 color_space_model = CGColorSpaceGetModel( | |
78 [[ns_image_rep colorSpace] CGColorSpace]); | |
79 CGColorSpaceModel decoded_color_space_model = | |
80 CGColorSpaceGetModel(color_space); | |
81 if (color_space_model == decoded_color_space_model) { | |
82 scoped_nsobject<NSColorSpace> ns_color_space( | |
83 [[NSColorSpace alloc] initWithCGColorSpace: color_space]); | |
Avi (use Gerrit)
2013/06/12 01:35:29
drive by: no space after the colon
| |
84 NSBitmapImageRep* ns_retagged_image_rep = | |
85 [ns_image_rep | |
86 bitmapImageRepByRetaggingWithColorSpace: ns_color_space]; | |
Avi (use Gerrit)
2013/06/12 01:35:29
drive by: no space after the colon
| |
87 if (ns_retagged_image_rep && ns_retagged_image_rep != ns_image_rep) { | |
88 [ns_retagged_image_rep retain]; | |
pkotwicz
2013/06/12 01:26:19
I am confused why I need to retain |ns_retagged_im
Avi (use Gerrit)
2013/06/12 01:35:29
It comes from -bitmapImageRepByRetaggingWithColorS
| |
89 ns_image_rep.reset(ns_retagged_image_rep); | |
90 } | |
91 } | |
92 | |
72 if (!image.get()) { | 93 if (!image.get()) { |
73 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor); | 94 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor); |
74 NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale, | 95 NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale, |
75 [ns_image_rep pixelsHigh] / scale); | 96 [ns_image_rep pixelsHigh] / scale); |
76 image.reset([[NSImage alloc] initWithSize:image_size]); | 97 image.reset([[NSImage alloc] initWithSize:image_size]); |
77 } | 98 } |
78 [image addRepresentation:ns_image_rep]; | 99 [image addRepresentation:ns_image_rep]; |
79 } | 100 } |
80 | 101 |
81 return image.release(); | 102 return image.release(); |
82 } | 103 } |
83 | 104 |
84 gfx::Size NSImageSize(NSImage* image) { | 105 gfx::Size NSImageSize(NSImage* image) { |
85 NSSize size = [image size]; | 106 NSSize size = [image size]; |
86 int width = static_cast<int>(size.width); | 107 int width = static_cast<int>(size.width); |
87 int height = static_cast<int>(size.height); | 108 int height = static_cast<int>(size.height); |
88 return gfx::Size(width, height); | 109 return gfx::Size(width, height); |
89 } | 110 } |
90 | 111 |
91 } // namespace internal | 112 } // namespace internal |
92 } // namespace gfx | 113 } // namespace gfx |
93 | 114 |
OLD | NEW |