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

Comparing CV/CV.xs (file contents):
Revision 1.42 by root, Sun Sep 14 12:47:59 2008 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
22#include <assert.h>
23
24#if WEBP
25#include <webp/decode.h>
26#endif
27
28#include "perlmulticore.h"
29
19#define IW 80 /* MUST match Schnauzer.pm! */ 30#define IW 80 /* MUST match Schnauzer.pm! */
20#define IH 60 /* MUST match Schnauzer.pm! */ 31#define IH 60 /* MUST match Schnauzer.pm! */
21 32
22#define RAND (seed = (seed + 7141) * 54773 % 134456) 33#define RAND (seed = (seed + 7141) * 54773 % 134456)
23 34
24#define LINELENGTH 240 35#define LINELENGTH 240
25 36
26#define ELLIPSIS "\xe2\x80\xa6" 37#define ELLIPSIS "\xe2\x80\xa6"
27 38
28typedef char *octet_string; 39typedef char *octet_string;
40
41static magic_t magic_cookie[2]; /* !mime, mime */
29 42
30struct jpg_err_mgr 43struct jpg_err_mgr
31{ 44{
32 struct jpeg_error_mgr err; 45 struct jpeg_error_mgr err;
33 jmp_buf setjmp_buffer; 46 jmp_buf setjmp_buffer;
34}; 47};
35 48
36static void 49static void
37cv_error_exit (j_common_ptr cinfo) 50cv_error_exit (j_common_ptr cinfo)
38{ 51{
52 cinfo->err->output_message (cinfo);
39 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); 53 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
40} 54}
41 55
42static void 56static void
43cv_error_output (j_common_ptr cinfo) 57cv_error_output (j_common_ptr cinfo)
44{ 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);
45 return; 64 return;
46} 65}
47 66
48static void 67static void
49rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, 68rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
146 if (!--a85_cnt) 165 if (!--a85_cnt)
147 { 166 {
148 a85_cnt = 4; 167 a85_cnt = 4;
149 if (a85_val) 168 if (a85_val)
150 { 169 {
151 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; 170 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
152 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; 171 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
153 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; 172 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
154 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; 173 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
155 a85_ptr[0] = (a85_val ) + 33; 174 a85_ptr[0] = (a85_val ) + 33;
156 175
157 a85_ptr += 5; 176 a85_ptr += 5;
158 } 177 }
182 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 201 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
183} 202}
184 203
185///////////////////////////////////////////////////////////////////////////// 204/////////////////////////////////////////////////////////////////////////////
186 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
187MODULE = Gtk2::CV PACKAGE = Gtk2::CV 254MODULE = Gtk2::CV PACKAGE = Gtk2::CV
188 255
189PROTOTYPES: ENABLE 256PROTOTYPES: ENABLE
190 257
191# missing function in perl. really :) 258# missing function in perl. really :)
204 271
205 OUTPUT: 272 OUTPUT:
206 RETVAL 273 RETVAL
207 274
208const char * 275const char *
209magic (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
210 CODE: 282 CODE:
211{ 283{
212 static magic_t cookie; 284 STRLEN len;
285 char *data = SvPVbyte (path_or_data, len);
213 286
214 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);
215 { 293 }
216 cookie = magic_open (MAGIC_NONE);
217 294
218 if (cookie) 295 perlinterp_release ();
219 magic_load (cookie, 0);
220 else
221 XSRETURN_UNDEF;
222 }
223 296
224 RETVAL = magic_file (cookie, path);
225}
226 OUTPUT:
227 RETVAL 297 RETVAL = ix & 2
298 ? magic_buffer (magic_cookie[ix & 1], data, len)
299 : magic_file (magic_cookie[ix & 1], data);
228 300
229const char * 301 perlinterp_acquire ();
230magic_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} 302}
247 OUTPUT: 303 OUTPUT:
248 RETVAL 304 RETVAL
249 305
250# missing/broken in Gtk2 perl module 306# missing/broken in Gtk2 perl module
266 RETVAL 322 RETVAL
267 323
268GdkPixbuf_noinc * 324GdkPixbuf_noinc *
269dealpha_expose (GdkPixbuf *pb) 325dealpha_expose (GdkPixbuf *pb)
270 CODE: 326 CODE:
327 perlinterp_release ();
271{ 328{
272 int w = gdk_pixbuf_get_width (pb); 329 int w = gdk_pixbuf_get_width (pb);
273 int h = gdk_pixbuf_get_height (pb); 330 int h = gdk_pixbuf_get_height (pb);
274 int bpp = gdk_pixbuf_get_n_channels (pb); 331 int bpp = gdk_pixbuf_get_n_channels (pb);
275 int x, y, i; 332 int x, y, i;
284 for (x = 0; x < w; x++) 341 for (x = 0; x < w; x++)
285 for (y = 0; y < h; y++) 342 for (y = 0; y < h; y++)
286 for (i = 0; i < 3; i++) 343 for (i = 0; i < 3; i++)
287 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];
288} 345}
346 perlinterp_acquire ();
289 OUTPUT: 347 OUTPUT:
290 RETVAL 348 RETVAL
291 349
292GdkPixbuf_noinc * 350GdkPixbuf_noinc *
293rotate (GdkPixbuf *pb, int angle) 351rotate (GdkPixbuf *pb, int angle)
294 CODE: 352 CODE:
353 perlinterp_release ();
354 if (angle < 0)
355 angle += 360;
295 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
296 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE 357 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
297 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN 358 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
298 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE 359 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
299 : angle); 360 : angle);
361 perlinterp_acquire ();
300 OUTPUT: 362 OUTPUT:
301 RETVAL 363 RETVAL
302 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
303GdkPixbuf_noinc * 403GdkPixbuf_noinc *
304load_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)
305 CODE: 481 CODE:
306{ 482{
307 struct jpeg_decompress_struct cinfo; 483 struct jpeg_decompress_struct cinfo;
308 struct jpg_err_mgr jerr; 484 struct jpg_err_mgr jerr;
309 guchar *data;
310 int rs; 485 int rs;
311 FILE *fp;
312 volatile GdkPixbuf *pb = 0; 486 volatile GdkPixbuf *pb = 0;
487 STRLEN data_len;
488 guchar *data = SvPVbyte (image_data, data_len);
313 489
314 RETVAL = 0; 490 RETVAL = 0;
315 491
316 fp = fopen (SvPVbyte_nolen (path), "rb"); 492 perlinterp_release ();
317
318 if (!fp)
319 XSRETURN_UNDEF;
320 493
321 cinfo.err = jpeg_std_error (&jerr.err); 494 cinfo.err = jpeg_std_error (&jerr.err);
322 495
323 jerr.err.error_exit = cv_error_exit; 496 jerr.err.error_exit = cv_error_exit;
324 jerr.err.output_message = cv_error_output; 497 jerr.err.output_message = cv_error_output;
325 498
326 if ((rs = setjmp (jerr.setjmp_buffer))) 499 if ((rs = setjmp (jerr.setjmp_buffer)))
327 { 500 {
328 fclose (fp);
329 jpeg_destroy_decompress (&cinfo); 501 jpeg_destroy_decompress (&cinfo);
330 502
331 if (pb) 503 if (pb)
332 g_object_unref ((gpointer)pb); 504 g_object_unref ((gpointer)pb);
333 505
506 perlinterp_acquire ();
334 XSRETURN_UNDEF; 507 XSRETURN_UNDEF;
335 } 508 }
336 509
337 jpeg_create_decompress (&cinfo); 510 jpeg_create_decompress (&cinfo);
511 cv_jpeg_mem_src (&cinfo, data, data_len);
338 512
339 jpeg_stdio_src (&cinfo, fp);
340 jpeg_read_header (&cinfo, TRUE); 513 jpeg_read_header (&cinfo, TRUE);
341 514
342 cinfo.dct_method = JDCT_DEFAULT; 515 cinfo.dct_method = JDCT_DEFAULT;
343 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 */
344 cinfo.do_block_smoothing = FALSE; 517 cinfo.do_block_smoothing = FALSE;
354 { 527 {
355 cinfo.dct_method = JDCT_FASTEST; 528 cinfo.dct_method = JDCT_FASTEST;
356 cinfo.do_fancy_upsampling = FALSE; 529 cinfo.do_fancy_upsampling = FALSE;
357 530
358 while (cinfo.scale_denom < 8 531 while (cinfo.scale_denom < 8
359 && cinfo.output_width >= IW*4 532 && cinfo.output_width >= iw*4
360 && cinfo.output_height >= IH*4) 533 && cinfo.output_height >= ih*4)
361 { 534 {
362 cinfo.scale_denom <<= 1; 535 cinfo.scale_denom <<= 1;
363 jpeg_calc_output_dimensions (&cinfo); 536 jpeg_calc_output_dimensions (&cinfo);
364 } 537 }
365 } 538 }
366 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
367 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);
368 if (!RETVAL) 550 if (!RETVAL)
369 longjmp (jerr.setjmp_buffer, 2); 551 longjmp (jerr.setjmp_buffer, 2);
370 552
371 data = gdk_pixbuf_get_pixels (RETVAL); 553 data = gdk_pixbuf_get_pixels (RETVAL);
372 rs = gdk_pixbuf_get_rowstride (RETVAL); 554 rs = gdk_pixbuf_get_rowstride (RETVAL);
373
374 if (cinfo.output_components != 3)
375 longjmp (jerr.setjmp_buffer, 3);
376 555
377 jpeg_start_decompress (&cinfo); 556 jpeg_start_decompress (&cinfo);
378 557
379 while (cinfo.output_scanline < cinfo.output_height) 558 while (cinfo.output_scanline < cinfo.output_height)
380 { 559 {
387 rp [3] = (guchar *)rp [2] + rs; 566 rp [3] = (guchar *)rp [2] + rs;
388 567
389 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 568 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
390 } 569 }
391 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
392 jpeg_finish_decompress (&cinfo); 600 jpeg_finish_decompress (&cinfo);
393 fclose (fp);
394 jpeg_destroy_decompress (&cinfo); 601 jpeg_destroy_decompress (&cinfo);
602 perlinterp_acquire ();
395} 603}
396 OUTPUT: 604 OUTPUT:
397 RETVAL 605 RETVAL
398 606
399void 607void
400compare (GdkPixbuf *a, GdkPixbuf *b) 608compare (GdkPixbuf *a, GdkPixbuf *b)
401 PPCODE: 609 PPCODE:
610 perlinterp_release ();
402{ 611{
403 int w = gdk_pixbuf_get_width (a); 612 int w = gdk_pixbuf_get_width (a);
404 int h = gdk_pixbuf_get_height (a); 613 int h = gdk_pixbuf_get_height (a);
614
405 int sa = gdk_pixbuf_get_rowstride (a); 615 int sa = gdk_pixbuf_get_rowstride (a);
406 int sb = gdk_pixbuf_get_rowstride (b); 616 int sb = gdk_pixbuf_get_rowstride (b);
407 617
408 guchar *pa = gdk_pixbuf_get_pixels (a); 618 guchar *pa = gdk_pixbuf_get_pixels (a);
409 guchar *pb = gdk_pixbuf_get_pixels (b); 619 guchar *pb = gdk_pixbuf_get_pixels (b);
432 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));
433 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));
434 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));
435 } 645 }
436 } 646 }
647
648 perlinterp_acquire ();
437 649
438 EXTEND (SP, 2); 650 EXTEND (SP, 2);
439 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 651 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
440 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 652 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
441} 653}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines