summaryrefslogtreecommitdiffstats
path: root/driver/pdf2jpeg.m
blob: d681b4a5424b58241679daf961bf1db66e035658 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* pdf2jpeg -- converts a PDF file to a JPEG file, using Cocoa
 *
 * 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
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation.  No representations are made about the suitability of this
 * software for any purpose.  It is provided "as is" without express or 
 * implied warranty.
 *
 * Inspired by clues provided by Jan Kujawa and Jonathan Hendry.
 */

#import <Cocoa/Cocoa.h>
#include <stdio.h>
#include <stdlib.h>

int
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;

  for (i = 1; i < argc; i++)
    {
      char c;
      if (argv[i][0] == '-' && argv[i][1] == '-')
        argv[i]++;
      if (!strcmp (argv[i], "-q") ||
          !strcmp (argv[i], "-qual") ||
          !strcmp (argv[i], "-quality"))
        {
          int q;
          if (1 != sscanf (argv[++i], " %d %c", &q, &c) ||
              q < 5 || q > 100)
            {
              fprintf (stderr, "%s: quality must be 5 - 100 (%d)\n",
                       progname, q);
              goto USAGE;
            }
          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") ||
               !strcmp (argv[i], "-vv") ||
               !strcmp (argv[i], "-vvv"))
        verbose += strlen(argv[i])-1;
      else if (argv[i][0] == '-')
        {
          fprintf (stderr, "%s: unknown option %s\n", progname, argv[i]);
          goto USAGE;
        }
      else if (!infile)
        infile  = argv[i];
      else if (!outfile)
        outfile = argv[i];
      else
        {
        USAGE:
          fprintf (stderr,
                   "usage: %s [-verbose] [-scale N] [-quality NN] "
                   "infile.pdf outfile.jpg\n",
                   progname);
          exit (1);
        }
    }

  if (!infile || !outfile)
    goto USAGE;


  // Much of Cocoa needs one of these to be available.
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

  //Need an NSApp instance to make [NSImage TIFFRepresentation] work
  NSApp = [NSApplication sharedApplication];
  [NSApp autorelease];

  if (verbose)
    fprintf (stderr, "%s: reading %s...\n", progname, infile);

  // Load the PDF file into an NSData object:
  NSData *pdf_data = [NSData dataWithContentsOfFile:
                               [NSString stringWithCString:infile
                                         encoding:NSUTF8StringEncoding]];

  // Create an NSPDFImageRep from the data:
  NSPDFImageRep *pdf_rep = [NSPDFImageRep imageRepWithData:pdf_data];

  // Create an NSImage instance
  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 drawInRect:rect];
  [image unlockFocus];

  // Load the NSImage's contents into an NSBitmapImageRep:
  NSBitmapImageRep *bit_rep = [NSBitmapImageRep
                                imageRepWithData:[image TIFFRepresentation]];

  // Write the bitmapImageRep to a JPEG file:
  if (bit_rep == nil)
    {
      fprintf (stderr, "%s: error converting image?\n", argv[0]);
      exit (1);
    }

  if (verbose)
    fprintf (stderr, "%s: writing %s (%d%% quality)...\n",
             progname, outfile, (int) (compression * 100));

  NSDictionary *props = [NSDictionary
                          dictionaryWithObject:
                            [NSNumber numberWithFloat:compression]
                          forKey:NSImageCompressionFactor];
  NSData *jpeg_data = [bit_rep representationUsingType:NSJPEGFileType
                               properties:props];

  [jpeg_data writeToFile:
               [NSString stringWithCString:outfile
                         encoding:NSUTF8StringEncoding]
             atomically:YES];
  [image release];

  [pool release];
  exit (0);
}