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.18 by root, Wed Jul 20 09:06:37 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
10#define IW 80 14#define IW 80 /* MUST match Schnauer.pm! */
15#define IH 60 /* MUST match Schnauer.pm! */
11 16
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 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}
13 67
14static guint32 a85_val; 68static guint32 a85_val;
15static guint a85_cnt; 69static guint a85_cnt;
16static guchar a85_buf[80], *a85_ptr; 70static guchar a85_buf[LINELENGTH], *a85_ptr;
17 71
18static void 72static void
19a85_init (void) 73a85_init (void)
20{ 74{
21 a85_cnt = 4; 75 a85_cnt = 4;
51 } 105 }
52 } 106 }
53 107
54} 108}
55 109
110static void
56a85_finish (PerlIO *fp) 111a85_finish (PerlIO *fp)
57{ 112{
58 while (a85_cnt != 4) 113 while (a85_cnt != 4)
59 a85_push (fp, 0); 114 a85_push (fp, 0);
60 115
63 *a85_ptr++ = '\n'; 118 *a85_ptr++ = '\n';
64 119
65 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 120 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
66} 121}
67 122
123/////////////////////////////////////////////////////////////////////////////
124
68MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 125MODULE = Gtk2::CV PACKAGE = Gtk2::CV
69 126
70PROTOTYPES: ENABLE 127PROTOTYPES: ENABLE
71 128
72GdkPixbuf_noinc * 129GdkPixbuf_noinc *
73transpose (GdkPixbuf *pb) 130transpose (GdkPixbuf *pb)
74 CODE: 131 CODE:
75{ 132{
76 int w = gdk_pixbuf_get_width (pb); 133 int w = gdk_pixbuf_get_width (pb);
77 int h = gdk_pixbuf_get_height (pb); 134 int h = gdk_pixbuf_get_height (pb);
78 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 135 int bpp = gdk_pixbuf_get_n_channels (pb);
79 int x, y, i; 136 int x, y, i;
80 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 137 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
81 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 138 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
82 139
83 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);
97flop (GdkPixbuf *pb) 154flop (GdkPixbuf *pb)
98 CODE: 155 CODE:
99{ 156{
100 int w = gdk_pixbuf_get_width (pb); 157 int w = gdk_pixbuf_get_width (pb);
101 int h = gdk_pixbuf_get_height (pb); 158 int h = gdk_pixbuf_get_height (pb);
102 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 159 int bpp = gdk_pixbuf_get_n_channels (pb);
103 int x, y, i; 160 int x, y, i;
104 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 161 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
105 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 162 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
106 163
107 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);
115 dst[(w - 1 - 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];
116} 173}
117 OUTPUT: 174 OUTPUT:
118 RETVAL 175 RETVAL
119 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;
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#############################################################################
271
120MODULE = 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 if (*path >= '0' && *path <= '9')
287 {
288 STRLEN el, nl = 0;
289 while (*path >= '0' && *path <= '9')
290 path++, nl++;
291
292 for (el = nl; el < 6; el++)
293 *dstp++ = '0';
294
295 memcpy (dstp, path - nl, nl);
296 dstp += nl;
297 }
298 else
299 {
300 STRLEN cl;
301 to_utf8_fold (path, dstp, &cl);
302 dstp += cl;
303 path += is_utf8_char (path);
304 }
305 }
306
307 RETVAL = newSVpvn (dst, dstp - dst);
308}
309 OUTPUT:
310 RETVAL
121 311
122GdkPixbuf_noinc * 312GdkPixbuf_noinc *
123p7_to_pb (int w, int h, guchar *src) 313p7_to_pb (int w, int h, guchar *src)
124 CODE: 314 CODE:
125{ 315{
150{ 340{
151 int w = gdk_pixbuf_get_width (pb); 341 int w = gdk_pixbuf_get_width (pb);
152 int h = gdk_pixbuf_get_height (pb); 342 int h = gdk_pixbuf_get_height (pb);
153 int x, y; 343 int x, y;
154 guchar *dst; 344 guchar *dst;
155 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 345 int bpp = gdk_pixbuf_get_n_channels (pb);
156 guchar *src = gdk_pixbuf_get_pixels (pb); 346 guchar *src = gdk_pixbuf_get_pixels (pb);
157 int sstr = gdk_pixbuf_get_rowstride (pb); 347 int sstr = gdk_pixbuf_get_rowstride (pb);
158 int Er[IW], Eg[IW], Eb[IW]; 348 int Er[IW], Eg[IW], Eb[IW];
159 int seed = 77; 349 int seed = 77;
160 350
177 for (x = 0; x < w; x++) 367 for (x = 0; x < w; x++)
178 { 368 {
179 int r, g, b; 369 int r, g, b;
180 guchar *p = src + x * bpp + y * sstr; 370 guchar *p = src + x * bpp + y * sstr;
181 371
182 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 372 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
183 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 373 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
184 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 374 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
185 375
186 r = r > 7 ? 7 : r < 0 ? 0 : r; 376 r = r > 7 ? 7 : r < 0 ? 0 : r;
187 g = g > 7 ? 7 : g < 0 ? 0 : g; 377 g = g > 7 ? 7 : g < 0 ? 0 : g;
188 b = b > 3 ? 3 : b < 0 ? 0 : b; 378 b = b > 3 ? 3 : b < 0 ? 0 : b;
189 379
190 er += p[0] - (r * 255 + 4) / 7; 380 er += p[0] - (r * 255 + 4) / 7;
191 eg += p[1] - (g * 255 + 4) / 7; 381 eg += p[1] - (g * 255 + 4) / 7;
192 eb += p[2] - (b * 255 + 2) / 3; 382 eb += p[2] - (b * 255 + 2) / 3;
193 383
194 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 384 Er[x] = er >> 1; er -= (er + 1) >> 1;
195 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 385 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
196 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 386 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
197 387
198 *dst++ = r << 5 | g << 2 | b; 388 *dst++ = r << 5 | g << 2 | b;
199 } 389 }
200 } 390 }
201} 391}
202 OUTPUT: 392 OUTPUT:
203 RETVAL 393 RETVAL
204 394
395SV *
396make_histogram (SV *ar)
397 CODE:
398{
399 int i;
400 AV *av, *result;
401
402 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
403 croak ("Not an array ref as first argument to make_histogram");
404
405 av = (AV *) SvRV (ar);
406 result = newAV ();
407
408 for (i = 0; i <= av_len (av); ++i)
409 {
410 const int HISTSIZE = 64;
411
412 int j;
413 SV *sv = *av_fetch (av, i, 1);
414 STRLEN len;
415 char *buf = SvPVbyte (sv, len);
416
417 int tmphist[HISTSIZE];
418 float *hist;
419
420 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1);
421 SvPOK_on (histsv);
422 SvCUR_set (histsv, HISTSIZE * sizeof (float));
423 hist = (float *)SvPVX (histsv);
424
425 Zero (tmphist, sizeof (tmphist), char);
426
427 for (j = len; j--; )
428 {
429 unsigned int idx
430 = ((*buf & 0xc0) >> 2)
431 | ((*buf & 0x18) >> 1)
432 | (*buf & 0x03);
433
434 ++tmphist[idx];
435 ++buf;
436 }
437
438 for (j = 0; j < HISTSIZE; ++j)
439 hist[j] = (float)tmphist[j] / (len + 1e-30);
440
441 av_push (result, histsv);
442 }
443
444 RETVAL = newRV_noinc ((SV *)result);
445}
446 OUTPUT:
447 RETVAL
448
449#############################################################################
450
205MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 451MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
206 452
207void 453void
208dump_pb (PerlIO *fp, GdkPixbuf *pb) 454dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
209 CODE: 455 CODE:
210{ 456{
211 int w = gdk_pixbuf_get_width (pb); 457 int w = gdk_pixbuf_get_width (pb);
212 int h = gdk_pixbuf_get_height (pb); 458 int h = gdk_pixbuf_get_height (pb);
213 int x, y, i; 459 int x, y, i;
214 guchar *dst; 460 guchar *dst;
215 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 461 int bpp = gdk_pixbuf_get_n_channels (pb);
216 guchar *src = gdk_pixbuf_get_pixels (pb); 462 guchar *src = gdk_pixbuf_get_pixels (pb);
217 int sstr = gdk_pixbuf_get_rowstride (pb); 463 int sstr = gdk_pixbuf_get_rowstride (pb);
218 464
219 a85_init (); 465 a85_init ();
220 466
221 for (y = 0; y < h; y++) 467 for (y = 0; y < h; y++)
222 for (x = 0; x < w; x++) 468 for (x = 0; x < w; x++)
223 for (i = 0; i < 3; i++) 469 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
224 a85_push (fp, src [x * bpp + y * sstr + i]); 470 a85_push (fp, src [x * bpp + y * sstr + i]);
225 471
226 a85_finish (fp); 472 a85_finish (fp);
227} 473}
228 474
475void
476dump_binary (PerlIO *fp, GdkPixbuf *pb)
477 CODE:
478{
479 int w = gdk_pixbuf_get_width (pb);
480 int h = gdk_pixbuf_get_height (pb);
481 int x, y, i;
482 guchar *dst;
483 int bpp = gdk_pixbuf_get_n_channels (pb);
484 guchar *src = gdk_pixbuf_get_pixels (pb);
485 int sstr = gdk_pixbuf_get_rowstride (pb);
229 486
487 for (y = 0; y < h; y++)
488 for (x = 0; x < w; x++)
489 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
490 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
491}
230 492
493#############################################################################
231 494
495MODULE = Gtk2::CV PACKAGE = Gtk2::CV
496
497SV *
498pb_to_hv84 (GdkPixbuf *pb)
499 CODE:
500{
501 int w = gdk_pixbuf_get_width (pb);
502 int h = gdk_pixbuf_get_height (pb);
503 int x, y;
504 guchar *dst;
505 int bpp = gdk_pixbuf_get_n_channels (pb);
506 guchar *src = gdk_pixbuf_get_pixels (pb);
507 int sstr = gdk_pixbuf_get_rowstride (pb);
508
509 RETVAL = newSV (6 * 8 * 12 / 8);
510 SvPOK_only (RETVAL);
511 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
512
513 dst = SvPVX (RETVAL);
514
515 /* some primitive error distribution + random dithering */
516
517 for (y = 0; y < h; y++)
518 {
519 guchar *p = src + y * sstr;
520
521 for (x = 0; x < w; x += 2)
522 {
523 unsigned int r, g, b, h, s, v, H, V1, V2;
524
525 if (bpp == 3)
526 r = *p++, g = *p++, b = *p++;
527 else if (bpp == 1)
528 r = g = b = *p++;
529 else
530 abort ();
531
532 rgb_to_hsv (r, g, b, &h, &s, &v);
533
534 H = (h * 15 / 255) << 4;
535 V1 = v;
536
537 if (bpp == 3)
538 r = *p++, g = *p++, b = *p++;
539 else if (bpp == 1)
540 r = g = b = *p++;
541 else
542 abort ();
543
544 rgb_to_hsv (r, g, b, &h, &s, &v);
545
546 H |= h * 15 / 255;
547 V2 = v;
548
549 *dst++ = H;
550 *dst++ = V1;
551 *dst++ = V2;
552 }
553 }
554}
555 OUTPUT:
556 RETVAL
557
558SV *
559hv84_to_av (unsigned char *hv84)
560 CODE:
561{
562 int i = 72 / 3;
563 AV *av = newAV ();
564
565 RETVAL = (SV *)newRV_noinc ((SV *)av);
566 while (i--)
567 {
568 int h = *hv84++;
569 int v1 = *hv84++;
570 int v2 = *hv84++;
571
572 av_push (av, newSViv (v1));
573 av_push (av, newSViv ((h >> 4) * 255 / 15));
574 av_push (av, newSViv (v2));
575 av_push (av, newSViv ((h & 15) * 255 / 15));
576 }
577}
578 OUTPUT:
579 RETVAL
580
581
582

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines