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