… | |
… | |
9 | #include <magic.h> |
9 | #include <magic.h> |
10 | |
10 | |
11 | #include <jpeglib.h> |
11 | #include <jpeglib.h> |
12 | #include <glib.h> |
12 | #include <glib.h> |
13 | #include <gtk/gtk.h> |
13 | #include <gtk/gtk.h> |
|
|
14 | #include <gdk/gdkx.h> |
14 | #include <gdk-pixbuf/gdk-pixbuf.h> |
15 | #include <gdk-pixbuf/gdk-pixbuf.h> |
15 | |
16 | |
16 | #include <gperl.h> |
17 | #include <gperl.h> |
17 | #include <gtk2perl.h> |
18 | #include <gtk2perl.h> |
18 | |
19 | |
|
|
20 | #include <assert.h> |
|
|
21 | |
|
|
22 | #if WEBP |
|
|
23 | #include <webp/decode.h> |
|
|
24 | #endif |
|
|
25 | |
|
|
26 | #include "perlmulticore.h" |
|
|
27 | |
19 | #define IW 80 /* MUST match Schnauzer.pm! */ |
28 | #define IW 80 /* MUST match Schnauzer.pm! */ |
20 | #define IH 60 /* MUST match Schnauzer.pm! */ |
29 | #define IH 60 /* MUST match Schnauzer.pm! */ |
21 | |
30 | |
22 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
31 | #define RAND (seed = (seed + 7141) * 54773 % 134456) |
23 | |
32 | |
24 | #define LINELENGTH 240 |
33 | #define LINELENGTH 240 |
25 | |
34 | |
26 | #define ELLIPSIS "\xe2\x80\xa6" |
35 | #define ELLIPSIS "\xe2\x80\xa6" |
27 | |
36 | |
28 | typedef char *octet_string; |
37 | typedef char *octet_string; |
|
|
38 | |
|
|
39 | static magic_t magic_cookie[2]; /* !mime, mime */ |
29 | |
40 | |
30 | struct jpg_err_mgr |
41 | struct jpg_err_mgr |
31 | { |
42 | { |
32 | struct jpeg_error_mgr err; |
43 | struct jpeg_error_mgr err; |
33 | jmp_buf setjmp_buffer; |
44 | jmp_buf setjmp_buffer; |
34 | }; |
45 | }; |
35 | |
46 | |
36 | static void |
47 | static void |
37 | cv_error_exit (j_common_ptr cinfo) |
48 | cv_error_exit (j_common_ptr cinfo) |
38 | { |
49 | { |
|
|
50 | cinfo->err->output_message (cinfo); |
39 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
51 | longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); |
40 | } |
52 | } |
41 | |
53 | |
42 | static void |
54 | static void |
43 | cv_error_output (j_common_ptr cinfo) |
55 | cv_error_output (j_common_ptr cinfo) |
44 | { |
56 | { |
|
|
57 | char msg[JMSG_LENGTH_MAX]; |
|
|
58 | |
|
|
59 | cinfo->err->format_message (cinfo, msg); |
|
|
60 | |
|
|
61 | fprintf (stderr, "JPEG decoding error: %s\n", msg); |
45 | return; |
62 | return; |
46 | } |
63 | } |
47 | |
64 | |
48 | static void |
65 | static void |
49 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
66 | rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, |
… | |
… | |
146 | if (!--a85_cnt) |
163 | if (!--a85_cnt) |
147 | { |
164 | { |
148 | a85_cnt = 4; |
165 | a85_cnt = 4; |
149 | if (a85_val) |
166 | if (a85_val) |
150 | { |
167 | { |
151 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
168 | a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; |
152 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
169 | a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; |
153 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
170 | a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; |
154 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
171 | a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; |
155 | a85_ptr[0] = (a85_val ) + 33; |
172 | a85_ptr[0] = (a85_val ) + 33; |
156 | |
173 | |
157 | a85_ptr += 5; |
174 | a85_ptr += 5; |
158 | } |
175 | } |
… | |
… | |
204 | |
221 | |
205 | OUTPUT: |
222 | OUTPUT: |
206 | RETVAL |
223 | RETVAL |
207 | |
224 | |
208 | const char * |
225 | const char * |
209 | magic (octet_string path) |
226 | magic (SV *path_or_data) |
|
|
227 | ALIAS: |
|
|
228 | magic = 0 |
|
|
229 | magic_mime = 1 |
|
|
230 | magic_buffer = 2 |
|
|
231 | magic_buffer_mime = 3 |
210 | CODE: |
232 | CODE: |
211 | { |
233 | { |
212 | static magic_t cookie; |
234 | STRLEN len; |
|
|
235 | char *data = SvPVbyte (path_or_data, len); |
213 | |
236 | |
|
|
237 | perlinterp_release (); |
|
|
238 | |
214 | if (!cookie) |
239 | if (!magic_cookie[0]) |
|
|
240 | { |
|
|
241 | magic_cookie[0] = magic_open (MAGIC_SYMLINK); |
|
|
242 | magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME); |
215 | { |
243 | } |
216 | cookie = magic_open (MAGIC_NONE); |
|
|
217 | |
244 | |
218 | if (cookie) |
|
|
219 | magic_load (cookie, 0); |
|
|
220 | else |
|
|
221 | XSRETURN_UNDEF; |
|
|
222 | } |
|
|
223 | |
|
|
224 | RETVAL = magic_file (cookie, path); |
|
|
225 | } |
|
|
226 | OUTPUT: |
|
|
227 | RETVAL |
245 | RETVAL = ix & 2 |
|
|
246 | ? magic_buffer (magic_cookie[ix], data, len) |
|
|
247 | : magic_file (magic_cookie[ix], data); |
228 | |
248 | |
229 | const char * |
249 | perlinterp_acquire (); |
230 | magic_mime (octet_string path) |
|
|
231 | CODE: |
|
|
232 | { |
|
|
233 | static magic_t cookie; |
|
|
234 | |
|
|
235 | if (!cookie) |
|
|
236 | { |
|
|
237 | cookie = magic_open (MAGIC_MIME); |
|
|
238 | |
|
|
239 | if (cookie) |
|
|
240 | magic_load (cookie, 0); |
|
|
241 | else |
|
|
242 | XSRETURN_UNDEF; |
|
|
243 | } |
|
|
244 | |
|
|
245 | RETVAL = magic_file (cookie, path); |
|
|
246 | } |
250 | } |
247 | OUTPUT: |
251 | OUTPUT: |
248 | RETVAL |
252 | RETVAL |
249 | |
253 | |
250 | # missing/broken in Gtk2 perl module |
254 | # missing/broken in Gtk2 perl module |
… | |
… | |
266 | RETVAL |
270 | RETVAL |
267 | |
271 | |
268 | GdkPixbuf_noinc * |
272 | GdkPixbuf_noinc * |
269 | dealpha_expose (GdkPixbuf *pb) |
273 | dealpha_expose (GdkPixbuf *pb) |
270 | CODE: |
274 | CODE: |
|
|
275 | perlinterp_release (); |
271 | { |
276 | { |
272 | int w = gdk_pixbuf_get_width (pb); |
277 | int w = gdk_pixbuf_get_width (pb); |
273 | int h = gdk_pixbuf_get_height (pb); |
278 | int h = gdk_pixbuf_get_height (pb); |
274 | int bpp = gdk_pixbuf_get_n_channels (pb); |
279 | int bpp = gdk_pixbuf_get_n_channels (pb); |
275 | int x, y, i; |
280 | int x, y, i; |
… | |
… | |
284 | for (x = 0; x < w; x++) |
289 | for (x = 0; x < w; x++) |
285 | for (y = 0; y < h; y++) |
290 | for (y = 0; y < h; y++) |
286 | for (i = 0; i < 3; i++) |
291 | for (i = 0; i < 3; i++) |
287 | dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
292 | dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; |
288 | } |
293 | } |
|
|
294 | perlinterp_acquire (); |
289 | OUTPUT: |
295 | OUTPUT: |
290 | RETVAL |
296 | RETVAL |
291 | |
297 | |
292 | GdkPixbuf_noinc * |
298 | GdkPixbuf_noinc * |
293 | rotate (GdkPixbuf *pb, int angle) |
299 | rotate (GdkPixbuf *pb, int angle) |
294 | CODE: |
300 | CODE: |
|
|
301 | perlinterp_release (); |
|
|
302 | if (angle < 0) |
|
|
303 | angle += 360; |
295 | RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
304 | RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE |
296 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
305 | : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE |
297 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
306 | : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN |
298 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
307 | : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE |
299 | : angle); |
308 | : angle); |
|
|
309 | perlinterp_acquire (); |
300 | OUTPUT: |
310 | OUTPUT: |
301 | RETVAL |
311 | RETVAL |
302 | |
312 | |
303 | GdkPixbuf_noinc * |
313 | GdkPixbuf_noinc * |
304 | load_jpeg (SV *path, int thumbnail=0) |
314 | decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
|
|
315 | CODE: |
|
|
316 | { |
|
|
317 | #if WEBP |
|
|
318 | guchar *data; |
|
|
319 | STRLEN data_size; |
|
|
320 | int alpha; |
|
|
321 | WebPDecoderConfig config; |
|
|
322 | int inw, inh; |
|
|
323 | |
|
|
324 | data = SvPVbyte (image_data, data_size); |
|
|
325 | |
|
|
326 | perlinterp_release (); |
|
|
327 | |
|
|
328 | RETVAL = 0; |
|
|
329 | |
|
|
330 | if (!WebPInitDecoderConfig (&config)) |
|
|
331 | goto err; |
|
|
332 | |
|
|
333 | config.options.use_threads = 1; |
|
|
334 | |
|
|
335 | if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK) |
|
|
336 | goto err; |
|
|
337 | |
|
|
338 | inw = config.input.width; |
|
|
339 | inh = config.input.height; |
|
|
340 | |
|
|
341 | if (thumbnail) |
|
|
342 | { |
|
|
343 | if (inw * ih > inh * iw) |
|
|
344 | ih = (iw * inh + inw - 1) / inw; |
|
|
345 | else |
|
|
346 | iw = (ih * inw + inh - 1) / inh; |
|
|
347 | |
|
|
348 | config.options.bypass_filtering = 1; |
|
|
349 | config.options.no_fancy_upsampling = 1; |
|
|
350 | |
|
|
351 | config.options.use_scaling = 1; |
|
|
352 | config.options.scaled_width = iw; |
|
|
353 | config.options.scaled_height = ih; |
|
|
354 | } |
|
|
355 | else |
|
|
356 | { |
|
|
357 | iw = inw; |
|
|
358 | ih = inh; |
|
|
359 | } |
|
|
360 | |
|
|
361 | alpha = config.input.has_alpha; |
|
|
362 | |
|
|
363 | RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih); |
|
|
364 | if (!RETVAL) |
|
|
365 | goto err; |
|
|
366 | |
|
|
367 | config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB; |
|
|
368 | config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL); |
|
|
369 | config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL); |
|
|
370 | config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL); |
|
|
371 | config.output.is_external_memory = 1; |
|
|
372 | |
|
|
373 | if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK) |
|
|
374 | { |
|
|
375 | g_object_unref (RETVAL); |
|
|
376 | RETVAL = 0; |
|
|
377 | goto err; |
|
|
378 | } |
|
|
379 | |
|
|
380 | err: |
|
|
381 | perlinterp_acquire (); |
|
|
382 | #else |
|
|
383 | croak ("load_webp: webp not enabled at compile time"); |
|
|
384 | #endif |
|
|
385 | } |
|
|
386 | OUTPUT: |
|
|
387 | RETVAL |
|
|
388 | |
|
|
389 | GdkPixbuf_noinc * |
|
|
390 | load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) |
305 | CODE: |
391 | CODE: |
306 | { |
392 | { |
307 | struct jpeg_decompress_struct cinfo; |
393 | struct jpeg_decompress_struct cinfo; |
308 | struct jpg_err_mgr jerr; |
394 | struct jpg_err_mgr jerr; |
309 | guchar *data; |
395 | guchar *data; |
… | |
… | |
316 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
402 | fp = fopen (SvPVbyte_nolen (path), "rb"); |
317 | |
403 | |
318 | if (!fp) |
404 | if (!fp) |
319 | XSRETURN_UNDEF; |
405 | XSRETURN_UNDEF; |
320 | |
406 | |
|
|
407 | perlinterp_release (); |
|
|
408 | |
321 | cinfo.err = jpeg_std_error (&jerr.err); |
409 | cinfo.err = jpeg_std_error (&jerr.err); |
322 | |
410 | |
323 | jerr.err.error_exit = cv_error_exit; |
411 | jerr.err.error_exit = cv_error_exit; |
324 | jerr.err.output_message = cv_error_output; |
412 | jerr.err.output_message = cv_error_output; |
325 | |
413 | |
… | |
… | |
329 | jpeg_destroy_decompress (&cinfo); |
417 | jpeg_destroy_decompress (&cinfo); |
330 | |
418 | |
331 | if (pb) |
419 | if (pb) |
332 | g_object_unref ((gpointer)pb); |
420 | g_object_unref ((gpointer)pb); |
333 | |
421 | |
|
|
422 | perlinterp_acquire (); |
334 | XSRETURN_UNDEF; |
423 | XSRETURN_UNDEF; |
335 | } |
424 | } |
336 | |
425 | |
337 | jpeg_create_decompress (&cinfo); |
426 | jpeg_create_decompress (&cinfo); |
338 | |
427 | |
… | |
… | |
354 | { |
443 | { |
355 | cinfo.dct_method = JDCT_FASTEST; |
444 | cinfo.dct_method = JDCT_FASTEST; |
356 | cinfo.do_fancy_upsampling = FALSE; |
445 | cinfo.do_fancy_upsampling = FALSE; |
357 | |
446 | |
358 | while (cinfo.scale_denom < 8 |
447 | while (cinfo.scale_denom < 8 |
359 | && cinfo.output_width >= IW*4 |
448 | && cinfo.output_width >= iw*4 |
360 | && cinfo.output_height >= IH*4) |
449 | && cinfo.output_height >= ih*4) |
361 | { |
450 | { |
362 | cinfo.scale_denom <<= 1; |
451 | cinfo.scale_denom <<= 1; |
363 | jpeg_calc_output_dimensions (&cinfo); |
452 | jpeg_calc_output_dimensions (&cinfo); |
364 | } |
453 | } |
365 | } |
454 | } |
366 | |
455 | |
|
|
456 | if (cinfo.output_components != 3) |
|
|
457 | longjmp (jerr.setjmp_buffer, 3); |
|
|
458 | |
|
|
459 | if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK) |
|
|
460 | { |
|
|
461 | cinfo.out_color_space = JCS_CMYK; |
|
|
462 | cinfo.output_components = 4; |
|
|
463 | } |
|
|
464 | |
367 | pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); |
465 | pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height); |
368 | if (!RETVAL) |
466 | if (!RETVAL) |
369 | longjmp (jerr.setjmp_buffer, 2); |
467 | longjmp (jerr.setjmp_buffer, 2); |
370 | |
468 | |
371 | data = gdk_pixbuf_get_pixels (RETVAL); |
469 | data = gdk_pixbuf_get_pixels (RETVAL); |
372 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
470 | rs = gdk_pixbuf_get_rowstride (RETVAL); |
373 | |
|
|
374 | if (cinfo.output_components != 3) |
|
|
375 | longjmp (jerr.setjmp_buffer, 3); |
|
|
376 | |
471 | |
377 | jpeg_start_decompress (&cinfo); |
472 | jpeg_start_decompress (&cinfo); |
378 | |
473 | |
379 | while (cinfo.output_scanline < cinfo.output_height) |
474 | while (cinfo.output_scanline < cinfo.output_height) |
380 | { |
475 | { |
… | |
… | |
387 | rp [3] = (guchar *)rp [2] + rs; |
482 | rp [3] = (guchar *)rp [2] + rs; |
388 | |
483 | |
389 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
484 | jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); |
390 | } |
485 | } |
391 | |
486 | |
|
|
487 | if (cinfo.out_color_space == JCS_CMYK) |
|
|
488 | { |
|
|
489 | guchar *end = data + cinfo.output_height * rs; |
|
|
490 | |
|
|
491 | while (data < end) |
|
|
492 | { |
|
|
493 | U32 c = data [0]; |
|
|
494 | U32 m = data [1]; |
|
|
495 | U32 y = data [2]; |
|
|
496 | U32 k = data [3]; |
|
|
497 | |
|
|
498 | if (0) |
|
|
499 | if (cinfo.Adobe_transform == 2) |
|
|
500 | { |
|
|
501 | c ^= 0xff; |
|
|
502 | m ^= 0xff; |
|
|
503 | y ^= 0xff; |
|
|
504 | k ^= 0xff; |
|
|
505 | } |
|
|
506 | |
|
|
507 | data [0] = (c * k + 0x80) / 0xff; |
|
|
508 | data [1] = (m * k + 0x80) / 0xff; |
|
|
509 | data [2] = (y * k + 0x80) / 0xff; |
|
|
510 | data [3] = 0xff; |
|
|
511 | |
|
|
512 | data += 4; |
|
|
513 | } |
|
|
514 | } |
|
|
515 | |
392 | jpeg_finish_decompress (&cinfo); |
516 | jpeg_finish_decompress (&cinfo); |
393 | fclose (fp); |
517 | fclose (fp); |
394 | jpeg_destroy_decompress (&cinfo); |
518 | jpeg_destroy_decompress (&cinfo); |
|
|
519 | perlinterp_acquire (); |
395 | } |
520 | } |
396 | OUTPUT: |
521 | OUTPUT: |
397 | RETVAL |
522 | RETVAL |
398 | |
523 | |
399 | void |
524 | void |
400 | compare (GdkPixbuf *a, GdkPixbuf *b) |
525 | compare (GdkPixbuf *a, GdkPixbuf *b) |
401 | PPCODE: |
526 | PPCODE: |
|
|
527 | perlinterp_release (); |
402 | { |
528 | { |
403 | int w = gdk_pixbuf_get_width (a); |
529 | int w = gdk_pixbuf_get_width (a); |
404 | int h = gdk_pixbuf_get_height (a); |
530 | int h = gdk_pixbuf_get_height (a); |
|
|
531 | |
405 | int sa = gdk_pixbuf_get_rowstride (a); |
532 | int sa = gdk_pixbuf_get_rowstride (a); |
406 | int sb = gdk_pixbuf_get_rowstride (b); |
533 | int sb = gdk_pixbuf_get_rowstride (b); |
407 | |
534 | |
408 | guchar *pa = gdk_pixbuf_get_pixels (a); |
535 | guchar *pa = gdk_pixbuf_get_pixels (a); |
409 | guchar *pb = gdk_pixbuf_get_pixels (b); |
536 | guchar *pb = gdk_pixbuf_get_pixels (b); |
… | |
… | |
432 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
559 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
433 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
560 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
434 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
561 | d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); |
435 | } |
562 | } |
436 | } |
563 | } |
|
|
564 | |
|
|
565 | perlinterp_acquire (); |
437 | |
566 | |
438 | EXTEND (SP, 2); |
567 | EXTEND (SP, 2); |
439 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
568 | PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); |
440 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
569 | PUSHs (sv_2mortal (newSVnv (peak / 255.))); |
441 | } |
570 | } |