Re: Fastest way to resize an image?
Re: Fastest way to resize an image?
- Subject: Re: Fastest way to resize an image?
- From: Jean-Nicolas Jolivet <email@hidden>
- Date: Sat, 22 Nov 2008 12:37:23 -0500
Thanks a lot for that! I'm implementing it right now and I'll do some
benchmarking ! :)
Just one thing, when you write:
[self drawInRect:NSMakeRect( 0, 0, [sourceRep pixelsWide], [sourceRep
pixelsHigh])];
Do you mean
[[sourceRep drawInRect:NSMakeRect( 0, 0, [sourceRep pixelsWide],
[sourceRep pixelsHigh])];
Just wondering because obviously my custom image class (which is
basically just a class holding paths and utility functions) has no
draw method...
Other than that, I like what I'm seeing! Thanks again! :)
Jean-Nicolas Jolivet
On 22-Nov-08, at 11:59 AM, Heinrich Giesen wrote:
Hi,
On 20.11.2008, at 09:05, Jean-Nicolas Jolivet wrote:
I have a bunch of images to resize.
anyway, right now I am loading an NSBitmapImageRep with the
imageRepFromFile: method... then creating a new, empty NSImage based
on the image size that I want to resize to (using initWithSize:)
and I
draw the BitmapImageRep on this image (while the focus is locked on
the NSImage)...
I understand that you are not happy with this way, I call this way
the old way,
which is now outdated but not deprecated, because since Tiger you
can draw directly
into an NSBitmapImageRep. (The above way first creates --lockfocus
does it--
and then draws into an NSCachedImageRep, from which a new
NSBitmapImageRep
can be made via TIFFRepresentation.)
I usually use the following code (or a similar one) which respects
the size
(resolution) of the sourceImage reliably!
(Searching in this forum for graphicsContextWithBitmapImageRep: will
give more hints.)
// first load the sourceImage
NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc]
initWithData: ..dataFromFile ..];
NSBitmapImageRep *newRep =
[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:[sourceRep
pixelsWide] // or a new value
pixelsHigh:[sourceRep
pixelsHigh]
bitsPerSample:8
samplesPerPixel:4
hasAlpha:YES // must
have alpha!
isPlanar:NO
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:0
bitsPerPixel:0 ];
[NSGraphicsContext saveGraphicsState];
NSGraphicsContext *context = [NSGraphicsContext
graphicsContextWithBitmapImageRep:newRep];
[NSGraphicsContext setCurrentContext:context];
// do not use drawAtPoint: !! it does not respect resolution due
to a bug
[self drawInRect:NSMakeRect( 0, 0, [sourceRep pixelsWide],
[sourceRep pixelsHigh])];
[NSGraphicsContext restoreGraphicsState];
[newRep setSize:[sourceRep size]]; // this sets the resolution
of the source
[newRep autorelease]; // if needed
Instead of [sourceRep pixelsWide] you should set the new number of
pixels. This value
must be the same in pixelsWide: and in the drawRect.
You see: NSImage is not used because we don't need it. If more
flexible drawOperations
are waanted, I use a new temporary NSImage and remove it as soon as
possible.
My tests showed a time speedup of at least a factor of 5.
BTW: the connection between size, pixelnumbers and resolution is:
size.width = 72.0*pixelsWide / resolutionX
size.height = 72.0*pixelsHigh / resolutionY
(size has the dimension of a length and the unit 1/72 inch;
resolution has the dimension "dots per length" and the
unit "dots per inch"; pixelsWide, pixelsHigh are dimensionless
numbers.)
good luck
Heinrich
--
Heinrich Giesen
email@hidden
Jean-Nicolas Jolivet
email@hidden
http://www.silverscripting.com
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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