Chromium Code Reviews| 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 image.reset([[NSImage alloc] initWithSize:NSMakeSize(16, 16)]); | |
| 36 [image lockFocus]; | |
| 37 [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set]; | |
| 38 NSRectFill(NSMakeRect(0, 0, 16, 16)); | |
|
Robert Sesek
2012/07/31 14:07:17
nit: you could declare this rect as a local, and t
cjhopman
2012/08/03 22:15:25
Done.
| |
| 39 [image unlockFocus]; | |
| 40 } | |
| 41 return image.release(); | |
| 42 } | |
| 43 | |
| 44 } // namespace internal | |
| 45 } // namespace gfx | |
| 46 | |
| OLD | NEW |