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 #import <AppKit/AppKit.h> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #include "base/memory/scoped_nsobject.h" |
| 11 |
| 12 namespace gfx { |
| 13 namespace internal { |
| 14 |
| 15 void PNGFromNSImage(NSImage* nsimage, std::vector<unsigned char>* png) { |
| 16 CGImageRef cg_image = [nsimage CGImageForProposedRect:NULL |
| 17 context:nil |
| 18 hints:nil]; |
| 19 scoped_nsobject<NSBitmapImageRep> ns_bitmap( |
| 20 [[NSBitmapImageRep alloc] initWithCGImage:cg_image]); |
| 21 NSData* ns_data = [ns_bitmap representationUsingType:NSPNGFileType |
| 22 properties:nil]; |
| 23 const unsigned char* bytes = |
| 24 static_cast<const unsigned char*>([ns_data bytes]); |
| 25 png->assign(bytes, bytes + [ns_data length]); |
| 26 } |
| 27 |
| 28 NSImage* NSImageFromPNG(const std::vector<unsigned char>& png) { |
| 29 scoped_nsobject<NSData> ns_data( |
| 30 [[NSData alloc] initWithBytes:&png.front() length:png.size()]); |
| 31 scoped_nsobject<NSImage> image([[NSImage alloc] initWithData:ns_data]); |
| 32 if (!image) { |
| 33 LOG(WARNING) << "Unable to decode PNG."; |
| 34 // Return a 16x16 red image to visually show error. |
| 35 NSRect rect = NSMakeRect(0, 0, 16, 16); |
| 36 image.reset([[NSImage alloc] initWithSize:rect.size]); |
| 37 [image lockFocus]; |
| 38 [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set]; |
| 39 NSRectFill(rect); |
| 40 [image unlockFocus]; |
| 41 } |
| 42 return image.release(); |
| 43 } |
| 44 |
| 45 } // namespace internal |
| 46 } // namespace gfx |
| 47 |
OLD | NEW |