Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(417)

Unified Diff: ui/gfx/image/image_mac.mm

Issue 10830207: Add support for PNG representation in gfx::Image (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Missed small change from previous CL. Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/gfx/image/image.cc ('k') | ui/gfx/image/image_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..ce02b5e160d979f6251fa237732311c4d7767b48
--- /dev/null
+++ b/ui/gfx/image/image_mac.mm
@@ -0,0 +1,47 @@
+// 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 cg_image = [nsimage CGImageForProposedRect:NULL
+ context:nil
+ hints:nil];
+ scoped_nsobject<NSBitmapImageRep> ns_bitmap(
+ [[NSBitmapImageRep alloc] initWithCGImage:cg_image]);
+ NSData* ns_data = [ns_bitmap representationUsingType:NSPNGFileType
+ properties:nil];
+ const unsigned char* bytes =
+ static_cast<const unsigned char*>([ns_data bytes]);
+ png->assign(bytes, bytes + [ns_data length]);
+}
+
+NSImage* NSImageFromPNG(const std::vector<unsigned char>& png) {
+ scoped_nsobject<NSData> ns_data(
+ [[NSData alloc] initWithBytes:&png.front() length:png.size()]);
+ scoped_nsobject<NSImage> image([[NSImage alloc] initWithData:ns_data]);
+ if (!image) {
+ LOG(WARNING) << "Unable to decode PNG.";
+ // Return a 16x16 red image to visually show error.
+ NSRect rect = NSMakeRect(0, 0, 16, 16);
+ image.reset([[NSImage alloc] initWithSize:rect.size]);
+ [image lockFocus];
+ [[NSColor colorWithDeviceRed:1.0 green:0.0 blue:0.0 alpha:1.0] set];
+ NSRectFill(rect);
+ [image unlockFocus];
+ }
+ return image.release();
+}
+
+} // namespace internal
+} // namespace gfx
+
« no previous file with comments | « ui/gfx/image/image.cc ('k') | ui/gfx/image/image_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698