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.
5 #include "ui/gfx/image/image.h"
7 #import <AppKit/AppKit.h>
9 #include "base/logging.h"
10 #include "base/memory/scoped_nsobject.h"
11 #include "ui/gfx/image/image_png_rep.h"
12 #include "ui/gfx/size.h"
19 // Returns a 16x16 red NSImage to visually show when a NSImage cannot be
20 // created from PNG data.
21 // Caller takes ownership of the returned NSImage.
22 NSImage* GetErrorNSImage() {
23 NSRect rect = NSMakeRect(0, 0, 16, 16);
24 NSImage* image = [[NSImage alloc] initWithSize:rect.size];
26 [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set];
34 scoped_refptr<base::RefCountedMemory> Get1xPNGBytesFromNSImage(
36 CGImageRef cg_image = [nsimage CGImageForProposedRect:NULL
39 scoped_nsobject<NSBitmapImageRep> ns_bitmap(
40 [[NSBitmapImageRep alloc] initWithCGImage:cg_image]);
41 NSData* ns_data = [ns_bitmap representationUsingType:NSPNGFileType
43 const unsigned char* bytes =
44 static_cast<const unsigned char*>([ns_data bytes]);
45 scoped_refptr<base::RefCountedBytes> refcounted_bytes(
46 new base::RefCountedBytes());
47 refcounted_bytes->data().assign(bytes, bytes + [ns_data length]);
48 return refcounted_bytes;
51 NSImage* NSImageFromPNG(const std::vector<gfx::ImagePNGRep>& image_png_reps) {
52 if (image_png_reps.empty()) {
53 LOG(ERROR) << "Unable to decode PNG.";
54 return GetErrorNSImage();
57 scoped_nsobject<NSImage> image;
58 for (size_t i = 0; i < image_png_reps.size(); ++i) {
59 scoped_refptr<base::RefCountedMemory> png = image_png_reps[i].raw_data;
61 scoped_nsobject<NSData> ns_data(
62 [[NSData alloc] initWithBytes:png->front() length:png->size()]);
63 scoped_nsobject<NSBitmapImageRep> ns_image_rep(
64 [[NSBitmapImageRep alloc] initWithData:ns_data]);
66 LOG(ERROR) << "Unable to decode PNG at "
67 << ui::GetScaleFactorScale(image_png_reps[i].scale_factor)
69 return GetErrorNSImage();
73 float scale = ui::GetScaleFactorScale(image_png_reps[i].scale_factor);
74 NSSize image_size = NSMakeSize([ns_image_rep pixelsWide] / scale,
75 [ns_image_rep pixelsHigh] / scale);
76 image.reset([[NSImage alloc] initWithSize:image_size]);
78 [image addRepresentation:ns_image_rep];
81 return image.release();
84 gfx::Size NSImageSize(NSImage* image) {
85 NSSize size = [image size];
86 int width = static_cast<int>(size.width);
87 int height = static_cast<int>(size.height);
88 return gfx::Size(width, height);
91 } // namespace internal