Re: Creating a bitmap of a scaled-down NSImage
Re: Creating a bitmap of a scaled-down NSImage
- Subject: Re: Creating a bitmap of a scaled-down NSImage
- From: John Stiles <email@hidden>
- Date: Mon, 26 Mar 2007 19:16:17 -0700
Cool, this looks like another solid way to do it. As I discovered
independently, -setSize and -setScalesWhenResized also works well :)
On Mar 26, 2007, at 7:14 PM, Guy English wrote:
Sure, basically you want to create a new context and draw the old
one into it resized. You kind of do that in a round about way with
the offscreen window trick. But we can use an NSImage as the
context directly since we can draw into it.
Here's some code (typed into Mail so don't trust it too much):
NSBitmapImageRep *GetNicelySizedBitmapImageRep( NSImage
*originalImageFromView )
{
NSImage *image = originalImageFromView;
NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize
( 512, 512 )];
[resizedImage lockFocus];
[image drawInRect: NSMakeRect( 0, 0, 512, 512 )
fromRect: NSMakeRect( 0, 0, [image size].width, [image
size].height )
operation: NSCompositeSourceOver
fraction: 1.0f];
[resizedImage unlockFocus];
// resizedImage now contains a 512x512 version of the original image
// get a bitmap image rep from it. there's other ways to do this
but this one is guaranteed to work. I think. :)
NSBitmapImageRep *bitmapRep = [NSBitmapImageRep imageRepWithData:
[resizedImage TIFFRepresentation]];
[resizedImage release];
return bitmapRep;
}
the resulting bitmap rep will be the correct size. It will,
however, have been the same as a 'size to fit' scaling in the image
view. If you have a non-square original image you want to fiddle
the drawInRect param so that it respects the aspect ratio and
positions it as you want.
Hope that helps,
Guy
On 26-Mar-07, at 10:05 PM, John Stiles wrote:
Could you explain this in a bit more depth?
On Mar 26, 2007, at 6:58 PM, Guy English wrote:
Hi John,
I may be missing something here but is there a reason you can't
just create an NSImage the size you need then draw the captured
views image into it using: [capture drawInRect: smallerImageRect
fromRect: biggerImageRect operation: NSCompositeSourceOver
fraction: 1.0f]; ?
Then get the bitmap image rep from the second, smaller image.
It's basically what you're doing with this offscreen window
stuff but less convoluted.
Guy
On 26-Mar-07, at 9:53 PM, John Stiles wrote:
Actually, I found one way to do it. It's a bit crude, though. I
create an offscreen NSWindow, embed an NSImageView in it, and
then set the NSImageView to show my NSImage. The NSImageView
knows how to scale down. Then I lock on the NSWindow and use
initWithFocusedViewRect on that.
This works, but is there a better way?
Here's a simple code snippet explaining what I mean. It's not
perfect—for non-square images, you can see the gray window
background surrounding the image—but I assume issues like that
can be cleaned up with a little smarts.
NSWindow * s_offscreenWindow = [[[NSWindow alloc]
initWithContentRect:NSMakeRect(0, 0, 512, 512)
styleMask:NSBorderlessWindowMask backing:NSBackingStoreRetained
defer:NO] autorelease];
NSImageView * imageView = [[[NSImageView alloc]
initWithFrame:NSMakeRect(0, 0, 512, 512)] autorelease];
[imageView setImage:image];
[s_offscreenWindow setContentView:imageView];
[[s_offscreenWindow contentView] display];
[[s_offscreenWindow contentView] lockFocus];
NSBitmapImageRep * bitmap = [[[NSBitmapImageRep alloc]
initWithFocusedViewRect:NSMakeRect(0, 0, 512, 512)] autorelease];
[[s_offscreenWindow contentView] unlockFocus];
On Mar 26, 2007, at 6:39 PM, John Stiles wrote:
I have an NSImage that has arrived from an external source that
I don't directly control. I need to scale it down and rasterize
it to an NSBitmapImageRep—the ultimate goal is to get it into
an OpenGL texture, and we have maximum texture-size limits that
I need to respect.
The documentation has some very useful code here: http://
developer.apple.com/documentation/Cocoa/Conceptual/
CocoaDrawingGuide/Images/chapter_7_section_5.html#//apple_ref/
doc/uid/TP40003290-CH208-BCICHFGA
The first technique, using initWithFocusedViewRect, works like
a charm if I don't need to change the image's size. However,
according to the docs, when using this technique, "you cannot
scale the content you capture." And unfortunately, I need to
support 10.3.9, so the NSView techniques listed don't apply to me.
What is the best approach? There must be a way to scale down
the image using the frameworks. I think I could figure out a
way to do it by rasterizing the image and then scaling that
bitmap down as a separate operation, but I'm thinking there
must be a better way.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40blizzard.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40gmail.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
40blizzard.com
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden