http://www.jwz.org/xscreensaver/xscreensaver-5.11.tar.gz
[xscreensaver] / driver / pdf2jpeg.m
index 60778fe10c472169a07c062314ef2dbbcd9eb594..d681b4a5424b58241679daf961bf1db66e035658 100644 (file)
@@ -1,6 +1,6 @@
 /* pdf2jpeg -- converts a PDF file to a JPEG file, using Cocoa
  *
- * Copyright (c) 2003 by Jamie Zawinski <jwz@jwz.org>
+ * Copyright (c) 2003, 2008 by 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
@@ -23,6 +23,7 @@ main (int argc, char** argv)
   const char *progname = argv[0];
   const char *infile = 0, *outfile = 0;
   double compression = 0.85;
+  double scale = 1.0;
   int verbose = 0;
   int i;
 
@@ -45,6 +46,18 @@ main (int argc, char** argv)
             }
           compression = q / 100.0;
         }
+      else if (!strcmp (argv[i], "-scale"))
+        {
+          float s;
+          if (1 != sscanf (argv[++i], " %f %c", &s, &c) ||
+              s <= 0 || s > 50)
+            {
+              fprintf (stderr, "%s: scale must be 0.0 - 50.0 (%f)\n",
+                       progname, s);
+              goto USAGE;
+            }
+          scale = s;
+        }
       else if (!strcmp (argv[i], "-verbose"))
         verbose++;
       else if (!strcmp (argv[i], "-v") ||
@@ -64,7 +77,7 @@ main (int argc, char** argv)
         {
         USAGE:
           fprintf (stderr,
-                   "usage: %s [-verbose] [-quality NN] "
+                   "usage: %s [-verbose] [-scale N] [-quality NN] "
                    "infile.pdf outfile.jpg\n",
                    progname);
           exit (1);
@@ -87,17 +100,23 @@ main (int argc, char** argv)
 
   // Load the PDF file into an NSData object:
   NSData *pdf_data = [NSData dataWithContentsOfFile:
-                               [NSString stringWithCString:infile]];
+                               [NSString stringWithCString:infile
+                                         encoding:NSUTF8StringEncoding]];
 
   // Create an NSPDFImageRep from the data:
   NSPDFImageRep *pdf_rep = [NSPDFImageRep imageRepWithData:pdf_data];
 
   // Create an NSImage instance
-  NSImage *image = [[NSImage alloc] initWithSize:[pdf_rep size]];
+  NSRect rect;
+  rect.size = [pdf_rep size];
+  rect.size.width  *= scale;
+  rect.size.height *= scale;
+  rect.origin.x = rect.origin.y = 0;
+  NSImage *image = [[NSImage alloc] initWithSize:rect.size];
 
   // Draw the PDFImageRep in the NSImage
   [image lockFocus];
-  [pdf_rep drawAtPoint:NSMakePoint(0.0,0.0)];
+  [pdf_rep drawInRect:rect];
   [image unlockFocus];
 
   // Load the NSImage's contents into an NSBitmapImageRep:
@@ -123,7 +142,8 @@ main (int argc, char** argv)
                                properties:props];
 
   [jpeg_data writeToFile:
-               [NSString stringWithCString:outfile]
+               [NSString stringWithCString:outfile
+                         encoding:NSUTF8StringEncoding]
              atomically:YES];
   [image release];