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