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 PROTECTED]


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to