diff options
author | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-28 19:38:50 +0000 |
---|---|---|
committer | rsesek@chromium.org <rsesek@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-07-28 19:38:50 +0000 |
commit | 8e953366dba4bc9e207a4e80a623635918093627 (patch) | |
tree | fe76b81f8341fadc24cc0cc41bab67dc4ada46cf /base | |
parent | 46f36a49b661fe4af87e95befb659464e3b1fbbc (diff) | |
download | chromium_src-8e953366dba4bc9e207a4e80a623635918093627.zip chromium_src-8e953366dba4bc9e207a4e80a623635918093627.tar.gz chromium_src-8e953366dba4bc9e207a4e80a623635918093627.tar.bz2 |
Move NSImage-to-CGImageRef conversion code into a common helper function in base/mac_util.h.
BUG=49571
TEST=Covered by unit_tests.
Review URL: http://codereview.chromium.org/3072005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@53997 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/mac_util.h | 8 | ||||
-rw-r--r-- | base/mac_util.mm | 33 | ||||
-rw-r--r-- | base/mac_util_unittest.mm | 17 |
3 files changed, 57 insertions, 1 deletions
diff --git a/base/mac_util.h b/base/mac_util.h index 4be3c89..d50e9e3 100644 --- a/base/mac_util.h +++ b/base/mac_util.h @@ -17,6 +17,7 @@ class FilePath; @class NSWindow; #else class NSBundle; +class NSImage; class NSWindow; #endif @@ -141,6 +142,13 @@ CFTypeRef GetValueFromDictionary(CFDictionaryRef dict, // Sets the process name as displayed in Activity Monitor to process_name. void SetProcessName(CFStringRef process_name); +// Converts a NSImage to a CGImageRef. Normally, the system frameworks can do +// this fine, especially on 10.6. On 10.5, however, CGImage cannot handle +// converting a PDF-backed NSImage into a CGImageRef. This function will +// rasterize the PDF into a bitmap CGImage. The caller is responsible for +// releasing the return value. +CGImageRef CopyNSImageToCGImage(NSImage* image); + } // namespace mac_util #endif // BASE_MAC_UTIL_H_ diff --git a/base/mac_util.mm b/base/mac_util.mm index cd7a949..ceea5c2 100644 --- a/base/mac_util.mm +++ b/base/mac_util.mm @@ -479,4 +479,37 @@ void SetProcessName(CFStringRef process_name) { LOG_IF(ERROR, err) << "Call to set process name failed, err " << err; } +// Converts a NSImage to a CGImageRef. Normally, the system frameworks can do +// this fine, especially on 10.6. On 10.5, however, CGImage cannot handle +// converting a PDF-backed NSImage into a CGImageRef. This function will +// rasterize the PDF into a bitmap CGImage. The caller is responsible for +// releasing the return value. +CGImageRef CopyNSImageToCGImage(NSImage* image) { + // This is based loosely on http://www.cocoadev.com/index.pl?CGImageRef . + NSSize size = [image size]; + scoped_cftyperef<CGContextRef> context( + CGBitmapContextCreate(NULL, // Allow CG to allocate memory. + size.width, + size.height, + 8, // bitsPerComponent + 0, // bytesPerRow - CG will calculate by default. + [[NSColorSpace genericRGBColorSpace] CGColorSpace], + kCGBitmapByteOrder32Host | + kCGImageAlphaPremultipliedFirst)); + if (!context.get()) + return NULL; + + [NSGraphicsContext saveGraphicsState]; + [NSGraphicsContext setCurrentContext: + [NSGraphicsContext graphicsContextWithGraphicsPort:context.get() + flipped:NO]]; + [image drawInRect:NSMakeRect(0,0, size.width, size.height) + fromRect:NSZeroRect + operation:NSCompositeCopy + fraction:1.0]; + [NSGraphicsContext restoreGraphicsState]; + + return CGBitmapContextCreateImage(context); +} + } // namespace mac_util diff --git a/base/mac_util_unittest.mm b/base/mac_util_unittest.mm index aebb731..f590ac1 100644 --- a/base/mac_util_unittest.mm +++ b/base/mac_util_unittest.mm @@ -1,4 +1,4 @@ -// Copyright (c) 2008-2009 The Chromium Authors. All rights reserved. +// Copyright (c) 2010 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. @@ -165,6 +165,21 @@ TEST_F(MacUtilTest, TestGetValueFromDictionary) { dict, CFSTR("no-exist"), CFStringGetTypeID())); } +TEST_F(MacUtilTest, CopyNSImageToCGImage) { + scoped_nsobject<NSImage> nsImage( + [[NSImage alloc] initWithSize:NSMakeSize(20, 20)]); + [nsImage lockFocus]; + [[NSColor redColor] set]; + NSRect rect = NSZeroRect; + rect.size = [nsImage size]; + NSRectFill(rect); + [nsImage unlockFocus]; + + scoped_cftyperef<CGImageRef> cgImage( + mac_util::CopyNSImageToCGImage(nsImage.get())); + EXPECT_TRUE(cgImage.get()); +} + } // namespace } // namespace mac_util |