ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
(Generate patch)

Comparing CV/CV.xs (file contents):
Revision 1.3 by root, Wed Nov 5 00:22:22 2003 UTC vs.
Revision 1.20 by root, Fri Jul 22 06:14:25 2005 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h>
6#include <setjmp.h>
7
8#include <jpeglib.h>
5#include <gdk-pixbuf/gdk-pixbuf.h> 9#include <gdk-pixbuf/gdk-pixbuf.h>
6 10
7#include <gperl.h> 11#include <gperl.h>
8#include <gtk2perl.h> 12#include <gtk2perl.h>
9 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
23struct jpg_err_mgr
24{
25 struct jpeg_error_mgr err;
26 jmp_buf setjmp_buffer;
27};
28
29static void
30cv_error_exit (j_common_ptr cinfo)
31{
32 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
33}
34
35static void
36cv_error_output (j_common_ptr cinfo)
37{
38 return;
39}
40
41static void
42rgb_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
68static guint32 a85_val;
69static guint a85_cnt;
70static guchar a85_buf[LINELENGTH], *a85_ptr;
71
72static void
73a85_init (void)
74{
75 a85_cnt = 4;
76 a85_ptr = a85_buf;
77}
78
79static void
80a85_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
110static void
111a85_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
10MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 125MODULE = Gtk2::CV PACKAGE = Gtk2::CV
11 126
12PROTOTYPES: ENABLE 127PROTOTYPES: ENABLE
13 128
14GdkPixbuf_noinc * 129GdkPixbuf_noinc *
15transpose (GdkPixbuf *pb) 130transpose (GdkPixbuf *pb)
16 CODE: 131 CODE:
17{ 132{
18 int w = gdk_pixbuf_get_width (pb); 133 int w = gdk_pixbuf_get_width (pb);
19 int h = gdk_pixbuf_get_height (pb); 134 int h = gdk_pixbuf_get_height (pb);
20 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 135 int bpp = gdk_pixbuf_get_n_channels (pb);
21 int x, y, i; 136 int x, y, i;
22 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 137 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
23 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 138 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
24 139
25 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 140 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
39flop (GdkPixbuf *pb) 154flop (GdkPixbuf *pb)
40 CODE: 155 CODE:
41{ 156{
42 int w = gdk_pixbuf_get_width (pb); 157 int w = gdk_pixbuf_get_width (pb);
43 int h = gdk_pixbuf_get_height (pb); 158 int h = gdk_pixbuf_get_height (pb);
44 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 159 int bpp = gdk_pixbuf_get_n_channels (pb);
45 int x, y, i; 160 int x, y, i;
46 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 161 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
47 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 162 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
48 163
49 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 164 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
52 dstr = gdk_pixbuf_get_rowstride (RETVAL); 167 dstr = gdk_pixbuf_get_rowstride (RETVAL);
53 168
54 for (y = 0; y < h; y++) 169 for (y = 0; y < h; y++)
55 for (x = 0; x < w; x++) 170 for (x = 0; x < w; x++)
56 for (i = 0; i < bpp; i++) 171 for (i = 0; i < bpp; i++)
57 dst[(w - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 172 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
58} 173}
59 OUTPUT: 174 OUTPUT:
175 RETVAL
176
177GdkPixbuf_noinc *
178load_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;
60 RETVAL 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 && (cinfo.output_width >> 1) >= IW
231 && (cinfo.output_height >> 1) >= IH)
232 {
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#############################################################################
61 271
62MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 272MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
273
274SV *
275foldcase (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 U8 ch = *path;
287
288 if (ch >= 'a' && ch <= 'z')
289 *dstp++ = *path++;
290 else if (ch >= '0' && ch <= '9')
291 {
292 STRLEN el, nl = 0;
293 while (*path >= '0' && *path <= '9' && path < pend)
294 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
63 315
64GdkPixbuf_noinc * 316GdkPixbuf_noinc *
65p7_to_pb (int w, int h, guchar *src) 317p7_to_pb (int w, int h, guchar *src)
66 CODE: 318 CODE:
67{ 319{
92{ 344{
93 int w = gdk_pixbuf_get_width (pb); 345 int w = gdk_pixbuf_get_width (pb);
94 int h = gdk_pixbuf_get_height (pb); 346 int h = gdk_pixbuf_get_height (pb);
95 int x, y; 347 int x, y;
96 guchar *dst; 348 guchar *dst;
97 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 349 int bpp = gdk_pixbuf_get_n_channels (pb);
98 guchar *src = gdk_pixbuf_get_pixels (pb); 350 guchar *src = gdk_pixbuf_get_pixels (pb);
99 int sstr = gdk_pixbuf_get_rowstride (pb); 351 int sstr = gdk_pixbuf_get_rowstride (pb);
352 int Er[IW], Eg[IW], Eb[IW];
353 int seed = 77;
100 354
101 RETVAL = newSV (w * h); 355 RETVAL = newSV (w * h);
102 SvPOK_only (RETVAL); 356 SvPOK_only (RETVAL);
103 SvCUR_set (RETVAL, w * h); 357 SvCUR_set (RETVAL, w * h);
104 358
105 dst = SvPVX (RETVAL); 359 dst = SvPVX (RETVAL);
106 360
361 memset (Er, 0, sizeof (int) * IW);
362 memset (Eg, 0, sizeof (int) * IW);
363 memset (Eb, 0, sizeof (int) * IW);
364
365 /* some primitive error distribution + random dithering */
366
107 for (y = 0; y < h; y++) 367 for (y = 0; y < h; y++)
108 { 368 {
109 /* use a very primitive form of error distribution. */
110 int er = 0, eg = 0, eb = 0; 369 int er = 0, eg = 0, eb = 0;
111 370
112 for (x = 0; x < w; x++) 371 for (x = 0; x < w; x++)
113 { 372 {
114 int r, g, b; 373 int r, g, b;
115 guchar *p = src + x * bpp + y * sstr; 374 guchar *p = src + x * bpp + y * sstr;
116 375
117 r = ((p[0] + er) * 7 + 128) / 255; 376 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
118 g = ((p[1] + eg) * 7 + 128) / 255; 377 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
119 b = ((p[2] + eb) * 3 + 128) / 255; 378 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
120 379
121 r = r > 7 ? 7 : r < 0 ? 0 : r; 380 r = r > 7 ? 7 : r < 0 ? 0 : r;
122 g = g > 7 ? 7 : g < 0 ? 0 : g; 381 g = g > 7 ? 7 : g < 0 ? 0 : g;
123 b = b > 3 ? 3 : b < 0 ? 0 : b; 382 b = b > 3 ? 3 : b < 0 ? 0 : b;
124 383
125 er += p[0] - (r * 255 + 4) / 7; 384 er += p[0] - (r * 255 + 4) / 7;
126 eg += p[1] - (g * 255 + 4) / 7; 385 eg += p[1] - (g * 255 + 4) / 7;
127 eb += p[2] - (b * 255 + 2) / 3; 386 eb += p[2] - (b * 255 + 2) / 3;
128 387
388 Er[x] = er >> 1; er -= (er + 1) >> 1;
389 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
390 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
391
129 *dst++ = r << 5 | g << 2 | b; 392 *dst++ = r << 5 | g << 2 | b;
130 } 393 }
131 } 394 }
132} 395}
133 OUTPUT: 396 OUTPUT:
134 RETVAL 397 RETVAL
135 398
399#############################################################################
136 400
401MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
137 402
403void
404dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
405 CODE:
406{
407 int w = gdk_pixbuf_get_width (pb);
408 int h = gdk_pixbuf_get_height (pb);
409 int x, y, i;
410 guchar *dst;
411 int bpp = gdk_pixbuf_get_n_channels (pb);
412 guchar *src = gdk_pixbuf_get_pixels (pb);
413 int sstr = gdk_pixbuf_get_rowstride (pb);
414
415 a85_init ();
416
417 for (y = 0; y < h; y++)
418 for (x = 0; x < w; x++)
419 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
420 a85_push (fp, src [x * bpp + y * sstr + i]);
421
422 a85_finish (fp);
423}
424
425void
426dump_binary (PerlIO *fp, GdkPixbuf *pb)
427 CODE:
428{
429 int w = gdk_pixbuf_get_width (pb);
430 int h = gdk_pixbuf_get_height (pb);
431 int x, y, i;
432 guchar *dst;
433 int bpp = gdk_pixbuf_get_n_channels (pb);
434 guchar *src = gdk_pixbuf_get_pixels (pb);
435 int sstr = gdk_pixbuf_get_rowstride (pb);
436
437 for (y = 0; y < h; y++)
438 for (x = 0; x < w; x++)
439 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
440 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
441}
442
443#############################################################################
444
445MODULE = Gtk2::CV PACKAGE = Gtk2::CV
446
447SV *
448pb_to_hv84 (GdkPixbuf *pb)
449 CODE:
450{
451 int w = gdk_pixbuf_get_width (pb);
452 int h = gdk_pixbuf_get_height (pb);
453 int x, y;
454 guchar *dst;
455 int bpp = gdk_pixbuf_get_n_channels (pb);
456 guchar *src = gdk_pixbuf_get_pixels (pb);
457 int sstr = gdk_pixbuf_get_rowstride (pb);
458
459 RETVAL = newSV (6 * 8 * 12 / 8);
460 SvPOK_only (RETVAL);
461 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
462
463 dst = SvPVX (RETVAL);
464
465 /* some primitive error distribution + random dithering */
466
467 for (y = 0; y < h; y++)
468 {
469 guchar *p = src + y * sstr;
470
471 for (x = 0; x < w; x += 2)
472 {
473 unsigned int r, g, b, h, s, v, H, V1, V2;
474
475 if (bpp == 3)
476 r = *p++, g = *p++, b = *p++;
477 else if (bpp == 1)
478 r = g = b = *p++;
479 else
480 abort ();
481
482 rgb_to_hsv (r, g, b, &h, &s, &v);
483
484 H = (h * 15 / 255) << 4;
485 V1 = v;
486
487 if (bpp == 3)
488 r = *p++, g = *p++, b = *p++;
489 else if (bpp == 1)
490 r = g = b = *p++;
491 else
492 abort ();
493
494 rgb_to_hsv (r, g, b, &h, &s, &v);
495
496 H |= h * 15 / 255;
497 V2 = v;
498
499 *dst++ = H;
500 *dst++ = V1;
501 *dst++ = V2;
502 }
503 }
504}
505 OUTPUT:
506 RETVAL
507
508SV *
509hv84_to_av (unsigned char *hv84)
510 CODE:
511{
512 int i = 72 / 3;
513 AV *av = newAV ();
514
515 RETVAL = (SV *)newRV_noinc ((SV *)av);
516 while (i--)
517 {
518 int h = *hv84++;
519 int v1 = *hv84++;
520 int v2 = *hv84++;
521
522 av_push (av, newSViv (v1));
523 av_push (av, newSViv ((h >> 4) * 255 / 15));
524 av_push (av, newSViv (v2));
525 av_push (av, newSViv ((h & 15) * 255 / 15));
526 }
527}
528 OUTPUT:
529 RETVAL
530
531#############################################################################
532
533MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
534
535SV *
536make_histograms (SV *ar)
537 CODE:
538{
539 int i;
540 AV *av, *result;
541
542 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
543 croak ("Not an array ref as first argument to make_histogram");
544
545 av = (AV *) SvRV (ar);
546 result = newAV ();
547
548 for (i = 0; i <= av_len (av); ++i)
549 {
550 const int HISTSIZE = 64;
551
552 int j;
553 SV *sv = *av_fetch (av, i, 1);
554 STRLEN len;
555 char *buf = SvPVbyte (sv, len);
556
557 int tmphist[HISTSIZE];
558 float *hist;
559
560 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
561 SvPOK_on (histsv);
562 SvCUR_set (histsv, HISTSIZE * sizeof (float));
563 hist = (float *)SvPVX (histsv);
564
565 Zero (tmphist, sizeof (tmphist), char);
566
567 for (j = len; j--; )
568 {
569 unsigned int idx
570 = ((*buf & 0xc0) >> 2)
571 | ((*buf & 0x18) >> 1)
572 | (*buf & 0x03);
573
574 ++tmphist[idx];
575 ++buf;
576 }
577
578 for (j = 0; j < HISTSIZE; ++j)
579 hist[j] = (float)tmphist[j] / (len + 1e-30);
580
581 av_push (result, histsv);
582 }
583
584 RETVAL = newRV_noinc ((SV *)result);
585}
586 OUTPUT:
587 RETVAL
588
589

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines