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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines