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

Comparing CV/CV.xs (file contents):
Revision 1.6 by root, Wed Nov 12 00:59:01 2003 UTC vs.
Revision 1.25 by root, Fri Aug 19 00:34:54 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)
13 20
21#define LINELENGTH 240
22
14#define ELLIPSIS "\xe2\x80\xa6" 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}
15 69
16static guint32 a85_val; 70static guint32 a85_val;
17static guint a85_cnt; 71static guint a85_cnt;
18static guchar a85_buf[80], *a85_ptr; 72static guchar a85_buf[LINELENGTH], *a85_ptr;
19 73
20static void 74static void
21a85_init (void) 75a85_init (void)
22{ 76{
23 a85_cnt = 4; 77 a85_cnt = 4;
53 } 107 }
54 } 108 }
55 109
56} 110}
57 111
112static void
58a85_finish (PerlIO *fp) 113a85_finish (PerlIO *fp)
59{ 114{
60 while (a85_cnt != 4) 115 while (a85_cnt != 4)
61 a85_push (fp, 0); 116 a85_push (fp, 0);
62 117
67 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 122 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
68} 123}
69 124
70///////////////////////////////////////////////////////////////////////////// 125/////////////////////////////////////////////////////////////////////////////
71 126
72MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 127MODULE = Gtk2::CV PACKAGE = Gtk2::CV
73 128
74PROTOTYPES: 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
75 143
76GdkPixbuf_noinc * 144GdkPixbuf_noinc *
77transpose (GdkPixbuf *pb) 145transpose (GdkPixbuf *pb)
78 CODE: 146 CODE:
79{ 147{
80 int w = gdk_pixbuf_get_width (pb); 148 int w = gdk_pixbuf_get_width (pb);
81 int h = gdk_pixbuf_get_height (pb); 149 int h = gdk_pixbuf_get_height (pb);
82 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 150 int bpp = gdk_pixbuf_get_n_channels (pb);
83 int x, y, i; 151 int x, y, i;
84 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 152 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
85 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 153 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
86 154
87 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 155 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
101flop (GdkPixbuf *pb) 169flop (GdkPixbuf *pb)
102 CODE: 170 CODE:
103{ 171{
104 int w = gdk_pixbuf_get_width (pb); 172 int w = gdk_pixbuf_get_width (pb);
105 int h = gdk_pixbuf_get_height (pb); 173 int h = gdk_pixbuf_get_height (pb);
106 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 174 int bpp = gdk_pixbuf_get_n_channels (pb);
107 int x, y, i; 175 int x, y, i;
108 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 176 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
109 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 177 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
110 178
111 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 179 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
119 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 187 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
120} 188}
121 OUTPUT: 189 OUTPUT:
122 RETVAL 190 RETVAL
123 191
192GdkPixbuf_noinc *
193load_jpeg (SV *path, int thumbnail=0)
194 CODE:
195{
196 struct jpeg_decompress_struct cinfo;
197 struct jpg_err_mgr jerr;
198 guchar *data;
199 int rs;
200 FILE *fp;
201 volatile GdkPixbuf *pb = 0;
202 gchar *filename;
203
204 RETVAL = 0;
205
206 filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0);
207 fp = fopen (filename, "rb");
208 g_free (filename);
209
210 if (!fp)
211 XSRETURN_UNDEF;
212
213 cinfo.err = jpeg_std_error (&jerr.err);
214
215 jerr.err.error_exit = cv_error_exit;
216 jerr.err.output_message = cv_error_output;
217
218 if ((rs = setjmp (jerr.setjmp_buffer)))
219 {
220 fclose (fp);
221 jpeg_destroy_decompress (&cinfo);
222
223 if (pb)
224 g_object_unref ((gpointer)pb);
225
226 XSRETURN_UNDEF;
227 }
228
229 jpeg_create_decompress (&cinfo);
230
231 jpeg_stdio_src (&cinfo, fp);
232 jpeg_read_header (&cinfo, TRUE);
233
234 cinfo.dct_method = JDCT_DEFAULT;
235 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
236 cinfo.do_block_smoothing = FALSE;
237 cinfo.out_color_space = JCS_RGB;
238 cinfo.quantize_colors = FALSE;
239
240 cinfo.scale_num = 1;
241 cinfo.scale_denom = 1;
242
243 jpeg_calc_output_dimensions (&cinfo);
244
245 if (thumbnail)
246 {
247 cinfo.dct_method = JDCT_FASTEST;
248 cinfo.do_fancy_upsampling = FALSE;
249
250 while (cinfo.scale_denom < 8
251 && cinfo.output_width >= IW*4
252 && cinfo.output_height >= IH*4)
253 {
254 cinfo.scale_denom <<= 1;
255 jpeg_calc_output_dimensions (&cinfo);
256 }
257 }
258
259 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
260 if (!RETVAL)
261 longjmp (jerr.setjmp_buffer, 2);
262
263 data = gdk_pixbuf_get_pixels (RETVAL);
264 rs = gdk_pixbuf_get_rowstride (RETVAL);
265
266 if (cinfo.output_components != 3)
267 longjmp (jerr.setjmp_buffer, 3);
268
269 jpeg_start_decompress (&cinfo);
270
271 while (cinfo.output_scanline < cinfo.output_height)
272 {
273 int remaining = cinfo.output_height - cinfo.output_scanline;
274 JSAMPROW rp[4];
275
276 rp [0] = data + cinfo.output_scanline * rs;
277 rp [1] = (guchar *)rp [0] + rs;
278 rp [2] = (guchar *)rp [1] + rs;
279 rp [3] = (guchar *)rp [2] + rs;
280
281 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
282 }
283
284 jpeg_finish_decompress (&cinfo);
285 fclose (fp);
286 jpeg_destroy_decompress (&cinfo);
287}
288 OUTPUT:
289 RETVAL
290
124############################################################################# 291#############################################################################
125 292
126MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 293MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
127 294
295SV *
296foldcase (SV *pathsv)
297 PROTOTYPE: $
298 CODE:
299{
300 STRLEN plen;
301 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
302 U8 *pend = path + plen;
303 U8 dst [plen * 6 * 3], *dstp = dst;
304
305 while (path < pend)
306 {
307 U8 ch = *path;
308
309 if (ch >= 'a' && ch <= 'z')
310 *dstp++ = *path++;
311 else if (ch >= '0' && ch <= '9')
312 {
313 STRLEN el, nl = 0;
314 while (*path >= '0' && *path <= '9' && path < pend)
315 path++, nl++;
316
317 for (el = nl; el < 6; el++)
318 *dstp++ = '0';
319
320 memcpy (dstp, path - nl, nl);
321 dstp += nl;
322 }
323 else
324 {
325 STRLEN cl;
326 to_utf8_fold (path, dstp, &cl);
327 dstp += cl;
328 path += is_utf8_char (path);
329 }
330 }
331
332 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
333}
334 OUTPUT:
335 RETVAL
336
128GdkPixbuf_noinc * 337GdkPixbuf_noinc *
129p7_to_pb (int w, int h, guchar *src) 338p7_to_pb (int w, int h, SV *src_sv)
339 PROTOTYPE: @
130 CODE: 340 CODE:
131{ 341{
132 int x, y; 342 int x, y;
133 guchar *dst, *d; 343 guchar *dst, *d;
134 int dstr; 344 int dstr;
345 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
135 346
136 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 347 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
137 dst = gdk_pixbuf_get_pixels (RETVAL); 348 dst = gdk_pixbuf_get_pixels (RETVAL);
138 dstr = gdk_pixbuf_get_rowstride (RETVAL); 349 dstr = gdk_pixbuf_get_rowstride (RETVAL);
139 350
156{ 367{
157 int w = gdk_pixbuf_get_width (pb); 368 int w = gdk_pixbuf_get_width (pb);
158 int h = gdk_pixbuf_get_height (pb); 369 int h = gdk_pixbuf_get_height (pb);
159 int x, y; 370 int x, y;
160 guchar *dst; 371 guchar *dst;
161 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 372 int bpp = gdk_pixbuf_get_n_channels (pb);
162 guchar *src = gdk_pixbuf_get_pixels (pb); 373 guchar *src = gdk_pixbuf_get_pixels (pb);
163 int sstr = gdk_pixbuf_get_rowstride (pb); 374 int sstr = gdk_pixbuf_get_rowstride (pb);
164 int Er[IW], Eg[IW], Eb[IW]; 375 int Er[IW], Eg[IW], Eb[IW];
165 int seed = 77; 376 int seed = 77;
166 377
167 RETVAL = newSV (w * h); 378 RETVAL = newSV (w * h);
168 SvPOK_only (RETVAL); 379 SvPOK_only (RETVAL);
169 SvCUR_set (RETVAL, w * h); 380 SvCUR_set (RETVAL, w * h);
170 381
171 dst = SvPVX (RETVAL); 382 dst = (guchar *)SvPVX (RETVAL);
172 383
173 memset (Er, 0, sizeof (int) * IW); 384 memset (Er, 0, sizeof (int) * IW);
174 memset (Eg, 0, sizeof (int) * IW); 385 memset (Eg, 0, sizeof (int) * IW);
175 memset (Eb, 0, sizeof (int) * IW); 386 memset (Eb, 0, sizeof (int) * IW);
176 387
183 for (x = 0; x < w; x++) 394 for (x = 0; x < w; x++)
184 { 395 {
185 int r, g, b; 396 int r, g, b;
186 guchar *p = src + x * bpp + y * sstr; 397 guchar *p = src + x * bpp + y * sstr;
187 398
188 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 399 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
189 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 400 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
190 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 401 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
191 402
192 r = r > 7 ? 7 : r < 0 ? 0 : r; 403 r = r > 7 ? 7 : r < 0 ? 0 : r;
193 g = g > 7 ? 7 : g < 0 ? 0 : g; 404 g = g > 7 ? 7 : g < 0 ? 0 : g;
194 b = b > 3 ? 3 : b < 0 ? 0 : b; 405 b = b > 3 ? 3 : b < 0 ? 0 : b;
195 406
196 er += p[0] - (r * 255 + 4) / 7; 407 er += p[0] - (r * 255 + 4) / 7;
197 eg += p[1] - (g * 255 + 4) / 7; 408 eg += p[1] - (g * 255 + 4) / 7;
198 eb += p[2] - (b * 255 + 2) / 3; 409 eb += p[2] - (b * 255 + 2) / 3;
199 410
200 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 411 Er[x] = er >> 1; er -= (er + 1) >> 1;
201 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 412 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
202 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 413 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
203 414
204 *dst++ = r << 5 | g << 2 | b; 415 *dst++ = r << 5 | g << 2 | b;
205 } 416 }
206 } 417 }
207} 418}
211############################################################################# 422#############################################################################
212 423
213MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 424MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
214 425
215void 426void
216dump_pb (PerlIO *fp, GdkPixbuf *pb) 427dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
217 CODE: 428 CODE:
218{ 429{
219 int w = gdk_pixbuf_get_width (pb); 430 int w = gdk_pixbuf_get_width (pb);
220 int h = gdk_pixbuf_get_height (pb); 431 int h = gdk_pixbuf_get_height (pb);
221 int x, y, i; 432 int x, y, i;
222 guchar *dst; 433 guchar *dst;
223 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 434 int bpp = gdk_pixbuf_get_n_channels (pb);
224 guchar *src = gdk_pixbuf_get_pixels (pb); 435 guchar *src = gdk_pixbuf_get_pixels (pb);
225 int sstr = gdk_pixbuf_get_rowstride (pb); 436 int sstr = gdk_pixbuf_get_rowstride (pb);
226 437
227 a85_init (); 438 a85_init ();
228 439
229 for (y = 0; y < h; y++) 440 for (y = 0; y < h; y++)
230 for (x = 0; x < w; x++) 441 for (x = 0; x < w; x++)
231 for (i = 0; i < 3; i++) 442 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
232 a85_push (fp, src [x * bpp + y * sstr + i]); 443 a85_push (fp, src [x * bpp + y * sstr + i]);
233 444
234 a85_finish (fp); 445 a85_finish (fp);
235} 446}
236 447
448void
449dump_binary (PerlIO *fp, GdkPixbuf *pb)
450 CODE:
451{
452 int w = gdk_pixbuf_get_width (pb);
453 int h = gdk_pixbuf_get_height (pb);
454 int x, y, i;
455 guchar *dst;
456 int bpp = gdk_pixbuf_get_n_channels (pb);
457 guchar *src = gdk_pixbuf_get_pixels (pb);
458 int sstr = gdk_pixbuf_get_rowstride (pb);
237 459
460 for (y = 0; y < h; y++)
461 for (x = 0; x < w; x++)
462 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
463 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
464}
238 465
466#############################################################################
239 467
468MODULE = Gtk2::CV PACKAGE = Gtk2::CV
469
470SV *
471pb_to_hv84 (GdkPixbuf *pb)
472 CODE:
473{
474 int w = gdk_pixbuf_get_width (pb);
475 int h = gdk_pixbuf_get_height (pb);
476 int x, y;
477 guchar *dst;
478 int bpp = gdk_pixbuf_get_n_channels (pb);
479 guchar *src = gdk_pixbuf_get_pixels (pb);
480 int sstr = gdk_pixbuf_get_rowstride (pb);
481
482 RETVAL = newSV (6 * 8 * 12 / 8);
483 SvPOK_only (RETVAL);
484 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
485
486 dst = (guchar *)SvPVX (RETVAL);
487
488 /* some primitive error distribution + random dithering */
489
490 for (y = 0; y < h; y++)
491 {
492 guchar *p = src + y * sstr;
493
494 for (x = 0; x < w; x += 2)
495 {
496 unsigned int r, g, b, h, s, v, H, V1, V2;
497
498 if (bpp == 3)
499 r = *p++, g = *p++, b = *p++;
500 else if (bpp == 1)
501 r = g = b = *p++;
502 else
503 abort ();
504
505 rgb_to_hsv (r, g, b, &h, &s, &v);
506
507 H = (h * 15 / 255) << 4;
508 V1 = v;
509
510 if (bpp == 3)
511 r = *p++, g = *p++, b = *p++;
512 else if (bpp == 1)
513 r = g = b = *p++;
514 else
515 abort ();
516
517 rgb_to_hsv (r, g, b, &h, &s, &v);
518
519 H |= h * 15 / 255;
520 V2 = v;
521
522 *dst++ = H;
523 *dst++ = V1;
524 *dst++ = V2;
525 }
526 }
527}
528 OUTPUT:
529 RETVAL
530
531SV *
532hv84_to_av (unsigned char *hv84)
533 CODE:
534{
535 int i = 72 / 3;
536 AV *av = newAV ();
537
538 RETVAL = (SV *)newRV_noinc ((SV *)av);
539 while (i--)
540 {
541 int h = *hv84++;
542 int v1 = *hv84++;
543 int v2 = *hv84++;
544
545 av_push (av, newSViv (v1));
546 av_push (av, newSViv ((h >> 4) * 255 / 15));
547 av_push (av, newSViv (v2));
548 av_push (av, newSViv ((h & 15) * 255 / 15));
549 }
550}
551 OUTPUT:
552 RETVAL
553
554#############################################################################
555
556MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
557
558SV *
559make_histograms (SV *ar)
560 CODE:
561{
562 int i;
563 AV *av, *result;
564
565 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
566 croak ("Not an array ref as first argument to make_histogram");
567
568 av = (AV *) SvRV (ar);
569 result = newAV ();
570
571 for (i = 0; i <= av_len (av); ++i)
572 {
573 const int HISTSIZE = 64;
574
575 int j;
576 SV *sv = *av_fetch (av, i, 1);
577 STRLEN len;
578 char *buf = SvPVbyte (sv, len);
579
580 int tmphist[HISTSIZE];
581 float *hist;
582
583 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
584 SvPOK_on (histsv);
585 SvCUR_set (histsv, HISTSIZE * sizeof (float));
586 hist = (float *)SvPVX (histsv);
587
588 Zero (tmphist, sizeof (tmphist), char);
589
590 for (j = len; j--; )
591 {
592 unsigned int idx
593 = ((*buf & 0xc0) >> 2)
594 | ((*buf & 0x18) >> 1)
595 | (*buf & 0x03);
596
597 ++tmphist[idx];
598 ++buf;
599 }
600
601 for (j = 0; j < HISTSIZE; ++j)
602 hist[j] = (float)tmphist[j] / (len + 1e-30);
603
604 av_push (result, histsv);
605 }
606
607 RETVAL = newRV_noinc ((SV *)result);
608}
609 OUTPUT:
610 RETVAL
611
612

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines