// // MPT_RimageAdaptor.mm // AUCoderDemo // // Created by Andrew Salamon on 9/5/06. // Copyright (c) 2006 Machine Perception Laboratory. University of California San Diego. // All rights reserved. #import "MPT_RimageAdaptor.h" #import #import "NSImageToRimage.h" @implementation MPT_RimageAdaptor + (MPT_RimageAdaptor *)adaptorWithRimage:(void *)newRimage { MPT_RimageAdaptor *adaptor = [[MPT_RimageAdaptor alloc] initWithRimage:newRimage]; return [adaptor autorelease]; } - (id)initWithRimage:(void *)newRimage { if( self = [super init] ) { rimage = newRimage; } return self; } - (id)initWithWidth:(float)width height:(float)height { if( self = [super init] ) { rimage = new RImage(width, height); } return self; } - (void)dealloc { if( rimage ) delete (RImage *)rimage; [super dealloc]; } - (void)encodeWithCoder:(NSCoder *)coder { RImage *loc = (RImage *)rimage; [coder encodeInt:loc->width forKey:@"width"]; [coder encodeInt:loc->height forKey:@"height"]; [coder encodeBytes:(uint8_t *)(loc->array) length:(loc->width * loc->height * sizeof(float)) forKey:@"pixels"]; } - (id)initWithCoder:(NSCoder *)coder { self = [super init]; int width = [coder decodeIntForKey:@"width"]; int height = [coder decodeIntForKey:@"height"]; rimage = new RImage(width, height); unsigned int check; float *tmp = (float *)[coder decodeBytesForKey:@"pixels" returnedLength:&check]; NSAssert( check == (width * height * sizeof(float)), @"Bad pixel size in initWithCoder for MPT_RimageAdaptor" ); memcpy( ((RImage *)rimage)->array, tmp, (width*height) ); return self; } - (void)setRimage:(void *)newRimage { if( rimage != newRimage ) { if( rimage ) delete (RImage *)rimage; rimage = newRimage; } } - (void *)rimage { return rimage; } - (float *)array { return ((RImage *)rimage)->array; } - (id)copyWithZone:(NSZone *)zone { RImage *cpRimage = new RImage( *(RImage *)rimage ); MPT_RimageAdaptor *cp = [[MPT_RimageAdaptor alloc] initWithRimage:cpRimage]; return cp; } - (id)copy { return [self copyWithZone:nil]; } - (NSImage *)image { return [NSImage imageFromRImage:(RImage *)rimage]; } @end