ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.34
Committed: Tue Mar 7 16:26:25 2006 UTC (18 years, 2 months ago) by root
Branch: MAIN
Changes since 1.33: +14 -13 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 if (x && y)
361 for (y = 0; y < h; y++)
362 {
363 guchar *pa_ = pa + y * sa;
364 guchar *pb_ = pb + y * sb;
365
366 for (x = 0; x < w; x++)
367 {
368 int d;
369
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 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
373 }
374 }
375
376 EXTEND (SP, 2);
377 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
378 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
379 }
380
381 #############################################################################
382
383 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
384
385 SV *
386 foldcase (SV *pathsv)
387 PROTOTYPE: $
388 CODE:
389 {
390 STRLEN plen;
391 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
392 U8 *pend = path + plen;
393 U8 dst [plen * 6 * 3], *dstp = dst;
394
395 while (path < pend)
396 {
397 U8 ch = *path;
398
399 if (ch >= 'a' && ch <= 'z')
400 *dstp++ = *path++;
401 else if (ch >= '0' && ch <= '9')
402 {
403 STRLEN el, nl = 0;
404 while (*path >= '0' && *path <= '9' && path < pend)
405 path++, nl++;
406
407 for (el = nl; el < 6; el++)
408 *dstp++ = '0';
409
410 memcpy (dstp, path - nl, nl);
411 dstp += nl;
412 }
413 else
414 {
415 STRLEN cl;
416 to_utf8_fold (path, dstp, &cl);
417 dstp += cl;
418 path += is_utf8_char (path);
419 }
420 }
421
422 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
423 }
424 OUTPUT:
425 RETVAL
426
427 GdkPixbuf_noinc *
428 p7_to_pb (int w, int h, SV *src_sv)
429 PROTOTYPE: @
430 CODE:
431 {
432 int x, y;
433 guchar *dst, *d;
434 int dstr;
435 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
436
437 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
438 dst = gdk_pixbuf_get_pixels (RETVAL);
439 dstr = gdk_pixbuf_get_rowstride (RETVAL);
440
441 for (y = 0; y < h; y++)
442 for (d = dst + y * dstr, x = 0; x < w; x++)
443 {
444 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
445 *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
446 *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
447
448 src++;
449 }
450 }
451 OUTPUT:
452 RETVAL
453
454 #############################################################################
455
456 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
457
458 void
459 dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
460 CODE:
461 {
462 int w = gdk_pixbuf_get_width (pb);
463 int h = gdk_pixbuf_get_height (pb);
464 int x, y, i;
465 guchar *dst;
466 int bpp = gdk_pixbuf_get_n_channels (pb);
467 guchar *src = gdk_pixbuf_get_pixels (pb);
468 int sstr = gdk_pixbuf_get_rowstride (pb);
469
470 a85_init ();
471
472 for (y = 0; y < h; y++)
473 for (x = 0; x < w; x++)
474 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
475 a85_push (fp, src [x * bpp + y * sstr + i]);
476
477 a85_finish (fp);
478 }
479
480 void
481 dump_binary (PerlIO *fp, GdkPixbuf *pb)
482 CODE:
483 {
484 int w = gdk_pixbuf_get_width (pb);
485 int h = gdk_pixbuf_get_height (pb);
486 int x, y, i;
487 guchar *dst;
488 int bpp = gdk_pixbuf_get_n_channels (pb);
489 guchar *src = gdk_pixbuf_get_pixels (pb);
490 int sstr = gdk_pixbuf_get_rowstride (pb);
491
492 for (y = 0; y < h; y++)
493 for (x = 0; x < w; x++)
494 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
495 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
496 }
497
498 #############################################################################
499
500 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
501
502 SV *
503 pb_to_hv84 (GdkPixbuf *pb)
504 CODE:
505 {
506 int w = gdk_pixbuf_get_width (pb);
507 int h = gdk_pixbuf_get_height (pb);
508 int x, y;
509 guchar *dst;
510 int bpp = gdk_pixbuf_get_n_channels (pb);
511 guchar *src = gdk_pixbuf_get_pixels (pb);
512 int sstr = gdk_pixbuf_get_rowstride (pb);
513
514 RETVAL = newSV (6 * 8 * 12 / 8);
515 SvPOK_only (RETVAL);
516 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
517
518 dst = (guchar *)SvPVX (RETVAL);
519
520 /* some primitive error distribution + random dithering */
521
522 for (y = 0; y < h; y++)
523 {
524 guchar *p = src + y * sstr;
525
526 for (x = 0; x < w; x += 2)
527 {
528 unsigned int r, g, b, h, s, v, H, V1, V2;
529
530 if (bpp == 3)
531 r = *p++, g = *p++, b = *p++;
532 else if (bpp == 1)
533 r = g = b = *p++;
534 else
535 abort ();
536
537 rgb_to_hsv (r, g, b, &h, &s, &v);
538
539 H = (h * 15 / 255) << 4;
540 V1 = v;
541
542 if (bpp == 3)
543 r = *p++, g = *p++, b = *p++;
544 else if (bpp == 1)
545 r = g = b = *p++;
546 else
547 abort ();
548
549 rgb_to_hsv (r, g, b, &h, &s, &v);
550
551 H |= h * 15 / 255;
552 V2 = v;
553
554 *dst++ = H;
555 *dst++ = V1;
556 *dst++ = V2;
557 }
558 }
559 }
560 OUTPUT:
561 RETVAL
562
563 SV *
564 hv84_to_av (unsigned char *hv84)
565 CODE:
566 {
567 int i = 72 / 3;
568 AV *av = newAV ();
569
570 RETVAL = (SV *)newRV_noinc ((SV *)av);
571 while (i--)
572 {
573 int h = *hv84++;
574 int v1 = *hv84++;
575 int v2 = *hv84++;
576
577 av_push (av, newSViv (v1));
578 av_push (av, newSViv ((h >> 4) * 255 / 15));
579 av_push (av, newSViv (v2));
580 av_push (av, newSViv ((h & 15) * 255 / 15));
581 }
582 }
583 OUTPUT:
584 RETVAL
585
586 #############################################################################
587
588 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
589
590 SV *
591 make_histograms (SV *ar)
592 CODE:
593 {
594 int i;
595 AV *av, *result;
596
597 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
598 croak ("Not an array ref as first argument to make_histogram");
599
600 av = (AV *) SvRV (ar);
601 result = newAV ();
602
603 for (i = 0; i <= av_len (av); ++i)
604 {
605 const int HISTSIZE = 64;
606
607 int j;
608 SV *sv = *av_fetch (av, i, 1);
609 STRLEN len;
610 char *buf = SvPVbyte (sv, len);
611
612 int tmphist[HISTSIZE];
613 float *hist;
614
615 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
616 SvPOK_on (histsv);
617 SvCUR_set (histsv, HISTSIZE * sizeof (float));
618 hist = (float *)SvPVX (histsv);
619
620 Zero (tmphist, sizeof (tmphist), char);
621
622 for (j = len; j--; )
623 {
624 unsigned int idx
625 = ((*buf & 0xc0) >> 2)
626 | ((*buf & 0x18) >> 1)
627 | (*buf & 0x03);
628
629 ++tmphist[idx];
630 ++buf;
631 }
632
633 for (j = 0; j < HISTSIZE; ++j)
634 hist[j] = (float)tmphist[j] / (len + 1e-30);
635
636 av_push (result, histsv);
637 }
638
639 RETVAL = newRV_noinc ((SV *)result);
640 }
641 OUTPUT:
642 RETVAL
643
644