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.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)
13 17
18#define LINELENGTH 240
19
14#define ELLIPSIS "\xe2\x80\xa6" 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}
15 66
16static guint32 a85_val; 67static guint32 a85_val;
17static guint a85_cnt; 68static guint a85_cnt;
18static guchar a85_buf[80], *a85_ptr; 69static guchar a85_buf[LINELENGTH], *a85_ptr;
19 70
20static void 71static void
21a85_init (void) 72a85_init (void)
22{ 73{
23 a85_cnt = 4; 74 a85_cnt = 4;
53 } 104 }
54 } 105 }
55 106
56} 107}
57 108
109static void
58a85_finish (PerlIO *fp) 110a85_finish (PerlIO *fp)
59{ 111{
60 while (a85_cnt != 4) 112 while (a85_cnt != 4)
61 a85_push (fp, 0); 113 a85_push (fp, 0);
62 114
67 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 119 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
68} 120}
69 121
70///////////////////////////////////////////////////////////////////////////// 122/////////////////////////////////////////////////////////////////////////////
71 123
72MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 124MODULE = Gtk2::CV PACKAGE = Gtk2::CV
73 125
74PROTOTYPES: ENABLE 126PROTOTYPES: ENABLE
75 127
76GdkPixbuf_noinc * 128GdkPixbuf_noinc *
77transpose (GdkPixbuf *pb) 129transpose (GdkPixbuf *pb)
78 CODE: 130 CODE:
79{ 131{
80 int w = gdk_pixbuf_get_width (pb); 132 int w = gdk_pixbuf_get_width (pb);
81 int h = gdk_pixbuf_get_height (pb); 133 int h = gdk_pixbuf_get_height (pb);
82 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 134 int bpp = gdk_pixbuf_get_n_channels (pb);
83 int x, y, i; 135 int x, y, i;
84 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 136 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
85 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 137 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
86 138
87 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);
101flop (GdkPixbuf *pb) 153flop (GdkPixbuf *pb)
102 CODE: 154 CODE:
103{ 155{
104 int w = gdk_pixbuf_get_width (pb); 156 int w = gdk_pixbuf_get_width (pb);
105 int h = gdk_pixbuf_get_height (pb); 157 int h = gdk_pixbuf_get_height (pb);
106 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 158 int bpp = gdk_pixbuf_get_n_channels (pb);
107 int x, y, i; 159 int x, y, i;
108 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 160 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
109 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 161 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
110 162
111 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);
115 167
116 for (y = 0; y < h; y++) 168 for (y = 0; y < h; y++)
117 for (x = 0; x < w; x++) 169 for (x = 0; x < w; x++)
118 for (i = 0; i < bpp; i++) 170 for (i = 0; i < bpp; i++)
119 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];
172}
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;
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);
120} 265}
121 OUTPUT: 266 OUTPUT:
122 RETVAL 267 RETVAL
123 268
124############################################################################# 269#############################################################################
156{ 301{
157 int w = gdk_pixbuf_get_width (pb); 302 int w = gdk_pixbuf_get_width (pb);
158 int h = gdk_pixbuf_get_height (pb); 303 int h = gdk_pixbuf_get_height (pb);
159 int x, y; 304 int x, y;
160 guchar *dst; 305 guchar *dst;
161 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 306 int bpp = gdk_pixbuf_get_n_channels (pb);
162 guchar *src = gdk_pixbuf_get_pixels (pb); 307 guchar *src = gdk_pixbuf_get_pixels (pb);
163 int sstr = gdk_pixbuf_get_rowstride (pb); 308 int sstr = gdk_pixbuf_get_rowstride (pb);
164 int Er[IW], Eg[IW], Eb[IW]; 309 int Er[IW], Eg[IW], Eb[IW];
165 int seed = 77; 310 int seed = 77;
166 311
183 for (x = 0; x < w; x++) 328 for (x = 0; x < w; x++)
184 { 329 {
185 int r, g, b; 330 int r, g, b;
186 guchar *p = src + x * bpp + y * sstr; 331 guchar *p = src + x * bpp + y * sstr;
187 332
188 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 333 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
189 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 334 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
190 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 335 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
191 336
192 r = r > 7 ? 7 : r < 0 ? 0 : r; 337 r = r > 7 ? 7 : r < 0 ? 0 : r;
193 g = g > 7 ? 7 : g < 0 ? 0 : g; 338 g = g > 7 ? 7 : g < 0 ? 0 : g;
194 b = b > 3 ? 3 : b < 0 ? 0 : b; 339 b = b > 3 ? 3 : b < 0 ? 0 : b;
195 340
196 er += p[0] - (r * 255 + 4) / 7; 341 er += p[0] - (r * 255 + 4) / 7;
197 eg += p[1] - (g * 255 + 4) / 7; 342 eg += p[1] - (g * 255 + 4) / 7;
198 eb += p[2] - (b * 255 + 2) / 3; 343 eb += p[2] - (b * 255 + 2) / 3;
199 344
200 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3; 345 Er[x] = er >> 1; er -= (er + 1) >> 1;
201 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3; 346 Eg[x] = eg >> 1; eg -= (eg + 1) >> 1;
202 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3; 347 Eb[x] = eb >> 1; eb -= (eb + 1) >> 1;
203 348
204 *dst++ = r << 5 | g << 2 | b; 349 *dst++ = r << 5 | g << 2 | b;
205 } 350 }
206 } 351 }
207} 352}
208 OUTPUT: 353 OUTPUT:
209 RETVAL 354 RETVAL
210 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
211############################################################################# 410#############################################################################
212 411
213MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript 412MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
214 413
215void 414void
216dump_pb (PerlIO *fp, GdkPixbuf *pb) 415dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
217 CODE: 416 CODE:
218{ 417{
219 int w = gdk_pixbuf_get_width (pb); 418 int w = gdk_pixbuf_get_width (pb);
220 int h = gdk_pixbuf_get_height (pb); 419 int h = gdk_pixbuf_get_height (pb);
221 int x, y, i; 420 int x, y, i;
222 guchar *dst; 421 guchar *dst;
223 int bpp = gdk_pixbuf_get_has_alpha (pb) ? 4 : 3; 422 int bpp = gdk_pixbuf_get_n_channels (pb);
224 guchar *src = gdk_pixbuf_get_pixels (pb); 423 guchar *src = gdk_pixbuf_get_pixels (pb);
225 int sstr = gdk_pixbuf_get_rowstride (pb); 424 int sstr = gdk_pixbuf_get_rowstride (pb);
226 425
227 a85_init (); 426 a85_init ();
228 427
229 for (y = 0; y < h; y++) 428 for (y = 0; y < h; y++)
230 for (x = 0; x < w; x++) 429 for (x = 0; x < w; x++)
231 for (i = 0; i < 3; i++) 430 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
232 a85_push (fp, src [x * bpp + y * sstr + i]); 431 a85_push (fp, src [x * bpp + y * sstr + i]);
233 432
234 a85_finish (fp); 433 a85_finish (fp);
235} 434}
236 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);
237 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}
238 453
454#############################################################################
239 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