ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.26
Committed: Sun Aug 21 02:18:30 2005 UTC (18 years, 9 months ago) by root
Branch: MAIN
Changes since 1.25: +25 -0 lines
Log Message:
*** empty log message ***

File Contents

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