ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.23
Committed: Tue Aug 16 23:50:39 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
Changes since 1.22: +10 -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    
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.22 # missing in Gtk2 perl module
132    
133     gboolean
134     gdk_net_wm_supports (GdkAtom property)
135     CODE:
136     #if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
137     RETVAL = gdk_net_wm_supports (property);
138     #else
139     RETVAL = 0;
140     #endif
141     OUTPUT:
142     RETVAL
143    
144 root 1.3 GdkPixbuf_noinc *
145 root 1.2 transpose (GdkPixbuf *pb)
146     CODE:
147     {
148     int w = gdk_pixbuf_get_width (pb);
149     int h = gdk_pixbuf_get_height (pb);
150 root 1.7 int bpp = gdk_pixbuf_get_n_channels (pb);
151 root 1.2 int x, y, i;
152     guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
153     int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
154    
155     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
156    
157     dst = gdk_pixbuf_get_pixels (RETVAL);
158     dstr = gdk_pixbuf_get_rowstride (RETVAL);
159    
160 root 1.3 for (y = 0; y < h; y++)
161     for (x = 0; x < w; x++)
162 root 1.2 for (i = 0; i < bpp; i++)
163     dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i];
164     }
165     OUTPUT:
166     RETVAL
167    
168 root 1.3 GdkPixbuf_noinc *
169 root 1.2 flop (GdkPixbuf *pb)
170     CODE:
171     {
172     int w = gdk_pixbuf_get_width (pb);
173     int h = gdk_pixbuf_get_height (pb);
174 root 1.7 int bpp = gdk_pixbuf_get_n_channels (pb);
175 root 1.2 int x, y, i;
176     guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
177     int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
178    
179     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
180    
181     dst = gdk_pixbuf_get_pixels (RETVAL);
182     dstr = gdk_pixbuf_get_rowstride (RETVAL);
183    
184 root 1.3 for (y = 0; y < h; y++)
185     for (x = 0; x < w; x++)
186 root 1.2 for (i = 0; i < bpp; i++)
187 root 1.4 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
188 root 1.2 }
189     OUTPUT:
190     RETVAL
191    
192 root 1.17 GdkPixbuf_noinc *
193 root 1.23 load_jpeg (SV *path, int thumbnail=0)
194 root 1.17 CODE:
195     {
196     struct jpeg_decompress_struct cinfo;
197     struct jpg_err_mgr jerr;
198     guchar *data;
199     int rs;
200     FILE *fp;
201     volatile GdkPixbuf *pb = 0;
202 root 1.23 gchar *filename;
203    
204 root 1.17 RETVAL = 0;
205    
206 root 1.23 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
207     fp = fopen (filename, "rb");
208     g_free (filename);
209    
210     if (!fp)
211 root 1.17 XSRETURN_UNDEF;
212    
213     cinfo.err = jpeg_std_error (&jerr.err);
214    
215     jerr.err.error_exit = cv_error_exit;
216     jerr.err.output_message = cv_error_output;
217    
218     if ((rs = setjmp (jerr.setjmp_buffer)))
219     {
220     fclose (fp);
221     jpeg_destroy_decompress (&cinfo);
222    
223     if (pb)
224     g_object_unref ((gpointer)pb);
225    
226     XSRETURN_UNDEF;
227     }
228    
229     jpeg_create_decompress (&cinfo);
230    
231     jpeg_stdio_src (&cinfo, fp);
232     jpeg_read_header (&cinfo, TRUE);
233    
234     cinfo.dct_method = JDCT_DEFAULT;
235     cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
236     cinfo.do_block_smoothing = FALSE;
237     cinfo.out_color_space = JCS_RGB;
238     cinfo.quantize_colors = FALSE;
239    
240     cinfo.scale_num = 1;
241     cinfo.scale_denom = 1;
242    
243     jpeg_calc_output_dimensions (&cinfo);
244    
245     if (thumbnail)
246     {
247     cinfo.dct_method = JDCT_FASTEST;
248     cinfo.do_fancy_upsampling = FALSE;
249    
250     while (cinfo.scale_denom < 8
251 root 1.21 && cinfo.output_width >= IW*4
252     && cinfo.output_height >= IH*4)
253 root 1.17 {
254     cinfo.scale_denom <<= 1;
255     jpeg_calc_output_dimensions (&cinfo);
256     }
257     }
258    
259     pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
260     if (!RETVAL)
261     longjmp (jerr.setjmp_buffer, 2);
262    
263     data = gdk_pixbuf_get_pixels (RETVAL);
264     rs = gdk_pixbuf_get_rowstride (RETVAL);
265    
266     if (cinfo.output_components != 3)
267     longjmp (jerr.setjmp_buffer, 3);
268    
269     jpeg_start_decompress (&cinfo);
270    
271     while (cinfo.output_scanline < cinfo.output_height)
272     {
273     int remaining = cinfo.output_height - cinfo.output_scanline;
274     JSAMPROW rp[4];
275    
276     rp [0] = data + cinfo.output_scanline * rs;
277     rp [1] = (guchar *)rp [0] + rs;
278     rp [2] = (guchar *)rp [1] + rs;
279     rp [3] = (guchar *)rp [2] + rs;
280    
281     jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
282     }
283    
284     jpeg_finish_decompress (&cinfo);
285     fclose (fp);
286     jpeg_destroy_decompress (&cinfo);
287     }
288     OUTPUT:
289     RETVAL
290    
291 root 1.6 #############################################################################
292    
293 root 1.2 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
294    
295 root 1.18 SV *
296     foldcase (SV *pathsv)
297     PROTOTYPE: $
298     CODE:
299     {
300     STRLEN plen;
301     U8 *path = SvPVutf8 (pathsv, plen);
302     U8 *pend = path + plen;
303     U8 dst [plen * 6 * 3], *dstp = dst;
304    
305     while (path < pend)
306     {
307 root 1.19 U8 ch = *path;
308    
309     if (ch >= 'a' && ch <= 'z')
310     *dstp++ = *path++;
311     else if (ch >= '0' && ch <= '9')
312 root 1.18 {
313     STRLEN el, nl = 0;
314 root 1.19 while (*path >= '0' && *path <= '9' && path < pend)
315 root 1.18 path++, nl++;
316    
317     for (el = nl; el < 6; el++)
318     *dstp++ = '0';
319    
320     memcpy (dstp, path - nl, nl);
321     dstp += nl;
322     }
323     else
324     {
325     STRLEN cl;
326     to_utf8_fold (path, dstp, &cl);
327     dstp += cl;
328     path += is_utf8_char (path);
329     }
330     }
331    
332     RETVAL = newSVpvn (dst, dstp - dst);
333     }
334     OUTPUT:
335     RETVAL
336    
337 root 1.3 GdkPixbuf_noinc *
338 root 1.2 p7_to_pb (int w, int h, guchar *src)
339 root 1.21 PROTOTYPE: @
340 root 1.2 CODE:
341     {
342     int x, y;
343     guchar *dst, *d;
344     int dstr;
345    
346     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
347     dst = gdk_pixbuf_get_pixels (RETVAL);
348     dstr = gdk_pixbuf_get_rowstride (RETVAL);
349    
350     for (y = 0; y < h; y++)
351     for (d = dst + y * dstr, x = 0; x < w; x++)
352     {
353 root 1.3 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
354     *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
355     *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
356 root 1.2
357     src++;
358     }
359 root 1.3 }
360     OUTPUT:
361     RETVAL
362    
363     SV *
364     pb_to_p7 (GdkPixbuf *pb)
365     CODE:
366     {
367     int w = gdk_pixbuf_get_width (pb);
368     int h = gdk_pixbuf_get_height (pb);
369     int x, y;
370     guchar *dst;
371 root 1.7 int bpp = gdk_pixbuf_get_n_channels (pb);
372 root 1.3 guchar *src = gdk_pixbuf_get_pixels (pb);
373     int sstr = gdk_pixbuf_get_rowstride (pb);
374 root 1.5 int Er[IW], Eg[IW], Eb[IW];
375     int seed = 77;
376 root 1.3
377     RETVAL = newSV (w * h);
378     SvPOK_only (RETVAL);
379     SvCUR_set (RETVAL, w * h);
380    
381     dst = SvPVX (RETVAL);
382    
383 root 1.5 memset (Er, 0, sizeof (int) * IW);
384     memset (Eg, 0, sizeof (int) * IW);
385     memset (Eb, 0, sizeof (int) * IW);
386    
387     /* some primitive error distribution + random dithering */
388    
389 root 1.3 for (y = 0; y < h; y++)
390     {
391     int er = 0, eg = 0, eb = 0;
392    
393     for (x = 0; x < w; x++)
394     {
395     int r, g, b;
396     guchar *p = src + x * bpp + y * sstr;
397    
398 root 1.17 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
399     g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
400     b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
401 root 1.3
402     r = r > 7 ? 7 : r < 0 ? 0 : r;
403     g = g > 7 ? 7 : g < 0 ? 0 : g;
404     b = b > 3 ? 3 : b < 0 ? 0 : b;
405    
406     er += p[0] - (r * 255 + 4) / 7;
407     eg += p[1] - (g * 255 + 4) / 7;
408     eb += p[2] - (b * 255 + 2) / 3;
409 root 1.5
410 root 1.17 Er[x] = er >> 1; er -= (er + 1) >> 1;
411     Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
412     Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
413 root 1.3
414     *dst++ = r << 5 | g << 2 | b;
415     }
416     }
417 root 1.2 }
418     OUTPUT:
419     RETVAL
420 root 1.6
421     #############################################################################
422 root 1.4
423     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
424    
425     void
426 root 1.7 dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
427 root 1.4 CODE:
428     {
429     int w = gdk_pixbuf_get_width (pb);
430     int h = gdk_pixbuf_get_height (pb);
431     int x, y, i;
432     guchar *dst;
433 root 1.7 int bpp = gdk_pixbuf_get_n_channels (pb);
434 root 1.4 guchar *src = gdk_pixbuf_get_pixels (pb);
435     int sstr = gdk_pixbuf_get_rowstride (pb);
436    
437     a85_init ();
438    
439     for (y = 0; y < h; y++)
440     for (x = 0; x < w; x++)
441 root 1.7 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
442 root 1.4 a85_push (fp, src [x * bpp + y * sstr + i]);
443    
444     a85_finish (fp);
445 root 1.7 }
446    
447     void
448     dump_binary (PerlIO *fp, GdkPixbuf *pb)
449     CODE:
450     {
451     int w = gdk_pixbuf_get_width (pb);
452     int h = gdk_pixbuf_get_height (pb);
453     int x, y, i;
454     guchar *dst;
455     int bpp = gdk_pixbuf_get_n_channels (pb);
456     guchar *src = gdk_pixbuf_get_pixels (pb);
457     int sstr = gdk_pixbuf_get_rowstride (pb);
458    
459     for (y = 0; y < h; y++)
460     for (x = 0; x < w; x++)
461     for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
462     PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
463 root 1.4 }
464 root 1.8
465     #############################################################################
466    
467     MODULE = Gtk2::CV PACKAGE = Gtk2::CV
468    
469     SV *
470     pb_to_hv84 (GdkPixbuf *pb)
471     CODE:
472     {
473     int w = gdk_pixbuf_get_width (pb);
474     int h = gdk_pixbuf_get_height (pb);
475     int x, y;
476     guchar *dst;
477     int bpp = gdk_pixbuf_get_n_channels (pb);
478     guchar *src = gdk_pixbuf_get_pixels (pb);
479     int sstr = gdk_pixbuf_get_rowstride (pb);
480    
481     RETVAL = newSV (6 * 8 * 12 / 8);
482     SvPOK_only (RETVAL);
483     SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
484    
485     dst = SvPVX (RETVAL);
486    
487     /* some primitive error distribution + random dithering */
488    
489     for (y = 0; y < h; y++)
490     {
491     guchar *p = src + y * sstr;
492    
493     for (x = 0; x < w; x += 2)
494     {
495     unsigned int r, g, b, h, s, v, H, V1, V2;
496    
497     if (bpp == 3)
498     r = *p++, g = *p++, b = *p++;
499     else if (bpp == 1)
500     r = g = b = *p++;
501     else
502     abort ();
503    
504     rgb_to_hsv (r, g, b, &h, &s, &v);
505    
506     H = (h * 15 / 255) << 4;
507     V1 = v;
508    
509     if (bpp == 3)
510     r = *p++, g = *p++, b = *p++;
511     else if (bpp == 1)
512     r = g = b = *p++;
513     else
514     abort ();
515    
516     rgb_to_hsv (r, g, b, &h, &s, &v);
517    
518     H |= h * 15 / 255;
519     V2 = v;
520    
521     *dst++ = H;
522     *dst++ = V1;
523     *dst++ = V2;
524     }
525     }
526     }
527     OUTPUT:
528     RETVAL
529 root 1.4
530 root 1.9 SV *
531     hv84_to_av (unsigned char *hv84)
532     CODE:
533     {
534     int i = 72 / 3;
535     AV *av = newAV ();
536    
537     RETVAL = (SV *)newRV_noinc ((SV *)av);
538     while (i--)
539     {
540     int h = *hv84++;
541     int v1 = *hv84++;
542     int v2 = *hv84++;
543    
544     av_push (av, newSViv (v1));
545     av_push (av, newSViv ((h >> 4) * 255 / 15));
546     av_push (av, newSViv (v2));
547     av_push (av, newSViv ((h & 15) * 255 / 15));
548     }
549     }
550     OUTPUT:
551     RETVAL
552 root 1.2
553 root 1.20 #############################################################################
554    
555     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
556    
557     SV *
558     make_histograms (SV *ar)
559     CODE:
560     {
561     int i;
562     AV *av, *result;
563    
564     if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
565     croak ("Not an array ref as first argument to make_histogram");
566    
567     av = (AV *) SvRV (ar);
568     result = newAV ();
569    
570     for (i = 0; i <= av_len (av); ++i)
571     {
572     const int HISTSIZE = 64;
573    
574     int j;
575     SV *sv = *av_fetch (av, i, 1);
576     STRLEN len;
577     char *buf = SvPVbyte (sv, len);
578    
579     int tmphist[HISTSIZE];
580     float *hist;
581    
582     SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
583     SvPOK_on (histsv);
584     SvCUR_set (histsv, HISTSIZE * sizeof (float));
585     hist = (float *)SvPVX (histsv);
586    
587     Zero (tmphist, sizeof (tmphist), char);
588    
589     for (j = len; j--; )
590     {
591     unsigned int idx
592     = ((*buf & 0xc0) >> 2)
593     | ((*buf & 0x18) >> 1)
594     | (*buf & 0x03);
595    
596     ++tmphist[idx];
597     ++buf;
598     }
599    
600     for (j = 0; j < HISTSIZE; ++j)
601     hist[j] = (float)tmphist[j] / (len + 1e-30);
602    
603     av_push (result, histsv);
604     }
605    
606     RETVAL = newRV_noinc ((SV *)result);
607     }
608     OUTPUT:
609     RETVAL
610 root 1.1
611