--- CV/CV.xs 2006/02/15 08:36:55 1.32 +++ CV/CV.xs 2017/12/23 04:11:49 1.51 @@ -4,19 +4,29 @@ #include #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 + +#if WEBP +#include +#endif + +#include "perlmulticore.h" + +#define IW 80 /* MUST match Schnauzer.pm! */ +#define IH 60 /* MUST match Schnauzer.pm! */ #define RAND (seed = (seed + 7141) * 54773 % 134456) @@ -24,6 +34,10 @@ #define ELLIPSIS "\xe2\x80\xa6" +typedef char *octet_string; + +static magic_t magic_cookie[2]; /* !mime, mime */ + struct jpg_err_mgr { struct jpeg_error_mgr err; @@ -33,12 +47,18 @@ static void cv_error_exit (j_common_ptr cinfo) { + cinfo->err->output_message (cinfo); longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); } static void cv_error_output (j_common_ptr cinfo) { + char msg[JMSG_LENGTH_MAX]; + + cinfo->err->format_message (cinfo, msg); + + fprintf (stderr, "JPEG decoding error: %s\n", msg); return; } @@ -69,6 +89,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; @@ -90,9 +165,9 @@ a85_cnt = 4; if (a85_val) { - a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; - a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; - a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; + a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; + a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; + a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; a85_ptr[0] = (a85_val ) + 33; @@ -148,48 +223,40 @@ 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; +magic (SV *path_or_data) + ALIAS: + magic = 0 + magic_mime = 1 + magic_buffer = 2 + magic_buffer_mime = 3 + CODE: +{ + STRLEN len; + char *data = SvPVbyte (path_or_data, len); + + perlinterp_release (); + + if (!magic_cookie[0]) + { + magic_cookie[0] = magic_open (MAGIC_SYMLINK); + magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME); } - 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 = ix & 2 + ? magic_buffer (magic_cookie[ix], data, len) + : magic_file (magic_cookie[ix], data); - RETVAL = magic_file (cookie, path); + perlinterp_acquire (); } OUTPUT: RETVAL -# missing in Gtk2 perl module +# 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) @@ -205,6 +272,7 @@ GdkPixbuf_noinc * dealpha_expose (GdkPixbuf *pb) CODE: + perlinterp_release (); { int w = gdk_pixbuf_get_width (pb); int h = gdk_pixbuf_get_height (pb); @@ -223,22 +291,103 @@ for (i = 0; i < 3; i++) dst[x * 3 + y * dstr + i] = src[x * bpp + y * sstr + i]; } + perlinterp_acquire (); OUTPUT: RETVAL GdkPixbuf_noinc * rotate (GdkPixbuf *pb, int angle) CODE: + perlinterp_release (); + if (angle < 0) + angle += 360; 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); + perlinterp_acquire (); + OUTPUT: + RETVAL + +GdkPixbuf_noinc * +decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) + CODE: +{ +#if WEBP + guchar *data; + STRLEN data_size; + int alpha; + WebPDecoderConfig config; + int inw, inh; + + data = SvPVbyte (image_data, data_size); + + perlinterp_release (); + + RETVAL = 0; + + if (!WebPInitDecoderConfig (&config)) + goto err; + + config.options.use_threads = 1; + + if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK) + goto err; + + inw = config.input.width; + inh = config.input.height; + + if (thumbnail) + { + if (inw * ih > inh * iw) + ih = (iw * inh + inw - 1) / inw; + else + iw = (ih * inw + inh - 1) / inh; + + config.options.bypass_filtering = 1; + config.options.no_fancy_upsampling = 1; + + config.options.use_scaling = 1; + config.options.scaled_width = iw; + config.options.scaled_height = ih; + } + else + { + iw = inw; + ih = inh; + } + + alpha = config.input.has_alpha; + + RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih); + if (!RETVAL) + goto err; + + config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB; + config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL); + config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL); + config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL); + config.output.is_external_memory = 1; + + if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK) + { + g_object_unref (RETVAL); + RETVAL = 0; + goto err; + } + + err: + perlinterp_acquire (); +#else + croak ("load_webp: webp not enabled at compile time"); +#endif +} OUTPUT: RETVAL GdkPixbuf_noinc * -load_jpeg (SV *path, int thumbnail=0) +load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) CODE: { struct jpeg_decompress_struct cinfo; @@ -255,6 +404,8 @@ if (!fp) XSRETURN_UNDEF; + perlinterp_release (); + cinfo.err = jpeg_std_error (&jerr.err); jerr.err.error_exit = cv_error_exit; @@ -268,6 +419,7 @@ if (pb) g_object_unref ((gpointer)pb); + perlinterp_acquire (); XSRETURN_UNDEF; } @@ -293,24 +445,30 @@ cinfo.do_fancy_upsampling = FALSE; while (cinfo.scale_denom < 8 - && cinfo.output_width >= IW*4 - && cinfo.output_height >= IH*4) + && cinfo.output_width >= iw*4 + && cinfo.output_height >= ih*4) { cinfo.scale_denom <<= 1; jpeg_calc_output_dimensions (&cinfo); } } - pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); + if (cinfo.output_components != 3) + longjmp (jerr.setjmp_buffer, 3); + + if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK) + { + cinfo.out_color_space = JCS_CMYK; + cinfo.output_components = 4; + } + + pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height); if (!RETVAL) longjmp (jerr.setjmp_buffer, 2); data = gdk_pixbuf_get_pixels (RETVAL); rs = gdk_pixbuf_get_rowstride (RETVAL); - if (cinfo.output_components != 3) - longjmp (jerr.setjmp_buffer, 3); - jpeg_start_decompress (&cinfo); while (cinfo.output_scanline < cinfo.output_height) @@ -326,24 +484,104 @@ jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); } + if (cinfo.out_color_space == JCS_CMYK) + { + guchar *end = data + cinfo.output_height * rs; + + while (data < end) + { + U32 c = data [0]; + U32 m = data [1]; + U32 y = data [2]; + U32 k = data [3]; + + if (0) + if (cinfo.Adobe_transform == 2) + { + c ^= 0xff; + m ^= 0xff; + y ^= 0xff; + k ^= 0xff; + } + + data [0] = (c * k + 0x80) / 0xff; + data [1] = (m * k + 0x80) / 0xff; + data [2] = (y * k + 0x80) / 0xff; + data [3] = 0xff; + + data += 4; + } + } + jpeg_finish_decompress (&cinfo); fclose (fp); jpeg_destroy_decompress (&cinfo); + perlinterp_acquire (); } OUTPUT: RETVAL +void +compare (GdkPixbuf *a, GdkPixbuf *b) + PPCODE: + perlinterp_release (); +{ + 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)); + } + } + + perlinterp_acquire (); + + 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 = (U8 *)SvPVutf8 (pathsv, plen); + U8 *path = (U8 *)SvPV (pathsv, plen); U8 *pend = path + plen; U8 dst [plen * 6 * 3], *dstp = dst; @@ -353,6 +591,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; @@ -366,12 +606,16 @@ 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 ((const char *)dst, dstp - dst); @@ -543,50 +787,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); - - int tmphist[HISTSIZE]; - float *hist; + SV *histsv = newSV (9 * sizeof (float) + 1); - 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); } @@ -596,4 +867,3 @@ OUTPUT: RETVAL -