XS.xs

filter: Statementstmt 0.0% 0 / 364 Branchbran 0.0% 0 / 108 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 472 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Image/Info/XS.pm »
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 #include "ppport.h"
6  
7 #include "headers.h"
8  
9 #include <stdio.h>
10  
11 static const size_t BUF_SIZE = 102400;
12  
13 typedef enum {
14 UNKNOWN,
15 IMAGE_BMP,
16 IMAGE_GIF,
17 IMAGE_PNG,
18 IMAGE_PSD,
19 IMAGE_JPEG,
20 IMAGE_TIFF,
21 IMAGE_ICO
22 } ImageType;
23  
24 static const unsigned char bmp_sig[2] = {'B', 'M'};
25  
26 static const unsigned char gif_sig[3] = {'G', 'I', 'F'};
27 static const unsigned char jpg_sig[3] = { 0xFF, 0xD8, 0xFF };
28  
29 static const unsigned char psd_sig[4] = {'8', 'B', 'P', 'S'};
30 static const unsigned char iitiff_sig[4] = {'I', 'I', 0x2A, 0x00};
31 static const unsigned char mmtiff_sig[4] = {'M', 'M', 0x00, 0x2A};
32 static const unsigned char ico_sig[8] = { 0x00, 0x00, 0x01, 0x00};
33  
34 static const unsigned char png_sig[8] = { 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
35  
36  
37 static const char *bmp_compression[7] = {"None",
38 "RLE8",
39 "RLE4",
40 "BITFIELDS",
41 "JPEG",
42 "PNG"
43 };
44  
45 static const char *psd_color[10] = {"Mono",
46 "Grayscale",
47 "Indexed",
48 "RGB",
49 "CMYK",
50 NULL,
51 NULL,
52 "Multichannel"
53 "Duotone",
54 "LAB"
55 };
56
57 static const char *png_color[10] = {"Gray",
58 NULL,
59 "RGB",
60 "Indexed",
61 "GrayA",
62 NULL,
63 "RGBA"
64 };
65  
66 static const char *png_filter[5] = {"None",
67 "Sub",
68 "Up",
69 "Average",
70 "Paeth"
71 };
72  
73 static const char *tiff_color[11] = {"WhiteIsZero",
74 "BlackIsZero",
75 "RGB",
76 "Indexed",
77 "Transparency Mask",
78 "CMYK",
79 "YCbCr",
80 "CIE L*a*b*",
81 "ICC L*a*b*",
82 "ITU L*a*b*"
83 };
84
85  
86  
87  
88 0 static uint16_t get_uint16(const unsigned char * data, const unsigned short rev_order)
89 {
90 0 ▶ if (rev_order)
Branch: 1 branch
BranchTrueFalseTotal
if (rev_order) 0 0 0
91 0 return (data[0] << 8) | data[1];
92 else
93 0 return (data[1] << 8) | data[0];
94  
95 }
96  
97 0 static uint32_t get_uint32(const unsigned char * data, const unsigned short rev_order)
98 {
99 0 ▶ if (rev_order)
Branch: 1 branch
BranchTrueFalseTotal
if (rev_order) 0 0 0
100 0 return (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3];
101 else
102 0 return (data[3] << 24) | (data[2] << 16) | (data[1] << 8) | data[0];
103 }
104  
105  
106 0 ImageType get_image_type(const unsigned char *data, const size_t size)
107 {
108 // 2 bytes
109 0 ▶ if (size >= 2)
Branch: 1 branch
BranchTrueFalseTotal
if (size >= 2) 0 0 0
110 {
111 0 ▶ if (memcmp(data, bmp_sig, 2) == 0) return IMAGE_BMP;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, bmp_sig, 2) == 0) return IMAGE_BMP; 0 0 0
112 }
113  
114 0 ▶ if (size >= 3)
Branch: 1 branch
BranchTrueFalseTotal
if (size >= 3) 0 0 0
115 {
116 0 ▶ if (memcmp(data, gif_sig, 3) == 0) return IMAGE_GIF;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, gif_sig, 3) == 0) return IMAGE_GIF; 0 0 0
117  
118 0 ▶ if (memcmp(data, jpg_sig, 3) == 0) return IMAGE_JPEG;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, jpg_sig, 3) == 0) return IMAGE_JPEG; 0 0 0
119 }
120  
121 0 ▶ if (size >= 4)
Branch: 1 branch
BranchTrueFalseTotal
if (size >= 4) 0 0 0
122 {
123 0 ▶ if (memcmp(data, psd_sig, 4) == 0) return IMAGE_PSD;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, psd_sig, 4) == 0) return IMAGE_PSD; 0 0 0
124  
125 0 ▶ if (memcmp(data, iitiff_sig, 4) == 0) return IMAGE_TIFF;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, iitiff_sig, 4) == 0) return IMAGE_TIFF; 0 0 0
126  
127 0 ▶ if (memcmp(data, mmtiff_sig, 4) == 0) return IMAGE_TIFF;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, mmtiff_sig, 4) == 0) return IMAGE_TIFF; 0 0 0
128  
129 0 ▶ if (memcmp(data, ico_sig, 4) == 0) return IMAGE_ICO;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, ico_sig, 4) == 0) return IMAGE_ICO; 0 0 0
130 }
131  
132 0 ▶ if (size >= 8)
Branch: 1 branch
BranchTrueFalseTotal
if (size >= 8) 0 0 0
133 {
134 0 ▶ if (memcmp(data, png_sig, 8) == 0) { return IMAGE_PNG; }
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, png_sig, 8) == 0) { return IMAGE_PNG; } 0 0 0
135 }
136  
137 0 }
138  
139  
140 0 IV get_png_info(const unsigned char *data, const size_t size, HV *hash)
141 {
142 0 ▶ if (size < sizeof(ImageInfoPNG) + 16) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < sizeof(ImageInfoPNG) + 16) return 0; 0 0 0
143  
144 0 ImageInfoPNG *png = (ImageInfoPNG*) (data + 16);
145
146 0 uint32_t width = get_uint32(png->width, 1);
147 0 uint32_t height = get_uint32(png->height, 1);
148  
149 0 hv_store(hash, "file_media_type", 15, newSVpv("image/png", 0), 0);
150 0 hv_store(hash, "file_ext", 8, newSVpv("png", 0), 0);
151
152
153 0 hv_store(hash, "width", 5, newSViv(width), 0);
154 0 hv_store(hash, "height", 6, newSViv(height), 0);
155 0 hv_store(hash, "bits", 4, newSViv(png->depth), 0);
156
157 0 ▶ if (png->compression == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (png->compression == 0) 0 0 0
158 0 hv_store(hash, "compression", 11, newSVpv("Deflate", 0), 0);
159 else
160 0 hv_store(hash, "compression", 11, newSViv(png->compression), 0);
161
162 0 ▶ hv_store(hash, "interlace", 9, newSVpv(png->interlace == 1 ? "Adam7" : "None", 0), 0);
Branch: 1 branch
BranchTrueFalseTotal
hv_store(hash, "interlace", 9, newSVpv(png->interlace == 1 ? "Adam7" : "None", 0), 0); 0 0 0
163
164 0 ▶ if (png->filter < sizeof(png_filter) / sizeof(png_filter[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (png->filter < sizeof(png_filter) / sizeof(png_filter[0])) 0 0 0
165 0 hv_store(hash, "filter", 6, newSVpv(png_filter[png->filter], 0), 0);
166  
167 0 ▶ if (png->color_type < sizeof(png_color) / sizeof(png_color[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (png->color_type < sizeof(png_color) / sizeof(png_color[0])) 0 0 0
168 0 hv_store(hash, "color_type", 10, newSVpv(png_color[png->color_type], 0), 0);
169  
170 0 return 1;
171 }
172  
173 0 IV get_jpeg_info(const unsigned char *data, const size_t size, HV *hash)
174 {
175 0 unsigned long pos = 4;
176  
177 0 uint16_t block_length = get_uint16(data + pos, 1);
178  
179 0 hv_store(hash, "file_media_type", 15, newSVpv("image/jpeg", 0), 0);
180 0 hv_store(hash, "file_ext", 8, newSVpv("jpg", 0), 0);
181
182
183 0 short unsigned int jpeg_type = 0;
184
185 0 ▶ while(pos < size)
Branch: 1 branch
BranchTrueFalseTotal
while(pos < size) 0 0 0
186 {
187 0 pos += block_length;
188  
189 0 ▶ if (pos >= size) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (pos >= size) return 0; 0 0 0
190 0 ▶ if (data[pos] != 0xFF) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (data[pos] != 0xFF) return 0; 0 0 0
191
192 0 jpeg_type = data[pos + 1];
193
194 0 switch(jpeg_type)
195 {
196 0 case JPEG_TYPE_BASELINE:
197 0 hv_store(hash, "jpeg_type", 9, newSVpv("Baseline", 0), 0); break;
198 0 case JPEG_TYPE_EXTENDED_SEQUENTIAL:
199 0 hv_store(hash, "jpeg_type", 9, newSVpv("Extended sequential", 0), 0); break;
200 0 case JPEG_TYPE_PROGRESSIVE:
201 0 hv_store(hash, "jpeg_type", 9, newSVpv("Progressive", 0), 0); break;
202 0 case JPEG_TYPE_LOSSLESS:
203 0 hv_store(hash, "jpeg_type", 9, newSVpv("Lossless", 0), 0); break;
204 0 case JPEG_TYPE_DIFFERENTIAL_SEQUENTIAL:
205 0 hv_store(hash, "jpeg_type", 9, newSVpv("Differential sequential", 0), 0); break;
206 0 case JPEG_TYPE_DIFFERENTIAL_PROGRESSIVE:
207 0 hv_store(hash, "jpeg_type", 9, newSVpv("Differential progressive", 0), 0); break;
208 0 case JPEG_TYPE_DIFFERENTIAL_LOSSLESS:
209 0 hv_store(hash, "jpeg_type", 9, newSVpv("Differential lossless", 0), 0); break;
210 0 case JPEG_TYPE_EXTENDED_SEQUENTIAL_AC:
211 0 hv_store(hash, "jpeg_type", 9, newSVpv("Extended sequential, arithmetic coding", 0), 0); break;
212 0 case JPEG_TYPE_PROGRESSIVE_AC:
213 0 hv_store(hash, "jpeg_type", 9, newSVpv("Progressive, arithmetic coding", 0), 0); break;
214 0 case JPEG_TYPE_LOSSLESS_AC:
215 0 hv_store(hash, "jpeg_type", 9, newSVpv("Lossless, arithmetic coding", 0), 0); break;
216 0 case JPEG_TYPE_DIFFERENTIAL_SEQUENTIAL_AC:
217 0 hv_store(hash, "jpeg_type", 9, newSVpv("Differential sequential, arithmetic coding", 0), 0); break;
218 0 case JPEG_TYPE_DIFFERENTIAL_PROGRESSIVE_AC:
219 0 hv_store(hash, "jpeg_type", 9, newSVpv("Differential progressive, arithmetic coding", 0), 0); break;
220 0 case JPEG_TYPE_DIFFERENTIAL_LOSSLESS_AC:
221 0 hv_store(hash, "jpeg_type", 9, newSVpv("Differential lossless, arithmetic coding", 0), 0); break;
222 0 default:
223 0 jpeg_type = 0;
224 }
225  
226 0 ▶ if (jpeg_type > 0)
Branch: 1 branch
BranchTrueFalseTotal
if (jpeg_type > 0) 0 0 0
227 {
228 // 0xFFC? block: [0xFFC?][ushort length][uchar precision][ushort x][ushort y]
229 0 uint8_t bits = data[pos + 4];
230 0 uint16_t height = get_uint16(data + pos + 5, 1);
231 0 uint16_t width = get_uint16(data + pos + 7, 1);
232 0 uint8_t num_comp = data[pos + 9];
233
234 0 hv_store(hash, "width", 5, newSViv(width), 0);
235 0 hv_store(hash, "height", 6, newSViv(height), 0);
236 0 hv_store(hash, "bits", 4, newSViv(bits), 0);
237 0 hv_store(hash, "samples_per_pixel", 17, newSViv(num_comp), 0);
238
239
240 0 switch(num_comp)
241 {
242 0 case 1:
243 0 hv_store(hash, "color_type", 10, newSVpv("Gray", 0), 0); break;
244 0 case 3:
245 0 hv_store(hash, "color_type", 10, newSVpv("YCbCr", 0), 0); break; //or RGB ?
246 0 case 4:
247 0 hv_store(hash, "color_type", 10, newSVpv("CMYK", 0), 0); break; //or YCCK ?
248 }
249
250 0 return 1;
251 }
252 else
253 {
254 0 pos += 2;
255 // Next block
256 0 block_length = get_uint16(data + pos, 1);
257 }
258 }
259  
260 0 return 1;
261 }
262  
263 0 IV get_bmp_info(const unsigned char *data, const size_t size, HV *hash)
264 {
265 0 ▶ if (size < (14 + sizeof(ImageInfoBMP))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < (14 + sizeof(ImageInfoBMP))) return 0; 0 0 0
266
267 0 ImageInfoBMP *bmp = (ImageInfoBMP*) (data + 14);
268
269 0 hv_store(hash, "file_media_type", 15, newSVpv("image/bmp", 0), 0);
270 0 hv_store(hash, "file_ext", 8, newSVpv("bmp", 0), 0);
271
272 0 hv_store(hash, "width", 5, newSViv(bmp->biWidth), 0);
273 0 hv_store(hash, "height", 6, newSViv(bmp->biHeight), 0);
274 0 hv_store(hash, "bits", 4, newSViv(bmp->biBitCount), 0);
275 0 hv_store(hash, "x_pixels_per_meter", 18, newSViv(bmp->biXPelsPerMeter), 0);
276 0 hv_store(hash, "y_pixels_per_meter", 18, newSViv(bmp->biYPelsPerMeter), 0);
277 0 hv_store(hash, "colors_used", 11, newSViv(bmp->biClrUsed), 0);
278 0 hv_store(hash, "colors_important", 16, newSViv(bmp->biClrUsed), 0);
279
280 0 ▶ if (bmp->biCompression <= sizeof(bmp_compression)/sizeof(bmp_compression[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (bmp->biCompression <= sizeof(bmp_compression)/sizeof(bmp_compression[0])) 0 0 0
281 0 hv_store(hash, "compression", 11, newSVpv(bmp_compression[bmp->biCompression], 0), 0);
282  
283 0 ▶ if (bmp->biBitCount < 24)
Branch: 1 branch
BranchTrueFalseTotal
if (bmp->biBitCount < 24) 0 0 0
284 0 hv_store(hash, "color_type", 10, newSVpv("Indexed", 0), 0);
285 else
286 0 hv_store(hash, "color_type", 10, newSVpv("RGB", 0), 0);
287  
288 0 return 1;
289 }
290  
291 0 IV get_gif_info(const unsigned char *data, const size_t size, HV *hash)
292 {
293  
294 0 ▶ if (size < sizeof(ImageInfoGIF)) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < sizeof(ImageInfoGIF)) return 0; 0 0 0
295  
296 0 ImageInfoGIF *gif = (ImageInfoGIF*) (data);
297
298 0 hv_store(hash, "file_media_type", 15, newSVpv("image/gif", 0), 0);
299 0 hv_store(hash, "file_ext", 8, newSVpv("gif", 0), 0);
300
301 0 hv_store(hash, "width", 5, newSViv(gif->ScreenWidth), 0);
302 0 hv_store(hash, "height", 6, newSViv(gif->ScreenHeight), 0);
303 0 hv_store(hash, "color_type", 10, newSVpv("Indexed", 0), 0);
304
305 0 hv_store(hash, "sorted_colors", 13, newSViv((gif->Packed & 0x08) ? 1 : 0), 0);
306
307 0 hv_store(hash, "bits", 4, newSViv(((gif->Packed & 0x70) >> 4) + 1), 0);
308
309 0 unsigned short color_table_size = 1 << ((gif->Packed & 0x07) + 1);
310 0 hv_store(hash, "color_table_size", 16, newSViv(color_table_size), 0);
311
312 0 hv_store(hash, "background_color", 16, newSViv(gif->BackgroundColor), 0);
313 0 hv_store(hash, "version", 7, newSVpv(gif->Version, 3), 0);
314
315
316 0 ▶ if (gif->AspectRatio != 0)
Branch: 1 branch
BranchTrueFalseTotal
if (gif->AspectRatio != 0) 0 0 0
317 {
318 0 double aspect_ratio = ((double)(gif->AspectRatio + 15) / 64);
319 0 hv_store(hash, "aspect_ratio", 12, newSVnv(aspect_ratio), 0);
320 }
321
322 0 int pos = sizeof(ImageInfoGIF) + (color_table_size * 3);
323 0 ▶ if (size > pos)
Branch: 1 branch
BranchTrueFalseTotal
if (size > pos) 0 0 0
324 {
325 0 ▶ if (memcmp("89a", gif->Version, 3) == 0 && data[pos] == 0xF9)
Branch: 2 branches
BranchTrueFalseTotal
if (memcmp("89a", gif->Version, 3) == 0 && data[pos] == 0xF9) 0 0 0
if (memcmp("89a", gif->Version, 3) == 0 && data[pos] == 0xF9) 0 0 0
326 0 hv_store(hash, "animated", 8, newSViv(1), 0);
327 else
328 0 hv_store(hash, "animated", 8, newSViv(0), 0);
329 }
330 0 else return 0;
331
332 0 return 1;
333 }
334  
335 0 IV get_psd_info(const unsigned char *data, const size_t size, HV *hash)
336 {
337 0 ▶ if (size < sizeof(ImageInfoPSD) + 4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < sizeof(ImageInfoPSD) + 4) return 0; 0 0 0
338  
339 0 ImageInfoPSD *psd = (ImageInfoPSD*) (data + 4);
340
341 0 uint32_t width = get_uint32(psd->width, 1);
342 0 uint32_t height = get_uint32(psd->height, 1);
343 0 uint16_t depth = get_uint16(psd->depth, 1);
344 0 uint16_t color_type = get_uint16(psd->mode, 1);
345
346 0 hv_store(hash, "file_media_type", 15, newSVpv(" image/photoshop", 0), 0);
347 0 hv_store(hash, "file_ext", 8, newSVpv("psd", 0), 0);
348
349 0 hv_store(hash, "width", 5, newSViv(width), 0);
350 0 hv_store(hash, "height", 6, newSViv(height), 0);
351 0 hv_store(hash, "bits", 4, newSViv(depth), 0);
352
353 0 ▶ if (color_type < 10)
Branch: 1 branch
BranchTrueFalseTotal
if (color_type < 10) 0 0 0
354 0 hv_store(hash, "color_type", 10, newSVpv(psd_color[color_type], 0), 0);
355  
356 0 return 1;
357 }
358  
359 0 IV get_tiff_info(const unsigned char *data, const size_t size, HV *hash)
360 {
361 0 ▶ if (size > 8)
Branch: 1 branch
BranchTrueFalseTotal
if (size > 8) 0 0 0
362 {
363 unsigned short rev_order;
364 0 ▶ if (memcmp(data, iitiff_sig, 4) == 0) { rev_order = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(data, iitiff_sig, 4) == 0) { rev_order = 0; } 0 0 0
365 0 ▶ else if (memcmp(data, mmtiff_sig, 4) == 0) { rev_order = 1; }
Branch: 1 branch
BranchTrueFalseTotal
else if (memcmp(data, mmtiff_sig, 4) == 0) { rev_order = 1; } 0 0 0
366 0 else { return 0; }
367  
368 // Get offset to first IFD
369 0 const uint32_t IFDOffset = get_uint32(data + 4, rev_order);
370 0 ▶ if (IFDOffset > size) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (IFDOffset > size) { return 0; } 0 0 0
371  
372 // Get number of tags in IFD
373 0 const uint16_t tags = get_uint16(data + IFDOffset, rev_order);
374 0 const uint32_t directory_size = 2 + tags * 12 + 4;
375 0 ▶ if (IFDOffset + directory_size > size) { return 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (IFDOffset + directory_size > size) { return 0; } 0 0 0
376  
377 // Iterate through directory
378 0 unsigned long tag_num = 0;
379 0 ▶ for(tag_num = 0; tag_num < tags; ++tag_num)
Branch: 1 branch
BranchTrueFalseTotal
for(tag_num = 0; tag_num < tags; ++tag_num) 0 0 0
380 {
381 0 unsigned long tag_value = 0;
382 0 const unsigned long entry_offset = IFDOffset + 2 + tag_num * 12;
383  
384 0 const unsigned char *entry = data + entry_offset;
385 0 const uint16_t tag = get_uint16(entry + 0, rev_order);
386 0 const uint16_t tag_type = get_uint16(entry + 2, rev_order);
387
388 0 switch(tag_type)
389 {
390 0 case TIFF_TAG_FMT_BYTE:
391 case TIFF_TAG_FMT_SBYTE:
392 0 tag_value = entry[8];
393 0 break;
394 0 case TIFF_TAG_FMT_USHORT:
395 0 tag_value = get_uint16(entry + 8, rev_order);
396 0 break;
397 0 case TIFF_TAG_FMT_SSHORT:
398 0 tag_value = get_uint16(entry + 8, rev_order);
399 0 break;
400 0 case TIFF_TAG_FMT_ULONG:
401 0 tag_value = get_uint32(entry + 8, rev_order);
402 0 break;
403 0 case TIFF_TAG_FMT_SLONG:
404 0 tag_value = get_uint32(entry + 8, rev_order);
405 0 break;
406 0 default:
407 0 continue;
408 }
409  
410 0 switch(tag)
411 {
412 0 case TIFF_TAG_IMAGEWIDTH:
413 0 hv_store(hash, "width", 5, newSViv(tag_value), 0);
414 0 break;
415 0 case TIFF_TAG_COMP_IMAGEWIDTH:
416 0 hv_store(hash, "exif_width", 10, newSViv(tag_value), 0);
417 0 break;
418 0 case TIFF_TAG_IMAGEHEIGHT:
419 0 hv_store(hash, "height", 6, newSViv(tag_value), 0);
420 0 break;
421 0 case TIFF_TAG_COMP_IMAGEHEIGHT:
422 0 hv_store(hash, "exif_height", 11, newSViv(tag_value), 0);
423 0 break;
424 0 case TIFF_TAG_BITS:
425 // hv_store(hash, "bits", 4, newSViv(tag_value), 0);
426 0 break;
427 0 case TIFF_TAG_COMPRESSION:
428 0 switch(tag_value)
429 {
430 0 case TIFF_COMPRESSION_NONE:
431 0 hv_store(hash, "compression", 11, newSVpv("None",0), 0);
432 0 break;
433 0 case TIFF_COMPRESSION_CCITTRLE:
434 0 hv_store(hash, "compression", 11, newSVpv("CCITT modified Huffman RLE",0), 0);
435 0 break;
436 0 case TIFF_COMPRESSION_CCITTFAX3:
437 0 hv_store(hash, "compression", 11, newSVpv("CCITT Group 3 fax encoding",0), 0);
438 0 break;
439 0 case TIFF_COMPRESSION_CCITTFAX4:
440 0 hv_store(hash, "compression", 11, newSVpv("CCITT Group 4 fax encoding",0), 0);
441 0 break;
442 0 case TIFF_COMPRESSION_LZW:
443 0 hv_store(hash, "compression", 11, newSVpv("LZW",0), 0);
444 0 break;
445 0 case TIFF_COMPRESSION_OJPEG:
446 0 hv_store(hash, "compression", 11, newSVpv("Original JPEG / Old-style JPEG (6.0)",0), 0);
447 0 break;
448 0 case TIFF_COMPRESSION_JPEG:
449 0 hv_store(hash, "compression", 11, newSVpv("JPEG",0), 0);
450 0 break;
451 0 case TIFF_COMPRESSION_NEXT:
452 0 hv_store(hash, "compression", 11, newSVpv("NeXT 2-bit RLE",0), 0);
453 0 break;
454 0 case TIFF_COMPRESSION_CCITTRLEW:
455 0 hv_store(hash, "compression", 11, newSVpv("CCITT RLE",0), 0);
456 0 break;
457 0 case TIFF_COMPRESSION_PACKBITS:
458 0 hv_store(hash, "compression", 11, newSVpv("Macintosh RLE",0), 0);
459 0 break;
460 0 case TIFF_COMPRESSION_THUNDERSCAN:
461 0 hv_store(hash, "compression", 11, newSVpv("ThunderScan RLE",0), 0);
462 0 break;
463 0 case TIFF_COMPRESSION_IT8CTPAD:
464 0 hv_store(hash, "compression", 11, newSVpv("IT8 CT w/padding",0), 0);
465 0 break;
466 0 case TIFF_COMPRESSION_IT8LW:
467 0 hv_store(hash, "compression", 11, newSVpv("IT8 Linework RLE",0), 0);
468 0 break;
469 0 case TIFF_COMPRESSION_IT8MP:
470 0 hv_store(hash, "compression", 11, newSVpv("IT8 Monochrome picture",0), 0);
471 0 break;
472 0 case TIFF_COMPRESSION_IT8BL:
473 0 hv_store(hash, "compression", 11, newSVpv("IT8 Binary line art",0), 0);
474 0 break;
475 0 case TIFF_COMPRESSION_PIXARFILM:
476 0 hv_store(hash, "compression", 11, newSVpv("Pixar companded 10bit LZW",0), 0);
477 0 break;
478 0 case TIFF_COMPRESSION_PIXARLOG:
479 0 hv_store(hash, "compression", 11, newSVpv("Pixar companded 11bit ZIP",0), 0);
480 0 break;
481 0 case TIFF_COMPRESSION_DEFLATE:
482 0 hv_store(hash, "compression", 11, newSVpv("Deflate",0), 0);
483 0 break;
484 0 case TIFF_COMPRESSION_ADOBE_DEFLATE:
485 0 hv_store(hash, "compression", 11, newSVpv("Adobe deflate",0), 0);
486 0 break;
487 0 case TIFF_COMPRESSION_DCS:
488 0 hv_store(hash, "compression", 11, newSVpv("Kodak DCS",0), 0);
489 0 break;
490 0 case TIFF_COMPRESSION_JBIG:
491 0 hv_store(hash, "compression", 11, newSVpv("ISO JBIG",0), 0);
492 0 break;
493 0 case TIFF_COMPRESSION_SGILOG:
494 0 hv_store(hash, "compression", 11, newSVpv("SGI Log Luminance RLE",0), 0);
495 0 break;
496 0 case TIFF_COMPRESSION_SGILOG24:
497 0 hv_store(hash, "compression", 11, newSVpv("SGI Log 24-bit",0), 0);
498 0 break;
499 0 case TIFF_COMPRESSION_JP2000:
500 0 hv_store(hash, "compression", 11, newSVpv("JPEG2000",0), 0);
501 0 break;
502 }
503 0 break;
504 0 case TIFF_TAG_COLORTYPE:
505 0 ▶ if (tag_value <= sizeof(tiff_color) / sizeof(tiff_color[0]))
Branch: 1 branch
BranchTrueFalseTotal
if (tag_value <= sizeof(tiff_color) / sizeof(tiff_color[0])) 0 0 0
506 0 hv_store(hash, "color_type", 10, newSVpv(tiff_color[tag_value], 0), 0);
507 0 break;
508 }
509 }
510 }
511
512 0 hv_store(hash, "file_media_type", 15, newSVpv("image/tiff", 0), 0);
513 0 hv_store(hash, "file_ext", 8, newSVpv("tif", 0), 0);
514
515 0 return 1;
516 }
517  
518  
519 0 IV get_ico_info(const unsigned char *data, const size_t size, HV *hash)
520 {
521 0 ▶ if (size > 8)
Branch: 1 branch
BranchTrueFalseTotal
if (size > 8) 0 0 0
522 {
523 0 unsigned long pos = 4;
524  
525  
526 0 hv_store(hash, "file_media_type", 15, newSVpv("image/x-icon", 0), 0);
527 0 hv_store(hash, "file_ext", 8, newSVpv("ico", 0), 0);
528  
529 0 uint16_t icon_count = (data[pos + 1] << 8) | data[pos];
530  
531 0 hv_store(hash, "icon_count", 10, newSViv(icon_count), 0);
532  
533 0 unsigned long offset = (icon_count - 1) * sizeof(ImageInfoICO);
534
535 0 pos += offset + 2;
536
537 0 ▶ if (size < (4 + offset + sizeof(ImageInfoICO))) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (size < (4 + offset + sizeof(ImageInfoICO))) return 0; 0 0 0
538
539 0 ImageInfoICO *ico = (ImageInfoICO*) (data + pos);
540
541 0 hv_store(hash, "width", 5, newSViv(ico->width), 0);
542 0 hv_store(hash, "height", 6, newSViv(ico->height), 0);
543 0 hv_store(hash, "bits", 4, newSViv(ico->bitCount), 0);
544
545 0 ▶ if (ico->nColors == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ico->nColors == 0) 0 0 0
546 {
547 0 hv_store(hash, "color_type", 10, newSVpv("RGB", 0), 0);
548 0 hv_store(hash, "colors", 6, newSViv(256), 0);
549 }
550 else
551 {
552 0 hv_store(hash, "color_type", 10, newSVpv("Indexed", 0), 0);
553 0 hv_store(hash, "colors", 6, newSViv(ico->nColors), 0);
554 }
555 }
556
557 0 return 1;
558 }
559  
560  
561  
562 MODULE = Image::Info::XS PACKAGE = Image::Info::XS
563  
564 SV* image_info(source)
565 SV *source;
566 PREINIT:
567 size_t image_data_size;
568 unsigned char *image_data;
569 0 unsigned short from_file = 0;
570
571 CODE:
572 0 ▶ if (SvTYPE(source) == SVt_PV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(source) == SVt_PV) 0 0 0
573 {
574 0 FILE *f = fopen(SvPV_nolen(source), "r");
575 0 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 0 0 0
576 {
577 0 warn("File open error");
578 0 XSRETURN_UNDEF;
579 }
580
581 0 image_data = malloc(BUF_SIZE);
582 0 image_data_size = fread(image_data, 1, BUF_SIZE, f);
583
584 0 fclose(f);
585
586 0 from_file = 1;
587 }
588 0 ▶ else if (SvROK(source) && SvTYPE(SvRV(source)) == SVt_PV)
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(source) && SvTYPE(SvRV(source)) == SVt_PV) 0 0 0
else if (SvROK(source) && SvTYPE(SvRV(source)) == SVt_PV) 0 0 0
589 {
590 0 image_data_size = SvCUR(SvRV(source));
591 0 image_data = SvPV_nolen(SvRV(source));
592
593 }
594 0 else XSRETURN_UNDEF;
595
596 0 ImageType image_type = get_image_type(image_data, image_data_size);
597
598 0 HV *hash = newHV();
599
600 0 unsigned short result = 0;
601
602 0 switch(image_type)
603 {
604 0 case IMAGE_BMP: result = get_bmp_info(image_data, image_data_size, hash); break;
605  
606 0 case IMAGE_GIF: result = get_gif_info(image_data, image_data_size, hash);break;
607  
608 0 case IMAGE_PNG: result = get_png_info(image_data, image_data_size, hash); break;
609  
610 0 case IMAGE_PSD: result = get_psd_info(image_data, image_data_size, hash); break;
611  
612 0 case IMAGE_JPEG: result = get_jpeg_info(image_data, image_data_size, hash); break;
613  
614 0 case IMAGE_TIFF: result = get_tiff_info(image_data, image_data_size, hash); break;
615  
616 0 case IMAGE_ICO: result = get_ico_info(image_data, image_data_size, hash); break;
617  
618 }
619
620 0 ▶ if (from_file) free(image_data);
Branch: 1 branch
BranchTrueFalseTotal
if (from_file) free(image_data); 0 0 0
621
622 0 ▶ if (result == 1)
Branch: 1 branch
BranchTrueFalseTotal
if (result == 1) 0 0 0
623 0 RETVAL = newRV_noinc((SV*) hash);
624 else
625 {
626 0 SvREFCNT_dec((SV*) hash);
627 0 XSRETURN_UNDEF;
628 }
629 OUTPUT:
630 RETVAL
631  
632  
633 SV* image_type(source)
634 SV *source;
635 PREINIT:
636 size_t image_data_size;
637 unsigned char *image_data;
638 0 unsigned short from_file = 0;
639
640 CODE:
641 0 ▶ if (SvTYPE(source) == SVt_PV)
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(source) == SVt_PV) 0 0 0
642 {
643  
644 0 FILE *f = fopen(SvPV_nolen(source), "r");
645 0 ▶ if (!f)
Branch: 1 branch
BranchTrueFalseTotal
if (!f) 0 0 0
646 {
647 0 warn("File open error");
648 0 XSRETURN_UNDEF;
649 }
650
651 0 image_data = malloc(BUF_SIZE);
652 0 image_data_size = fread(image_data, 1, BUF_SIZE, f);
653
654 0 fclose(f);
655
656 0 from_file = 1;
657 }
658 0 ▶ if (SvROK(source) && SvTYPE(SvRV(source)) == SVt_PV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(source) && SvTYPE(SvRV(source)) == SVt_PV) 0 0 0
if (SvROK(source) && SvTYPE(SvRV(source)) == SVt_PV) 0 0 0
659 {
660 0 image_data_size = SvCUR(SvRV(source));
661 0 image_data = SvPV_nolen(SvRV(source));
662
663 }
664
665 0 ImageType image_type = get_image_type(image_data, image_data_size);
666
667 0 ▶ if (from_file) free(image_data);
Branch: 1 branch
BranchTrueFalseTotal
if (from_file) free(image_data); 0 0 0
668
669 0 RETVAL = NULL;
670
671 0 switch(image_type)
672 {
673 0 case IMAGE_BMP: RETVAL = newSVpv("BMP", 0); break;
674  
675 0 case IMAGE_GIF: RETVAL = newSVpv("GIF", 0); break;
676  
677 0 case IMAGE_PNG: RETVAL = newSVpv("PNG", 0); break;
678  
679 0 case IMAGE_PSD: RETVAL = newSVpv("PSD", 0); break;
680  
681 0 case IMAGE_JPEG: RETVAL = newSVpv("JPEG", 0); break;
682  
683 0 case IMAGE_TIFF: RETVAL = newSVpv("TIFF", 0); break;
684  
685 0 case IMAGE_ICO: RETVAL = newSVpv("ICO", 0); break;
686 }
687
688 OUTPUT:
689 RETVAL
↑ Summary blib/lib/Image/Info/XS.pm »