tags:

views:

30

answers:

2

Hi there.

I am taking photos from a webcam in my Cocoa application and I would like to zoom in on the centre of the image I receive. I start by receiving a CIImage and eventually save an NSImage.

How would I go about zooming in on either of these objects?

I'd appreciate any advice.

Thanks, Ricky.

A: 

“Zoom” means a couple of things. You'll need at least to crop the image, and you may want to scale up. Or you may want to reserve scaling for display only.

CGImage

To crop it, use a CICrop filter.

To scale it, use either a CILanczosScaleTransform filter or a CIAffineTransform filter.

To crop and scale it, use both filters. Simply pass the output of the crop as the input of the scale.

NSImage

Crop and scale are the same operation here. You'll need to create a new, empty NSImage of the desired size (whether it's the size of the source crop if you won't zoom or an increased size if you will zoom), lock focus on it, draw the crop rectangle from the source image into the bounding rectangle of the destination image, and unlock focus.

If the destination rectangle is not the same size as the source (crop) rectangle, it will scale; if they are the same size, it will simply copy or composite pixel-to-pixel.

Peter Hosey
A: 

Hi Peter.

I looked up CICrop in Apple's documentation and tried the following, except I'm not getting an outputted image.

CIVector *extent = [CIVector vectorWithX:0 Y:0 Z:50 W:50];
CIFilter *transition = [CIFilter filterWithName:@"CICopyMachineTransition"];
[transition setDefaults];
[transition setValue:extent forKey:@"inputExtent"];
[transition setValue:capturedImage forKey:@"inputImage"];
[transition setValue:capturedImage forKey:@"inputTargetImage"];
[transition setValue:[NSNumber numberWithFloat:1.0] forKey:@"inputTime"];

CIFilter *crop = [CIFilter filterWithName:@"CICrop" keysAndValues:@"inputImage", [transition valueForKey:@"outputImage"], @"inputRectangle", [CIVector vectorWithX:0 Y:0 Z:50 W:50], nil];
capturedImage = [crop valueForKey:@"outputImage"];

Does anything stand out as being wrong here? I'm not getting any errors or warnings at compile time or runtime.

Thanks for your help. Ricky.

Ricky