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