~ubuntu-branches/ubuntu/dapper/xscreensaver/dapper

« back to all changes in this revision

Viewing changes to driver/pdf2jpeg.m

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2005-10-11 21:00:42 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20051011210042-u7q6zslgevdxspr3
Tags: 4.21-4ubuntu17
updated pt_BR again, fixed to UTF-8 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* pdf2jpeg -- converts a PDF file to a JPEG file, using Cocoa
 
2
 *
 
3
 * Copyright (c) 2003 by Jamie Zawinski <jwz@jwz.org>
 
4
 *
 
5
 * Permission to use, copy, modify, distribute, and sell this software and its
 
6
 * documentation for any purpose is hereby granted without fee, provided that
 
7
 * the above copyright notice appear in all copies and that both that
 
8
 * copyright notice and this permission notice appear in supporting
 
9
 * documentation.  No representations are made about the suitability of this
 
10
 * software for any purpose.  It is provided "as is" without express or 
 
11
 * implied warranty.
 
12
 *
 
13
 * Inspired by clues provided by Jan Kujawa and Jonathan Hendry.
 
14
 */
 
15
 
 
16
#import <Cocoa/Cocoa.h>
 
17
#include <stdio.h>
 
18
#include <stdlib.h>
 
19
 
 
20
int
 
21
main (int argc, char** argv)
 
22
{
 
23
  const char *progname = argv[0];
 
24
  const char *infile = 0, *outfile = 0;
 
25
  double compression = 0.85;
 
26
  int verbose = 0;
 
27
  int i;
 
28
 
 
29
  for (i = 1; i < argc; i++)
 
30
    {
 
31
      char c;
 
32
      if (argv[i][0] == '-' && argv[i][1] == '-')
 
33
        argv[i]++;
 
34
      if (!strcmp (argv[i], "-q") ||
 
35
          !strcmp (argv[i], "-qual") ||
 
36
          !strcmp (argv[i], "-quality"))
 
37
        {
 
38
          int q;
 
39
          if (1 != sscanf (argv[++i], " %d %c", &q, &c) ||
 
40
              q < 5 || q > 100)
 
41
            {
 
42
              fprintf (stderr, "%s: quality must be 5 - 100 (%d)\n",
 
43
                       progname, q);
 
44
              goto USAGE;
 
45
            }
 
46
          compression = q / 100.0;
 
47
        }
 
48
      else if (!strcmp (argv[i], "-verbose"))
 
49
        verbose++;
 
50
      else if (!strcmp (argv[i], "-v") ||
 
51
               !strcmp (argv[i], "-vv") ||
 
52
               !strcmp (argv[i], "-vvv"))
 
53
        verbose += strlen(argv[i])-1;
 
54
      else if (argv[i][0] == '-')
 
55
        {
 
56
          fprintf (stderr, "%s: unknown option %s\n", progname, argv[i]);
 
57
          goto USAGE;
 
58
        }
 
59
      else if (!infile)
 
60
        infile  = argv[i];
 
61
      else if (!outfile)
 
62
        outfile = argv[i];
 
63
      else
 
64
        {
 
65
        USAGE:
 
66
          fprintf (stderr,
 
67
                   "usage: %s [-verbose] [-quality NN] "
 
68
                   "infile.pdf outfile.jpg\n",
 
69
                   progname);
 
70
          exit (1);
 
71
        }
 
72
    }
 
73
 
 
74
  if (!infile || !outfile)
 
75
    goto USAGE;
 
76
 
 
77
 
 
78
  // Much of Cocoa needs one of these to be available.
 
79
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 
80
 
 
81
  //Need an NSApp instance to make [NSImage TIFFRepresentation] work
 
82
  NSApp = [NSApplication sharedApplication];
 
83
  [NSApp autorelease];
 
84
 
 
85
  if (verbose)
 
86
    fprintf (stderr, "%s: reading %s...\n", progname, infile);
 
87
 
 
88
  // Load the PDF file into an NSData object:
 
89
  NSData *pdf_data = [NSData dataWithContentsOfFile:
 
90
                               [NSString stringWithCString:infile]];
 
91
 
 
92
  // Create an NSPDFImageRep from the data:
 
93
  NSPDFImageRep *pdf_rep = [NSPDFImageRep imageRepWithData:pdf_data];
 
94
 
 
95
  // Create an NSImage instance
 
96
  NSImage *image = [[NSImage alloc] initWithSize:[pdf_rep size]];
 
97
 
 
98
  // Draw the PDFImageRep in the NSImage
 
99
  [image lockFocus];
 
100
  [pdf_rep drawAtPoint:NSMakePoint(0.0,0.0)];
 
101
  [image unlockFocus];
 
102
 
 
103
  // Load the NSImage's contents into an NSBitmapImageRep:
 
104
  NSBitmapImageRep *bit_rep = [NSBitmapImageRep
 
105
                                imageRepWithData:[image TIFFRepresentation]];
 
106
 
 
107
  // Write the bitmapImageRep to a JPEG file:
 
108
  if (bit_rep == nil)
 
109
    {
 
110
      fprintf (stderr, "%s: error converting image?\n", argv[0]);
 
111
      exit (1);
 
112
    }
 
113
 
 
114
  if (verbose)
 
115
    fprintf (stderr, "%s: writing %s (%d%% quality)...\n",
 
116
             progname, outfile, (int) (compression * 100));
 
117
 
 
118
  NSDictionary *props = [NSDictionary
 
119
                          dictionaryWithObject:
 
120
                            [NSNumber numberWithFloat:compression]
 
121
                          forKey:NSImageCompressionFactor];
 
122
  NSData *jpeg_data = [bit_rep representationUsingType:NSJPEGFileType
 
123
                               properties:props];
 
124
 
 
125
  [jpeg_data writeToFile:
 
126
               [NSString stringWithCString:outfile]
 
127
             atomically:YES];
 
128
  [image release];
 
129
 
 
130
  [pool release];
 
131
  exit (0);
 
132
}