#import #include "rimageFromNSImage.h" #import "NSImageToRimage.h" void checkPool(); bool rimageFromNSImage( RImage &rimage, const std::string &path ) { checkPool(); NSString *sourcePath = [NSString stringWithCString:path.c_str()]; NSImage *image = [[[NSImage alloc] initWithContentsOfFile:sourcePath] autorelease]; if( image ) { RImage *tempImage = [image rimageRep]; if( tempImage ) { rimage = *tempImage; delete tempImage; return true; } } return false; } //static NSString *ExtensionForBitmapImageFileType( NSBitmapImageFileType bitmapImageFileType ) //{ // NSString *result = @""; // switch( bitmapImageFileType ) // { // case NSTIFFFileType: result = @".tiff"; break; // case NSBMPFileType: result = @".bmp"; break; // case NSGIFFileType: result = @".gif"; break; // case NSJPEGFileType: result = @".jpg"; break; // case NSPNGFileType: result = @".png"; break; // case NSJPEG2000FileType: result = @".jp2"; break; // } // return result; //} static NSBitmapImageFileType convertFiletypeToNS( CERT::Images::filetype format ) { NSBitmapImageFileType result = NSBMPFileType; switch( format ) { case CERT::Images::tiff: result = NSTIFFFileType; break; case CERT::Images::bmp: result = NSBMPFileType; break; case CERT::Images::gif: result = NSGIFFileType; break; case CERT::Images::jpeg: result = NSJPEGFileType; break; case CERT::Images::png: result = NSPNGFileType; break; case CERT::Images::jpeg2000: result = NSJPEG2000FileType; break; case CERT::Images::rimage: result = NSBMPFileType; break; case CERT::Images::end_types: break; } return result; } bool saveRimageToFile( RImage &rimage, const std::string &path, CERT::Images::filetype format ) { checkPool(); NSImage *image = [NSImage imageFromRImage:&rimage]; // NSImageCompressFactor is only valid for TIFF and JPEG types, // but doesn't seem to cause any trouble with the other file types. NSData *bitmapData = [NSBitmapImageRep representationOfImageRepsInArray:[image representations] usingType:convertFiletypeToNS(format) properties:[NSDictionary dictionaryWithObject:[NSDecimalNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]]; if( bitmapData && ([bitmapData length] > 0) ) { NSString *pathStr = [NSString stringWithCString:path.c_str()]; return [bitmapData writeToFile:pathStr atomically:true]; } return false; } static NSAutoreleasePool *pool = nil; void initForNSImage() { // Needed to add this otherwise trying to use tiff representations in NSImage threw an exception. // This needs to be an option, because if we try to run from the command line while not logged into the console we get error and die. // The try/catch block doesn't seem to work. /* Try comparing the current user to the owner of /dev/console, unless we're running as root: kalman:CERTWrapper $ ls -l /dev/console crw------- 1 asalamon asalamon 0, 0 Mar 6 17:35 /dev/console kalman:CERTWrapper$ whoami asalamon */ NSFileManager *fileManager = [NSFileManager defaultManager]; BOOL writable = [fileManager isWritableFileAtPath:@"/dev/console"]; // NSLog( @"Console is writable: %@", writable?@"YES":@"NO" ); if( writable ) [NSApplication sharedApplication]; // @try { // [NSApplication sharedApplication]; // } // @catch( NSException *exception ) // { // // Ignore it, I hope. // } pool = [[NSAutoreleasePool alloc] init]; } void releaseForNSImage() { [pool release]; pool = nil; } void checkPool() { static int cnt = 0; if( (0 != cnt) && ( 0 == (cnt % 100) ) ) { [pool release]; pool = [[NSAutoreleasePool alloc] init]; } ++cnt; }