ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
(Generate patch)

Comparing CV/CV.xs (file contents):
Revision 1.16 by root, Wed Jul 20 02:26:46 2005 UTC vs.
Revision 1.33 by root, Fri Feb 17 08:48:06 2006 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#include <math.h>
8
9#include <magic.h>
10
11#include <jpeglib.h>
12#include <glib.h>
13#include <gtk/gtk.h>
5#include <gdk-pixbuf/gdk-pixbuf.h> 14#include <gdk-pixbuf/gdk-pixbuf.h>
6 15
7#include <gperl.h> 16#include <gperl.h>
8#include <gtk2perl.h> 17#include <gtk2perl.h>
9 18
10#define IW 80 19#define IW 80 /* MUST match Schnauzer.pm! */
20#define IH 60 /* MUST match Schnauzer.pm! */
11 21
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 22#define RAND (seed = (seed + 7141) * 54773 % 134456)
13 23
14#define LINELENGTH 240 24#define LINELENGTH 240
15 25
16#define ELLIPSIS "\xe2\x80\xa6" 26#define ELLIPSIS "\xe2\x80\xa6"
27
28struct jpg_err_mgr
29{
30 struct jpeg_error_mgr err;
31 jmp_buf setjmp_buffer;
32};
33
34static void
35cv_error_exit (j_common_ptr cinfo)
36{
37 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
38}
39
40static void
41cv_error_output (j_common_ptr cinfo)
42{
43 return;
44}
45
46static void
47rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
48 unsigned int *h, unsigned int *s, unsigned int *v)
49{
50 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
51 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
52 unsigned int delta = mx - mn;
53
54 *v = mx;
55
56 *s = mx ? delta * 255 / mx : 0;
57
58 if (delta == 0)
59 *h = 0;
60 else
61 {
62 if (r == mx)
63 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
64 else if (g == mx)
65 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
66 else if (b == mx)
67 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
68
69 *h &= 255;
70 }
71}
17 72
18static guint32 a85_val; 73static guint32 a85_val;
19static guint a85_cnt; 74static guint a85_cnt;
20static guchar a85_buf[LINELENGTH], *a85_ptr; 75static guchar a85_buf[LINELENGTH], *a85_ptr;
21 76
68 *a85_ptr++ = '\n'; 123 *a85_ptr++ = '\n';
69 124
70 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 125 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
71} 126}
72 127
73static void
74rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
75 unsigned int *h, unsigned int *s, unsigned int *v)
76{
77 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
78 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
79 unsigned int delta = mx - mn;
80
81 *v = mx;
82
83 *s = mx ? delta * 255 / mx : 0;
84
85 if (delta == 0)
86 *h = 0;
87 else
88 {
89 if (r == mx)
90 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
91 else if (g == mx)
92 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
93 else if (b == mx)
94 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
95
96 *h &= 255;
97 }
98}
99
100///////////////////////////////////////////////////////////////////////////// 128/////////////////////////////////////////////////////////////////////////////
101 129
102MODULE = Gtk2::CV PACKAGE = Gtk2::CV 130MODULE = Gtk2::CV PACKAGE = Gtk2::CV
103 131
104PROTOTYPES: ENABLE 132PROTOTYPES: ENABLE
105 133
134# missing function in perl. really :)
135int
136common_prefix_length (a, b)
137 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
138 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
139 CODE:
140 RETVAL = 0;
141
142 while (*a == *b && *a)
143 {
144 RETVAL += (*a & 0xc0) != 0x80;
145 a++, b++;
146 }
147
148 OUTPUT:
149 RETVAL
150
151const char *
152magic (const char *path)
153 CODE:
154{
155 static magic_t cookie;
156
157 if (!cookie)
158 {
159 cookie = magic_open (MAGIC_NONE);
160
161 if (cookie)
162 magic_load (cookie, 0);
163 else
164 XSRETURN_UNDEF;
165 }
166
167 RETVAL = magic_file (cookie, path);
168}
169 OUTPUT:
170 RETVAL
171
172const char *
173magic_mime (const char *path)
174 CODE:
175{
176 static magic_t cookie;
177
178 if (!cookie)
179 {
180 cookie = magic_open (MAGIC_MIME);
181
182 if (cookie)
183 magic_load (cookie, 0);
184 else
185 XSRETURN_UNDEF;
186 }
187
188 RETVAL = magic_file (cookie, path);
189}
190 OUTPUT:
191 RETVAL
192
193# missing in Gtk2 perl module
194
195gboolean
196gdk_net_wm_supports (GdkAtom property)
197 CODE:
198#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
199 RETVAL = gdk_net_wm_supports (property);
200#else
201 RETVAL = 0;
202#endif
203 OUTPUT:
204 RETVAL
205
106GdkPixbuf_noinc * 206GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 207dealpha_expose (GdkPixbuf *pb)
108 CODE: 208 CODE:
109{ 209{
110 int w = gdk_pixbuf_get_width (pb); 210 int w = gdk_pixbuf_get_width (pb);
111 int h = gdk_pixbuf_get_height (pb); 211 int h = gdk_pixbuf_get_height (pb);
112 int bpp = gdk_pixbuf_get_n_channels (pb); 212 int bpp = gdk_pixbuf_get_n_channels (pb);
113 int x, y, i; 213 int x, y, i;
114 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 214 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
115 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 215 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
116 216
117 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 217 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
118 218
119 dst = gdk_pixbuf_get_pixels (RETVAL); 219 dst = gdk_pixbuf_get_pixels (RETVAL);
120 dstr = gdk_pixbuf_get_rowstride (RETVAL); 220 dstr = gdk_pixbuf_get_rowstride (RETVAL);
121 221
222 for (x = 0; x < w; x++)
223 for (y = 0; y < h; y++)
224 for (i = 0; i < 3; i++)
225 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
226}
227 OUTPUT:
228 RETVAL
229
230GdkPixbuf_noinc *
231rotate (GdkPixbuf *pb, int angle)
232 CODE:
233 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
234 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
235 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
236 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
237 : angle);
238 OUTPUT:
239 RETVAL
240
241GdkPixbuf_noinc *
242load_jpeg (SV *path, int thumbnail=0)
243 CODE:
244{
245 struct jpeg_decompress_struct cinfo;
246 struct jpg_err_mgr jerr;
247 guchar *data;
248 int rs;
249 FILE *fp;
250 volatile GdkPixbuf *pb = 0;
251
252 RETVAL = 0;
253
254 fp = fopen (SvPVbyte_nolen (path), "rb");
255
256 if (!fp)
257 XSRETURN_UNDEF;
258
259 cinfo.err = jpeg_std_error (&jerr.err);
260
261 jerr.err.error_exit = cv_error_exit;
262 jerr.err.output_message = cv_error_output;
263
264 if ((rs = setjmp (jerr.setjmp_buffer)))
265 {
266 fclose (fp);
267 jpeg_destroy_decompress (&cinfo);
268
269 if (pb)
270 g_object_unref ((gpointer)pb);
271
272 XSRETURN_UNDEF;
273 }
274
275 jpeg_create_decompress (&cinfo);
276
277 jpeg_stdio_src (&cinfo, fp);
278 jpeg_read_header (&cinfo, TRUE);
279
280 cinfo.dct_method = JDCT_DEFAULT;
281 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
282 cinfo.do_block_smoothing = FALSE;
283 cinfo.out_color_space = JCS_RGB;
284 cinfo.quantize_colors = FALSE;
285
286 cinfo.scale_num = 1;
287 cinfo.scale_denom = 1;
288
289 jpeg_calc_output_dimensions (&cinfo);
290
291 if (thumbnail)
292 {
293 cinfo.dct_method = JDCT_FASTEST;
294 cinfo.do_fancy_upsampling = FALSE;
295
296 while (cinfo.scale_denom < 8
297 && cinfo.output_width >= IW*4
298 && cinfo.output_height >= IH*4)
299 {
300 cinfo.scale_denom <<= 1;
301 jpeg_calc_output_dimensions (&cinfo);
302 }
303 }
304
305 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
306 if (!RETVAL)
307 longjmp (jerr.setjmp_buffer, 2);
308
309 data = gdk_pixbuf_get_pixels (RETVAL);
310 rs = gdk_pixbuf_get_rowstride (RETVAL);
311
312 if (cinfo.output_components != 3)
313 longjmp (jerr.setjmp_buffer, 3);
314
315 jpeg_start_decompress (&cinfo);
316
317 while (cinfo.output_scanline < cinfo.output_height)
318 {
319 int remaining = cinfo.output_height - cinfo.output_scanline;
320 JSAMPROW rp[4];
321
322 rp [0] = data + cinfo.output_scanline * rs;
323 rp [1] = (guchar *)rp [0] + rs;
324 rp [2] = (guchar *)rp [1] + rs;
325 rp [3] = (guchar *)rp [2] + rs;
326
327 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
328 }
329
330 jpeg_finish_decompress (&cinfo);
331 fclose (fp);
332 jpeg_destroy_decompress (&cinfo);
333}
334 OUTPUT:
335 RETVAL
336
337void
338compare (GdkPixbuf *a, GdkPixbuf *b)
339 PPCODE:
340{
341 int w = gdk_pixbuf_get_width (a);
342 int h = gdk_pixbuf_get_height (a);
343 int sa = gdk_pixbuf_get_rowstride (a);
344 int sb = gdk_pixbuf_get_rowstride (b);
345
346 guchar *pa = gdk_pixbuf_get_pixels (a);
347 guchar *pb = gdk_pixbuf_get_pixels (b);
348
349 int x, y;
350
351 assert (w == gdk_pixbuf_get_width (b));
352 assert (h == gdk_pixbuf_get_height (b));
353
354 assert (gdk_pixbuf_get_n_channels (a) == 3);
355 assert (gdk_pixbuf_get_n_channels (b) == 3);
356
357 double diff = 0.;
358 int peak = 0;
359
122 for (y = 0; y < h; y++) 360 for (y = 0; y < h; y++)
361 {
362 guchar *pa_ = pa + y * sa;
363 guchar *pb_ = pb + y * sb;
364
123 for (x = 0; x < w; x++) 365 for (x = 0; x < w; x ++)
366 {
367 int d;
368
369 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
370 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
371 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
372 }
373 }
374
375 EXTEND (SP, 2);
376 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
377 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
378}
379
380#############################################################################
381
382MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
383
384SV *
385foldcase (SV *pathsv)
386 PROTOTYPE: $
387 CODE:
388{
389 STRLEN plen;
390 U8 *path = (U8 *)SvPVutf8 (pathsv, plen);
391 U8 *pend = path + plen;
392 U8 dst [plen * 6 * 3], *dstp = dst;
393
394 while (path < pend)
395 {
396 U8 ch = *path;
397
398 if (ch >= 'a' && ch <= 'z')
399 *dstp++ = *path++;
400 else if (ch >= '0' && ch <= '9')
401 {
402 STRLEN el, nl = 0;
403 while (*path >= '0' && *path <= '9' && path < pend)
404 path++, nl++;
405
124 for (i = 0; i < bpp; i++) 406 for (el = nl; el < 6; el++)
125 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 407 *dstp++ = '0';
408
409 memcpy (dstp, path - nl, nl);
410 dstp += nl;
411 }
412 else
413 {
414 STRLEN cl;
415 to_utf8_fold (path, dstp, &cl);
416 dstp += cl;
417 path += is_utf8_char (path);
418 }
419 }
420
421 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
126} 422}
127 OUTPUT: 423 OUTPUT:
128 RETVAL 424 RETVAL
129 425
130GdkPixbuf_noinc * 426GdkPixbuf_noinc *
131flop (GdkPixbuf *pb)
132 CODE:
133{
134 int w = gdk_pixbuf_get_width (pb);
135 int h = gdk_pixbuf_get_height (pb);
136 int bpp = gdk_pixbuf_get_n_channels (pb);
137 int x, y, i;
138 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
139 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
140
141 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h);
142
143 dst = gdk_pixbuf_get_pixels (RETVAL);
144 dstr = gdk_pixbuf_get_rowstride (RETVAL);
145
146 for (y = 0; y < h; y++)
147 for (x = 0; x < w; x++)
148 for (i = 0; i < bpp; i++)
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i];
150}
151 OUTPUT:
152 RETVAL
153
154#############################################################################
155
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
157
158GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 427p7_to_pb (int w, int h, SV *src_sv)
428 PROTOTYPE: @
160 CODE: 429 CODE:
161{ 430{
162 int x, y; 431 int x, y;
163 guchar *dst, *d; 432 guchar *dst, *d;
164 int dstr; 433 int dstr;
434 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
165 435
166 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 436 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
167 dst = gdk_pixbuf_get_pixels (RETVAL); 437 dst = gdk_pixbuf_get_pixels (RETVAL);
168 dstr = gdk_pixbuf_get_rowstride (RETVAL); 438 dstr = gdk_pixbuf_get_rowstride (RETVAL);
169 439
178 } 448 }
179} 449}
180 OUTPUT: 450 OUTPUT:
181 RETVAL 451 RETVAL
182 452
453#############################################################################
454
455MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
456
457void
458dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
459 CODE:
460{
461 int w = gdk_pixbuf_get_width (pb);
462 int h = gdk_pixbuf_get_height (pb);
463 int x, y, i;
464 guchar *dst;
465 int bpp = gdk_pixbuf_get_n_channels (pb);
466 guchar *src = gdk_pixbuf_get_pixels (pb);
467 int sstr = gdk_pixbuf_get_rowstride (pb);
468
469 a85_init ();
470
471 for (y = 0; y < h; y++)
472 for (x = 0; x < w; x++)
473 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
474 a85_push (fp, src [x * bpp + y * sstr + i]);
475
476 a85_finish (fp);
477}
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);
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}
496
497#############################################################################
498
499MODULE = Gtk2::CV PACKAGE = Gtk2::CV
500
183SV * 501SV *
184pb_to_p7 (GdkPixbuf *pb) 502pb_to_hv84 (GdkPixbuf *pb)
185 CODE: 503 CODE:
186{ 504{
187 int w = gdk_pixbuf_get_width (pb); 505 int w = gdk_pixbuf_get_width (pb);
188 int h = gdk_pixbuf_get_height (pb); 506 int h = gdk_pixbuf_get_height (pb);
189 int x, y; 507 int x, y;
190 guchar *dst; 508 guchar *dst;
191 int bpp = gdk_pixbuf_get_n_channels (pb); 509 int bpp = gdk_pixbuf_get_n_channels (pb);
192 guchar *src = gdk_pixbuf_get_pixels (pb); 510 guchar *src = gdk_pixbuf_get_pixels (pb);
193 int sstr = gdk_pixbuf_get_rowstride (pb); 511 int sstr = gdk_pixbuf_get_rowstride (pb);
194 int Er[IW], Eg[IW], Eb[IW];
195 int seed = 77;
196 512
197 RETVAL = newSV (w * h); 513 RETVAL = newSV (6 * 8 * 12 / 8);
198 SvPOK_only (RETVAL); 514 SvPOK_only (RETVAL);
199 SvCUR_set (RETVAL, w * h); 515 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
200 516
201 dst = SvPVX (RETVAL); 517 dst = (guchar *)SvPVX (RETVAL);
202
203 memset (Er, 0, sizeof (int) * IW);
204 memset (Eg, 0, sizeof (int) * IW);
205 memset (Eb, 0, sizeof (int) * IW);
206 518
207 /* some primitive error distribution + random dithering */ 519 /* some primitive error distribution + random dithering */
208 520
209 for (y = 0; y < h; y++) 521 for (y = 0; y < h; y++)
210 { 522 {
211 int er = 0, eg = 0, eb = 0; 523 guchar *p = src + y * sstr;
212 524
213 for (x = 0; x < w; x++) 525 for (x = 0; x < w; x += 2)
214 { 526 {
215 int r, g, b; 527 unsigned int r, g, b, h, s, v, H, V1, V2;
216 guchar *p = src + x * bpp + y * sstr;
217 528
218 r = ((p[0] + er + Er[x]) * 7 + 128) / 255; 529 if (bpp == 3)
219 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255; 530 r = *p++, g = *p++, b = *p++;
220 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255; 531 else if (bpp == 1)
532 r = g = b = *p++;
533 else
534 abort ();
221 535
222 r = r > 7 ? 7 : r < 0 ? 0 : r; 536 rgb_to_hsv (r, g, b, &h, &s, &v);
223 g = g > 7 ? 7 : g < 0 ? 0 : g;
224 b = b > 3 ? 3 : b < 0 ? 0 : b;
225 537
226 er += p[0] - (r * 255 + 4) / 7; 538 H = (h * 15 / 255) << 4;
227 eg += p[1] - (g * 255 + 4) / 7; 539 V1 = v;
228 eb += p[2] - (b * 255 + 2) / 3;
229 540
230 Er[x] = er / 2; er -= er / 2 + RAND % 5 - 2; 541 if (bpp == 3)
231 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 5 - 2; 542 r = *p++, g = *p++, b = *p++;
232 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 5 - 2; 543 else if (bpp == 1)
544 r = g = b = *p++;
545 else
546 abort ();
233 547
234 *dst++ = r << 5 | g << 2 | b; 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;
235 } 556 }
236 } 557 }
237} 558}
238 OUTPUT: 559 OUTPUT:
239 RETVAL 560 RETVAL
240 561
241SV * 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
587MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
588
589SV *
242make_histogram (SV *ar) 590make_histograms (SV *ar)
243 CODE: 591 CODE:
244{ 592{
245 int i; 593 int i;
246 AV *av, *result; 594 AV *av, *result;
247 595
291} 639}
292 OUTPUT: 640 OUTPUT:
293 RETVAL 641 RETVAL
294 642
295 643
296
297#############################################################################
298
299MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
300
301void
302dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
303 CODE:
304{
305 int w = gdk_pixbuf_get_width (pb);
306 int h = gdk_pixbuf_get_height (pb);
307 int x, y, i;
308 guchar *dst;
309 int bpp = gdk_pixbuf_get_n_channels (pb);
310 guchar *src = gdk_pixbuf_get_pixels (pb);
311 int sstr = gdk_pixbuf_get_rowstride (pb);
312
313 a85_init ();
314
315 for (y = 0; y < h; y++)
316 for (x = 0; x < w; x++)
317 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
318 a85_push (fp, src [x * bpp + y * sstr + i]);
319
320 a85_finish (fp);
321}
322
323void
324dump_binary (PerlIO *fp, GdkPixbuf *pb)
325 CODE:
326{
327 int w = gdk_pixbuf_get_width (pb);
328 int h = gdk_pixbuf_get_height (pb);
329 int x, y, i;
330 guchar *dst;
331 int bpp = gdk_pixbuf_get_n_channels (pb);
332 guchar *src = gdk_pixbuf_get_pixels (pb);
333 int sstr = gdk_pixbuf_get_rowstride (pb);
334
335 for (y = 0; y < h; y++)
336 for (x = 0; x < w; x++)
337 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
338 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
339}
340
341#############################################################################
342
343MODULE = Gtk2::CV PACKAGE = Gtk2::CV
344
345SV *
346pb_to_hv84 (GdkPixbuf *pb)
347 CODE:
348{
349 int w = gdk_pixbuf_get_width (pb);
350 int h = gdk_pixbuf_get_height (pb);
351 int x, y;
352 guchar *dst;
353 int bpp = gdk_pixbuf_get_n_channels (pb);
354 guchar *src = gdk_pixbuf_get_pixels (pb);
355 int sstr = gdk_pixbuf_get_rowstride (pb);
356
357 RETVAL = newSV (6 * 8 * 12 / 8);
358 SvPOK_only (RETVAL);
359 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
360
361 dst = SvPVX (RETVAL);
362
363 /* some primitive error distribution + random dithering */
364
365 for (y = 0; y < h; y++)
366 {
367 guchar *p = src + y * sstr;
368
369 for (x = 0; x < w; x += 2)
370 {
371 unsigned int r, g, b, h, s, v, H, V1, V2;
372
373 if (bpp == 3)
374 r = *p++, g = *p++, b = *p++;
375 else if (bpp == 1)
376 r = g = b = *p++;
377 else
378 abort ();
379
380 rgb_to_hsv (r, g, b, &h, &s, &v);
381
382 H = (h * 15 / 255) << 4;
383 V1 = v;
384
385 if (bpp == 3)
386 r = *p++, g = *p++, b = *p++;
387 else if (bpp == 1)
388 r = g = b = *p++;
389 else
390 abort ();
391
392 rgb_to_hsv (r, g, b, &h, &s, &v);
393
394 H |= h * 15 / 255;
395 V2 = v;
396
397 *dst++ = H;
398 *dst++ = V1;
399 *dst++ = V2;
400 }
401 }
402}
403 OUTPUT:
404 RETVAL
405
406SV *
407hv84_to_av (unsigned char *hv84)
408 CODE:
409{
410 int i = 72 / 3;
411 AV *av = newAV ();
412
413 RETVAL = (SV *)newRV_noinc ((SV *)av);
414 while (i--)
415 {
416 int h = *hv84++;
417 int v1 = *hv84++;
418 int v2 = *hv84++;
419
420 av_push (av, newSViv (v1));
421 av_push (av, newSViv ((h >> 4) * 255 / 15));
422 av_push (av, newSViv (v2));
423 av_push (av, newSViv ((h & 15) * 255 / 15));
424 }
425}
426 OUTPUT:
427 RETVAL
428
429
430

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines