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.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
10#define IW 80 13#define IW 80 /* MUST match Schnauer.pm! */
14#define IH 60 /* MUST match Schnauer.pm! */
11 15
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 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}
13 66
14static guint32 a85_val; 67static guint32 a85_val;
15static guint a85_cnt; 68static guint a85_cnt;
16static guchar a85_buf[80], *a85_ptr; 69static guchar a85_buf[LINELENGTH], *a85_ptr;
17 70
18static void 71static void
19a85_init (void) 72a85_init (void)
20{ 73{
21 a85_cnt = 4; 74 a85_cnt = 4;
51 } 104 }
52 } 105 }
53 106
54} 107}
55 108
109static void
56a85_finish (PerlIO *fp) 110a85_finish (PerlIO *fp)
57{ 111{
58 while (a85_cnt != 4) 112 while (a85_cnt != 4)
59 a85_push (fp, 0); 113 a85_push (fp, 0);
60 114
63 *a85_ptr++ = '\n'; 117 *a85_ptr++ = '\n';
64 118
65 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 119 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
66} 120}
67 121
122/////////////////////////////////////////////////////////////////////////////
123
68MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 124MODULE = Gtk2::CV PACKAGE = Gtk2::CV
69 125
70PROTOTYPES: ENABLE 126PROTOTYPES: ENABLE
71 127
72GdkPixbuf_noinc * 128GdkPixbuf_noinc *
73transpose (GdkPixbuf *pb) 129transpose (GdkPixbuf *pb)
74 CODE: 130 CODE:
75{ 131{
76 int w = gdk_pixbuf_get_width (pb); 132 int w = gdk_pixbuf_get_width (pb);
77 int h = gdk_pixbuf_get_height (pb); 133 int h = gdk_pixbuf_get_height (pb);
78 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 134 int bpp = gdk_pixbuf_get_n_channels (pb);
79 int x, y, i; 135 int x, y, i;
80 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 136 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
81 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 137 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
82 138
83 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);
97flop (GdkPixbuf *pb) 153flop (GdkPixbuf *pb)
98 CODE: 154 CODE:
99{ 155{
100 int w = gdk_pixbuf_get_width (pb); 156 int w = gdk_pixbuf_get_width (pb);
101 int h = gdk_pixbuf_get_height (pb); 157 int h = gdk_pixbuf_get_height (pb);
102 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 158 int bpp = gdk_pixbuf_get_n_channels (pb);
103 int x, y, i; 159 int x, y, i;
104 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 160 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
105 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 161 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
106 162
107 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);
114 for (i = 0; i < bpp; i++) 170 for (i = 0; i < bpp; i++)
115 dst[(w - 1 - 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];
116} 172}
117 OUTPUT: 173 OUTPUT:
118 RETVAL 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;
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#############################################################################
119 270
120MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 271MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
121 272
122GdkPixbuf_noinc * 273GdkPixbuf_noinc *
123p7_to_pb (int w, int h, guchar *src) 274p7_to_pb (int w, int h, guchar *src)
150{ 301{
151 int w = gdk_pixbuf_get_width (pb); 302 int w = gdk_pixbuf_get_width (pb);
152 int h = gdk_pixbuf_get_height (pb); 303 int h = gdk_pixbuf_get_height (pb);
153 int x, y; 304 int x, y;
154 guchar *dst; 305 guchar *dst;
155 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 306 int bpp = gdk_pixbuf_get_n_channels (pb);
156 guchar *src = gdk_pixbuf_get_pixels (pb); 307 guchar *src = gdk_pixbuf_get_pixels (pb);
157 int sstr = gdk_pixbuf_get_rowstride (pb); 308 int sstr = gdk_pixbuf_get_rowstride (pb);
158 int Er[IW], Eg[IW], Eb[IW]; 309 int Er[IW], Eg[IW], Eb[IW];
159 int seed = 77; 310 int seed = 77;
160 311
177 for (x = 0; x < w; x++) 328 for (x = 0; x < w; x++)
178 { 329 {
179 int r, g, b; 330 int r, g, b;
180 guchar *p = src + x * bpp + y * sstr; 331 guchar *p = src + x * bpp + y * sstr;
181 332
182 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 333 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
183 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 334 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
184 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 335 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
185 336
186 r = r > 7 ? 7 : r < 0 ? 0 : r; 337 r = r > 7 ? 7 : r < 0 ? 0 : r;
187 g = g > 7 ? 7 : g < 0 ? 0 : g; 338 g = g > 7 ? 7 : g < 0 ? 0 : g;
188 b = b > 3 ? 3 : b < 0 ? 0 : b; 339 b = b > 3 ? 3 : b < 0 ? 0 : b;
189 340
190 er += p[0] - (r * 255 + 4) / 7; 341 er += p[0] - (r * 255 + 4) / 7;
191 eg += p[1] - (g * 255 + 4) / 7; 342 eg += p[1] - (g * 255 + 4) / 7;
192 eb += p[2] - (b * 255 + 2) / 3; 343 eb += p[2] - (b * 255 + 2) / 3;
193 344
194 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 345 Er[x] = er >> 1; er -= (er + 1) >> 1;
195 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 346 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
196 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 347 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
197 348
198 *dst++ = r << 5 | g << 2 | b; 349 *dst++ = r << 5 | g << 2 | b;
199 } 350 }
200 } 351 }
201} 352}
202 OUTPUT: 353 OUTPUT:
203 RETVAL 354 RETVAL
204 355
356SV *
357make_histogram (SV *ar)
358 CODE:
359{
360 int i;
361 AV *av, *result;
362
363 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
364 croak ("Not an array ref as first argument to make_histogram");
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
205MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 412MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
206 413
207void 414void
208dump_pb (PerlIO *fp, GdkPixbuf *pb) 415dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
209 CODE: 416 CODE:
210{ 417{
211 int w = gdk_pixbuf_get_width (pb); 418 int w = gdk_pixbuf_get_width (pb);
212 int h = gdk_pixbuf_get_height (pb); 419 int h = gdk_pixbuf_get_height (pb);
213 int x, y, i; 420 int x, y, i;
214 guchar *dst; 421 guchar *dst;
215 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 422 int bpp = gdk_pixbuf_get_n_channels (pb);
216 guchar *src = gdk_pixbuf_get_pixels (pb); 423 guchar *src = gdk_pixbuf_get_pixels (pb);
217 int sstr = gdk_pixbuf_get_rowstride (pb); 424 int sstr = gdk_pixbuf_get_rowstride (pb);
218 425
219 a85_init (); 426 a85_init ();
220 427
221 for (y = 0; y < h; y++) 428 for (y = 0; y < h; y++)
222 for (x = 0; x < w; x++) 429 for (x = 0; x < w; x++)
223 for (i = 0; i < 3; i++) 430 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
224 a85_push (fp, src [x * bpp + y * sstr + i]); 431 a85_push (fp, src [x * bpp + y * sstr + i]);
225 432
226 a85_finish (fp); 433 a85_finish (fp);
227} 434}
228 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);
229 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}
230 453
454#############################################################################
231 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