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