ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
(Generate patch)

Comparing CV/CV.xs (file contents):
Revision 1.45 by root, Fri Aug 29 18:35:25 2014 UTC vs.
Revision 1.57 by root, Mon Jul 30 22:55:46 2018 UTC

7#include <math.h> 7#include <math.h>
8 8
9#include <magic.h> 9#include <magic.h>
10 10
11#include <jpeglib.h> 11#include <jpeglib.h>
12#include <jerror.h>
13
12#include <glib.h> 14#include <glib.h>
13#include <gtk/gtk.h> 15#include <gtk/gtk.h>
16#include <gdk/gdkx.h>
14#include <gdk-pixbuf/gdk-pixbuf.h> 17#include <gdk-pixbuf/gdk-pixbuf.h>
15 18
16#include <gperl.h> 19#include <gperl.h>
17#include <gtk2perl.h> 20#include <gtk2perl.h>
18 21
19#include <assert.h> 22#include <assert.h>
20 23
24#if WEBP
25#include <webp/demux.h>
26#include <webp/decode.h>
27#endif
28
29#include "perlmulticore.h"
30
21#define IW 80 /* MUST match Schnauzer.pm! */ 31#define IW 80 /* MUST match Schnauzer.pm! */
22#define IH 60 /* MUST match Schnauzer.pm! */ 32#define IH 60 /* MUST match Schnauzer.pm! */
23 33
24#define RAND (seed = (seed + 7141) * 54773 % 134456) 34#define RAND (seed = (seed + 7141) * 54773 % 134456)
25 35
26#define LINELENGTH 240 36#define LINELENGTH 240
27 37
28#define ELLIPSIS "\xe2\x80\xa6" 38#define ELLIPSIS "\xe2\x80\xa6"
29 39
30typedef char *octet_string; 40typedef char *octet_string;
41
42static magic_t magic_cookie[2]; /* !mime, mime */
31 43
32struct jpg_err_mgr 44struct jpg_err_mgr
33{ 45{
34 struct jpeg_error_mgr err; 46 struct jpeg_error_mgr err;
35 jmp_buf setjmp_buffer; 47 jmp_buf setjmp_buffer;
36}; 48};
37 49
38static void 50static void
39cv_error_exit (j_common_ptr cinfo) 51cv_error_exit (j_common_ptr cinfo)
40{ 52{
53 cinfo->err->output_message (cinfo);
41 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); 54 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
42} 55}
43 56
44static void 57static void
45cv_error_output (j_common_ptr cinfo) 58cv_error_output (j_common_ptr cinfo)
46{ 59{
60 char msg[JMSG_LENGTH_MAX];
61
62 cinfo->err->format_message (cinfo, msg);
63
64 fprintf (stderr, "JPEG decoding error: %s\n", msg);
47 return; 65 return;
48} 66}
49 67
50static void 68static void
51rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, 69rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
148 if (!--a85_cnt) 166 if (!--a85_cnt)
149 { 167 {
150 a85_cnt = 4; 168 a85_cnt = 4;
151 if (a85_val) 169 if (a85_val)
152 { 170 {
153 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; 171 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
154 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; 172 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
155 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; 173 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
156 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; 174 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
157 a85_ptr[0] = (a85_val ) + 33; 175 a85_ptr[0] = (a85_val ) + 33;
158 176
159 a85_ptr += 5; 177 a85_ptr += 5;
160 } 178 }
183 201
184 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 202 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
185} 203}
186 204
187///////////////////////////////////////////////////////////////////////////// 205/////////////////////////////////////////////////////////////////////////////
206// memory source for libjpeg
207
208static void cv_ms_init (j_decompress_ptr cinfo)
209{
210}
211
212static void cv_ms_term (j_decompress_ptr cinfo)
213{
214}
215
216static boolean cv_ms_fill (j_decompress_ptr cinfo)
217{
218 // unexpected EOF, warn and generate fake EOI marker
219
220 WARNMS (cinfo, JWRN_JPEG_EOF);
221
222 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src;
223
224 static const JOCTET eoi[] = { 0xFF, JPEG_EOI };
225
226 src->next_input_byte = eoi;
227 src->bytes_in_buffer = sizeof (eoi);
228
229 return TRUE;
230}
231
232static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes)
233{
234 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src;
235
236 src->next_input_byte += num_bytes;
237 src->bytes_in_buffer -= num_bytes;
238}
239
240static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen)
241{
242 struct jpeg_source_mgr *src;
243
244 if (!cinfo->src)
245 cinfo->src = (struct jpeg_source_mgr *)
246 (*cinfo->mem->alloc_small) (
247 (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr)
248 );
249
250 src = (struct jpeg_source_mgr *)cinfo->src;
251 src->init_source = cv_ms_init;
252 src->fill_input_buffer = cv_ms_fill;
253 src->skip_input_data = cv_ms_skip;
254 src->resync_to_restart = jpeg_resync_to_restart;
255 src->term_source = cv_ms_term;
256 src->next_input_byte = (JOCTET *)buf;
257 src->bytes_in_buffer = buflen;
258}
259
260/////////////////////////////////////////////////////////////////////////////
188 261
189MODULE = Gtk2::CV PACKAGE = Gtk2::CV 262MODULE = Gtk2::CV PACKAGE = Gtk2::CV
190 263
191PROTOTYPES: ENABLE 264PROTOTYPES: ENABLE
192 265
266# calculate the common prefix length of two strings
193# missing function in perl. really :) 267# missing function in perl. really :)
194int 268int
195common_prefix_length (a, b) 269common_prefix_length (a, b)
196 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg); 270 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
197 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg); 271 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
206 280
207 OUTPUT: 281 OUTPUT:
208 RETVAL 282 RETVAL
209 283
210const char * 284const char *
211magic (octet_string path) 285magic (SV *path_or_data)
286 ALIAS:
287 magic = 0
288 magic_mime = 1
289 magic_buffer = 2
290 magic_buffer_mime = 3
212 CODE: 291 CODE:
213{ 292{
214 static magic_t cookie; 293 STRLEN len;
294 char *data = SvPVbyte (path_or_data, len);
215 295
216 if (!cookie) 296 if (!magic_cookie[0])
217 { 297 {
218 cookie = magic_open (MAGIC_SYMLINK); 298 magic_cookie[0] = magic_open (MAGIC_SYMLINK);
219 299 magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE);
220 if (cookie)
221 magic_load (cookie, 0); 300 magic_load (magic_cookie[0], 0);
222 else 301 magic_load (magic_cookie[1], 0);
223 XSRETURN_UNDEF;
224 }
225
226 RETVAL = magic_file (cookie, path);
227}
228 OUTPUT:
229 RETVAL
230
231const char *
232magic_mime (octet_string path)
233 CODE:
234{
235 static magic_t cookie;
236
237 if (!cookie)
238 { 302 }
239 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
240 303
241 if (cookie) 304 perlinterp_release ();
242 magic_load (cookie, 0);
243 else
244 XSRETURN_UNDEF;
245 }
246 305
247 RETVAL = magic_file (cookie, path); 306 RETVAL = ix & 2
307 ? magic_buffer (magic_cookie[ix & 1], data, len)
308 : magic_file (magic_cookie[ix & 1], data);
309
310 perlinterp_acquire ();
248} 311}
249 OUTPUT: 312 OUTPUT:
250 RETVAL 313 RETVAL
251 314
252# missing/broken in Gtk2 perl module 315# missing/broken in Gtk2 perl module
268 RETVAL 331 RETVAL
269 332
270GdkPixbuf_noinc * 333GdkPixbuf_noinc *
271dealpha_expose (GdkPixbuf *pb) 334dealpha_expose (GdkPixbuf *pb)
272 CODE: 335 CODE:
336 perlinterp_release ();
273{ 337{
274 int w = gdk_pixbuf_get_width (pb); 338 int w = gdk_pixbuf_get_width (pb);
275 int h = gdk_pixbuf_get_height (pb); 339 int h = gdk_pixbuf_get_height (pb);
276 int bpp = gdk_pixbuf_get_n_channels (pb); 340 int bpp = gdk_pixbuf_get_n_channels (pb);
277 int x, y, i; 341 int x, y, i;
286 for (x = 0; x < w; x++) 350 for (x = 0; x < w; x++)
287 for (y = 0; y < h; y++) 351 for (y = 0; y < h; y++)
288 for (i = 0; i < 3; i++) 352 for (i = 0; i < 3; i++)
289 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; 353 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
290} 354}
355 perlinterp_acquire ();
291 OUTPUT: 356 OUTPUT:
292 RETVAL 357 RETVAL
293 358
294GdkPixbuf_noinc * 359GdkPixbuf_noinc *
295rotate (GdkPixbuf *pb, int angle) 360rotate (GdkPixbuf *pb, int angle)
296 CODE: 361 CODE:
362 perlinterp_release ();
297 if (angle < 0) 363 if (angle < 0)
298 angle += 360; 364 angle += 360;
299 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE 365 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
300 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 366 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
301 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 367 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
302 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 368 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
303 : angle); 369 : angle);
370 perlinterp_acquire ();
304 OUTPUT: 371 OUTPUT:
305 RETVAL 372 RETVAL
306 373
374const char *
375filetype (SV *image_data)
376 CODE:
377{
378 STRLEN data_len;
379 U8 *data = SvPVbyte (image_data, data_len);
380
381 if (data_len >= 20
382 && data[0] == 0xff
383 && data[1] == 0xd8
384 && data[2] == 0xff)
385 RETVAL = "jpg";
386 else if (data_len >= 12
387 && data[ 0] == (U8)'R'
388 && data[ 1] == (U8)'I'
389 && data[ 2] == (U8)'F'
390 && data[ 3] == (U8)'F'
391 && data[ 8] == (U8)'W'
392 && data[ 9] == (U8)'E'
393 && data[10] == (U8)'B'
394 && data[11] == (U8)'P')
395 RETVAL = "webp";
396 else if (data_len >= 16
397 && data[ 0] == 0x89
398 && data[ 1] == (U8)'P'
399 && data[ 2] == (U8)'N'
400 && data[ 3] == (U8)'G'
401 && data[ 4] == 0x0d
402 && data[ 5] == 0x0a
403 && data[ 6] == 0x1a
404 && data[ 7] == 0x0a)
405 RETVAL = "png";
406 else
407 RETVAL = "other";
408}
409 OUTPUT:
410 RETVAL
411
307GdkPixbuf_noinc * 412GdkPixbuf_noinc *
308load_jpeg (SV *path, int thumbnail=0) 413decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
414 CODE:
415{
416#if WEBP
417 STRLEN data_size;
418 int alpha;
419 WebPData data;
420 WebPDemuxer *demux;
421 WebPIterator iter;
422 WebPDecoderConfig config;
423 int inw, inh;
424
425 data.bytes = (uint8_t *)SvPVbyte (image_data, data_size);
426 data.size = data_size;
427
428 perlinterp_release ();
429
430 RETVAL = 0;
431
432 if (!(demux = WebPDemux (&data)))
433 goto err_demux;
434
435 if (!WebPDemuxGetFrame (demux, 1, &iter))
436 goto err_iter;
437
438 if (!WebPInitDecoderConfig (&config))
439 goto err_iter;
440
441 config.options.use_threads = 1;
442
443 if (WebPGetFeatures (iter.fragment.bytes, iter.fragment.size, &config.input) != VP8_STATUS_OK)
444 goto err_iter;
445
446 inw = config.input.width;
447 inh = config.input.height;
448
449 if (thumbnail)
450 {
451 if (inw * ih > inh * iw)
452 ih = (iw * inh + inw - 1) / inw;
453 else
454 iw = (ih * inw + inh - 1) / inh;
455
456 config.options.bypass_filtering = 1;
457 config.options.no_fancy_upsampling = 1;
458
459 config.options.use_scaling = 1;
460 config.options.scaled_width = iw;
461 config.options.scaled_height = ih;
462 }
463 else
464 {
465 iw = inw;
466 ih = inh;
467 }
468
469 alpha = !!config.input.has_alpha;
470
471 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, iw, ih);
472 if (!RETVAL)
473 goto err_iter;
474
475 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB;
476 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL);
477 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL);
478 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL);
479 config.output.is_external_memory = 1;
480
481 if (WebPDecode (iter.fragment.bytes, iter.fragment.size, &config) != VP8_STATUS_OK)
482 {
483 g_object_unref (RETVAL);
484 RETVAL = 0;
485 goto err_iter;
486 }
487
488 err_iter:
489 WebPDemuxReleaseIterator (&iter);
490 err_demux:
491 WebPDemuxDelete (demux);
492
493 perlinterp_acquire ();
494#else
495 croak ("load_webp: webp not enabled at compile time");
496#endif
497}
498 OUTPUT:
499 RETVAL
500
501GdkPixbuf_noinc *
502decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
309 CODE: 503 CODE:
310{ 504{
311 struct jpeg_decompress_struct cinfo; 505 struct jpeg_decompress_struct cinfo;
312 struct jpg_err_mgr jerr; 506 struct jpg_err_mgr jerr;
313 guchar *data;
314 int rs; 507 int rs;
315 FILE *fp;
316 volatile GdkPixbuf *pb = 0; 508 volatile GdkPixbuf *pb = 0;
509 STRLEN data_len;
510 guchar *data = SvPVbyte (image_data, data_len);
317 511
318 RETVAL = 0; 512 RETVAL = 0;
319 513
320 fp = fopen (SvPVbyte_nolen (path), "rb"); 514 perlinterp_release ();
321
322 if (!fp)
323 XSRETURN_UNDEF;
324 515
325 cinfo.err = jpeg_std_error (&jerr.err); 516 cinfo.err = jpeg_std_error (&jerr.err);
326 517
327 jerr.err.error_exit = cv_error_exit; 518 jerr.err.error_exit = cv_error_exit;
328 jerr.err.output_message = cv_error_output; 519 jerr.err.output_message = cv_error_output;
329 520
330 if ((rs = setjmp (jerr.setjmp_buffer))) 521 if ((rs = setjmp (jerr.setjmp_buffer)))
331 { 522 {
332 fclose (fp);
333 jpeg_destroy_decompress (&cinfo); 523 jpeg_destroy_decompress (&cinfo);
334 524
335 if (pb) 525 if (pb)
336 g_object_unref ((gpointer)pb); 526 g_object_unref ((gpointer)pb);
337 527
528 perlinterp_acquire ();
338 XSRETURN_UNDEF; 529 XSRETURN_UNDEF;
339 } 530 }
340 531
532 if (!data_len)
533 longjmp (jerr.setjmp_buffer, 4);
534
341 jpeg_create_decompress (&cinfo); 535 jpeg_create_decompress (&cinfo);
536 cv_jpeg_mem_src (&cinfo, data, data_len);
342 537
343 jpeg_stdio_src (&cinfo, fp);
344 jpeg_read_header (&cinfo, TRUE); 538 jpeg_read_header (&cinfo, TRUE);
345 539
346 cinfo.dct_method = JDCT_DEFAULT; 540 cinfo.dct_method = JDCT_DEFAULT;
347 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ 541 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
348 cinfo.do_block_smoothing = FALSE; 542 cinfo.do_block_smoothing = FALSE;
358 { 552 {
359 cinfo.dct_method = JDCT_FASTEST; 553 cinfo.dct_method = JDCT_FASTEST;
360 cinfo.do_fancy_upsampling = FALSE; 554 cinfo.do_fancy_upsampling = FALSE;
361 555
362 while (cinfo.scale_denom < 8 556 while (cinfo.scale_denom < 8
363 && cinfo.output_width >= IW*4 557 && cinfo.output_width >= iw*4
364 && cinfo.output_height >= IH*4) 558 && cinfo.output_height >= ih*4)
365 { 559 {
366 cinfo.scale_denom <<= 1; 560 cinfo.scale_denom <<= 1;
367 jpeg_calc_output_dimensions (&cinfo); 561 jpeg_calc_output_dimensions (&cinfo);
368 } 562 }
369 } 563 }
370 564
565 if (cinfo.output_components != 3)
566 longjmp (jerr.setjmp_buffer, 3);
567
568 if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK)
569 {
570 cinfo.out_color_space = JCS_CMYK;
571 cinfo.output_components = 4;
572 }
573
371 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); 574 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height);
372 if (!RETVAL) 575 if (!RETVAL)
373 longjmp (jerr.setjmp_buffer, 2); 576 longjmp (jerr.setjmp_buffer, 2);
374 577
375 data = gdk_pixbuf_get_pixels (RETVAL); 578 data = gdk_pixbuf_get_pixels (RETVAL);
376 rs = gdk_pixbuf_get_rowstride (RETVAL); 579 rs = gdk_pixbuf_get_rowstride (RETVAL);
377
378 if (cinfo.output_components != 3)
379 longjmp (jerr.setjmp_buffer, 3);
380 580
381 jpeg_start_decompress (&cinfo); 581 jpeg_start_decompress (&cinfo);
382 582
383 while (cinfo.output_scanline < cinfo.output_height) 583 while (cinfo.output_scanline < cinfo.output_height)
384 { 584 {
391 rp [3] = (guchar *)rp [2] + rs; 591 rp [3] = (guchar *)rp [2] + rs;
392 592
393 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 593 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
394 } 594 }
395 595
596 if (cinfo.out_color_space == JCS_CMYK)
597 {
598 guchar *end = data + cinfo.output_height * rs;
599
600 while (data < end)
601 {
602 U32 c = data [0];
603 U32 m = data [1];
604 U32 y = data [2];
605 U32 k = data [3];
606
607 if (0)
608 if (cinfo.Adobe_transform == 2)
609 {
610 c ^= 0xff;
611 m ^= 0xff;
612 y ^= 0xff;
613 k ^= 0xff;
614 }
615
616 data [0] = (c * k + 0x80) / 0xff;
617 data [1] = (m * k + 0x80) / 0xff;
618 data [2] = (y * k + 0x80) / 0xff;
619 data [3] = 0xff;
620
621 data += 4;
622 }
623 }
624
396 jpeg_finish_decompress (&cinfo); 625 jpeg_finish_decompress (&cinfo);
397 fclose (fp);
398 jpeg_destroy_decompress (&cinfo); 626 jpeg_destroy_decompress (&cinfo);
627 perlinterp_acquire ();
399} 628}
400 OUTPUT: 629 OUTPUT:
401 RETVAL 630 RETVAL
402 631
403void 632void
404compare (GdkPixbuf *a, GdkPixbuf *b) 633compare (GdkPixbuf *a, GdkPixbuf *b)
405 PPCODE: 634 PPCODE:
635 perlinterp_release ();
406{ 636{
407 int w = gdk_pixbuf_get_width (a); 637 int w = gdk_pixbuf_get_width (a);
408 int h = gdk_pixbuf_get_height (a); 638 int h = gdk_pixbuf_get_height (a);
409 639
410 int sa = gdk_pixbuf_get_rowstride (a); 640 int sa = gdk_pixbuf_get_rowstride (a);
437 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 667 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
438 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 668 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
439 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 669 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
440 } 670 }
441 } 671 }
672
673 perlinterp_acquire ();
442 674
443 EXTEND (SP, 2); 675 EXTEND (SP, 2);
444 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 676 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
445 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 677 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
446} 678}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines