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