Created
February 8, 2010 05:16
-
-
Save atr000/297895 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <Foundation/Foundation.h> | |
#import <AppKit/AppKit.h> | |
// I am printing a short hint about using croppic | |
void printUsage (void) { | |
NSArray *croppicArgs = [[NSArray alloc] initWithObjects:@"-w [width]", | |
@"-h [height]", | |
@"-x [x coordinate]", | |
@"-y [coordinate]", | |
@"-i [input image file path]", | |
@"-o [output image file path]", | |
@"-f [output image format]"]; | |
printf("\nUsage: croppic\n\n"); | |
NSEnumerator *enumerator = [croppicArgs objectEnumerator]; | |
id croppicArg; | |
while (croppicArg = [enumerator nextObject]) { | |
printf("\t%s\n", [croppicArg UTF8String]); | |
} | |
printf("\n\tValid output image formats: tiff, jpg, png\n\n"); | |
printf("\tExample: croppic -i /Users/steve/Desktop/input.tiff -o /Users/steve/Desktop/output.jpg -w 300 -h 200 -x 10 -y 20 -f jpg\n\n"); | |
[croppicArgs release]; | |
} | |
// I am actually doing the crop :-) | |
int main (int argc, const char * argv[]) { | |
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; | |
NSApplication *app = [NSApplication sharedApplication]; | |
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; | |
// did the user ask for help? | |
NSMutableArray *args = [NSMutableArray arrayWithArray:[[NSProcessInfo processInfo] arguments]]; | |
if ([args containsObject:@"-help"] || ([args count] < 2)) { | |
printUsage(); | |
return 0; | |
} | |
// validating user input | |
NSString *inputFilePath = [userDefaults stringForKey:@"i"]; | |
if (inputFilePath == NULL) { | |
printf("ERROR: Missing input image file path\n"); | |
return 1; | |
} | |
NSString *outputFilePath = [userDefaults stringForKey:@"o"]; | |
if (outputFilePath == NULL) { | |
printf("ERROR: Missing input image file path\n"); | |
return 1; | |
} | |
NSString *outputFormat = [userDefaults stringForKey:@"f"]; | |
if (outputFormat == NULL) { | |
printf("ERROR: Missing outout image format\n"); | |
return 1; | |
} else { | |
NSArray *validOutputFormats = [NSArray arrayWithObjects:@"tiff", @"jpg", @"png", nil]; | |
if (! [validOutputFormats containsObject:outputFormat]) { | |
printf("ERROR: Invalid output image format\n%s\n", [outputFormat UTF8String]); | |
return 1; | |
} | |
} | |
float outputImageWidth; | |
if ([userDefaults objectForKey:@"w"] == nil) { | |
printf("ERROR: Missing width\n"); | |
return 1; | |
} else { | |
outputImageWidth = [userDefaults floatForKey:@"w"]; | |
} | |
float outputImageHeight; | |
if ([userDefaults objectForKey:@"h"] == nil) { | |
printf("ERROR: Missing height\n"); | |
return 1; | |
} else { | |
outputImageHeight = [userDefaults floatForKey:@"h"]; | |
} | |
float xcoord; | |
if ([userDefaults objectForKey:@"x"] == nil) { | |
printf("ERROR: Missing x coordinate\n"); | |
return 1; | |
} else { | |
xcoord = [userDefaults floatForKey:@"x"]; | |
} | |
float ycoord; | |
if ([userDefaults objectForKey:@"y"] == nil) { | |
printf("ERROR: Missing y coordinate\n"); | |
return 1; | |
} else { | |
ycoord = [userDefaults floatForKey:@"y"]; | |
} | |
// does the given image input file path exist? | |
NSFileManager *fileManager = [NSFileManager defaultManager]; | |
BOOL isDir; | |
if (! [fileManager fileExistsAtPath:inputFilePath isDirectory:&isDir]) { | |
printf("ERROR: The given input image file path does not exist\n%s\n", [inputFilePath UTF8String]); | |
return 1; | |
} else { | |
if (isDir) { | |
printf("ERROR: The given input image file path points to a directory\n%s\n", [inputFilePath UTF8String]); | |
return 1; | |
} | |
} | |
// creating the input image object | |
NSImage *inputImage = [[NSImage alloc] initWithContentsOfFile:inputFilePath]; | |
if (inputImage == NULL) { | |
printf("ERROR: Loading the image failed\n%s\n", [inputFilePath UTF8String]); | |
return 1; | |
} | |
// creating the output image object | |
NSSize outputImageSize = NSMakeSize(outputImageWidth, outputImageHeight); | |
NSImage *outputImage = [[NSImage alloc] initWithSize:outputImageSize]; | |
// the rectangular the user wants to extract from the input image | |
NSRect inputRect = NSMakeRect(xcoord, ycoord, outputImageWidth, outputImageHeight); | |
// the rectangular we will be darwing to in our new output image | |
NSRect outputRect = NSMakeRect(0.0, 0.0, outputImageWidth, outputImageHeight); | |
[outputImage lockFocus]; | |
[inputImage drawInRect: outputRect | |
fromRect: inputRect | |
operation: NSCompositeSourceOver | |
fraction: 1.0]; | |
[outputImage unlockFocus]; | |
// writing the new output image file in the required format | |
NSData *imageData = [outputImage TIFFRepresentation]; | |
if ([outputFormat isEqualToString:@"jpg"]) { | |
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; | |
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:1.0] forKey:NSImageCompressionFactor]; | |
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps]; | |
} else if ([outputFormat isEqualToString:@"png"]) { | |
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; | |
imageData = [imageRep representationUsingType:NSPNGFileType properties:nil]; | |
} | |
BOOL writeSuccess = [imageData writeToFile:outputFilePath atomically:NO]; | |
if (! writeSuccess) { | |
printf("ERROR: Could not write output image file\n%s\n", [outputFilePath UTF8String]); | |
return 1; | |
} | |
// releasing the allocated and initialzed objects | |
[inputImage release]; | |
[outputImage release]; | |
[pool drain]; | |
// bye bye | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment