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

Unified Diff: base/mac_util.mm

Issue 3072005: Move NSImage-to-CGImageRef conversion code into a common helper function in base/mac_util.h. (Closed) Base URL: http://src.chromium.org/git/chromium.git
Patch Set: Fix test Created 10 years, 5 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 | « base/mac_util.h ('k') | base/mac_util_unittest.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/mac_util.mm
diff --git a/base/mac_util.mm b/base/mac_util.mm
index cd7a9492280650b5a48b4754de6bdd8e981eeca6..ceea5c260c6167847602c8ad16ff648d1a8c8951 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
« no previous file with comments | « base/mac_util.h ('k') | base/mac_util_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698