Index: ui/gfx/image/image_mac.mm |
diff --git a/ui/gfx/image/image_mac.mm b/ui/gfx/image/image_mac.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..65929ef9011001549f03ae45d0caf62850952089 |
--- /dev/null |
+++ b/ui/gfx/image/image_mac.mm |
@@ -0,0 +1,42 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ui/gfx/image/image.h" |
+ |
+#import <AppKit/AppKit.h> |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_nsobject.h" |
+ |
+namespace gfx { |
+namespace internal { |
+ |
+void PNGFromNSImage(NSImage* nsimage, std::vector<unsigned char>* png) { |
+ CGImageRef cgimage = [nsimage CGImageForProposedRect:NULL |
Robert Sesek
2012/07/24 18:59:17
These functions should only be indented 2 spaces.
Robert Sesek
2012/07/24 18:59:17
Why do you need to go through CGImage? Can you not
cjhopman
2012/07/25 21:13:29
Done.
cjhopman
2012/07/25 21:13:29
I really don't know about this stuff, so here's so
Robert Sesek
2012/07/31 14:07:16
Thanks for the links. This looks right, then.
|
+ context:nil |
+ hints:nil]; |
+ scoped_nsobject<NSBitmapImageRep> nsbitmap( |
Robert Sesek
2012/07/24 18:59:17
naming: ns_bitmap and cg_image
cjhopman
2012/07/25 21:13:29
Done.
|
+ [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); |
+ NSData* nsdata = [nsbitmap representationUsingType:NSPNGFileType |
+ properties:nil]; |
+ const unsigned char* bytes = |
+ static_cast<const unsigned char*>([nsdata bytes]); |
+ png->assign(bytes, bytes + [nsdata length]); |
+} |
+ |
+NSImage* NSImageFromPNG(const std::vector<unsigned char>& png) { |
+ scoped_nsobject<NSData> nsdata( |
+ [[NSData alloc] initWithBytes:&png.front() |
+ length:png.size()]); |
+ scoped_nsobject<NSImage> image([[NSImage alloc] initWithData:nsdata]); |
+ if (!image) { |
+ LOG(WARNING) << "Unable to decode PNG, returning empty bitmap."; |
+ image.reset([[NSImage alloc] initWithSize:NSMakeSize(1, 1)]); |
+ } |
+ return image.release(); |
+} |
+ |
+} // namespace internal |
+} // namespace gfx |
+ |