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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines