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