From http://www.jwz.org/xscreensaver/xscreensaver-5.32.tar.gz
[xscreensaver] / hacks / webcollage-helper-cocoa.m
index f05aee166f9d6ecf778a9bcddaec5f7077ff76c4..0a6e9e36be27890cc044bdd5b5b856e0b2102efa 100644 (file)
@@ -1,5 +1,5 @@
 /* webcollage-helper-cocoa --- scales and pastes one image into another
- * xscreensaver, Copyright (c) 2002-2009 Jamie Zawinski <jwz@jwz.org>
+ * xscreensaver, Copyright (c) 2002-2014 Jamie Zawinski <jwz@jwz.org>
  *
  * Permission to use, copy, modify, distribute, and sell this software and its
  * documentation for any purpose is hereby granted without fee, provided that
@@ -105,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);
 
@@ -114,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 firstObject];
+  image.size = NSMakeSize (rep.pixelsWide, rep.pixelsHigh);
+
   return image;
 }
 
@@ -214,6 +222,7 @@ bevel_image (NSImage *img, int bevel_pct,
                 fraction: 1.0];
   [img unlockFocus];
 
+  [rep release];
   [bevel_img release];
 
   if (verbose_p)
@@ -374,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];