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

Comparing CV/CV.xs (file contents):
Revision 1.8 by root, Mon Apr 5 20:55:10 2004 UTC vs.
Revision 1.43 by root, Sun May 3 08:16:47 2009 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#include <assert.h>
20
21#define IW 80 /* MUST match Schnauzer.pm! */
22#define IH 60 /* MUST match Schnauzer.pm! */
11 23
12#define RAND (seed = (seed + 7141) * 54773 % 134456) 24#define RAND (seed = (seed + 7141) * 54773 % 134456)
13 25
14#define LINELENGTH 240 26#define LINELENGTH 240
15 27
16#define ELLIPSIS "\xe2\x80\xa6" 28#define ELLIPSIS "\xe2\x80\xa6"
29
30typedef char *octet_string;
31
32struct jpg_err_mgr
33{
34 struct jpeg_error_mgr err;
35 jmp_buf setjmp_buffer;
36};
37
38static void
39cv_error_exit (j_common_ptr cinfo)
40{
41 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
42}
43
44static void
45cv_error_output (j_common_ptr cinfo)
46{
47 return;
48}
49
50static void
51rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
52 unsigned int *h, unsigned int *s, unsigned int *v)
53{
54 unsigned int mx = r; if (g > mx) mx = g; if (b > mx) mx = b;
55 unsigned int mn = r; if (g < mn) mn = g; if (b < mn) mn = b;
56 unsigned int delta = mx - mn;
57
58 *v = mx;
59
60 *s = mx ? delta * 255 / mx : 0;
61
62 if (delta == 0)
63 *h = 0;
64 else
65 {
66 if (r == mx)
67 *h = ((int)g - (int)b) * 255 / (int)(delta * 3);
68 else if (g == mx)
69 *h = ((int)b - (int)r) * 255 / (int)(delta * 3) + 52;
70 else if (b == mx)
71 *h = ((int)r - (int)g) * 255 / (int)(delta * 3) + 103;
72
73 *h &= 255;
74 }
75}
76
77struct feature {
78 float v1, v2, v3; // mean, square, cube
79 int n;
80};
81
82static void
83feature_init (struct feature *f)
84{
85 f->v1 = 0.;
86 f->v2 = 0.;
87 f->v3 = 0.;
88 f->n = 0;
89}
90
91// didn't find an algorithm to neatly do mean, variance and skew in one pass.
92// elmex ist schuld.
93static void
94feature_update_pass_1 (struct feature *f, unsigned int v)
95{
96 f->v1 += v;
97 f->n += 1;
98}
99
100static void
101feature_finish_pass_1 (struct feature *f)
102{
103 if (f->n < 1)
104 return;
105
106 f->v1 /= f->n;
107}
108
109static void
110feature_update_pass_2 (struct feature *f, unsigned int v)
111{
112 float d = v - f->v1;
113
114 f->v2 += d * d;
115 f->v3 += d * d * d;
116}
117
118static void
119feature_finish_pass_2 (struct feature *f)
120{
121 if (f->n < 1)
122 return;
123
124 f->v2 /= f->n;
125 f->v3 /= f->n;
126
127 f->v1 /= 255.;
128 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
129 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
130}
17 131
18static guint32 a85_val; 132static guint32 a85_val;
19static guint a85_cnt; 133static guint a85_cnt;
20static guchar a85_buf[LINELENGTH], *a85_ptr; 134static guchar a85_buf[LINELENGTH], *a85_ptr;
21 135
68 *a85_ptr++ = '\n'; 182 *a85_ptr++ = '\n';
69 183
70 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 184 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
71} 185}
72 186
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///////////////////////////////////////////////////////////////////////////// 187/////////////////////////////////////////////////////////////////////////////
101 188
102MODULE = Gtk2::CV PACKAGE = Gtk2::CV::ImageWindow 189MODULE = Gtk2::CV PACKAGE = Gtk2::CV
103 190
104PROTOTYPES: ENABLE 191PROTOTYPES: ENABLE
105 192
193# missing function in perl. really :)
194int
195common_prefix_length (a, b)
196 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
197 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
198 CODE:
199 RETVAL = 0;
200
201 while (*a == *b && *a)
202 {
203 RETVAL += (*a & 0xc0) != 0x80;
204 a++, b++;
205 }
206
207 OUTPUT:
208 RETVAL
209
210const char *
211magic (octet_string path)
212 CODE:
213{
214 static magic_t cookie;
215
216 if (!cookie)
217 {
218 cookie = magic_open (MAGIC_NONE);
219
220 if (cookie)
221 magic_load (cookie, 0);
222 else
223 XSRETURN_UNDEF;
224 }
225
226 RETVAL = magic_file (cookie, path);
227}
228 OUTPUT:
229 RETVAL
230
231const char *
232magic_mime (octet_string path)
233 CODE:
234{
235 static magic_t cookie;
236
237 if (!cookie)
238 {
239 cookie = magic_open (MAGIC_MIME);
240
241 if (cookie)
242 magic_load (cookie, 0);
243 else
244 XSRETURN_UNDEF;
245 }
246
247 RETVAL = magic_file (cookie, path);
248}
249 OUTPUT:
250 RETVAL
251
252# missing/broken in Gtk2 perl module
253
254void
255gdk_window_clear_hints (GdkWindow *window)
256 CODE:
257 gdk_window_set_geometry_hints (window, 0, 0);
258
259gboolean
260gdk_net_wm_supports (GdkAtom property)
261 CODE:
262#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
263 RETVAL = gdk_net_wm_supports (property);
264#else
265 RETVAL = 0;
266#endif
267 OUTPUT:
268 RETVAL
269
106GdkPixbuf_noinc * 270GdkPixbuf_noinc *
107transpose (GdkPixbuf *pb) 271dealpha_expose (GdkPixbuf *pb)
108 CODE: 272 CODE:
109{ 273{
110 int w = gdk_pixbuf_get_width (pb); 274 int w = gdk_pixbuf_get_width (pb);
111 int h = gdk_pixbuf_get_height (pb); 275 int h = gdk_pixbuf_get_height (pb);
112 int bpp = gdk_pixbuf_get_n_channels (pb); 276 int bpp = gdk_pixbuf_get_n_channels (pb);
113 int x, y, i; 277 int x, y, i;
114 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 278 guchar *src = gdk_pixbuf_get_pixels (pb), *dst;
115 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 279 int sstr = gdk_pixbuf_get_rowstride (pb), dstr;
116 280
117 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); 281 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
118 282
119 dst = gdk_pixbuf_get_pixels (RETVAL); 283 dst = gdk_pixbuf_get_pixels (RETVAL);
120 dstr = gdk_pixbuf_get_rowstride (RETVAL); 284 dstr = gdk_pixbuf_get_rowstride (RETVAL);
121 285
122 for (y = 0; y < h; y++)
123 for (x = 0; x < w; x++) 286 for (x = 0; x < w; x++)
287 for (y = 0; y < h; y++)
124 for (i = 0; i < bpp; i++) 288 for (i = 0; i < 3; i++)
125 dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; 289 dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i];
126} 290}
127 OUTPUT: 291 OUTPUT:
128 RETVAL 292 RETVAL
129 293
130GdkPixbuf_noinc * 294GdkPixbuf_noinc *
131flop (GdkPixbuf *pb) 295rotate (GdkPixbuf *pb, int angle)
132 CODE: 296 CODE:
133{ 297 RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE
134 int w = gdk_pixbuf_get_width (pb); 298 : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE
135 int h = gdk_pixbuf_get_height (pb); 299 : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN
136 int bpp = gdk_pixbuf_get_n_channels (pb); 300 : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE
137 int x, y, i; 301 : angle);
138 guchar *src = gdk_pixbuf_get_pixels (pb), *dst; 302 OUTPUT:
139 int sstr = gdk_pixbuf_get_rowstride (pb), dstr; 303 RETVAL
140 304
141 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); 305GdkPixbuf_noinc *
306load_jpeg (SV *path, int thumbnail=0)
307 CODE:
308{
309 struct jpeg_decompress_struct cinfo;
310 struct jpg_err_mgr jerr;
311 guchar *data;
312 int rs;
313 FILE *fp;
314 volatile GdkPixbuf *pb = 0;
142 315
316 RETVAL = 0;
317
318 fp = fopen (SvPVbyte_nolen (path), "rb");
319
320 if (!fp)
321 XSRETURN_UNDEF;
322
323 cinfo.err = jpeg_std_error (&jerr.err);
324
325 jerr.err.error_exit = cv_error_exit;
326 jerr.err.output_message = cv_error_output;
327
328 if ((rs = setjmp (jerr.setjmp_buffer)))
329 {
330 fclose (fp);
331 jpeg_destroy_decompress (&cinfo);
332
333 if (pb)
334 g_object_unref ((gpointer)pb);
335
336 XSRETURN_UNDEF;
337 }
338
339 jpeg_create_decompress (&cinfo);
340
341 jpeg_stdio_src (&cinfo, fp);
342 jpeg_read_header (&cinfo, TRUE);
343
344 cinfo.dct_method = JDCT_DEFAULT;
345 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
346 cinfo.do_block_smoothing = FALSE;
347 cinfo.out_color_space = JCS_RGB;
348 cinfo.quantize_colors = FALSE;
349
350 cinfo.scale_num = 1;
351 cinfo.scale_denom = 1;
352
353 jpeg_calc_output_dimensions (&cinfo);
354
355 if (thumbnail)
356 {
357 cinfo.dct_method = JDCT_FASTEST;
358 cinfo.do_fancy_upsampling = FALSE;
359
360 while (cinfo.scale_denom < 8
361 && cinfo.output_width >= IW*4
362 && cinfo.output_height >= IH*4)
363 {
364 cinfo.scale_denom <<= 1;
365 jpeg_calc_output_dimensions (&cinfo);
366 }
367 }
368
369 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height);
370 if (!RETVAL)
371 longjmp (jerr.setjmp_buffer, 2);
372
143 dst = gdk_pixbuf_get_pixels (RETVAL); 373 data = gdk_pixbuf_get_pixels (RETVAL);
144 dstr = gdk_pixbuf_get_rowstride (RETVAL); 374 rs = gdk_pixbuf_get_rowstride (RETVAL);
145 375
376 if (cinfo.output_components != 3)
377 longjmp (jerr.setjmp_buffer, 3);
378
379 jpeg_start_decompress (&cinfo);
380
381 while (cinfo.output_scanline < cinfo.output_height)
382 {
383 int remaining = cinfo.output_height - cinfo.output_scanline;
384 JSAMPROW rp[4];
385
386 rp [0] = data + cinfo.output_scanline * rs;
387 rp [1] = (guchar *)rp [0] + rs;
388 rp [2] = (guchar *)rp [1] + rs;
389 rp [3] = (guchar *)rp [2] + rs;
390
391 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
392 }
393
394 jpeg_finish_decompress (&cinfo);
395 fclose (fp);
396 jpeg_destroy_decompress (&cinfo);
397}
398 OUTPUT:
399 RETVAL
400
401void
402compare (GdkPixbuf *a, GdkPixbuf *b)
403 PPCODE:
404{
405 int w = gdk_pixbuf_get_width (a);
406 int h = gdk_pixbuf_get_height (a);
407
408 int sa = gdk_pixbuf_get_rowstride (a);
409 int sb = gdk_pixbuf_get_rowstride (b);
410
411 guchar *pa = gdk_pixbuf_get_pixels (a);
412 guchar *pb = gdk_pixbuf_get_pixels (b);
413
414 int x, y;
415
416 assert (w == gdk_pixbuf_get_width (b));
417 assert (h == gdk_pixbuf_get_height (b));
418
419 assert (gdk_pixbuf_get_n_channels (a) == 3);
420 assert (gdk_pixbuf_get_n_channels (b) == 3);
421
422 double diff = 0.;
423 int peak = 0;
424
425 if (w && h)
146 for (y = 0; y < h; y++) 426 for (y = 0; y < h; y++)
427 {
428 guchar *pa_ = pa + y * sa;
429 guchar *pb_ = pb + y * sb;
430
147 for (x = 0; x < w; x++) 431 for (x = 0; x < w; x++)
148 for (i = 0; i < bpp; i++) 432 {
149 dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; 433 int d;
434
435 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
436 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
437 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
438 }
439 }
440
441 EXTEND (SP, 2);
442 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
443 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
150} 444}
151 OUTPUT:
152 RETVAL
153 445
154############################################################################# 446#############################################################################
155 447
156MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer 448MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
157 449
450# currently only works for filenames (octet strings)
451
452SV *
453foldcase (SV *pathsv)
454 PROTOTYPE: $
455 CODE:
456{
457 STRLEN plen;
458 U8 *path = (U8 *)SvPV (pathsv, plen);
459 U8 *pend = path + plen;
460 U8 dst [plen * 6 * 3], *dstp = dst;
461
462 while (path < pend)
463 {
464 U8 ch = *path;
465
466 if (ch >= 'a' && ch <= 'z')
467 *dstp++ = *path++;
468 else if (ch >= 'A' && ch <= 'Z')
469 *dstp++ = *path++ + ('a' - 'A');
470 else if (ch >= '0' && ch <= '9')
471 {
472 STRLEN el, nl = 0;
473 while (*path >= '0' && *path <= '9' && path < pend)
474 path++, nl++;
475
476 for (el = nl; el < 6; el++)
477 *dstp++ = '0';
478
479 memcpy (dstp, path - nl, nl);
480 dstp += nl;
481 }
482 else
483 *dstp++ = *path++;
484#if 0
485 else
486 {
487 STRLEN cl;
488 to_utf8_fold (path, dstp, &cl);
489 dstp += cl;
490 path += is_utf8_char (path);
491 }
492#endif
493 }
494
495 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
496}
497 OUTPUT:
498 RETVAL
499
158GdkPixbuf_noinc * 500GdkPixbuf_noinc *
159p7_to_pb (int w, int h, guchar *src) 501p7_to_pb (int w, int h, SV *src_sv)
502 PROTOTYPE: @
160 CODE: 503 CODE:
161{ 504{
162 int x, y; 505 int x, y;
163 guchar *dst, *d; 506 guchar *dst, *d;
164 int dstr; 507 int dstr;
508 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
165 509
166 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); 510 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
167 dst = gdk_pixbuf_get_pixels (RETVAL); 511 dst = gdk_pixbuf_get_pixels (RETVAL);
168 dstr = gdk_pixbuf_get_rowstride (RETVAL); 512 dstr = gdk_pixbuf_get_rowstride (RETVAL);
169 513
178 } 522 }
179} 523}
180 OUTPUT: 524 OUTPUT:
181 RETVAL 525 RETVAL
182 526
527#############################################################################
528
529MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
530
531void
532dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
533 CODE:
534{
535 int w = gdk_pixbuf_get_width (pb);
536 int h = gdk_pixbuf_get_height (pb);
537 int x, y, i;
538 guchar *dst;
539 int bpp = gdk_pixbuf_get_n_channels (pb);
540 guchar *src = gdk_pixbuf_get_pixels (pb);
541 int sstr = gdk_pixbuf_get_rowstride (pb);
542
543 a85_init ();
544
545 for (y = 0; y < h; y++)
546 for (x = 0; x < w; x++)
547 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
548 a85_push (fp, src [x * bpp + y * sstr + i]);
549
550 a85_finish (fp);
551}
552
553void
554dump_binary (PerlIO *fp, GdkPixbuf *pb)
555 CODE:
556{
557 int w = gdk_pixbuf_get_width (pb);
558 int h = gdk_pixbuf_get_height (pb);
559 int x, y, i;
560 guchar *dst;
561 int bpp = gdk_pixbuf_get_n_channels (pb);
562 guchar *src = gdk_pixbuf_get_pixels (pb);
563 int sstr = gdk_pixbuf_get_rowstride (pb);
564
565 for (y = 0; y < h; y++)
566 for (x = 0; x < w; x++)
567 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
568 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
569}
570
571#############################################################################
572
573MODULE = Gtk2::CV PACKAGE = Gtk2::CV
574
183SV * 575SV *
184pb_to_p7 (GdkPixbuf *pb) 576pb_to_hv84 (GdkPixbuf *pb)
185 CODE: 577 CODE:
186{ 578{
187 int w = gdk_pixbuf_get_width (pb); 579 int w = gdk_pixbuf_get_width (pb);
188 int h = gdk_pixbuf_get_height (pb); 580 int h = gdk_pixbuf_get_height (pb);
189 int x, y; 581 int x, y;
190 guchar *dst; 582 guchar *dst;
191 int bpp = gdk_pixbuf_get_n_channels (pb); 583 int bpp = gdk_pixbuf_get_n_channels (pb);
192 guchar *src = gdk_pixbuf_get_pixels (pb); 584 guchar *src = gdk_pixbuf_get_pixels (pb);
193 int sstr = gdk_pixbuf_get_rowstride (pb); 585 int sstr = gdk_pixbuf_get_rowstride (pb);
194 int Er[IW], Eg[IW], Eb[IW];
195 int seed = 77;
196
197 RETVAL = newSV (w * h);
198 SvPOK_only (RETVAL);
199 SvCUR_set (RETVAL, w * h);
200
201 dst = 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
207 /* some primitive error distribution + random dithering */
208
209 for (y = 0; y < h; y++)
210 {
211 int er = 0, eg = 0, eb = 0;
212
213 for (x = 0; x < w; x++)
214 {
215 int r, g, b;
216 guchar *p = src + x * bpp + y * sstr;
217
218 r = ((p[0] + er + Er[x]) * 7 + 128) / 255;
219 g = ((p[1] + eg + Eg[x]) * 7 + 128) / 255;
220 b = ((p[2] + eb + Eb[x]) * 3 + 128) / 255;
221
222 r = r > 7 ? 7 : r < 0 ? 0 : r;
223 g = g > 7 ? 7 : g < 0 ? 0 : g;
224 b = b > 3 ? 3 : b < 0 ? 0 : b;
225
226 er += p[0] - (r * 255 + 4) / 7;
227 eg += p[1] - (g * 255 + 4) / 7;
228 eb += p[2] - (b * 255 + 2) / 3;
229
230 Er[x] = er / 2; er -= er / 2 + RAND % 7 - 3;
231 Eg[x] = eg / 2; eg -= eg / 2 + RAND % 7 - 3;
232 Eb[x] = eb / 2; eb -= eb / 2 + RAND % 7 - 3;
233
234 *dst++ = r << 5 | g << 2 | b;
235 }
236 }
237}
238 OUTPUT:
239 RETVAL
240
241#############################################################################
242
243MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
244
245void
246dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
247 CODE:
248{
249 int w = gdk_pixbuf_get_width (pb);
250 int h = gdk_pixbuf_get_height (pb);
251 int x, y, i;
252 guchar *dst;
253 int bpp = gdk_pixbuf_get_n_channels (pb);
254 guchar *src = gdk_pixbuf_get_pixels (pb);
255 int sstr = gdk_pixbuf_get_rowstride (pb);
256
257 a85_init ();
258
259 for (y = 0; y < h; y++)
260 for (x = 0; x < w; x++)
261 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
262 a85_push (fp, src [x * bpp + y * sstr + i]);
263
264 a85_finish (fp);
265}
266
267void
268dump_binary (PerlIO *fp, GdkPixbuf *pb)
269 CODE:
270{
271 int w = gdk_pixbuf_get_width (pb);
272 int h = gdk_pixbuf_get_height (pb);
273 int x, y, i;
274 guchar *dst;
275 int bpp = gdk_pixbuf_get_n_channels (pb);
276 guchar *src = gdk_pixbuf_get_pixels (pb);
277 int sstr = gdk_pixbuf_get_rowstride (pb);
278
279 for (y = 0; y < h; y++)
280 for (x = 0; x < w; x++)
281 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
282 PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
283}
284
285#############################################################################
286
287MODULE = Gtk2::CV PACKAGE = Gtk2::CV
288
289SV *
290pb_to_hv84 (GdkPixbuf *pb)
291 CODE:
292{
293 int w = gdk_pixbuf_get_width (pb);
294 int h = gdk_pixbuf_get_height (pb);
295 int x, y;
296 guchar *dst;
297 int bpp = gdk_pixbuf_get_n_channels (pb);
298 guchar *src = gdk_pixbuf_get_pixels (pb);
299 int sstr = gdk_pixbuf_get_rowstride (pb);
300 586
301 RETVAL = newSV (6 * 8 * 12 / 8); 587 RETVAL = newSV (6 * 8 * 12 / 8);
302 SvPOK_only (RETVAL); 588 SvPOK_only (RETVAL);
303 SvCUR_set (RETVAL, 6 * 8 * 12 / 8); 589 SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
304 590
305 dst = SvPVX (RETVAL); 591 dst = (guchar *)SvPVX (RETVAL);
306 592
307 /* some primitive error distribution + random dithering */ 593 /* some primitive error distribution + random dithering */
308 594
309 for (y = 0; y < h; y++) 595 for (y = 0; y < h; y++)
310 { 596 {
345 } 631 }
346} 632}
347 OUTPUT: 633 OUTPUT:
348 RETVAL 634 RETVAL
349 635
636SV *
637hv84_to_av (unsigned char *hv84)
638 CODE:
639{
640 int i = 72 / 3;
641 AV *av = newAV ();
350 642
643 RETVAL = (SV *)newRV_noinc ((SV *)av);
644 while (i--)
645 {
646 int h = *hv84++;
647 int v1 = *hv84++;
648 int v2 = *hv84++;
351 649
650 av_push (av, newSViv (v1));
651 av_push (av, newSViv ((h >> 4) * 255 / 15));
652 av_push (av, newSViv (v2));
653 av_push (av, newSViv ((h & 15) * 255 / 15));
654 }
655}
656 OUTPUT:
657 RETVAL
352 658
659#############################################################################
660
661MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
662
663SV *
664extract_features (SV *ar)
665 CODE:
666{
667 int i;
668 AV *av, *result;
669
670 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
671 croak ("Not an array ref as first argument to extract_features");
672
673 av = (AV *) SvRV (ar);
674 result = newAV ();
675
676 for (i = 0; i <= av_len (av); ++i)
677 {
678 SV *sv = *av_fetch (av, i, 1);
679 SV *histsv = newSV (9 * sizeof (float) + 1);
680
681 SvPOK_on (histsv);
682 SvCUR_set (histsv, 9 * sizeof (float));
683 float *hist = (float *)SvPVX (histsv);
684
685 struct feature f_h, f_s, f_v;
686 feature_init (&f_h);
687 feature_init (&f_s);
688 feature_init (&f_v);
689
690 {
691 STRLEN len;
692 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
693 while (len >= 3)
694 {
695 unsigned int r, g, b, h, s, v;
696 r = *buf++; g = *buf++; b = *buf++;
697 rgb_to_hsv (r, g, b, &h, &s, &v);
698
699 feature_update_pass_1 (&f_h, h);
700 feature_update_pass_1 (&f_s, s);
701 feature_update_pass_1 (&f_v, v);
702
703 len -= 3;
704 }
705
706 feature_finish_pass_1 (&f_h);
707 feature_finish_pass_1 (&f_s);
708 feature_finish_pass_1 (&f_v);
709 }
710
711 {
712 STRLEN len;
713 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
714 while (len >= 3)
715 {
716 unsigned int r, g, b, h, s, v;
717 r = *buf++; g = *buf++; b = *buf++;
718 rgb_to_hsv (r, g, b, &h, &s, &v);
719
720 feature_update_pass_2 (&f_h, h);
721 feature_update_pass_2 (&f_s, s);
722 feature_update_pass_2 (&f_v, v);
723
724 len -= 3;
725 }
726
727 feature_finish_pass_2 (&f_h);
728 feature_finish_pass_2 (&f_s);
729 feature_finish_pass_2 (&f_v);
730 }
731
732 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
733 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
734 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
735
736 av_push (result, histsv);
737 }
738
739 RETVAL = newRV_noinc ((SV *)result);
740}
741 OUTPUT:
742 RETVAL
743

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines