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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines