X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fwebcollage-helper-cocoa.m;h=f85b7071f9258d6ba4d8308b2b22e9e445460a46;hb=refs%2Fremotes%2Fgithub%2Fmaster2;hp=05f95c4e2002f66b7253019e64cb1123d680e724;hpb=c494fd2e6b3b25582375d62e40f4f5cc984ca424;p=xscreensaver diff --git a/hacks/webcollage-helper-cocoa.m b/hacks/webcollage-helper-cocoa.m index 05f95c4e..f85b7071 100644 --- a/hacks/webcollage-helper-cocoa.m +++ b/hacks/webcollage-helper-cocoa.m @@ -1,5 +1,5 @@ /* webcollage-helper-cocoa --- scales and pastes one image into another - * xscreensaver, Copyright (c) 2002-2006 Jamie Zawinski + * xscreensaver, Copyright (c) 2002-2014 Jamie Zawinski * * Permission to use, copy, modify, distribute, and sell this software and its * documentation for any purpose is hereby granted without fee, provided that @@ -20,6 +20,13 @@ #include #include + +#if MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4 + typedef int NSInteger; + typedef unsigned int NSUInteger; +#endif + + char *progname; static int verbose_p = 0; @@ -98,7 +105,7 @@ load_image (const char *file) NSImage *image = [[NSImage alloc] initWithContentsOfFile: [NSString stringWithCString: file - encoding: kCFStringEncodingUTF8]]; + encoding: NSUTF8StringEncoding]]; if (! image) image = load_ppm_image (file); @@ -107,6 +114,14 @@ load_image (const char *file) exit (1); } + + // [NSImage size] defaults to the image size in points instead of pixels, + // so if an image file specified "pixels per inch" we can end up with + // absurdly sized images. Set it back to 1:1 pixel:point. + // + NSImageRep *rep = [image.representations objectAtIndex:0]; + image.size = NSMakeSize (rep.pixelsWide, rep.pixelsHigh); + return image; } @@ -142,7 +157,7 @@ bevel_image (NSImage *img, int bevel_pct, bytesPerRow: 0 bitsPerPixel: 0]; - int xx, yy; + NSInteger xx, yy; double *ramp = (double *) malloc (sizeof(*ramp) * (bevel_size + 1)); if (!ramp) @@ -183,7 +198,7 @@ bevel_image (NSImage *img, int bevel_pct, r = rx * ry; if (r != 1) { - unsigned int p[4]; + NSUInteger p[4]; p[0] = 0xFF * r; p[1] = p[2] = p[3] = 0xFF; [rep setPixel:p atX:xx y:yy]; @@ -207,6 +222,7 @@ bevel_image (NSImage *img, int bevel_pct, fraction: 1.0]; [img unlockFocus]; + [rep release]; [bevel_img release]; if (verbose_p) @@ -240,7 +256,7 @@ paste (const char *paste_file, progname, paste_file, paste_w, paste_h); } - if (bevel_pct > 0) + if (bevel_pct > 0 && paste_w > 5 && paste_h > 5) bevel_image (paste_img, bevel_pct, from_x, from_y, w, h, from_scale); @@ -296,7 +312,8 @@ write_image (NSImage *img, const char *file) properties:props]; [jpeg_data writeToFile: - [NSString stringWithCString:file] + [NSString stringWithCString:file + encoding:NSISOLatin1StringEncoding] atomically:YES]; if (verbose_p) @@ -366,13 +383,19 @@ main (int argc, char **argv) s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_x, &dummy)) usage(); s = argv[i++]; if (1 != sscanf (s, " %d %c", &to_y, &dummy)) usage(); s = argv[i++]; if (1 != sscanf (s, " %d %c", &w, &dummy)) usage(); - s = argv[i++]; if (1 != sscanf (s, " %d %c", &h, &dummy)) usage(); + s = argv[i]; if (1 != sscanf (s, " %d %c", &h, &dummy)) usage(); bevel_pct = 10; /* #### */ if (w < 0) usage(); if (h < 0) usage(); + if (w == 0 || h == 0 || + w > 10240 || h > 10240) { + fprintf (stderr, "%s: absurd size: %d x %d\n", progname, w, h); + exit (1); + } + // Much of Cocoa needs one of these to be available. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];