ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.17
Committed: Wed Jul 20 06:20:36 2005 UTC (18 years, 10 months ago) by root
Branch: MAIN
Changes since 1.16: +149 -36 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <setjmp.h>
6
7 #include <jpeglib.h>
8 #include <gdk-pixbuf/gdk-pixbuf.h>
9
10 #include <gperl.h>
11 #include <gtk2perl.h>
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
22 struct jpg_err_mgr
23 {
24 struct jpeg_error_mgr err;
25 jmp_buf setjmp_buffer;
26 };
27
28 static void
29 cv_error_exit (j_common_ptr cinfo)
30 {
31 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
32 }
33
34 static void
35 cv_error_output (j_common_ptr cinfo)
36 {
37 return;
38 }
39
40 static void
41 rgb_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
67 static guint32 a85_val;
68 static guint a85_cnt;
69 static guchar a85_buf[LINELENGTH], *a85_ptr;
70
71 static void
72 a85_init (void)
73 {
74 a85_cnt = 4;
75 a85_ptr = a85_buf;
76 }
77
78 static void
79 a85_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
109 static void
110 a85_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
124 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
125
126 PROTOTYPES: ENABLE
127
128 GdkPixbuf_noinc *
129 transpose (GdkPixbuf *pb)
130 CODE:
131 {
132 int w = gdk_pixbuf_get_width (pb);
133 int h = gdk_pixbuf_get_height (pb);
134 int bpp = gdk_pixbuf_get_n_channels (pb);
135 int x, y, i;
136 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
137 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
138
139 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w);
140
141 dst = gdk_pixbuf_get_pixels (RETVAL);
142 dstr = gdk_pixbuf_get_rowstride (RETVAL);
143
144 for (y = 0; y < h; y++)
145 for (x = 0; x < w; x++)
146 for (i = 0; i < bpp; i++)
147 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i];
148 }
149 OUTPUT:
150 RETVAL
151
152 GdkPixbuf_noinc *
153 flop (GdkPixbuf *pb)
154 CODE:
155 {
156 int w = gdk_pixbuf_get_width (pb);
157 int h = gdk_pixbuf_get_height (pb);
158 int bpp = gdk_pixbuf_get_n_channels (pb);
159 int x, y, i;
160 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
161 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
162
163 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
164
165 dst = gdk_pixbuf_get_pixels (RETVAL);
166 dstr = gdk_pixbuf_get_rowstride (RETVAL);
167
168 for (y = 0; y < h; y++)
169 for (x = 0; x < w; x++)
170 for (i = 0; i < bpp; i++)
171 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
172 }
173 OUTPUT:
174 RETVAL
175
176 GdkPixbuf_noinc *
177 load_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 #############################################################################
270
271 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
272
273 GdkPixbuf_noinc *
274 p7_to_pb (int w, int h, guchar *src)
275 CODE:
276 {
277 int x, y;
278 guchar *dst, *d;
279 int dstr;
280
281 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
282 dst = gdk_pixbuf_get_pixels (RETVAL);
283 dstr = gdk_pixbuf_get_rowstride (RETVAL);
284
285 for (y = 0; y < h; y++)
286 for (d = dst + y * dstr, x = 0; x < w; x++)
287 {
288 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
289 *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
290 *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
291
292 src++;
293 }
294 }
295 OUTPUT:
296 RETVAL
297
298 SV *
299 pb_to_p7 (GdkPixbuf *pb)
300 CODE:
301 {
302 int w = gdk_pixbuf_get_width (pb);
303 int h = gdk_pixbuf_get_height (pb);
304 int x, y;
305 guchar *dst;
306 int bpp = gdk_pixbuf_get_n_channels (pb);
307 guchar *src = gdk_pixbuf_get_pixels (pb);
308 int sstr = gdk_pixbuf_get_rowstride (pb);
309 int Er[IW], Eg[IW], Eb[IW];
310 int seed = 77;
311
312 RETVAL = newSV (w * h);
313 SvPOK_only (RETVAL);
314 SvCUR_set (RETVAL, w * h);
315
316 dst = SvPVX (RETVAL);
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
324 for (y = 0; y < h; y++)
325 {
326 int er = 0, eg = 0, eb = 0;
327
328 for (x = 0; x < w; x++)
329 {
330 int r, g, b;
331 guchar *p = src + x * bpp + y * sstr;
332
333 r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255;
334 g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255;
335 b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255;
336
337 r = r > 7 ? 7 : r < 0 ? 0 : r;
338 g = g > 7 ? 7 : g < 0 ? 0 : g;
339 b = b > 3 ? 3 : b < 0 ? 0 : b;
340
341 er += p[0] - (r * 255 + 4) / 7;
342 eg += p[1] - (g * 255 + 4) / 7;
343 eb += p[2] - (b * 255 + 2) / 3;
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
349 *dst++ = r << 5 | g << 2 | b;
350 }
351 }
352 }
353 OUTPUT:
354 RETVAL
355
356 SV *
357 make_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
412 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
413
414 void
415 dump_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
436 void
437 dump_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
456 MODULE = Gtk2::CV PACKAGE = Gtk2::CV
457
458 SV *
459 pb_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
519 SV *
520 hv84_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