--- CV/CV.xs 2005/09/24 01:25:17 1.29 +++ CV/CV.xs 2006/06/22 20:50:05 1.36 @@ -4,6 +4,9 @@ #include #include +#include + +#include #include #include @@ -13,8 +16,8 @@ #include #include -#define IW 80 /* MUST match Schnauer.pm! */ -#define IH 60 /* MUST match Schnauer.pm! */ +#define IW 80 /* MUST match Schnauzer.pm! */ +#define IH 60 /* MUST match Schnauzer.pm! */ #define RAND (seed = (seed + 7141) * 54773 % 134456) @@ -67,6 +70,62 @@ } } +struct feature { + float v1, v2, v3; // mean, square, cube + int n; +}; + +static void +feature_init (struct feature *f) +{ + f->v1 = 0.; + f->v2 = 0.; + f->v3 = 0.; + f->n = 0; +} + +// didn't find an algorithm to neatly do mean, variance and skew in one pass. +// elmex ist schuld. +static void +feature_update_pass_1 (struct feature *f, unsigned int v) +{ + f->v1 += v; + f->n += 1; +} + +static void +feature_finish_pass_1 (struct feature *f) +{ + if (f->n < 1) + return; + + f->v1 /= f->n; +} + +static void +feature_update_pass_2 (struct feature *f, unsigned int v) +{ + float d = v - f->v1; + + f->v2 += d * d; + f->v3 += d * d * d; +} + +static void +feature_finish_pass_2 (struct feature *f) +{ + if (f->n < 1) + return; + + f->v2 /= f->n; + f->v3 /= f->n; + + // this is a peculiar weightung, but it works fine + f->v1 /= 255.; + f->v2 /= 128. * 128.; + f->v3 /= 128. * 128. * 64.; +} + static guint32 a85_val; static guint a85_cnt; static guchar a85_buf[LINELENGTH], *a85_ptr; @@ -145,6 +204,48 @@ OUTPUT: RETVAL +const char * +magic (const char *path) + CODE: +{ + static magic_t cookie; + + if (!cookie) + { + cookie = magic_open (MAGIC_NONE); + + if (cookie) + magic_load (cookie, 0); + else + XSRETURN_UNDEF; + } + + RETVAL = magic_file (cookie, path); +} + OUTPUT: + RETVAL + +const char * +magic_mime (const char *path) + CODE: +{ + static magic_t cookie; + + if (!cookie) + { + cookie = magic_open (MAGIC_MIME); + + if (cookie) + magic_load (cookie, 0); + else + XSRETURN_UNDEF; + } + + RETVAL = magic_file (cookie, path); +} + OUTPUT: + RETVAL + # missing in Gtk2 perl module gboolean @@ -203,13 +304,10 @@ int rs; FILE *fp; volatile GdkPixbuf *pb = 0; - gchar *filename; RETVAL = 0; - filename = g_filename_from_utf8 (SvPVutf8_nolen (path), -1, 0, 0, 0); - fp = fopen (filename, "rb"); - g_free (filename); + fp = fopen (SvPVbyte_nolen (path), "rb"); if (!fp) XSRETURN_UNDEF; @@ -292,6 +390,50 @@ OUTPUT: RETVAL +void +compare (GdkPixbuf *a, GdkPixbuf *b) + PPCODE: +{ + int w = gdk_pixbuf_get_width (a); + int h = gdk_pixbuf_get_height (a); + int sa = gdk_pixbuf_get_rowstride (a); + int sb = gdk_pixbuf_get_rowstride (b); + + guchar *pa = gdk_pixbuf_get_pixels (a); + guchar *pb = gdk_pixbuf_get_pixels (b); + + int x, y; + + assert (w == gdk_pixbuf_get_width (b)); + assert (h == gdk_pixbuf_get_height (b)); + + assert (gdk_pixbuf_get_n_channels (a) == 3); + assert (gdk_pixbuf_get_n_channels (b) == 3); + + double diff = 0.; + int peak = 0; + + if (w && h) + for (y = 0; y < h; y++) + { + guchar *pa_ = pa + y * sa; + guchar *pb_ = pb + y * sb; + + for (x = 0; x < w; x++) + { + int d; + + d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); + d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); + d = ((int)*pa_++) - ((int)*pb_++); diff += d*d; peak = MAX (peak, abs (d)); + } + } + + EXTEND (SP, 2); + PUSHs (sv_2mortal (newSVnv (sqrt (diff / (w * h * 3. * 255. * 255.))))); + PUSHs (sv_2mortal (newSVnv (peak / 255.))); +} + ############################################################################# MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Schnauzer @@ -502,6 +644,87 @@ MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster SV * +extract_features (SV *ar) + CODE: +{ + int i; + AV *av, *result; + + if (!SvROK (ar) || SvTYPE (SvRV (ar)) != SVt_PVAV) + croak ("Not an array ref as first argument to make_histogram"); + + av = (AV *) SvRV (ar); + result = newAV (); + + for (i = 0; i <= av_len (av); ++i) + { + SV *sv = *av_fetch (av, i, 1); + SV *histsv = newSV (9 * sizeof (float) + 1); + + SvPOK_on (histsv); + SvCUR_set (histsv, 9 * sizeof (float)); + float *hist = (float *)SvPVX (histsv); + + struct feature f_h, f_s, f_v; + feature_init (&f_h); + feature_init (&f_s); + feature_init (&f_v); + + { + STRLEN len; + unsigned char *buf = (unsigned char *)SvPVbyte (sv, len); + while (len >= 3) + { + unsigned int r, g, b, h, s, v; + r = *buf++; g = *buf++; b = *buf++; + rgb_to_hsv (r, g, b, &h, &s, &v); + + feature_update_pass_1 (&f_h, h); + feature_update_pass_1 (&f_s, s); + feature_update_pass_1 (&f_v, v); + + len -= 3; + } + + feature_finish_pass_1 (&f_h); + feature_finish_pass_1 (&f_s); + feature_finish_pass_1 (&f_v); + } + + { + STRLEN len; + unsigned char *buf = (unsigned char *)SvPVbyte (sv, len); + while (len >= 3) + { + unsigned int r, g, b, h, s, v; + r = *buf++; g = *buf++; b = *buf++; + rgb_to_hsv (r, g, b, &h, &s, &v); + + feature_update_pass_2 (&f_h, h); + feature_update_pass_2 (&f_s, s); + feature_update_pass_2 (&f_v, v); + + len -= 3; + } + + feature_finish_pass_2 (&f_h); + feature_finish_pass_2 (&f_s); + feature_finish_pass_2 (&f_v); + } + + hist [0] = f_h.v1 * 2.; hist [1] = f_h.v2 * 2.; hist [2] = f_h.v3 * 2.; + hist [3] = f_s.v1 ; hist [4] = f_s.v2 ; hist [5] = f_s.v3 ; + hist [6] = f_v.v1 ; hist [7] = f_v.v2 ; hist [8] = f_v.v3 ; + + av_push (result, histsv); + } + + RETVAL = newRV_noinc ((SV *)result); +} + OUTPUT: + RETVAL + +SV * make_histograms (SV *ar) CODE: {