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 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.
| |
17 context:nil | |
18 hints:nil]; | |
19 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.
| |
20 [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); | |
21 NSData* nsdata = [nsbitmap representationUsingType:NSPNGFileType | |
22 properties:nil]; | |
23 const unsigned char* bytes = | |
24 static_cast<const unsigned char*>([nsdata bytes]); | |
25 png->assign(bytes, bytes + [nsdata length]); | |
26 } | |
27 | |
28 NSImage* NSImageFromPNG(const std::vector<unsigned char>& png) { | |
29 scoped_nsobject<NSData> nsdata( | |
30 [[NSData alloc] initWithBytes:&png.front() | |
31 length:png.size()]); | |
32 scoped_nsobject<NSImage> image([[NSImage alloc] initWithData:nsdata]); | |
33 if (!image) { | |
34 LOG(WARNING) << "Unable to decode PNG, returning empty bitmap."; | |
35 image.reset([[NSImage alloc] initWithSize:NSMakeSize(1, 1)]); | |
36 } | |
37 return image.release(); | |
38 } | |
39 | |
40 } // namespace internal | |
41 } // namespace gfx | |
42 | |
OLD | NEW |