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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines