ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
Revision: 1.49
Committed: Wed May 24 19:22:32 2017 UTC (6 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-1_61
Changes since 1.48: +16 -7 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 root 1.49 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 root 1.4 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 root 1.49 if (0)
431     if (cinfo.Adobe_transform == 2)
432     {
433     c ^= 0xff;
434     m ^= 0xff;
435     y ^= 0xff;
436     k ^= 0xff;
437     }
438    
439     data [0] = (c * k + 0x80) / 0xff;
440     data [1] = (m * k + 0x80) / 0xff;
441     data [2] = (y * k + 0x80) / 0xff;
442     data [3] = 0xff;
443 root 1.48
444     data += 4;
445     }
446     }
447    
448 root 1.17 jpeg_finish_decompress (&cinfo);
449     fclose (fp);
450     jpeg_destroy_decompress (&cinfo);
451 root 1.46 perlinterp_acquire ();
452 root 1.17 }
453     OUTPUT:
454     RETVAL
455    
456 root 1.33 void
457     compare (GdkPixbuf *a, GdkPixbuf *b)
458     PPCODE:
459 root 1.46 perlinterp_release ();
460 root 1.33 {
461     int w = gdk_pixbuf_get_width (a);
462     int h = gdk_pixbuf_get_height (a);
463 root 1.43
464 root 1.33 int sa = gdk_pixbuf_get_rowstride (a);
465     int sb = gdk_pixbuf_get_rowstride (b);
466    
467     guchar *pa = gdk_pixbuf_get_pixels (a);
468     guchar *pb = gdk_pixbuf_get_pixels (b);
469    
470     int x, y;
471    
472     assert (w == gdk_pixbuf_get_width (b));
473     assert (h == gdk_pixbuf_get_height (b));
474    
475     assert (gdk_pixbuf_get_n_channels (a) == 3);
476     assert (gdk_pixbuf_get_n_channels (b) == 3);
477    
478     double diff = 0.;
479     int peak = 0;
480    
481 root 1.35 if (w && h)
482 root 1.34 for (y = 0; y < h; y++)
483     {
484     guchar *pa_ = pa + y * sa;
485     guchar *pb_ = pb + y * sb;
486 root 1.33
487 root 1.34 for (x = 0; x < w; x++)
488     {
489     int d;
490    
491     d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
492     d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
493     d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
494     }
495     }
496 root 1.33
497 root 1.46 perlinterp_acquire ();
498    
499 root 1.33 EXTEND (SP, 2);
500     PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
501     PUSHs (sv_2mortal (newSVnv (peak / 255.)));
502     }
503    
504 root 1.6 #############################################################################
505    
506 root 1.2 MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer
507    
508 root 1.41 # currently only works for filenames (octet strings)
509    
510 root 1.18 SV *
511     foldcase (SV *pathsv)
512     PROTOTYPE: $
513     CODE:
514     {
515     STRLEN plen;
516 root 1.41 U8 *path = (U8 *)SvPV (pathsv, plen);
517 root 1.18 U8 *pend = path + plen;
518     U8 dst [plen * 6 * 3], *dstp = dst;
519    
520     while (path < pend)
521     {
522 root 1.19 U8 ch = *path;
523    
524     if (ch >= 'a' && ch <= 'z')
525     *dstp++ = *path++;
526 root 1.41 else if (ch >= 'A' && ch <= 'Z')
527     *dstp++ = *path++ + ('a' - 'A');
528 root 1.19 else if (ch >= '0' && ch <= '9')
529 root 1.18 {
530     STRLEN el, nl = 0;
531 root 1.19 while (*path >= '0' && *path <= '9' && path < pend)
532 root 1.18 path++, nl++;
533    
534     for (el = nl; el < 6; el++)
535     *dstp++ = '0';
536    
537     memcpy (dstp, path - nl, nl);
538     dstp += nl;
539     }
540     else
541 root 1.41 *dstp++ = *path++;
542     #if 0
543     else
544 root 1.18 {
545     STRLEN cl;
546     to_utf8_fold (path, dstp, &cl);
547     dstp += cl;
548     path += is_utf8_char (path);
549     }
550 root 1.41 #endif
551 root 1.18 }
552    
553 root 1.25 RETVAL = newSVpvn ((const char *)dst, dstp - dst);
554 root 1.18 }
555     OUTPUT:
556     RETVAL
557    
558 root 1.3 GdkPixbuf_noinc *
559 root 1.24 p7_to_pb (int w, int h, SV *src_sv)
560 root 1.21 PROTOTYPE: @
561 root 1.2 CODE:
562     {
563     int x, y;
564     guchar *dst, *d;
565     int dstr;
566 root 1.25 guchar *src = (guchar *)SvPVbyte_nolen (src_sv);
567 root 1.2
568     RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h);
569     dst = gdk_pixbuf_get_pixels (RETVAL);
570     dstr = gdk_pixbuf_get_rowstride (RETVAL);
571    
572     for (y = 0; y < h; y++)
573     for (d = dst + y * dstr, x = 0; x < w; x++)
574     {
575 root 1.3 *d++ = (((*src >> 5) & 7) * 255 + 4) / 7;
576     *d++ = (((*src >> 2) & 7) * 255 + 4) / 7;
577     *d++ = (((*src >> 0) & 3) * 255 + 2) / 3;
578 root 1.2
579     src++;
580     }
581 root 1.3 }
582     OUTPUT:
583     RETVAL
584    
585 root 1.6 #############################################################################
586 root 1.4
587     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript
588    
589     void
590 root 1.7 dump_ascii85 (PerlIO *fp, GdkPixbuf *pb)
591 root 1.4 CODE:
592     {
593     int w = gdk_pixbuf_get_width (pb);
594     int h = gdk_pixbuf_get_height (pb);
595     int x, y, i;
596     guchar *dst;
597 root 1.7 int bpp = gdk_pixbuf_get_n_channels (pb);
598 root 1.4 guchar *src = gdk_pixbuf_get_pixels (pb);
599     int sstr = gdk_pixbuf_get_rowstride (pb);
600    
601     a85_init ();
602    
603     for (y = 0; y < h; y++)
604     for (x = 0; x < w; x++)
605 root 1.7 for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
606 root 1.4 a85_push (fp, src [x * bpp + y * sstr + i]);
607    
608     a85_finish (fp);
609 root 1.7 }
610    
611     void
612     dump_binary (PerlIO *fp, GdkPixbuf *pb)
613     CODE:
614     {
615     int w = gdk_pixbuf_get_width (pb);
616     int h = gdk_pixbuf_get_height (pb);
617     int x, y, i;
618     guchar *dst;
619     int bpp = gdk_pixbuf_get_n_channels (pb);
620     guchar *src = gdk_pixbuf_get_pixels (pb);
621     int sstr = gdk_pixbuf_get_rowstride (pb);
622    
623     for (y = 0; y < h; y++)
624     for (x = 0; x < w; x++)
625     for (i = 0; i < (bpp < 3 ? 1 : 3); i++)
626     PerlIO_putc (fp, src [x * bpp + y * sstr + i]);
627 root 1.4 }
628 root 1.8
629     #############################################################################
630    
631     MODULE = Gtk2::CV PACKAGE = Gtk2::CV
632    
633     SV *
634     pb_to_hv84 (GdkPixbuf *pb)
635     CODE:
636     {
637     int w = gdk_pixbuf_get_width (pb);
638     int h = gdk_pixbuf_get_height (pb);
639     int x, y;
640     guchar *dst;
641     int bpp = gdk_pixbuf_get_n_channels (pb);
642     guchar *src = gdk_pixbuf_get_pixels (pb);
643     int sstr = gdk_pixbuf_get_rowstride (pb);
644    
645     RETVAL = newSV (6 * 8 * 12 / 8);
646     SvPOK_only (RETVAL);
647     SvCUR_set (RETVAL, 6 * 8 * 12 / 8);
648    
649 root 1.25 dst = (guchar *)SvPVX (RETVAL);
650 root 1.8
651     /* some primitive error distribution + random dithering */
652    
653     for (y = 0; y < h; y++)
654     {
655     guchar *p = src + y * sstr;
656    
657     for (x = 0; x < w; x += 2)
658     {
659     unsigned int r, g, b, h, s, v, H, V1, V2;
660    
661     if (bpp == 3)
662     r = *p++, g = *p++, b = *p++;
663     else if (bpp == 1)
664     r = g = b = *p++;
665     else
666     abort ();
667    
668     rgb_to_hsv (r, g, b, &h, &s, &v);
669    
670     H = (h * 15 / 255) << 4;
671     V1 = v;
672    
673     if (bpp == 3)
674     r = *p++, g = *p++, b = *p++;
675     else if (bpp == 1)
676     r = g = b = *p++;
677     else
678     abort ();
679    
680     rgb_to_hsv (r, g, b, &h, &s, &v);
681    
682     H |= h * 15 / 255;
683     V2 = v;
684    
685     *dst++ = H;
686     *dst++ = V1;
687     *dst++ = V2;
688     }
689     }
690     }
691     OUTPUT:
692     RETVAL
693 root 1.4
694 root 1.9 SV *
695     hv84_to_av (unsigned char *hv84)
696     CODE:
697     {
698     int i = 72 / 3;
699     AV *av = newAV ();
700    
701     RETVAL = (SV *)newRV_noinc ((SV *)av);
702     while (i--)
703     {
704     int h = *hv84++;
705     int v1 = *hv84++;
706     int v2 = *hv84++;
707    
708     av_push (av, newSViv (v1));
709     av_push (av, newSViv ((h >> 4) * 255 / 15));
710     av_push (av, newSViv (v2));
711     av_push (av, newSViv ((h & 15) * 255 / 15));
712     }
713     }
714     OUTPUT:
715     RETVAL
716 root 1.2
717 root 1.20 #############################################################################
718    
719     MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
720    
721     SV *
722 root 1.36 extract_features (SV *ar)
723     CODE:
724     {
725     int i;
726     AV *av, *result;
727    
728     if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
729 root 1.39 croak ("Not an array ref as first argument to extract_features");
730 root 1.36
731     av = (AV *) SvRV (ar);
732     result = newAV ();
733    
734     for (i = 0; i <= av_len (av); ++i)
735     {
736     SV *sv = *av_fetch (av, i, 1);
737     SV *histsv = newSV (9 * sizeof (float) + 1);
738    
739     SvPOK_on (histsv);
740     SvCUR_set (histsv, 9 * sizeof (float));
741     float *hist = (float *)SvPVX (histsv);
742    
743     struct feature f_h, f_s, f_v;
744     feature_init (&f_h);
745     feature_init (&f_s);
746     feature_init (&f_v);
747    
748     {
749     STRLEN len;
750     unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
751     while (len >= 3)
752     {
753     unsigned int r, g, b, h, s, v;
754     r = *buf++; g = *buf++; b = *buf++;
755     rgb_to_hsv (r, g, b, &h, &s, &v);
756    
757     feature_update_pass_1 (&f_h, h);
758     feature_update_pass_1 (&f_s, s);
759     feature_update_pass_1 (&f_v, v);
760    
761     len -= 3;
762     }
763    
764     feature_finish_pass_1 (&f_h);
765     feature_finish_pass_1 (&f_s);
766     feature_finish_pass_1 (&f_v);
767     }
768    
769     {
770     STRLEN len;
771     unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
772     while (len >= 3)
773     {
774     unsigned int r, g, b, h, s, v;
775     r = *buf++; g = *buf++; b = *buf++;
776     rgb_to_hsv (r, g, b, &h, &s, &v);
777    
778     feature_update_pass_2 (&f_h, h);
779     feature_update_pass_2 (&f_s, s);
780     feature_update_pass_2 (&f_v, v);
781    
782     len -= 3;
783     }
784    
785     feature_finish_pass_2 (&f_h);
786     feature_finish_pass_2 (&f_s);
787     feature_finish_pass_2 (&f_v);
788     }
789    
790     hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
791     hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
792 root 1.38 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
793 root 1.36
794     av_push (result, histsv);
795     }
796    
797     RETVAL = newRV_noinc ((SV *)result);
798     }
799     OUTPUT:
800     RETVAL
801