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
  
153
  
154
  
155
  
156
  
157
  
158
  
159
  
160
  
161
  
162
  
163
  
164
  
165
  
166
  
167
  
168
  
169
  
170
  
171
  
172
  
173
  
174
  
175
  
176
  
177
  
178
  
179
  
180
  
181
  
182
  
183
  
184
  
185
  
186
  
187
  
188
  
189
  
190
  
191
  
192
  
193
  
194
  
195
  
196
  
197
  
198
  
199
  
200
  
201
  
202
  
203
  
204
  
205
  
206
  
207
  
208
  
209
  
210
  
211
  
212
  
213
  
214
  
215
  
216
  
217
  
218
  
219
  
220
  
221
  
222
  
223
  
224
  
225
  
226
  
227
  
228
  
229
  
230
  
231
  
232
  
233
  
234
  
235
  
236
  
237
  
238
  
239
  
240
  
241
  
242
  
243
  
244
  
245
  
246
  
247
  
248
  
249
  
250
  
251
  
252
  
253
  
254
  
255
  
256
  
257
  
258
  
259
  
260
  
261
  
262
  
263
  
264
  
265
  
266
  
267
  
268
  
269
  
270
  
271
  
272
  
273
  
274
  
275
  
276
  
277
  
278
  
279
  
280
  
281
  
282
  
283
  
284
  
285
  
286
  
287
  
288
  
289
  
290
  
291
  
292
  
293
  
294
  
295
  
296
  
297
  
298
  
299
  
300
  
301
  
302
  
303
  
304
  
305
  
306
  
307
  
308
  
309
  
310
  
311
  
312
  
313
  
314
  
315
  
316
  
317
  
318
  
319
  
320
  
321
  
322
  
323
  
324
  
325
  
326
  
327
  
328
  
329
  
330
  
331
  
332
  
333
  
334
  
335
  
336
  
337
  
338
  
339
  
340
  
341
  
342
  
343
  
344
  
345
  
346
  
347
  
#pike __REAL_VERSION__ 
 
//! @appears Image.PS 
//! Codec for the Adobe page description language PostScript. 
//! Uses Ghostscript for decoding or built-in support. 
 
protected string find_in_path( string file ) 
{ 
  string path=getenv("PATH"); 
  foreach(path ? path/":" : ({}) , path) 
    if(file_stat(path=combine_path(path,file))) 
      return path; 
} 
 
//! Decodes the postscript @[data] into an image object 
//! using Ghostscript. 
//! @param options 
//!   Optional decoding parameters. 
//!   @mapping 
//!     @member int "dpi" 
//!       The resolution the image should be rendered in. 
//!       Defaults to 100. 
//!     @member string "device" 
//!       The selected Ghostscript device. Defaults to "ppmraw". 
//!     @member string "binary" 
//!       Path to the Ghostscript binary to be used. If missing 
//!       the environment paths will be searched for a file "gs" 
//!       to be used instead. 
//!     @member int(0..1) "force_gs" 
//!      Forces use of Ghostscript for EPS files instead 
//!      of Pikes native support. 
//!   @member int(0..1) "eps_crop" 
//!      Use -dEPSCrop option to Ghostscript to crop the 
//!      BoundingBox for a EPS file. 
//!   @member int(0..1) "cie_color" 
//!      Use -dUseCIEColor option to Ghostscript for 
//!      mapping color values through a CIE color space. 
//!     @member string "file" 
//!        Filename to read. If this is specified, it will be 
//!        passed along to the @tt{gs@} binary, so that it can 
//!        read the file directly. If this is specified @[data] 
//!        may be set to @expr{0@} (zero). 
//!   @endmapping 
//! 
//! @note 
//!   Some versions of @tt{gs@} on MacOS X have problems with 
//!   reading files on stdin. If this occurrs, try writing to 
//!   a plain file and specifying the @tt{file@} option. 
//! 
//! @note 
//!   @tt{gs@} versions 7.x and earlier don't support rendering 
//!   of EPSes if they are specified with the @tt{file@} option. 
//!   If this is a problem, upgrade to @tt{gs@} version 8.x or 
//!   later. 
object decode( string data, mapping|void options ) 
{ 
  if(!options) options = ([]); 
  if (data) { 
    if(has_prefix(data, "\xc5\xd0\xd3\xc6")) { 
      // DOS EPS Binary Header. 
      int ps_start, ps_len, meta_start, meta_len, tiff_start, tiff_len, sum; 
      sscanf(data, "%*4c%-4c%-4c%-4c%-4c%-4c%-4c%-2c", 
             ps_start, ps_len, meta_start, meta_len, 
             tiff_start, tiff_len, sum); 
#if constant(Image.TIFF.decode) 
      if (tiff_start && tiff_len) { 
        return Image.TIFF.decode(data[tiff_start..tiff_start + tiff_len-1]); 
      } 
#endif 
      data = data[ps_start..ps_start+ps_len-1]; 
    } 
    if(data[0..3] != "%!PS") 
      error("This is not a postscript file!\n"); 
 
    if(!options->force_gs) 
    { 
      if (has_prefix(data, "%!PS-Adobe-3.0 EPSF-3.0")) { 
        int width, height, bits, ncols; 
        int nbws, width2, encoding; 
        string init_tag; 
        if ((sscanf(data, 
                    "%*s%%ImageData:%*[ ]%d%*[ ]%d%*[ ]%d%*[ ]%d%" 
                    "*[ ]%d%*[ ]%d%*[ ]%d%*[ ]\"%s\"", 
                    width, height, bits, ncols, 
                    nbws, width2, encoding, init_tag) > 7) && 
            (width == width2) && (width > 0) && (height > 0) && (bits == 8) && 
            (< 1, 2 >)[encoding]) { 
          // Image data present. 
          int len; 
          string term; 
          string raw; 
          if ((sscanf(data, "%*s%%%%BeginBinary:%*[ ]%d%[\r\n]%s", 
                      len, term, raw) == 5) && 
              (len>0) && has_prefix(raw, init_tag + term)) { 
            raw = raw[sizeof(init_tag+term)..len-1]; 
            if (encoding == 2) { 
              raw = String.hex2string(raw - term); 
            } 
            if (sizeof(raw) == width*height*(ncols+nbws)) { 
              array(string) rows = raw/width; 
              if ((<3,4>)[ncols]) { 
                // RGB or CMYK image. 
                array(string) channels = allocate(ncols, ""); 
                int c; 
                for (c = 0; c < ncols; c++) { 
                  int i; 
                  for (i = c; i < sizeof(rows); i += ncols+nbws) { 
                    channels[c] += rows[i]; 
                  } 
                } 
                if (ncols == 3) { 
                  return Image.Image(width, height, "rgb", @channels); 
                } 
                return Image.Image(width, height, "adjusted_cmyk", @channels); 
              } 
              string grey = ""; 
              int i; 
              for(i = ncols; i < sizeof(rows); i += ncols+nbws) { 
                grey += rows[i]; 
              } 
              return Image.Image(width, height, "rgb", grey, grey, grey); 
            } 
          } 
        } 
      } 
    } 
  } 
 
  Stdio.File fd = Stdio.File(); 
  object fd2 = fd->pipe(); 
 
  Stdio.File fd3 = Stdio.File(); 
  object fd4 = fd3->pipe(); 
 
  array command = ({ 
    options->binary||find_in_path("gs")||("/bin/sh -c gs "), 
    "-quiet", 
    "-sDEVICE="+(options->device||"ppmraw"), 
    "-r"+(options->dpi||100), 
    "-dBATCH", 
    "-dNOPAUSE"}); 
 
  if(options->eps_crop) 
    command += ({"-dEPSCrop"}); 
 
  if(options->cie_color) 
    command += ({"-dUseCIEColor"}); 
 
  command += ({ 
    "-sOutputFile=-", 
    options->file || "-", 
    "-c", 
    "quit", 
  }); 
 
  Process.Process pid = Process.create_process( command, ([ 
    "stdin":fd, 
    "stdout":fd4, 
    "stderr":fd4, 
  ])); 
  fd->close(); 
  fd4->close(); 
 
  // Backend to use. 
  Pike.Backend be = Pike.Backend(); 
 
  // Kill the gs binary after 30 seconds in case it hangs. 
  mixed co = 
    be->call_out(lambda(Process.Process pid) { 
                   if (!pid->status()) { 
                     pid->kill(9); 
                   } 
                 }, 30, pid); 
  if(!options->file) 
  { 
    if(!has_value(data, "showpage")) 
      data += "\nshowpage\n"; 
    be->add_file(fd2); 
    Stdio.sendfile(({ data }), 0, 0, sizeof(data), 0, fd2, 
                   lambda(int n) { 
                     fd2->close(); 
                   }); 
  } else { 
    fd2->close(); 
  } 
  string output = ""; 
  be->add_file(fd3); 
  fd3->set_nonblocking(lambda(mixed ignored, string s) { 
                         output += s; 
                       }, 0, 
                       lambda() { 
                         fd3->close(); 
                         destruct(fd3); 
                       }); 
  mixed err = catch { 
    while (fd3) { 
      be(1.0); 
    } 
  }; 
#if 0 
  if (err) { 
    werror("Backend failed: %s\n", describe_backtrace(err)); 
  } 
#endif 
  be->remove_call_out(co); 
  int ret_code = pid->wait(); 
  if(ret_code) 
    error("Ghostscript failed with exit code: %O:\n%s\n", ret_code, output); 
  object i= Image.PNM.decode( output ); 
 
  if (data) { 
 
    if(data && sscanf(data, "%*s\n%%%%BoundingBox: %s\n", string bbox) == 2 && 
       !options->eps_crop) 
    { 
      int x0,x1,y0,y1; 
      sscanf(bbox, "%d %d %d %d", x0,y0,x1,y1 ); 
      int llx = (int)((x0/72.0) * (options->dpi||100)+0.01); 
      int lly = (int)((y0/72.0) * (options->dpi||100)+0.01); 
      int urx = (int)((x1/72.0) * (options->dpi||100)+0.01); 
      int ury = (int)((y1/72.0) * (options->dpi||100)+0.01); 
 
      if(urx && ury) 
        i = i->mirrory()->copy(llx,lly,urx,ury)->mirrory(); 
    } 
  } 
  return i; 
} 
 
//! Calls decode and returns the image in the "image" 
//! index of the mapping. This method is present for 
//! API reasons. 
mapping _decode( string data, mapping|void options ) 
{ 
  return ([ "image":decode( data,options ) ]); 
} 
 
 
 
//! Encodes the image object @[img] as a postscript 3.0 file. 
//! @param options 
//!   Optional extra encoding parameters. 
//!   @mapping 
//!     @member int "dpi" 
//!       The resolution of the encoded image. Defaults to 100. 
//!     @member int(0..1) "eps" 
//!       If the resulting image should be an eps instead of ps. 
//!       Defaults to 0, no. 
//!   @endmapping 
string encode(  object img, mapping|void options ) 
{ 
  int w = img->xsize(), h = img->ysize(); 
  string i = (string)img; 
  float scl = 72.0 / ((options&&options->dpi)||100); 
  img = 0; 
  string res; 
  res =("%!PS-Adobe-3.0\n" 
        "%%DocumentData: Clean8Bit\n" 
        "%%BoundingBox: 0 0 "+(int)ceil(w*scl)+" "+(int)ceil(h*scl)+"\n" 
        "%%EndComments\n" 
        "%%BeginProlog\n" 
        "30 dict begin\n" 
        "/tmpstr 256 string def\n" 
        "/dnl{currentfile tmpstr readline pop pop}bind def\n" 
        "/di{dnl gsave scale 2 copy pop string/pxdat exch def\n" 
        " 2 copy 8 [4 2 roll dup 0 0 4 2 roll neg 0 3 2 roll] {currentfile\n" 
        " pxdat readstring pop}false 3 colorimage grestore}bind def\n" 
        "%%EndProlog\n"); 
 
  res += sprintf("%d %d %f %f di\n", w, h, scl*w, scl*h); 
  res +="%%BeginData "+sizeof(i)+" Binary Bytes\n" + i +"\n%%EndData\n"; 
 
  if( !options || !options->eps ) 
    res += "showpage\n"; 
  res += "%%Trailer\n" "end\n" "%%EOF\n"; 
  return res; 
} 
 
//! Same as encode. Present for API reasons. 
function _encode = encode; 
 
//! Decodes the header of the postscript @[data] into a mapping. 
//! 
//! @mapping 
//!   @member int "xsize" 
//!   @member int "ysize" 
//!     Size of image 
//!   @member string "type" 
//!     File type information as MIME type. Always "application/postscript". 
//!   @member string "color_space" 
//!     Color space of image. "GRAYSCALE", "LAB", RGB", "CMYK" or "UNKNOWN" 
//!  @endmapping 
//! 
public mapping decode_header(string data) { 
  if(has_prefix(data, "\xc5\xd0\xd3\xc6")) { 
    // DOS EPS Binary Header. 
    int ps_start, ps_len, meta_start, meta_len, tiff_start, tiff_len, sum; 
    sscanf(data, "%*4c%-4c%-4c%-4c%-4c%-4c%-4c%-2c", 
           ps_start, ps_len, meta_start, meta_len, 
           tiff_start, tiff_len, sum); 
#if constant(Image.TIFF.decode_header) 
    if (tiff_start && tiff_len) { 
      return Image.TIFF.decode_header(data[tiff_start..tiff_start + tiff_len-1]); 
    } 
#endif 
    data = data[ps_start..ps_start+ps_len-1]; 
  } 
 
  if (has_prefix(data, "%!PS-Adobe-3.0 EPSF-3.0")) { 
    int width, height, bits, ncols; 
    int nbws, width2, encoding; 
    string init_tag; 
    if ((sscanf(data, 
                  "%*s%%ImageData:%*[ ]%d%*[ ]%d%*[ ]%d%*[ ]%d%" 
                  "*[ ]%d%*[ ]%d%*[ ]%d%*[ ]\"%s\"", 
                  width, height, bits, ncols, 
                  nbws, width2, encoding, init_tag) > 7) && 
          (width == width2) && (width > 0) && (height > 0) && (bits == 8) && 
          (< 1, 2 >)[encoding]) { 
            string color_space; 
            switch (ncols) { 
              case 1: 
                color_space = "GRAYSCALE"; 
                break; 
              case 2: 
                color_space = "LAB"; 
                break; 
              case 3: 
                color_space = "RGB"; 
                break; 
              case 4: 
                color_space = "CMYK"; 
              break; 
              default: 
                color_space = "UNKNOWN"; 
                break; 
            } 
        return ([ "type": "application/postscript", 
                  "xsize": width, 
                  "ysize": height, 
                  "color_space": color_space ]); 
      } 
    } 
  return 0; 
}