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
  
348
  
349
  
350
  
351
  
352
  
353
  
354
  
355
  
356
  
357
  
358
  
359
  
360
  
361
  
362
  
363
  
364
  
365
  
366
  
367
  
368
  
369
  
370
  
371
  
372
  
373
  
374
  
375
  
376
  
377
  
378
  
379
  
380
  
381
  
382
  
383
  
384
  
385
  
386
  
387
  
388
  
389
  
390
  
391
  
392
  
393
  
394
  
395
  
396
  
397
  
398
  
399
  
400
  
401
  
402
  
403
  
404
  
405
  
406
  
407
  
408
  
409
  
410
  
411
  
412
  
413
  
414
  
415
  
416
  
417
  
418
  
419
  
420
  
421
  
422
  
423
  
424
  
425
  
426
  
427
  
428
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "global.h" 
 
#include <math.h> 
#include <ctype.h> 
 
#include "operators.h" 
#include "builtin_functions.h" 
#include "interpret.h" 
#include "svalue.h" 
#include "mapping.h" 
#include "pike_error.h" 
#include "pike_types.h" 
 
#include "image.h" 
 
#include "encodings.h" 
 
 
/*! @module Image 
 */ 
 
/*! @module ANY 
 *! 
 *!  These method calls other decoding methods 
 *!  and has some heuristics for what type of image 
 *!  this is. 
 *! 
 */ 
 
#define sp Pike_sp 
#define CHAR2(a,b) ((((unsigned char)(a))<<8)|((unsigned char)(b))) 
 
/* PNG module uses "type" for something else than what we want to use 
   it for.  Rename "type" to "_type", and insert our own "type"...  */ 
 
static void fix_png_mapping(void) 
{ 
  struct svalue *s; 
  if(TYPEOF(sp[-1]) != T_MAPPING) return; 
  if((s = low_mapping_string_lookup(sp[-1].u.mapping, literal_type_string))) { 
    /* s is a pointer into the mapping data, so we have to push it onto 
     * the stack, as mapping_insert might rehash, which would lead to a 
     * use after free. 
     */ 
    push_static_text("_type"); 
    push_svalue(s); 
    mapping_insert(sp[-3].u.mapping, &sp[-2], &sp[-1]); 
    pop_n_elems(2); 
  } 
  ref_push_string(literal_type_string); 
  push_static_text("image/png"); 
  mapping_insert(sp[-3].u.mapping, &sp[-2], &sp[-1]); 
  pop_n_elems(2); 
} 
 
 
static void call_resolved( const char *function ) 
{ 
  push_text(function); 
  APPLY_MASTER("resolv_or_error",1); 
  stack_swap(); 
  f_call_function(2); 
} 
 
/*! @decl mapping _decode(string data) 
 *! @decl object decode(string data) 
 *! @decl object decode_alpha(string data) 
 *! 
 *! Tries heuristics to find the correct method 
 *! of decoding the data, then calls that method. 
 *! 
 *! The result of _decode() is a mapping that contains 
 *! 
 *! @mapping 
 *!   @member string "type" 
 *!     File type information as MIME type (ie "image/jpeg" or similar) 
 *!   @member Image.Image "image" 
 *!     the image object 
 *!   @member int alpha 
 *!     the alpha channel or 0 if N/A 
 *! @endmapping 
 *! 
 *! @note 
 *!     Throws upon failure. 
 */ 
void image_any__decode(INT32 args) 
{ 
   if (args!=1 || TYPEOF(sp[-args]) != T_STRING) 
      Pike_error("Image.ANY.decode: illegal arguments\n"); 
 
   if (sp[-args].u.string->len<4) 
      Pike_error("Image.ANY.decode: too short string\n"); 
 
   /* ok, try the heuristics */ 
   switch (CHAR2(sp[-args].u.string->str[0],sp[-args].u.string->str[1])) 
   { 
      case CHAR2('P','1'): 
      case CHAR2('P','2'): 
      case CHAR2('P','3'): 
      case CHAR2('P','4'): 
      case CHAR2('P','5'): 
      case CHAR2('P','6'): 
      case CHAR2('P','7'): 
         /* ok, a PNM */ 
         img_pnm_decode(1); 
         push_static_text("image/x-pnm"); 
         goto simple_image; 
 
      case CHAR2(255,216): 
         /* JFIF */ 
         call_resolved("Image.JPEG._decode"); 
         return; 
 
      case CHAR2('g','i'): 
         /* XCF */ 
         call_resolved("Image.XCF._decode"); 
         return; 
 
      case CHAR2(137,'P'): 
         /* PNG */ 
         call_resolved("Image.PNG._decode"); 
         fix_png_mapping(); 
         return; 
 
      case CHAR2('G','I'): 
         /* GIF */ 
         call_resolved("Image.GIF.decode_map"); 
         return; 
 
      case CHAR2('8','B'): 
        /* Photoshop (8BPS) */ 
        call_resolved("Image.PSD._decode"); 
        return; 
 
      case CHAR2('F','O'): 
         /* ILBM (probably) */ 
         img_ilbm_decode(1); 
         push_static_text("image/x-ilbm"); 
         goto simple_image; 
 
      case CHAR2('I','I'):  /* Little endian. */ 
      case CHAR2('M','M'):  /* Big endian. */ 
        /* TIFF */ 
        call_resolved("Image.TIFF._decode"); 
        return; 
 
       case CHAR2('R','I'): 
        /* RIFF, used for WebP */ 
        call_resolved("Image.WebP._decode"); 
        return; 
 
      case CHAR2('B','M'): 
         /* BMP */ 
         img_bmp__decode(1); 
         return; 
 
      case CHAR2(0x59,0xa6): 
         /* RAS */ 
         img_ras_decode(1); 
         push_static_text("image/x-sun-raster"); 
         goto simple_image; 
 
      case CHAR2('P','V'): 
      case CHAR2('G','B'): 
         /* PVR */ 
         image_pvr_f__decode(1); 
         return; 
 
      case CHAR2(0x10,0): 
         /* TIM */ 
         image_tim_f__decode(1); 
         return; 
 
      case CHAR2(0xc5, 0xd0): 
        /* FIXME: DOS EPS Binary File Header. */ 
        goto unknown_format; 
        break; 
 
      case CHAR2(0,0): 
         switch (CHAR2(sp[-args].u.string->str[2],sp[-args].u.string->str[3])) 
         { 
            case CHAR2(0,'k'): 
               /* XWD */ 
               image_xwd__decode(1); 
               return; /* done */ 
         } 
 
         goto unknown_format; 
 
      default: 
        if( sp[-args].u.string->str[0] == 10 ) { 
          /* PCX */ 
          image_pcx_decode(1); 
          push_static_text("image/x-pcx"); 
          goto simple_image; 
        } 
unknown_format: 
         Pike_error("Unknown image format.\n"); 
   } 
 
simple_image: 
   /* on stack: object image,string type */ 
   f_aggregate(2); 
   push_static_text("image"); 
   ref_push_string(literal_type_string); 
   f_aggregate(2); 
   stack_swap(); 
   f_mkmapping(2); 
   return; 
} 
 
 
/*! @decl mapping decode_header(string data) 
 *! 
 *! Tries heuristics to find the correct method 
 *! of decoding the header, then calls that method. 
 *! 
 *! The resulting mapping depends on wich decode_header method that 
 *! is executed, but these keys will probably exist 
 *! 
 *! @mapping 
 *!   @member int "xsize" 
 *!   @member int "ysize" 
 *!       Size of image 
 *!   @member string "type" 
 *!     File type information as MIME type. 
 *!   @member string "color_space" 
 *!     Color space of image. 
 *! @endmapping 
 *! 
 *! @note 
 *!     Throws upon failure. 
 */ 
void image_any_decode_header(INT32 args) 
{ 
   if (args!=1 || TYPEOF(sp[-args]) != T_STRING) 
      Pike_error("Image.ANY.decode_header: illegal arguments\n"); 
 
   if (sp[-args].u.string->len<4) 
      Pike_error("Image.ANY.decode_header: too short string\n"); 
 
   /* ok, try the heuristics */ 
   switch (CHAR2(sp[-args].u.string->str[0],sp[-args].u.string->str[1])) 
   { 
      case CHAR2('P','1'): 
      case CHAR2('P','2'): 
      case CHAR2('P','3'): 
      case CHAR2('P','4'): 
      case CHAR2('P','5'): 
      case CHAR2('P','6'): 
      case CHAR2('P','7'): 
         Pike_error("Image.ANY.decode: decoding of PNM header unimplemented\n"); 
 
      case CHAR2(255,216): 
         /* JFIF */ 
        call_resolved("Image.JPEG.decode_header"); 
        return; 
 
      case CHAR2(137,'P'): 
         /* PNG */ 
         call_resolved("Image.PNG.decode_header"); 
         fix_png_mapping(); 
         return; 
 
      case CHAR2('g','i'): 
         /* XCF */ 
         call_resolved("Image.XCF._decode"); 
         return; 
 
      case CHAR2('G','I'): 
         /* GIF */ 
         call_resolved("Image.GIF.decode_map"); 
         return; 
 
      case CHAR2('F','O'): 
         Pike_error("Image.ANY.decode: decoding of ILBM header unimplemented\n"); 
 
      case CHAR2('I','I'):  /* Little endian. */ 
      case CHAR2('M','M'):  /* Big endian. */ 
        /* TIFF */ 
        call_resolved("Image.TIFF.decode_header"); 
        return; 
 
       case CHAR2('R','I'): 
           /* RIFF, used for WebP */ 
        call_resolved("Image.WebP._decode"); 
        return; 
 
      case CHAR2('B','M'): 
         /* BMP */ 
         img_bmp_decode_header(1); 
         return; 
 
      case CHAR2(0x59,0xa6): 
         /* RAS */ 
         Pike_error("Image.ANY.decode: decoding of RAS header unimplemented\n"); 
 
      case CHAR2('P','V'): 
      case CHAR2('G','B'): 
         /* PVR */ 
         image_pvr_f_decode_header(1); 
         return; 
 
      case CHAR2(0x10,0): 
         /* TIM */ 
         image_tim_f_decode_header(1); 
         return; 
 
      case CHAR2(0xc5, 0xd0): 
      case CHAR2('%','!'): 
        /* PS */ 
        call_resolved("Image.PS.decode_header"); 
        return; 
 
      case CHAR2(0,0): 
         switch (CHAR2(sp[-args].u.string->str[2],sp[-args].u.string->str[3])) 
         { 
            case CHAR2(0,'k'): 
               /* XWD */ 
               image_xwd_decode_header(1); 
               return; /* done */ 
         } 
 
         goto unknown_format; 
 
      default: 
unknown_format: 
         Pike_error("Unknown image format.\n"); 
   } 
} 
 
void image_any_decode(INT32 args) 
{ 
  char *resolve = NULL; 
 
  if (args!=1 || TYPEOF(sp[-args]) != T_STRING) 
      Pike_error("Image.ANY.decode: illegal arguments\n"); 
 
   if (sp[-args].u.string->len<4) 
      Pike_error("Image.ANY.decode: too short string\n"); 
 
   /* Heuristics for code with image only decoder, for increased 
      efficiency and safety. */ 
   switch (CHAR2(sp[-args].u.string->str[0],sp[-args].u.string->str[1])) 
   { 
   case CHAR2(255,216): 
     /* JFIF */ 
     resolve = "Image.JPEG.decode"; 
     break; 
 
   case CHAR2('g','i'): 
     /* XCF */ 
     resolve = "Image.XCF.decode"; 
     break; 
 
   case CHAR2('R','I'): 
     /* RIFF */ 
     resolve = "Image.WebP.decode"; 
     break; 
 
   case CHAR2('G','I'): 
     /* GIF */ 
     resolve = "Image.GIF.decode"; 
     break; 
 
   case CHAR2(137,'P'): 
     /* PNG */ 
     resolve = "Image.PNG.decode"; 
     break; 
 
   case CHAR2('8','B'): 
     /* Photoshop (8BPS) */ 
     resolve = "Image.PSD.decode"; 
     break; 
 
   case CHAR2('I','I'):     /* Little endian. */ 
   case CHAR2('M','M'):     /* Big endian. */ 
     /* TIFF */ 
     resolve = "Image.TIFF.decode"; 
     break; 
   } 
 
   if(resolve) 
   { 
     call_resolved( resolve ); 
     return; 
   } 
 
   image_any__decode(args); 
   push_static_text("image"); 
   f_index(2); 
} 
 
void image_any_decode_alpha(INT32 args) 
{ 
   image_any__decode(args); 
   push_static_text("alpha"); 
   f_index(2); 
} 
 
/*! @endmodule 
 */ 
 
/*! @endmodule 
 */ 
 
/** module *******************************************/ 
 
void init_image_any(void) 
{ 
  ADD_FUNCTION("_decode",image_any__decode,tFunc(tStr,tMapping), 0); 
  ADD_FUNCTION("decode_header",image_any_decode_header,tFunc(tStr,tMapping),0); 
 
  ADD_FUNCTION("decode",image_any_decode,tFunc(tStr,tObj),0); 
  ADD_FUNCTION("decode_alpha",image_any_decode_alpha,tFunc(tStr,tObj),0); 
} 
 
void exit_image_any(void) 
{ 
}