ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.34
Committed: Tue Mar 7 16:26:25 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.33: +14 -13 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 root 1.34 if (x && y)
361     for (y = 0; y < h; y++)
362     {
363     guchar *pa_ = pa + y * sa;
364     guchar *pb_ = pb + y * sb;
365 root 1.33
366 root 1.34 for (x = 0; x < w; x++)
367     {
368     int d;
369    
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     d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
373     }
374     }
375 root 1.33
376     EXTEND (SP, 2);
377     PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
378     PUSHs (sv_2mortal (newSVnv (peak / 255.)));
379     }
380    
381 root 1.6 #############################################################################
382    
383 root 1.2 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
384    
385 root 1.18 SV *
386     foldcase (SV *pathsv)
387     PROTOTYPE: $
388     CODE:
389     {
390     STRLEN plen;
391 root 1.25 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
392 root 1.18 U8 *pend = path + plen;
393     U8 dst [plen * 6 * 3], *dstp = dst;
394    
395     while (path < pend)
396     {
397 root 1.19 U8 ch = *path;
398    
399     if (ch >= 'a' && ch <= 'z')
400     *dstp++ = *path++;
401     else if (ch >= '0' && ch <= '9')
402 root 1.18 {
403     STRLEN el, nl = 0;
404 root 1.19 while (*path >= '0' && *path <= '9' && path < pend)
405 root 1.18 path++, nl++;
406    
407     for (el = nl; el < 6; el++)
408     *dstp++ = '0';
409    
410     memcpy (dstp, path - nl, nl);
411     dstp += nl;
412     }
413     else
414     {
415     STRLEN cl;
416     to_utf8_fold (path, dstp, &cl);
417     dstp += cl;
418     path += is_utf8_char (path);
419     }
420     }
421    
422 root 1.25 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
423 root 1.18 }
424     OUTPUT:
425     RETVAL
426    
427 root 1.3 GdkPixbuf_noinc *
428 root 1.24 p7_to_pb (int w, int h, SV *src_sv)
429 root 1.21 PROTOTYPE: @
430 root 1.2 CODE:
431     {
432     int x, y;
433     guchar *dst, *d;
434     int dstr;
435 root 1.25 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
436 root 1.2
437     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
438     dst = gdk_pixbuf_get_pixels (RETVAL);
439     dstr = gdk_pixbuf_get_rowstride (RETVAL);
440    
441     for (y = 0; y < h; y++)
442     for (d = dst + y * dstr, x = 0; x < w; x++)
443     {
444 root 1.3 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
445     *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
446     *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
447 root 1.2
448     src++;
449     }
450 root 1.3 }
451     OUTPUT:
452     RETVAL
453    
454 root 1.6 #############################################################################
455 root 1.4
456     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
457    
458     void
459 root 1.7 dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
460 root 1.4 CODE:
461     {
462     int w = gdk_pixbuf_get_width (pb);
463     int h = gdk_pixbuf_get_height (pb);
464     int x, y, i;
465     guchar *dst;
466 root 1.7 int bpp = gdk_pixbuf_get_n_channels (pb);
467 root 1.4 guchar *src = gdk_pixbuf_get_pixels (pb);
468     int sstr = gdk_pixbuf_get_rowstride (pb);
469    
470     a85_init ();
471    
472     for (y = 0; y < h; y++)
473     for (x = 0; x < w; x++)
474 root 1.7 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
475 root 1.4 a85_push (fp, src [x * bpp + y * sstr + i]);
476    
477     a85_finish (fp);
478 root 1.7 }
479    
480     void
481     dump_binary (PerlIO *fp, GdkPixbuf *pb)
482     CODE:
483     {
484     int w = gdk_pixbuf_get_width (pb);
485     int h = gdk_pixbuf_get_height (pb);
486     int x, y, i;
487     guchar *dst;
488     int bpp = gdk_pixbuf_get_n_channels (pb);
489     guchar *src = gdk_pixbuf_get_pixels (pb);
490     int sstr = gdk_pixbuf_get_rowstride (pb);
491    
492     for (y = 0; y < h; y++)
493     for (x = 0; x < w; x++)
494     for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
495     PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
496 root 1.4 }
497 root 1.8
498     #############################################################################
499    
500     MODULE = Gtk2::CV PACKAGE = Gtk2::CV
501    
502     SV *
503     pb_to_hv84 (GdkPixbuf *pb)
504     CODE:
505     {
506     int w = gdk_pixbuf_get_width (pb);
507     int h = gdk_pixbuf_get_height (pb);
508     int x, y;
509     guchar *dst;
510     int bpp = gdk_pixbuf_get_n_channels (pb);
511     guchar *src = gdk_pixbuf_get_pixels (pb);
512     int sstr = gdk_pixbuf_get_rowstride (pb);
513    
514     RETVAL = newSV (6 * 8 * 12 / 8);
515     SvPOK_only (RETVAL);
516     SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
517    
518 root 1.25 dst = (guchar *)SvPVX (RETVAL);
519 root 1.8
520     /* some primitive error distribution + random dithering */
521    
522     for (y = 0; y < h; y++)
523     {
524     guchar *p = src + y * sstr;
525    
526     for (x = 0; x < w; x += 2)
527     {
528     unsigned int r, g, b, h, s, v, H, V1, V2;
529    
530     if (bpp == 3)
531     r = *p++, g = *p++, b = *p++;
532     else if (bpp == 1)
533     r = g = b = *p++;
534     else
535     abort ();
536    
537     rgb_to_hsv (r, g, b, &h, &s, &v);
538    
539     H = (h * 15 / 255) << 4;
540     V1 = v;
541    
542     if (bpp == 3)
543     r = *p++, g = *p++, b = *p++;
544     else if (bpp == 1)
545     r = g = b = *p++;
546     else
547     abort ();
548    
549     rgb_to_hsv (r, g, b, &h, &s, &v);
550    
551     H |= h * 15 / 255;
552     V2 = v;
553    
554     *dst++ = H;
555     *dst++ = V1;
556     *dst++ = V2;
557     }
558     }
559     }
560     OUTPUT:
561     RETVAL
562 root 1.4
563 root 1.9 SV *
564     hv84_to_av (unsigned char *hv84)
565     CODE:
566     {
567     int i = 72 / 3;
568     AV *av = newAV ();
569    
570     RETVAL = (SV *)newRV_noinc ((SV *)av);
571     while (i--)
572     {
573     int h = *hv84++;
574     int v1 = *hv84++;
575     int v2 = *hv84++;
576    
577     av_push (av, newSViv (v1));
578     av_push (av, newSViv ((h >> 4) * 255 / 15));
579     av_push (av, newSViv (v2));
580     av_push (av, newSViv ((h & 15) * 255 / 15));
581     }
582     }
583     OUTPUT:
584     RETVAL
585 root 1.2
586 root 1.20 #############################################################################
587    
588     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
589    
590     SV *
591     make_histograms (SV *ar)
592     CODE:
593     {
594     int i;
595     AV *av, *result;
596    
597     if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
598     croak ("Not an array ref as first argument to make_histogram");
599    
600     av = (AV *) SvRV (ar);
601     result = newAV ();
602    
603     for (i = 0; i <= av_len (av); ++i)
604     {
605     const int HISTSIZE = 64;
606    
607     int j;
608     SV *sv = *av_fetch (av, i, 1);
609     STRLEN len;
610     char *buf = SvPVbyte (sv, len);
611    
612     int tmphist[HISTSIZE];
613     float *hist;
614    
615     SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
616     SvPOK_on (histsv);
617     SvCUR_set (histsv, HISTSIZE * sizeof (float));
618     hist = (float *)SvPVX (histsv);
619    
620     Zero (tmphist, sizeof (tmphist), char);
621    
622     for (j = len; j--; )
623     {
624     unsigned int idx
625     = ((*buf & 0xc0) >> 2)
626     | ((*buf & 0x18) >> 1)
627     | (*buf & 0x03);
628    
629     ++tmphist[idx];
630     ++buf;
631     }
632    
633     for (j = 0; j < HISTSIZE; ++j)
634     hist[j] = (float)tmphist[j] / (len + 1e-30);
635    
636     av_push (result, histsv);
637     }
638    
639     RETVAL = newRV_noinc ((SV *)result);
640     }
641     OUTPUT:
642     RETVAL
643 root 1.1
644