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 13:36:05 -0500
While we're on the subject, I was wondering... does NSBitmapImageRep
has an equivalent to NSImage's
drawInRect: fromRect: operation: fraction:
Problem is... the ImageRep technique works well when I'm just scaling
an image..it's quite fast too, however I am also cropping (in some
cases)... and unfortunately, without the "fromRect" parameter, it's
pretty much impossible to crop... right now I'm drawing my ImageRep in
an intermediate NSImage so that i can use drawInRect: fromRect: when
cropping but it takes tons of time (like 2-3x the time it takes by
using just an ImageRep...) being able to use drawInRect: fromRect on
an ImageRep would save me ton of time and headaches...
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