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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines