Rotating images and scaling the results: some files pixelate
Rotating images and scaling the results: some files pixelate
- Subject: Rotating images and scaling the results: some files pixelate
- From: Annard Brouwer <email@hidden>
- Date: Thu, 13 Jul 2006 13:19:11 +0200
Hello,
I have been struggling with this for a long time. I need to rotate
scanned images (with large dpi) and I wrote code that does this. But
the results always look iffy and especially when I start to scale
them up.
It turns out that the new image only contains a cached image rep:
(gdb) po [tmpImage representations]
<NSCFArray 0x6cefd10>(
NSCachedImageRep 0x6ac5110 Size={601, 304}
ColorSpace=NSCalibratedRGBColorSpace BPS=8 Pixels=601x304 Alpha=YES
)
I have tried several things such as using the best image rep, or
changing the graphics context to a virtual printer, but I always end
up with a cached image rep that is based on the dpi of the screen
like above.
What do I need to do so my drawing is done using the highest bitmap
image rep available and the cached image rep is at that resolution?
Here is the code for you to take a look at:
- (NSImage *)rotatedImage:(NSImage *)image
angle:(int)alpha
{
float factorW, factorH, dW, dH;
NSAffineTransform *centreOp, *rotateOp;
NSImage *tmpImage;
NSPoint startPoint;
NSGraphicsContext* graphicsContext;
BOOL wasAntialiasing;
NSImageInterpolation previousImageInterpolation;
if (0 == alpha)
return image;
factorW = fabs(cos(rad(alpha)));
factorH = fabs(sin(rad(alpha)));
dW = [image size].width * factorW + [image size].height * factorH;
dH = [image size].width * factorH + [image size].height * factorW;
tmpImage = [[NSImage alloc] initWithSize: NSMakeSize(dW, dH)];
centreOp = [NSAffineTransform transform];
[centreOp translateXBy: dW / 2 yBy: dH / 2];
rotateOp = [NSAffineTransform transform];
[rotateOp rotateByDegrees: alpha];
[rotateOp appendTransform: centreOp];
[image setMatchesOnMultipleResolution: NO];
[image setUsesEPSOnResolutionMismatch: YES];
[tmpImage lockFocus];
graphicsContext = [NSGraphicsContext currentContext];
wasAntialiasing = [graphicsContext shouldAntialias];
previousImageInterpolation = [graphicsContext imageInterpolation];
[graphicsContext setShouldAntialias: YES];
[graphicsContext setImageInterpolation: NSImageInterpolationHigh];
[rotateOp concat];
startPoint = NSMakePoint(-[image size].width / 2, -[image
size].height / 2);
[image drawAtPoint: startPoint
fromRect: NSMakeRect(0, 0, [image size].width, [image
size].height)
operation: NSCompositeCopy
fraction: 1.0];
[graphicsContext setShouldAntialias: wasAntialiasing];
[graphicsContext setImageInterpolation:
previousImageInterpolation];
[tmpImage unlockFocus];
[tmpImage setDataRetained: YES];
[tmpImage setScalesWhenResized: YES];
return [tmpImage autorelease];
}
Thank you for your help!
Annard
_______________________________________________
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