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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines