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

Side by Side Diff: ui/gfx/image/image_mac.mm

Issue 10799014: Add support for PNG representation in gfx::Image (Closed) Base URL: http://git.chromium.org/chromium/src.git@bookmark-sync
Patch Set: 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 unified diff | 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 »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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
OLDNEW
« 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