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