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

Comparing CV/CV.xs (file contents):
Revision 1.33 by root, Fri Feb 17 08:48:06 2006 UTC vs.
Revision 1.40 by root, Sat Nov 25 15:00:51 2006 UTC

68 68
69 *h &= 255; 69 *h &= 255;
70 } 70 }
71} 71}
72 72
73struct feature {
74 float v1, v2, v3; // mean, square, cube
75 int n;
76};
77
78static void
79feature_init (struct feature *f)
80{
81 f->v1 = 0.;
82 f->v2 = 0.;
83 f->v3 = 0.;
84 f->n = 0;
85}
86
87// didn't find an algorithm to neatly do mean, variance and skew in one pass.
88// elmex ist schuld.
89static void
90feature_update_pass_1 (struct feature *f, unsigned int v)
91{
92 f->v1 += v;
93 f->n += 1;
94}
95
96static void
97feature_finish_pass_1 (struct feature *f)
98{
99 if (f->n < 1)
100 return;
101
102 f->v1 /= f->n;
103}
104
105static void
106feature_update_pass_2 (struct feature *f, unsigned int v)
107{
108 float d = v - f->v1;
109
110 f->v2 += d * d;
111 f->v3 += d * d * d;
112}
113
114static void
115feature_finish_pass_2 (struct feature *f)
116{
117 if (f->n < 1)
118 return;
119
120 f->v2 /= f->n;
121 f->v3 /= f->n;
122
123 f->v1 /= 255.;
124 f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2);
125 f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.);
126}
127
73static guint32 a85_val; 128static guint32 a85_val;
74static guint a85_cnt; 129static guint a85_cnt;
75static guchar a85_buf[LINELENGTH], *a85_ptr; 130static guchar a85_buf[LINELENGTH], *a85_ptr;
76 131
77static void 132static void
188 RETVAL = magic_file (cookie, path); 243 RETVAL = magic_file (cookie, path);
189} 244}
190 OUTPUT: 245 OUTPUT:
191 RETVAL 246 RETVAL
192 247
193# missing in Gtk2 perl module 248# missing/broken in Gtk2 perl module
249
250void
251gdk_window_clear_hints (GdkWindow *window)
252 CODE:
253 gdk_window_set_geometry_hints (window, 0, 0);
194 254
195gboolean 255gboolean
196gdk_net_wm_supports (GdkAtom property) 256gdk_net_wm_supports (GdkAtom property)
197 CODE: 257 CODE:
198#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE) 258#if defined(GDK_WINDOWING_X11) && !defined(GDK_MULTIHEAD_SAFE)
355 assert (gdk_pixbuf_get_n_channels (b) == 3); 415 assert (gdk_pixbuf_get_n_channels (b) == 3);
356 416
357 double diff = 0.; 417 double diff = 0.;
358 int peak = 0; 418 int peak = 0;
359 419
420 if (w && h)
360 for (y = 0; y < h; y++) 421 for (y = 0; y < h; y++)
361 { 422 {
362 guchar *pa_ = pa + y * sa; 423 guchar *pa_ = pa + y * sa;
363 guchar *pb_ = pb + y * sb; 424 guchar *pb_ = pb + y * sb;
364 425
365 for (x = 0; x < w; x ++) 426 for (x = 0; x < w; x++)
366 { 427 {
367 int d; 428 int d;
368 429
369 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); 430 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)); 431 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)); 432 d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d));
372 } 433 }
373 } 434 }
374 435
375 EXTEND (SP, 2); 436 EXTEND (SP, 2);
376 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); 437 PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.)))));
377 PUSHs (sv_2mortal (newSVnv (peak / 255.))); 438 PUSHs (sv_2mortal (newSVnv (peak / 255.)));
378} 439}
585############################################################################# 646#############################################################################
586 647
587MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster 648MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster
588 649
589SV * 650SV *
590make_histograms (SV *ar) 651extract_features (SV *ar)
591 CODE: 652 CODE:
592{ 653{
593 int i; 654 int i;
594 AV *av, *result; 655 AV *av, *result;
595 656
596 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) 657 if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV)
597 croak ("Not an array ref as first argument to make_histogram"); 658 croak ("Not an array ref as first argument to extract_features");
598 659
599 av = (AV *) SvRV (ar); 660 av = (AV *) SvRV (ar);
600 result = newAV (); 661 result = newAV ();
601 662
602 for (i = 0; i <= av_len (av); ++i) 663 for (i = 0; i <= av_len (av); ++i)
603 { 664 {
604 const int HISTSIZE = 64;
605
606 int j;
607 SV *sv = *av_fetch (av, i, 1); 665 SV *sv = *av_fetch (av, i, 1);
608 STRLEN len;
609 char *buf = SvPVbyte (sv, len);
610
611 int tmphist[HISTSIZE];
612 float *hist;
613
614 SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); 666 SV *histsv = newSV (9 * sizeof (float) + 1);
667
615 SvPOK_on (histsv); 668 SvPOK_on (histsv);
616 SvCUR_set (histsv, HISTSIZE * sizeof (float)); 669 SvCUR_set (histsv, 9 * sizeof (float));
617 hist = (float *)SvPVX (histsv); 670 float *hist = (float *)SvPVX (histsv);
618 671
619 Zero (tmphist, sizeof (tmphist), char); 672 struct feature f_h, f_s, f_v;
673 feature_init (&f_h);
674 feature_init (&f_s);
675 feature_init (&f_v);
620 676
621 for (j = len; j--; )
622 {
623 unsigned int idx
624 = ((*buf & 0xc0) >> 2)
625 | ((*buf & 0x18) >> 1)
626 | (*buf & 0x03);
627
628 ++tmphist[idx];
629 ++buf;
630 }
631 677 {
632 for (j = 0; j < HISTSIZE; ++j) 678 STRLEN len;
633 hist[j] = (float)tmphist[j] / (len + 1e-30); 679 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
680 while (len >= 3)
681 {
682 unsigned int r, g, b, h, s, v;
683 r = *buf++; g = *buf++; b = *buf++;
684 rgb_to_hsv (r, g, b, &h, &s, &v);
685
686 feature_update_pass_1 (&f_h, h);
687 feature_update_pass_1 (&f_s, s);
688 feature_update_pass_1 (&f_v, v);
689
690 len -= 3;
691 }
692
693 feature_finish_pass_1 (&f_h);
694 feature_finish_pass_1 (&f_s);
695 feature_finish_pass_1 (&f_v);
696 }
697
698 {
699 STRLEN len;
700 unsigned char *buf = (unsigned char *)SvPVbyte (sv, len);
701 while (len >= 3)
702 {
703 unsigned int r, g, b, h, s, v;
704 r = *buf++; g = *buf++; b = *buf++;
705 rgb_to_hsv (r, g, b, &h, &s, &v);
706
707 feature_update_pass_2 (&f_h, h);
708 feature_update_pass_2 (&f_s, s);
709 feature_update_pass_2 (&f_v, v);
710
711 len -= 3;
712 }
713
714 feature_finish_pass_2 (&f_h);
715 feature_finish_pass_2 (&f_s);
716 feature_finish_pass_2 (&f_v);
717 }
718
719 hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.;
720 hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ;
721 hist [6] = f_v.v1 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5;
634 722
635 av_push (result, histsv); 723 av_push (result, histsv);
636 } 724 }
637 725
638 RETVAL = newRV_noinc ((SV *)result); 726 RETVAL = newRV_noinc ((SV *)result);
639} 727}
640 OUTPUT: 728 OUTPUT:
641 RETVAL 729 RETVAL
642 730
643

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines