Nearest Neighbor Image Unit

2010-02-06 23:53:26 -08:00

I originally wrote this as an application using NSImage (with NSImageInterpolationNone), but decided to rewrite it as an Image Unit. So, here it is.

3 Responses to “Nearest Neighbor Image Unit”

  1. Gus Mueller Says:

    Another way to do this is with CIPixellate:

    http://lists.apple.com/archives/quartz-dev/2009/Aug/msg00009.html

  2. Peter Hosey Says:

    Gus Mueller: I just tried it. It doesn’t work: The output is no different.

    This makes sense, since filters only affect the inputs to other filters, not the other filters themselves. CIPixellate(scale=1.0) + CIAffineTransform(scale=x) doesn’t make the latter a nearest-neighbor scale; the latter does the same thing it would have done without the CIPixellate, with (since the CIPixellate has the identity scale) the same input image.

  3. Gus Mueller Says:

    Works for me:

    CIFilter * scaler = [CIFilter filterWithName:@"CIPixellate"];
                
    [scaler setDefaults];
    [scaler setValue:[NSNumber numberWithFloat:1] forKey:@"inputScale"];
    [scaler setValue:img forKey:@"inputImage"];
                
    img = [scaler valueForKey:@"outputImage"];
                
    CIFilter *f = [CIFilter filterWithName:@"CIAffineTransform"];
                
    NSAffineTransform *t = [NSAffineTransform transform];
    [t scaleXBy:_scale yBy:_scale];
                
    [f setValue:t forKey:@"inputTransform"];
    [f setValue:img forKey:@"inputImage"];
    img = [f valueForKey:@"outputImage"];

Leave a Reply

Do not delete the second sentence.