ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.48
Committed: Wed May 24 05:00:57 2017 UTC (6 years, 11 months ago) by root
Branch: MAIN
Changes since 1.47: +36 -4 lines
Log Message:
*** empty log message ***

File Contents

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