--- CV/CV.xs 2005/08/14 02:25:44 1.22 +++ CV/CV.xs 2006/03/07 16:45:53 1.35 @@ -4,15 +4,20 @@ #include #include +#include + +#include #include +#include +#include #include #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) @@ -126,6 +131,65 @@ PROTOTYPES: ENABLE +# 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 (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 @@ -140,7 +204,7 @@ RETVAL GdkPixbuf_noinc * -transpose (GdkPixbuf *pb) +dealpha_expose (GdkPixbuf *pb) CODE: { int w = gdk_pixbuf_get_width (pb); @@ -150,45 +214,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 +248,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,6 +334,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 @@ -290,7 +388,7 @@ CODE: { STRLEN plen; - U8 *path = SvPVutf8 (pathsv, plen); + U8 *path = (U8 *)SvPVutf8 (pathsv, plen); U8 *pend = path + plen; U8 dst [plen * 6 * 3], *dstp = dst; @@ -321,19 +419,20 @@ } } - 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 +451,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 +515,7 @@ SvPOK_only (RETVAL); SvCUR_set (RETVAL, 6 * 8 * 12 / 8); - dst = SvPVX (RETVAL); + dst = (guchar *)SvPVX (RETVAL); /* some primitive error distribution + random dithering */