ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.33
Committed: Fri Feb 17 08:48:06 2006 UTC (18 years, 3 months ago) by root
Branch: MAIN
Changes since 1.32: +46 -2 lines
Log Message:
*** empty log message ***

File Contents

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