ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.43
Committed: Sun May 3 08:16:47 2009 UTC (15 years ago) by root
Branch: MAIN
CVS Tags: rel-1_55, rel-1_56
Changes since 1.42: +3 -0 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <string.h>
6 #include <setjmp.h>
7 #include <math.h>
8
9 #include <magic.h>
10
11 #include <jpeglib.h>
12 #include <glib.h>
13 #include <gtk/gtk.h>
14 #include <gdk-pixbuf/gdk-pixbuf.h>
15
16 #include <gperl.h>
17 #include <gtk2perl.h>
18
19 #include <assert.h>
20
21 #define IW 80 /* MUST match Schnauzer.pm! */
22 #define IH 60 /* MUST match Schnauzer.pm! */
23
24 #define RAND (seed = (seed + 7141) * 54773 % 134456)
25
26 #define LINELENGTH 240
27
28 #define ELLIPSIS "\xe2\x80\xa6"
29
30 typedef char *octet_string;
31
32 struct jpg_err_mgr
33 {
34 struct jpeg_error_mgr err;
35 jmp_buf setjmp_buffer;
36 };
37
38 static void
39 cv_error_exit (j_common_ptr cinfo)
40 {
41 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
42 }
43
44 static void
45 cv_error_output (j_common_ptr cinfo)
46 {
47 return;
48 }
49
50 static void
51 rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
52 unsigned int *h, unsigned int *s, unsigned int *v)
53 {
54 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
55 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
56 unsigned int delta = mx - mn;
57
58 *v = mx;
59
60 *s = mx ? delta * 255 / mx : 0;
61
62 if (delta == 0)
63 *h = 0;
64 else
65 {
66 if (r == mx)
67 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
68 else if (g == mx)
69 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
70 else if (b == mx)
71 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
72
73 *h &= 255;
74 }
75 }
76
77 struct feature {
78 float v1, v2, v3; // mean, square, cube
79 int n;
80 };
81
82 static void
83 feature_init (struct feature *f)
84 {
85 f->v1 = 0.;
86 f->v2 = 0.;
87 f->v3 = 0.;
88 f->n = 0;
89 }
90
91 // didn't find an algorithm to neatly do mean, variance and skew in one pass.
92 // elmex ist schuld.
93 static void
94 feature_update_pass_1 (struct feature *f, unsigned int v)
95 {
96 f->v1 += v;
97 f->n += 1;
98 }
99
100 static void
101 feature_finish_pass_1 (struct feature *f)
102 {
103 if (f->n < 1)
104 return;
105
106 f->v1 /= f->n;
107 }
108
109 static void
110 feature_update_pass_2 (struct feature *f, unsigned int v)
111 {
112 float d = v - f->v1;
113
114 f->v2 += d * d;
115 f->v3 += d * d * d;
116 }
117
118 static void
119 feature_finish_pass_2 (struct feature *f)
120 {
121 if (f->n < 1)
122 return;
123
124 f->v2 /= f->n;
125 f->v3 /= f->n;
126
127 f->v1 /= 255.;
128 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
129 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
130 }
131
132 static guint32 a85_val;
133 static guint a85_cnt;
134 static guchar a85_buf[LINELENGTH], *a85_ptr;
135
136 static void
137 a85_init (void)
138 {
139 a85_cnt = 4;
140 a85_ptr = a85_buf;
141 }
142
143 static void
144 a85_push (PerlIO *fp, guchar c)
145 {
146 a85_val = a85_val << 8 | c;
147
148 if (!--a85_cnt)
149 {
150 a85_cnt = 4;
151 if (a85_val)
152 {
153 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
154 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
155 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
156 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
157 a85_ptr[0] = (a85_val ) + 33;
158
159 a85_ptr += 5;
160 }
161 else
162 *a85_ptr++ = 'z';
163
164 if (a85_ptr >= a85_buf + sizeof (a85_buf) - 7)
165 {
166 *a85_ptr++ = '\n';
167 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
168 a85_ptr = a85_buf;
169 }
170 }
171
172 }
173
174 static void
175 a85_finish (PerlIO *fp)
176 {
177 while (a85_cnt != 4)
178 a85_push (fp, 0);
179
180 *a85_ptr++ = '~'; // probably buggy end-marker
181 *a85_ptr++ = '>'; // probably buggy end-marker
182 *a85_ptr++ = '\n';
183
184 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
185 }
186
187 /////////////////////////////////////////////////////////////////////////////
188
189 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
190
191 PROTOTYPES: ENABLE
192
193 # missing function in perl. really :)
194 int
195 common_prefix_length (a, b)
196 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
197 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
198 CODE:
199 RETVAL = 0;
200
201 while (*a == *b && *a)
202 {
203 RETVAL += (*a & 0xc0) != 0x80;
204 a++, b++;
205 }
206
207 OUTPUT:
208 RETVAL
209
210 const char *
211 magic (octet_string path)
212 CODE:
213 {
214 static magic_t cookie;
215
216 if (!cookie)
217 {
218 cookie = magic_open (MAGIC_NONE);
219
220 if (cookie)
221 magic_load (cookie, 0);
222 else
223 XSRETURN_UNDEF;
224 }
225
226 RETVAL = magic_file (cookie, path);
227 }
228 OUTPUT:
229 RETVAL
230
231 const char *
232 magic_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 }
249 OUTPUT:
250 RETVAL
251
252 # missing/broken in Gtk2 perl module
253
254 void
255 gdk_window_clear_hints (GdkWindow *window)
256 CODE:
257 gdk_window_set_geometry_hints (window, 0, 0);
258
259 gboolean
260 gdk_net_wm_supports (GdkAtom property)
261 CODE:
262 #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
263 RETVAL = gdk_net_wm_supports (property);
264 #else
265 RETVAL = 0;
266 #endif
267 OUTPUT:
268 RETVAL
269
270 GdkPixbuf_noinc *
271 dealpha_expose (GdkPixbuf *pb)
272 CODE:
273 {
274 int w = gdk_pixbuf_get_width (pb);
275 int h = gdk_pixbuf_get_height (pb);
276 int bpp = gdk_pixbuf_get_n_channels (pb);
277 int x, y, i;
278 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
279 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
280
281 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
282
283 dst = gdk_pixbuf_get_pixels (RETVAL);
284 dstr = gdk_pixbuf_get_rowstride (RETVAL);
285
286 for (x = 0; x < w; x++)
287 for (y = 0; y < h; y++)
288 for (i = 0; i < 3; i++)
289 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
290 }
291 OUTPUT:
292 RETVAL
293
294 GdkPixbuf_noinc *
295 rotate (GdkPixbuf *pb, int angle)
296 CODE:
297 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
298 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
299 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
300 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
301 : angle);
302 OUTPUT:
303 RETVAL
304
305 GdkPixbuf_noinc *
306 load_jpeg (SV *path, int thumbnail=0)
307 CODE:
308 {
309 struct jpeg_decompress_struct cinfo;
310 struct jpg_err_mgr jerr;
311 guchar *data;
312 int rs;
313 FILE *fp;
314 volatile GdkPixbuf *pb = 0;
315
316 RETVAL = 0;
317
318 fp = fopen (SvPVbyte_nolen (path), "rb");
319
320 if (!fp)
321 XSRETURN_UNDEF;
322
323 cinfo.err = jpeg_std_error (&jerr.err);
324
325 jerr.err.error_exit = cv_error_exit;
326 jerr.err.output_message = cv_error_output;
327
328 if ((rs = setjmp (jerr.setjmp_buffer)))
329 {
330 fclose (fp);
331 jpeg_destroy_decompress (&cinfo);
332
333 if (pb)
334 g_object_unref ((gpointer)pb);
335
336 XSRETURN_UNDEF;
337 }
338
339 jpeg_create_decompress (&cinfo);
340
341 jpeg_stdio_src (&cinfo, fp);
342 jpeg_read_header (&cinfo, TRUE);
343
344 cinfo.dct_method = JDCT_DEFAULT;
345 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
346 cinfo.do_block_smoothing = FALSE;
347 cinfo.out_color_space = JCS_RGB;
348 cinfo.quantize_colors = FALSE;
349
350 cinfo.scale_num = 1;
351 cinfo.scale_denom = 1;
352
353 jpeg_calc_output_dimensions (&cinfo);
354
355 if (thumbnail)
356 {
357 cinfo.dct_method = JDCT_FASTEST;
358 cinfo.do_fancy_upsampling = FALSE;
359
360 while (cinfo.scale_denom < 8
361 && cinfo.output_width >= IW*4
362 && cinfo.output_height >= IH*4)
363 {
364 cinfo.scale_denom <<= 1;
365 jpeg_calc_output_dimensions (&cinfo);
366 }
367 }
368
369 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
370 if (!RETVAL)
371 longjmp (jerr.setjmp_buffer, 2);
372
373 data = gdk_pixbuf_get_pixels (RETVAL);
374 rs = gdk_pixbuf_get_rowstride (RETVAL);
375
376 if (cinfo.output_components != 3)
377 longjmp (jerr.setjmp_buffer, 3);
378
379 jpeg_start_decompress (&cinfo);
380
381 while (cinfo.output_scanline < cinfo.output_height)
382 {
383 int remaining = cinfo.output_height - cinfo.output_scanline;
384 JSAMPROW rp[4];
385
386 rp [0] = data + cinfo.output_scanline * rs;
387 rp [1] = (guchar *)rp [0] + rs;
388 rp [2] = (guchar *)rp [1] + rs;
389 rp [3] = (guchar *)rp [2] + rs;
390
391 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
392 }
393
394 jpeg_finish_decompress (&cinfo);
395 fclose (fp);
396 jpeg_destroy_decompress (&cinfo);
397 }
398 OUTPUT:
399 RETVAL
400
401 void
402 compare (GdkPixbuf *a, GdkPixbuf *b)
403 PPCODE:
404 {
405 int w = gdk_pixbuf_get_width (a);
406 int h = gdk_pixbuf_get_height (a);
407
408 int sa = gdk_pixbuf_get_rowstride (a);
409 int sb = gdk_pixbuf_get_rowstride (b);
410
411 guchar *pa = gdk_pixbuf_get_pixels (a);
412 guchar *pb = gdk_pixbuf_get_pixels (b);
413
414 int x, y;
415
416 assert (w == gdk_pixbuf_get_width (b));
417 assert (h == gdk_pixbuf_get_height (b));
418
419 assert (gdk_pixbuf_get_n_channels (a) == 3);
420 assert (gdk_pixbuf_get_n_channels (b) == 3);
421
422 double diff = 0.;
423 int peak = 0;
424
425 if (w && h)
426 for (y = 0; y < h; y++)
427 {
428 guchar *pa_ = pa + y * sa;
429 guchar *pb_ = pb + y * sb;
430
431 for (x = 0; x < w; x++)
432 {
433 int d;
434
435 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
436 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
437 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
438 }
439 }
440
441 EXTEND (SP, 2);
442 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
443 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
444 }
445
446 #############################################################################
447
448 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
449
450 # currently only works for filenames (octet strings)
451
452 SV *
453 foldcase (SV *pathsv)
454 PROTOTYPE: $
455 CODE:
456 {
457 STRLEN plen;
458 U8 *path = (U8 *)SvPV (pathsv, plen);
459 U8 *pend = path + plen;
460 U8 dst [plen * 6 * 3], *dstp = dst;
461
462 while (path < pend)
463 {
464 U8 ch = *path;
465
466 if (ch >= 'a' && ch <= 'z')
467 *dstp++ = *path++;
468 else if (ch >= 'A' && ch <= 'Z')
469 *dstp++ = *path++ + ('a' - 'A');
470 else if (ch >= '0' && ch <= '9')
471 {
472 STRLEN el, nl = 0;
473 while (*path >= '0' && *path <= '9' && path < pend)
474 path++, nl++;
475
476 for (el = nl; el < 6; el++)
477 *dstp++ = '0';
478
479 memcpy (dstp, path - nl, nl);
480 dstp += nl;
481 }
482 else
483 *dstp++ = *path++;
484 #if 0
485 else
486 {
487 STRLEN cl;
488 to_utf8_fold (path, dstp, &cl);
489 dstp += cl;
490 path += is_utf8_char (path);
491 }
492 #endif
493 }
494
495 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
496 }
497 OUTPUT:
498 RETVAL
499
500 GdkPixbuf_noinc *
501 p7_to_pb (int w, int h, SV *src_sv)
502 PROTOTYPE: @
503 CODE:
504 {
505 int x, y;
506 guchar *dst, *d;
507 int dstr;
508 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
509
510 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
511 dst = gdk_pixbuf_get_pixels (RETVAL);
512 dstr = gdk_pixbuf_get_rowstride (RETVAL);
513
514 for (y = 0; y < h; y++)
515 for (d = dst + y * dstr, x = 0; x < w; x++)
516 {
517 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
518 *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
519 *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
520
521 src++;
522 }
523 }
524 OUTPUT:
525 RETVAL
526
527 #############################################################################
528
529 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
530
531 void
532 dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
533 CODE:
534 {
535 int w = gdk_pixbuf_get_width (pb);
536 int h = gdk_pixbuf_get_height (pb);
537 int x, y, i;
538 guchar *dst;
539 int bpp = gdk_pixbuf_get_n_channels (pb);
540 guchar *src = gdk_pixbuf_get_pixels (pb);
541 int sstr = gdk_pixbuf_get_rowstride (pb);
542
543 a85_init ();
544
545 for (y = 0; y < h; y++)
546 for (x = 0; x < w; x++)
547 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
548 a85_push (fp, src [x * bpp + y * sstr + i]);
549
550 a85_finish (fp);
551 }
552
553 void
554 dump_binary (PerlIO *fp, GdkPixbuf *pb)
555 CODE:
556 {
557 int w = gdk_pixbuf_get_width (pb);
558 int h = gdk_pixbuf_get_height (pb);
559 int x, y, i;
560 guchar *dst;
561 int bpp = gdk_pixbuf_get_n_channels (pb);
562 guchar *src = gdk_pixbuf_get_pixels (pb);
563 int sstr = gdk_pixbuf_get_rowstride (pb);
564
565 for (y = 0; y < h; y++)
566 for (x = 0; x < w; x++)
567 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
568 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
569 }
570
571 #############################################################################
572
573 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
574
575 SV *
576 pb_to_hv84 (GdkPixbuf *pb)
577 CODE:
578 {
579 int w = gdk_pixbuf_get_width (pb);
580 int h = gdk_pixbuf_get_height (pb);
581 int x, y;
582 guchar *dst;
583 int bpp = gdk_pixbuf_get_n_channels (pb);
584 guchar *src = gdk_pixbuf_get_pixels (pb);
585 int sstr = gdk_pixbuf_get_rowstride (pb);
586
587 RETVAL = newSV (6 * 8 * 12 / 8);
588 SvPOK_only (RETVAL);
589 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
590
591 dst = (guchar *)SvPVX (RETVAL);
592
593 /* some primitive error distribution + random dithering */
594
595 for (y = 0; y < h; y++)
596 {
597 guchar *p = src + y * sstr;
598
599 for (x = 0; x < w; x += 2)
600 {
601 unsigned int r, g, b, h, s, v, H, V1, V2;
602
603 if (bpp == 3)
604 r = *p++, g = *p++, b = *p++;
605 else if (bpp == 1)
606 r = g = b = *p++;
607 else
608 abort ();
609
610 rgb_to_hsv (r, g, b, &h, &s, &v);
611
612 H = (h * 15 / 255) << 4;
613 V1 = v;
614
615 if (bpp == 3)
616 r = *p++, g = *p++, b = *p++;
617 else if (bpp == 1)
618 r = g = b = *p++;
619 else
620 abort ();
621
622 rgb_to_hsv (r, g, b, &h, &s, &v);
623
624 H |= h * 15 / 255;
625 V2 = v;
626
627 *dst++ = H;
628 *dst++ = V1;
629 *dst++ = V2;
630 }
631 }
632 }
633 OUTPUT:
634 RETVAL
635
636 SV *
637 hv84_to_av (unsigned char *hv84)
638 CODE:
639 {
640 int i = 72 / 3;
641 AV *av = newAV ();
642
643 RETVAL = (SV *)newRV_noinc ((SV *)av);
644 while (i--)
645 {
646 int h = *hv84++;
647 int v1 = *hv84++;
648 int v2 = *hv84++;
649
650 av_push (av, newSViv (v1));
651 av_push (av, newSViv ((h >> 4) * 255 / 15));
652 av_push (av, newSViv (v2));
653 av_push (av, newSViv ((h & 15) * 255 / 15));
654 }
655 }
656 OUTPUT:
657 RETVAL
658
659 #############################################################################
660
661 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
662
663 SV *
664 extract_features (SV *ar)
665 CODE:
666 {
667 int i;
668 AV *av, *result;
669
670 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
671 croak ("Not an array ref as first argument to extract_features");
672
673 av = (AV *) SvRV (ar);
674 result = newAV ();
675
676 for (i = 0; i <= av_len (av); ++i)
677 {
678 SV *sv = *av_fetch (av, i, 1);
679 SV *histsv = newSV (9 * sizeof (float) + 1);
680
681 SvPOK_on (histsv);
682 SvCUR_set (histsv, 9 * sizeof (float));
683 float *hist = (float *)SvPVX (histsv);
684
685 struct feature f_h, f_s, f_v;
686 feature_init (&f_h);
687 feature_init (&f_s);
688 feature_init (&f_v);
689
690 {
691 STRLEN len;
692 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
693 while (len >= 3)
694 {
695 unsigned int r, g, b, h, s, v;
696 r = *buf++; g = *buf++; b = *buf++;
697 rgb_to_hsv (r, g, b, &h, &s, &v);
698
699 feature_update_pass_1 (&f_h, h);
700 feature_update_pass_1 (&f_s, s);
701 feature_update_pass_1 (&f_v, v);
702
703 len -= 3;
704 }
705
706 feature_finish_pass_1 (&f_h);
707 feature_finish_pass_1 (&f_s);
708 feature_finish_pass_1 (&f_v);
709 }
710
711 {
712 STRLEN len;
713 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
714 while (len >= 3)
715 {
716 unsigned int r, g, b, h, s, v;
717 r = *buf++; g = *buf++; b = *buf++;
718 rgb_to_hsv (r, g, b, &h, &s, &v);
719
720 feature_update_pass_2 (&f_h, h);
721 feature_update_pass_2 (&f_s, s);
722 feature_update_pass_2 (&f_v, v);
723
724 len -= 3;
725 }
726
727 feature_finish_pass_2 (&f_h);
728 feature_finish_pass_2 (&f_s);
729 feature_finish_pass_2 (&f_v);
730 }
731
732 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
733 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
734 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
735
736 av_push (result, histsv);
737 }
738
739 RETVAL = newRV_noinc ((SV *)result);
740 }
741 OUTPUT:
742 RETVAL
743