--- CV/CV.xs 2005/08/14 02:25:44 1.22 +++ CV/CV.xs 2009/05/03 08:16:47 1.43 @@ -4,15 +4,22 @@ #include #include +#include + +#include #include +#include +#include #include #include #include -#define IW 80 /* MUST match Schnauer.pm! */ -#define IH 60 /* MUST match Schnauer.pm! */ +#include + +#define IW 80 /* MUST match Schnauzer.pm! */ +#define IH 60 /* MUST match Schnauzer.pm! */ #define RAND (seed = (seed + 7141) * 54773 % 134456) @@ -20,6 +27,8 @@ #define ELLIPSIS "\xe2\x80\xa6" +typedef char *octet_string; + struct jpg_err_mgr { struct jpeg_error_mgr err; @@ -65,6 +74,61 @@ } } +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; + + f->v1 /= 255.; + f->v2 /= 255. * 255.; f->v2 = sqrtf (f->v2); + f->v3 /= 255. * 255. * 255.; f->v3 = powf (fabsf (f->v3), 1./3.); +} + static guint32 a85_val; static guint a85_cnt; static guchar a85_buf[LINELENGTH], *a85_ptr; @@ -126,7 +190,71 @@ PROTOTYPES: ENABLE -# missing in Gtk2 perl module +# missing function in perl. really :) +int +common_prefix_length (a, b) + unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg); + unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg); + CODE: + RETVAL = 0; + + while (*a == *b && *a) + { + RETVAL += (*a & 0xc0) != 0x80; + a++, b++; + } + + OUTPUT: + RETVAL + +const char * +magic (octet_string 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 (octet_string 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/broken in Gtk2 perl module + +void +gdk_window_clear_hints (GdkWindow *window) + CODE: + gdk_window_set_geometry_hints (window, 0, 0); gboolean gdk_net_wm_supports (GdkAtom property) @@ -140,7 +268,7 @@ RETVAL GdkPixbuf_noinc * -transpose (GdkPixbuf *pb) +dealpha_expose (GdkPixbuf *pb) CODE: { int w = gdk_pixbuf_get_width (pb); @@ -150,45 +278,32 @@ guchar *src = gdk_pixbuf_get_pixels (pb), *dst; int sstr = gdk_pixbuf_get_rowstride (pb), dstr; - RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, h, w); + RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); dst = gdk_pixbuf_get_pixels (RETVAL); dstr = gdk_pixbuf_get_rowstride (RETVAL); - for (y = 0; y < h; y++) - for (x = 0; x < w; x++) - for (i = 0; i < bpp; i++) - dst[y * bpp + x * dstr + i] = src[x * bpp + y * sstr + i]; + for (x = 0; x < w; x++) + for (y = 0; y < h; y++) + for (i = 0; i < 3; i++) + dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; } OUTPUT: RETVAL GdkPixbuf_noinc * -flop (GdkPixbuf *pb) +rotate (GdkPixbuf *pb, int angle) CODE: -{ - int w = gdk_pixbuf_get_width (pb); - int h = gdk_pixbuf_get_height (pb); - int bpp = gdk_pixbuf_get_n_channels (pb); - int x, y, i; - guchar *src = gdk_pixbuf_get_pixels (pb), *dst; - int sstr = gdk_pixbuf_get_rowstride (pb), dstr; - - RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, bpp == 4, 8, w, h); - - dst = gdk_pixbuf_get_pixels (RETVAL); - dstr = gdk_pixbuf_get_rowstride (RETVAL); - - for (y = 0; y < h; y++) - for (x = 0; x < w; x++) - for (i = 0; i < bpp; i++) - dst[(w - 1 - x) * bpp + y * dstr + i] = src[x * bpp + y * sstr + i]; -} + RETVAL = gdk_pixbuf_rotate_simple (pb, angle == 0 ? GDK_PIXBUF_ROTATE_NONE + : angle == 90 ? GDK_PIXBUF_ROTATE_COUNTERCLOCKWISE + : angle == 180 ? GDK_PIXBUF_ROTATE_UPSIDEDOWN + : angle == 270 ? GDK_PIXBUF_ROTATE_CLOCKWISE + : angle); OUTPUT: RETVAL GdkPixbuf_noinc * -load_jpeg (char *path, int thumbnail=0) +load_jpeg (SV *path, int thumbnail=0) CODE: { struct jpeg_decompress_struct cinfo; @@ -197,9 +312,12 @@ int rs; FILE *fp; volatile GdkPixbuf *pb = 0; + RETVAL = 0; - if (!(fp = fopen (path, "rb"))) + fp = fopen (SvPVbyte_nolen (path), "rb"); + + if (!fp) XSRETURN_UNDEF; cinfo.err = jpeg_std_error (&jerr.err); @@ -280,17 +398,64 @@ 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 +# currently only works for filenames (octet strings) + SV * foldcase (SV *pathsv) PROTOTYPE: $ CODE: { STRLEN plen; - U8 *path = SvPVutf8 (pathsv, plen); + U8 *path = (U8 *)SvPV (pathsv, plen); U8 *pend = path + plen; U8 dst [plen * 6 * 3], *dstp = dst; @@ -300,6 +465,8 @@ if (ch >= 'a' && ch <= 'z') *dstp++ = *path++; + else if (ch >= 'A' && ch <= 'Z') + *dstp++ = *path++ + ('a' - 'A'); else if (ch >= '0' && ch <= '9') { STRLEN el, nl = 0; @@ -313,27 +480,32 @@ dstp += nl; } else + *dstp++ = *path++; +#if 0 + else { STRLEN cl; to_utf8_fold (path, dstp, &cl); dstp += cl; path += is_utf8_char (path); } +#endif } - RETVAL = newSVpvn (dst, dstp - dst); + RETVAL = newSVpvn ((const char *)dst, dstp - dst); } OUTPUT: RETVAL GdkPixbuf_noinc * -p7_to_pb (int w, int h, guchar *src) +p7_to_pb (int w, int h, SV *src_sv) PROTOTYPE: @ CODE: { int x, y; guchar *dst, *d; int dstr; + guchar *src = (guchar *)SvPVbyte_nolen (src_sv); RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, w, h); dst = gdk_pixbuf_get_pixels (RETVAL); @@ -352,64 +524,6 @@ OUTPUT: RETVAL -SV * -pb_to_p7 (GdkPixbuf *pb) - CODE: -{ - int w = gdk_pixbuf_get_width (pb); - int h = gdk_pixbuf_get_height (pb); - int x, y; - guchar *dst; - int bpp = gdk_pixbuf_get_n_channels (pb); - guchar *src = gdk_pixbuf_get_pixels (pb); - int sstr = gdk_pixbuf_get_rowstride (pb); - int Er[IW], Eg[IW], Eb[IW]; - int seed = 77; - - RETVAL = newSV (w * h); - SvPOK_only (RETVAL); - SvCUR_set (RETVAL, w * h); - - dst = SvPVX (RETVAL); - - memset (Er, 0, sizeof (int) * IW); - memset (Eg, 0, sizeof (int) * IW); - memset (Eb, 0, sizeof (int) * IW); - - /* some primitive error distribution + random dithering */ - - for (y = 0; y < h; y++) - { - int er = 0, eg = 0, eb = 0; - - for (x = 0; x < w; x++) - { - int r, g, b; - guchar *p = src + x * bpp + y * sstr; - - r = ((p[0] + er + Er[x]) * 7 + (RAND & 127) + 64) / 255; - g = ((p[1] + eg + Eg[x]) * 7 + (RAND & 127) + 64) / 255; - b = ((p[2] + eb + Eb[x]) * 3 + (RAND & 127) + 64) / 255; - - r = r > 7 ? 7 : r < 0 ? 0 : r; - g = g > 7 ? 7 : g < 0 ? 0 : g; - b = b > 3 ? 3 : b < 0 ? 0 : b; - - er += p[0] - (r * 255 + 4) / 7; - eg += p[1] - (g * 255 + 4) / 7; - eb += p[2] - (b * 255 + 2) / 3; - - Er[x] = er >> 1; er -= (er + 1) >> 1; - Eg[x] = eg >> 1; eg -= (eg + 1) >> 1; - Eb[x] = eb >> 1; eb -= (eb + 1) >> 1; - - *dst++ = r << 5 | g << 2 | b; - } - } -} - OUTPUT: - RETVAL - ############################################################################# MODULE = Gtk2::CV PACKAGE = Gtk2::CV::PostScript @@ -474,7 +588,7 @@ SvPOK_only (RETVAL); SvCUR_set (RETVAL, 6 * 8 * 12 / 8); - dst = SvPVX (RETVAL); + dst = (guchar *)SvPVX (RETVAL); /* some primitive error distribution + random dithering */ @@ -547,50 +661,77 @@ MODULE = Gtk2::CV PACKAGE = Gtk2::CV::Plugin::RCluster SV * -make_histograms (SV *ar) - CODE: +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"); + croak ("Not an array ref as first argument to extract_features"); av = (AV *) SvRV (ar); result = newAV (); for (i = 0; i <= av_len (av); ++i) { - const int HISTSIZE = 64; - - int j; SV *sv = *av_fetch (av, i, 1); - STRLEN len; - char *buf = SvPVbyte (sv, len); + SV *histsv = newSV (9 * sizeof (float) + 1); - int tmphist[HISTSIZE]; - float *hist; - - SV *histsv = newSV (HISTSIZE * sizeof (float) + 1); SvPOK_on (histsv); - SvCUR_set (histsv, HISTSIZE * sizeof (float)); - hist = (float *)SvPVX (histsv); + SvCUR_set (histsv, 9 * sizeof (float)); + float *hist = (float *)SvPVX (histsv); - Zero (tmphist, sizeof (tmphist), char); + struct feature f_h, f_s, f_v; + feature_init (&f_h); + feature_init (&f_s); + feature_init (&f_v); - for (j = len; j--; ) - { - unsigned int idx - = ((*buf & 0xc0) >> 2) - | ((*buf & 0x18) >> 1) - | (*buf & 0x03); + { + 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); + } - ++tmphist[idx]; - ++buf; - } - - for (j = 0; j < HISTSIZE; ++j) - hist[j] = (float)tmphist[j] / (len + 1e-30); + { + 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 * .5; hist [7] = f_v.v2 * .5; hist [8] = f_v.v3 * .5; av_push (result, histsv); } @@ -600,4 +741,3 @@ OUTPUT: RETVAL -