On Oct 9, 2014, at 8:05 PM, Ken Thomases <k...@codeweavers.com> wrote:

> On Oct 9, 2014, at 9:08 PM, Carl Hoefs <newsli...@autonomy.caltech.edu> wrote:
> 
>> I need to scale an NSImage's dimensions to exactly 640x480 pixels before 
>> sending it remotely for processing.
> 
>> What is the correct way to do this?
> 
> Create an NSBitmapImageRep with the appropriate properties.  Create an 
> NSGraphicsContext from that bitmap image rep using +[NSGraphicsContext 
> graphicsContextWithBitmapImageRep:].  Make that graphics context current.  
> Draw the image.  Flush and restore the graphics context.  Get the data from 
> the bitmap image rep in an image file format using 
> -representationUsingType:properties: or (if you really want TIFF) 
> -TIFFRepresentationUsingCompression:factor:.

Awesome pointers, I got it to work! The tricky part was the actual scaling. It 
would crop but not scale, so resorted to using CIImage. If there’s a cleaner 
way, please let me know!

    NSRect imgRect = NSMakeRect(0.0, 0.0, 640.0, 480.0);
    NSSize imgSize = imgRect.size;
    NSBitmapImageRep *offscreenRep = [[NSBitmapImageRep alloc]
                                       initWithBitmapDataPlanes:NULL
                                                     pixelsWide:imgSize.width
                                                     pixelsHigh:imgSize.height
                                                  bitsPerSample:8
                                                samplesPerPixel:4
                                                       hasAlpha:YES
                                                       isPlanar:NO
                                                 
colorSpaceName:NSDeviceRGBColorSpace
                                                   
bitmapFormat:NSAlphaFirstBitmapFormat
                                                    
bytesPerRow:8*4*imgSize.width
                                                   bitsPerPixel:32];
    // set offscreen context
    NSGraphicsContext *gc = [NSGraphicsContext 
graphicsContextWithBitmapImageRep:offscreenRep];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:gc];
    
    // scale the image from cgRect down to imgRect
    CIImage *ciImage = [CIImage imageWithData:dataObj];
    CGRect cgRect = [ciImage extent];
    [[gc CIContext] drawImage:ciImage inRect:imgRect fromRect:cgRect];

    // reset the current context
    [NSGraphicsContext restoreGraphicsState];
    
    // create an NSImage and add the rep to it    
    NSImage *img = [[NSImage alloc] initWithSize:imgSize];
    [img addRepresentation:offscreenRep];
    NSLog(@"* Final Image: %@",img);


* Final Image: <NSImage 0x608000276c80 Size={640, 480} Reps=(
    "NSBitmapImageRep 0x6080000b6b00 Size={640, 480} ColorSpace=(not yet 
loaded) BPS=8 BPP=(not yet loaded) Pixels=640x480 Alpha=NO Planar=NO 
Format=(not yet loaded) CurrentBacking=nil (faulting) 
CGImageSource=0x608000167200"
)>

Thanks again!
-Carl


_______________________________________________

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

This email sent to arch...@mail-archive.com

Reply via email to