Re: Resizing images
Re: Resizing images
- Subject: Re: Resizing images
- From: "Juan P. Pertierra" <email@hidden>
- Date: Thu, 22 Dec 2005 05:51:59 +0000
You could use the vImage lirbary for resizing. You'd need to create an NSBitmapRep from your NSImage first. You
can lookup both vImage and NSBitmapImageRep class in the XCode documentation or in the Apple Developer
website. vImage uses the Lanczos resizing algorithm which yields good results.
Or, you can write a simple algorithm such as bilinear in little time, but i think it will be simpler to use vImage.
Cheers,
Juan
>
-------Original Message-------
>
From: Luke Burton <email@hidden>
>
Subject: Resizing images
>
Sent: 22 Dec '05 04:27
>
>
Dear cocoa-dev people,
>
>
I am new to Cocoa and Objective-C, but am a competent C++ programmer.
>
>
I have kicked off an open source project called SimpleViewer iPhoto Export.
>
It exports photos from iPhoto into a directory, and prepares them into a
>
nice flash gallery format. You can see some examples (and download source
>
code and binaries) from my homepage: http://www.hagus.net/taxonomy/term/14
>
>
Now. The reason I'm writing is that my resizing code appears to create
>
pretty jagged images, which is less than desireable! I'm quite lost as to
>
how I can improve my resize "algorithm", as I really pieced together this
>
code using trial and error, and various usenet postings. If someone could
>
suggest some improvements, I would be most grateful.
>
>
// Given source file "source", resize the longest/shortest edge to be "size"
>
long, and write to "destination".
>
+(NSString*)resizeFile:(NSString*)source destination:(NSString*)destination
>
size:(int)size scaleLongest:(BOOL)scaleLongest
>
{
>
NSFileManager * fileManager = [NSFileManager defaultManager];
>
if (![fileManager fileExistsAtPath:source])
>
return [NSString stringWithFormat:@"Unable to find source file
>
\"%s\"", [source UTF8String]];
>
>
NSImage * myImage = [[NSImage alloc] initWithContentsOfFile:source];
>
NSRect srcRect = {NSZeroPoint, [myImage size]};
>
>
float targetWidth = 0, targetHeight = 0;
>
// SNIP - chopped out the code that actually calculates these values.
>
>
printf("Translating %f/%f to %f/%f\n", srcRect.size.width,
>
srcRect.size.height, targetWidth, targetHeight);
>
>
[myImage lockFocus];
>
[myImage setScalesWhenResized:TRUE];
>
[myImage setSize:NSMakeSize(targetWidth, targetHeight)];
>
[myImage drawAtPoint:NSZeroPoint fromRect:srcRect
>
operation:NSCompositeCopy fraction:1.0];
>
>
NSBitmapImageRep * imageRep = [[NSBitmapImageRep alloc]
>
initWithData:[myImage TIFFRepresentation]];
>
>
NSData * data = [imageRep representationUsingType:NSJPEGFileType
>
properties:nil];
>
[data writeToFile:destination atomically:NO];
>
>
[myImage unlockFocus];
>
[imageRep release];
>
[myImage release];
>
>
return nil;
>
}
>
>
>
>
--
>
Luke Burton.
>
>
Yes, questions. Morphology, longevity, incept dates.
>
_______________________________________________
>
Do not post admin requests to the list. They will be ignored.
>
Cocoa-dev mailing list (email@hidden)
>
Help/Unsubscribe/Update your Subscription:
>
>
This email sent to email@hidden
>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden