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

Comparing CV/CV.xs (file contents):
Revision 1.5 by root, Sat Nov 8 22:18:50 2003 UTC vs.
Revision 1.27 by root, Sun Aug 21 02:23:52 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
10#define IW 80 16#define IW 80 /* MUST match Schnauer.pm! */
17#define IH 60 /* MUST match Schnauer.pm! */
11 18
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 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}
13 69
14static guint32 a85_val; 70static guint32 a85_val;
15static guint a85_cnt; 71static guint a85_cnt;
16static guchar a85_buf[80], *a85_ptr; 72static guchar a85_buf[LINELENGTH], *a85_ptr;
17 73
18static void 74static void
19a85_init (void) 75a85_init (void)
20{ 76{
21 a85_cnt = 4; 77 a85_cnt = 4;
51 } 107 }
52 } 108 }
53 109
54} 110}
55 111
112static void
56a85_finish (PerlIO *fp) 113a85_finish (PerlIO *fp)
57{ 114{
58 while (a85_cnt != 4) 115 while (a85_cnt != 4)
59 a85_push (fp, 0); 116 a85_push (fp, 0);
60 117
63 *a85_ptr++ = '\n'; 120 *a85_ptr++ = '\n';
64 121
65 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 122 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
66} 123}
67 124
125/////////////////////////////////////////////////////////////////////////////
126
68MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 127MODULE = Gtk2::CV PACKAGE = Gtk2::CV
69 128
70PROTOTYPES: ENABLE 129PROTOTYPES: ENABLE
130
131# missing in Gtk2 perl module
132
133gboolean
134gdk_net_wm_supports (GdkAtom property)
135 CODE:
136#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
137 RETVAL = gdk_net_wm_supports (property);
138#else
139 RETVAL = 0;
140#endif
141 OUTPUT:
142 RETVAL
143
144GdkPixbuf_noinc *
145dealpha_expose (GdkPixbuf *pb)
146 CODE:
147{
148 int w = gdk_pixbuf_get_width (pb);
149 int h = gdk_pixbuf_get_height (pb);
150 int bpp = gdk_pixbuf_get_n_channels (pb);
151 int x, y, i;
152 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
153 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
154
155 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
156
157 dst = gdk_pixbuf_get_pixels (RETVAL);
158 dstr = gdk_pixbuf_get_rowstride (RETVAL);
159
160 for (x = 0; x < w; x++)
161 for (y = 0; y < h; y++)
162 for (i = 0; i < 3; i++)
163 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
164}
165 OUTPUT:
166 RETVAL
71 167
72GdkPixbuf_noinc * 168GdkPixbuf_noinc *
73transpose (GdkPixbuf *pb) 169transpose (GdkPixbuf *pb)
74 CODE: 170 CODE:
75{ 171{
76 int w = gdk_pixbuf_get_width (pb); 172 int w = gdk_pixbuf_get_width (pb);
77 int h = gdk_pixbuf_get_height (pb); 173 int h = gdk_pixbuf_get_height (pb);
78 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 174 int bpp = gdk_pixbuf_get_n_channels (pb);
79 int x, y, i; 175 int x, y, i;
80 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 176 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
81 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 177 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
82 178
83 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 179 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
97flop (GdkPixbuf *pb) 193flop (GdkPixbuf *pb)
98 CODE: 194 CODE:
99{ 195{
100 int w = gdk_pixbuf_get_width (pb); 196 int w = gdk_pixbuf_get_width (pb);
101 int h = gdk_pixbuf_get_height (pb); 197 int h = gdk_pixbuf_get_height (pb);
102 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 198 int bpp = gdk_pixbuf_get_n_channels (pb);
103 int x, y, i; 199 int x, y, i;
104 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 200 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
105 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 201 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
106 202
107 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 203 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
115 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 211 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
116} 212}
117 OUTPUT: 213 OUTPUT:
118 RETVAL 214 RETVAL
119 215
216GdkPixbuf_noinc *
217load_jpeg (SV *path, int thumbnail=0)
218 CODE:
219{
220 struct jpeg_decompress_struct cinfo;
221 struct jpg_err_mgr jerr;
222 guchar *data;
223 int rs;
224 FILE *fp;
225 volatile GdkPixbuf *pb = 0;
226 gchar *filename;
227
228 RETVAL = 0;
229
230 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
231 fp = fopen (filename, "rb");
232 g_free (filename);
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
287 data = gdk_pixbuf_get_pixels (RETVAL);
288 rs = gdk_pixbuf_get_rowstride (RETVAL);
289
290 if (cinfo.output_components != 3)
291 longjmp (jerr.setjmp_buffer, 3);
292
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);
311}
312 OUTPUT:
313 RETVAL
314
315#############################################################################
316
120MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 317MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
121 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
122GdkPixbuf_noinc * 361GdkPixbuf_noinc *
123p7_to_pb (int w, int h, guchar *src) 362p7_to_pb (int w, int h, SV *src_sv)
363 PROTOTYPE: @
124 CODE: 364 CODE:
125{ 365{
126 int x, y; 366 int x, y;
127 guchar *dst, *d; 367 guchar *dst, *d;
128 int dstr; 368 int dstr;
369 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
129 370
130 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 371 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
131 dst = gdk_pixbuf_get_pixels (RETVAL); 372 dst = gdk_pixbuf_get_pixels (RETVAL);
132 dstr = gdk_pixbuf_get_rowstride (RETVAL); 373 dstr = gdk_pixbuf_get_rowstride (RETVAL);
133 374
150{ 391{
151 int w = gdk_pixbuf_get_width (pb); 392 int w = gdk_pixbuf_get_width (pb);
152 int h = gdk_pixbuf_get_height (pb); 393 int h = gdk_pixbuf_get_height (pb);
153 int x, y; 394 int x, y;
154 guchar *dst; 395 guchar *dst;
155 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 396 int bpp = gdk_pixbuf_get_n_channels (pb);
156 guchar *src = gdk_pixbuf_get_pixels (pb); 397 guchar *src = gdk_pixbuf_get_pixels (pb);
157 int sstr = gdk_pixbuf_get_rowstride (pb); 398 int sstr = gdk_pixbuf_get_rowstride (pb);
158 int Er[IW], Eg[IW], Eb[IW]; 399 int Er[IW], Eg[IW], Eb[IW];
159 int seed = 77; 400 int seed = 77;
160 401
161 RETVAL = newSV (w * h); 402 RETVAL = newSV (w * h);
162 SvPOK_only (RETVAL); 403 SvPOK_only (RETVAL);
163 SvCUR_set (RETVAL, w * h); 404 SvCUR_set (RETVAL, w * h);
164 405
165 dst = SvPVX (RETVAL); 406 dst = (guchar *)SvPVX (RETVAL);
166 407
167 memset (Er, 0, sizeof (int) * IW); 408 memset (Er, 0, sizeof (int) * IW);
168 memset (Eg, 0, sizeof (int) * IW); 409 memset (Eg, 0, sizeof (int) * IW);
169 memset (Eb, 0, sizeof (int) * IW); 410 memset (Eb, 0, sizeof (int) * IW);
170 411
177 for (x = 0; x < w; x++) 418 for (x = 0; x < w; x++)
178 { 419 {
179 int r, g, b; 420 int r, g, b;
180 guchar *p = src + x * bpp + y * sstr; 421 guchar *p = src + x * bpp + y * sstr;
181 422
182 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 423 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
183 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 424 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
184 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 425 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
185 426
186 r = r > 7 ? 7 : r < 0 ? 0 : r; 427 r = r > 7 ? 7 : r < 0 ? 0 : r;
187 g = g > 7 ? 7 : g < 0 ? 0 : g; 428 g = g > 7 ? 7 : g < 0 ? 0 : g;
188 b = b > 3 ? 3 : b < 0 ? 0 : b; 429 b = b > 3 ? 3 : b < 0 ? 0 : b;
189 430
190 er += p[0] - (r * 255 + 4) / 7; 431 er += p[0] - (r * 255 + 4) / 7;
191 eg += p[1] - (g * 255 + 4) / 7; 432 eg += p[1] - (g * 255 + 4) / 7;
192 eb += p[2] - (b * 255 + 2) / 3; 433 eb += p[2] - (b * 255 + 2) / 3;
193 434
194 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 435 Er[x] = er >> 1; er -= (er + 1) >> 1;
195 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 436 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
196 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 437 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
197 438
198 *dst++ = r << 5 | g << 2 | b; 439 *dst++ = r << 5 | g << 2 | b;
199 } 440 }
200 } 441 }
201} 442}
202 OUTPUT: 443 OUTPUT:
203 RETVAL 444 RETVAL
204 445
446#############################################################################
447
205MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 448MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
206 449
207void 450void
208dump_pb (PerlIO *fp, GdkPixbuf *pb) 451dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
209 CODE: 452 CODE:
210{ 453{
211 int w = gdk_pixbuf_get_width (pb); 454 int w = gdk_pixbuf_get_width (pb);
212 int h = gdk_pixbuf_get_height (pb); 455 int h = gdk_pixbuf_get_height (pb);
213 int x, y, i; 456 int x, y, i;
214 guchar *dst; 457 guchar *dst;
215 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 458 int bpp = gdk_pixbuf_get_n_channels (pb);
216 guchar *src = gdk_pixbuf_get_pixels (pb); 459 guchar *src = gdk_pixbuf_get_pixels (pb);
217 int sstr = gdk_pixbuf_get_rowstride (pb); 460 int sstr = gdk_pixbuf_get_rowstride (pb);
218 461
219 a85_init (); 462 a85_init ();
220 463
221 for (y = 0; y < h; y++) 464 for (y = 0; y < h; y++)
222 for (x = 0; x < w; x++) 465 for (x = 0; x < w; x++)
223 for (i = 0; i < 3; i++) 466 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
224 a85_push (fp, src [x * bpp + y * sstr + i]); 467 a85_push (fp, src [x * bpp + y * sstr + i]);
225 468
226 a85_finish (fp); 469 a85_finish (fp);
227} 470}
228 471
472void
473dump_binary (PerlIO *fp, GdkPixbuf *pb)
474 CODE:
475{
476 int w = gdk_pixbuf_get_width (pb);
477 int h = gdk_pixbuf_get_height (pb);
478 int x, y, i;
479 guchar *dst;
480 int bpp = gdk_pixbuf_get_n_channels (pb);
481 guchar *src = gdk_pixbuf_get_pixels (pb);
482 int sstr = gdk_pixbuf_get_rowstride (pb);
229 483
484 for (y = 0; y < h; y++)
485 for (x = 0; x < w; x++)
486 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
487 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
488}
230 489
490#############################################################################
231 491
492MODULE = Gtk2::CV PACKAGE = Gtk2::CV
493
494SV *
495pb_to_hv84 (GdkPixbuf *pb)
496 CODE:
497{
498 int w = gdk_pixbuf_get_width (pb);
499 int h = gdk_pixbuf_get_height (pb);
500 int x, y;
501 guchar *dst;
502 int bpp = gdk_pixbuf_get_n_channels (pb);
503 guchar *src = gdk_pixbuf_get_pixels (pb);
504 int sstr = gdk_pixbuf_get_rowstride (pb);
505
506 RETVAL = newSV (6 * 8 * 12 / 8);
507 SvPOK_only (RETVAL);
508 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
509
510 dst = (guchar *)SvPVX (RETVAL);
511
512 /* some primitive error distribution + random dithering */
513
514 for (y = 0; y < h; y++)
515 {
516 guchar *p = src + y * sstr;
517
518 for (x = 0; x < w; x += 2)
519 {
520 unsigned int r, g, b, h, s, v, H, V1, V2;
521
522 if (bpp == 3)
523 r = *p++, g = *p++, b = *p++;
524 else if (bpp == 1)
525 r = g = b = *p++;
526 else
527 abort ();
528
529 rgb_to_hsv (r, g, b, &h, &s, &v);
530
531 H = (h * 15 / 255) << 4;
532 V1 = v;
533
534 if (bpp == 3)
535 r = *p++, g = *p++, b = *p++;
536 else if (bpp == 1)
537 r = g = b = *p++;
538 else
539 abort ();
540
541 rgb_to_hsv (r, g, b, &h, &s, &v);
542
543 H |= h * 15 / 255;
544 V2 = v;
545
546 *dst++ = H;
547 *dst++ = V1;
548 *dst++ = V2;
549 }
550 }
551}
552 OUTPUT:
553 RETVAL
554
555SV *
556hv84_to_av (unsigned char *hv84)
557 CODE:
558{
559 int i = 72 / 3;
560 AV *av = newAV ();
561
562 RETVAL = (SV *)newRV_noinc ((SV *)av);
563 while (i--)
564 {
565 int h = *hv84++;
566 int v1 = *hv84++;
567 int v2 = *hv84++;
568
569 av_push (av, newSViv (v1));
570 av_push (av, newSViv ((h >> 4) * 255 / 15));
571 av_push (av, newSViv (v2));
572 av_push (av, newSViv ((h & 15) * 255 / 15));
573 }
574}
575 OUTPUT:
576 RETVAL
577
578#############################################################################
579
580MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
581
582SV *
583make_histograms (SV *ar)
584 CODE:
585{
586 int i;
587 AV *av, *result;
588
589 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
590 croak ("Not an array ref as first argument to make_histogram");
591
592 av = (AV *) SvRV (ar);
593 result = newAV ();
594
595 for (i = 0; i <= av_len (av); ++i)
596 {
597 const int HISTSIZE = 64;
598
599 int j;
600 SV *sv = *av_fetch (av, i, 1);
601 STRLEN len;
602 char *buf = SvPVbyte (sv, len);
603
604 int tmphist[HISTSIZE];
605 float *hist;
606
607 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
608 SvPOK_on (histsv);
609 SvCUR_set (histsv, HISTSIZE * sizeof (float));
610 hist = (float *)SvPVX (histsv);
611
612 Zero (tmphist, sizeof (tmphist), char);
613
614 for (j = len; j--; )
615 {
616 unsigned int idx
617 = ((*buf & 0xc0) >> 2)
618 | ((*buf & 0x18) >> 1)
619 | (*buf & 0x03);
620
621 ++tmphist[idx];
622 ++buf;
623 }
624
625 for (j = 0; j < HISTSIZE; ++j)
626 hist[j] = (float)tmphist[j] / (len + 1e-30);
627
628 av_push (result, histsv);
629 }
630
631 RETVAL = newRV_noinc ((SV *)result);
632}
633 OUTPUT:
634 RETVAL
635
636

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines