Chromium Code Reviews| Index: skia/ext/skia_utils_mac.mm |
| diff --git a/skia/ext/skia_utils_mac.mm b/skia/ext/skia_utils_mac.mm |
| index c78121abd8593b40f2d9305d899380f6fe8ffb56..4013d0a6a10d31ae5c12a522c4728411b3960892 100644 |
| --- a/skia/ext/skia_utils_mac.mm |
| +++ b/skia/ext/skia_utils_mac.mm |
| @@ -8,6 +8,7 @@ |
| #include "base/logging.h" |
| #include "base/mac/scoped_cftyperef.h" |
| +#include "base/memory/scoped_nsobject.h" |
| #include "base/memory/scoped_ptr.h" |
| #include "skia/ext/bitmap_platform_device_mac.h" |
| #include "third_party/skia/include/utils/mac/SkCGUtils.h" |
| @@ -116,15 +117,20 @@ SkBitmap CGImageToSkBitmap(CGImageRef image) { |
| } |
| SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { |
| + return NSImageRepToSkBitmap([image bestRepresentationForDevice:nil], |
| + size, is_opaque); |
| +} |
| + |
| +SkBitmap NSImageRepToSkBitmap(NSImageRep* image, NSSize size, bool is_opaque) { |
| SkBitmap bitmap; |
| bitmap.setConfig(SkBitmap::kARGB_8888_Config, size.width, size.height); |
| - if (bitmap.allocPixels() != true) |
| + if (!bitmap.allocPixels()) |
| return bitmap; // Return |bitmap| which should respond true to isNull(). |
| bitmap.setIsOpaque(is_opaque); |
| base::mac::ScopedCFTypeRef<CGColorSpaceRef> color_space( |
| - CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| void* data = bitmap.getPixels(); |
| // Allocate a bitmap context with 4 components per pixel (BGRA). Apple |
| @@ -145,21 +151,18 @@ SkBitmap NSImageToSkBitmap(NSImage* image, NSSize size, bool is_opaque) { |
| #undef HAS_ARGB_SHIFTS |
| // Something went really wrong. Best guess is that the bitmap data is invalid. |
| - DCHECK(context != NULL); |
| + DCHECK(context); |
| // Save the current graphics context so that we can restore it later. |
| [NSGraphicsContext saveGraphicsState]; |
| // Dummy context that we will draw into. |
| NSGraphicsContext* context_cocoa = |
| - [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; |
| + [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO]; |
| [NSGraphicsContext setCurrentContext:context_cocoa]; |
| // This will stretch any images to |size| if it does not fit or is non-square. |
| - [image drawInRect:NSMakeRect(0, 0, size.width, size.height) |
| - fromRect:NSZeroRect |
| - operation:NSCompositeCopy |
| - fraction:1.0]; |
| + [image drawInRect:NSMakeRect(0, 0, size.width, size.height)]; |
| // Done drawing, restore context. |
| [NSGraphicsContext restoreGraphicsState]; |
| @@ -173,13 +176,12 @@ NSImage* SkBitmapToNSImageWithColorSpace(const SkBitmap& skiaBitmap, |
| return nil; |
| // First convert SkBitmap to CGImageRef. |
| - CGImageRef cgimage = |
| - SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace); |
| + base::mac::ScopedCFTypeRef<CGImageRef> cgimage( |
| + SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace)); |
| // Now convert to NSImage. |
| - NSBitmapImageRep* bitmap = [[[NSBitmapImageRep alloc] |
| - initWithCGImage:cgimage] autorelease]; |
| - CFRelease(cgimage); |
| + scoped_nsobject<NSBitmapImageRep> bitmap( |
| + [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); |
| NSImage* image = [[[NSImage alloc] init] autorelease]; |
| [image addRepresentation:bitmap]; |
| [image setSize:NSMakeSize(skiaBitmap.width(), skiaBitmap.height())]; |
| @@ -192,6 +194,37 @@ NSImage* SkBitmapToNSImage(const SkBitmap& skiaBitmap) { |
| return SkBitmapToNSImageWithColorSpace(skiaBitmap, colorSpace.get()); |
| } |
| +NSImage* SkBitmapsToNSImage(const std::vector<const SkBitmap*>& bitmaps) { |
| + if (bitmaps.empty()) |
| + return nil; |
| + |
| + base::mac::ScopedCFTypeRef<CGColorSpaceRef> colorSpace( |
|
Robert Sesek
2011/04/18 22:48:13
nit: use under_scores in C++, so this should be co
sail
2011/04/19 00:21:53
Done.
|
| + CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB)); |
| + NSImage* image = [[[NSImage alloc] init] autorelease]; |
| + NSSize minSize = NSZeroSize; |
|
Robert Sesek
2011/04/18 22:48:13
nit: min_size
sail
2011/04/19 00:21:53
Done.
|
| + |
| + for (std::vector<const SkBitmap*>::const_iterator it = bitmaps.begin(); |
| + it != bitmaps.end(); ++it) { |
| + const SkBitmap& skiaBitmap = **it; |
| + // First convert SkBitmap to CGImageRef. |
| + base::mac::ScopedCFTypeRef<CGImageRef> cgimage( |
| + SkCreateCGImageRefWithColorspace(skiaBitmap, colorSpace)); |
| + |
| + // Now convert to NSImage. |
| + scoped_nsobject<NSBitmapImageRep> bitmap( |
| + [[NSBitmapImageRep alloc] initWithCGImage:cgimage]); |
| + [image addRepresentation:bitmap]; |
| + |
| + if (minSize.width == 0 || minSize.width > skiaBitmap.width()) { |
| + minSize.width = skiaBitmap.width(); |
| + minSize.height = skiaBitmap.height(); |
| + } |
| + } |
| + |
| + [image setSize:minSize]; |
| + return image; |
| +} |
| + |
| SkBitmap AppplicationIconAtSize(int size) { |
| NSImage* image = [NSImage imageNamed:@"NSApplicationIcon"]; |
| return NSImageToSkBitmap(image, NSMakeSize(size, size), /* is_opaque=*/true); |