--- CV/CV.xs 2015/07/04 05:31:17 1.46 +++ CV/CV.xs 2017/12/27 17:48:16 1.56 @@ -9,8 +9,11 @@ #include #include +#include + #include #include +#include #include #include @@ -18,6 +21,11 @@ #include +#if WEBP +#include +#include +#endif + #include "perlmulticore.h" #define IW 80 /* MUST match Schnauzer.pm! */ @@ -31,6 +39,8 @@ typedef char *octet_string; +static magic_t magic_cookie[2]; /* !mime, mime */ + struct jpg_err_mgr { struct jpeg_error_mgr err; @@ -40,12 +50,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; } @@ -152,9 +168,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; @@ -187,11 +203,61 @@ } ///////////////////////////////////////////////////////////////////////////// +// memory source for libjpeg + +static void cv_ms_init (j_decompress_ptr cinfo) +{ +} + +static void cv_ms_term (j_decompress_ptr cinfo) +{ +} + +static boolean cv_ms_fill (j_decompress_ptr cinfo) +{ + ERREXIT (cinfo, JERR_INPUT_EMPTY); + + return TRUE; +} + +static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes) +{ + if (num_bytes > 0) + { + struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src; + + src->next_input_byte += num_bytes; + src->bytes_in_buffer -= num_bytes; + } +} + +static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen) +{ + struct jpeg_source_mgr *src; + + if (!cinfo->src) + cinfo->src = (struct jpeg_source_mgr *) + (*cinfo->mem->alloc_small) ( + (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr) + ); + + src = (struct jpeg_source_mgr *)cinfo->src; + src->init_source = cv_ms_init; + src->fill_input_buffer = cv_ms_fill; + src->skip_input_data = cv_ms_skip; + src->resync_to_restart = jpeg_resync_to_restart; + src->term_source = cv_ms_term; + src->next_input_byte = (JOCTET *)buf; + src->bytes_in_buffer = buflen; +} + +///////////////////////////////////////////////////////////////////////////// MODULE = Gtk2::CV PACKAGE = Gtk2::CV PROTOTYPES: ENABLE +# calculate the common prefix length of two strings # missing function in perl. really :) int common_prefix_length (a, b) @@ -210,44 +276,31 @@ RETVAL const char * -magic (octet_string path) +magic (SV *path_or_data) + ALIAS: + magic = 0 + magic_mime = 1 + magic_buffer = 2 + magic_buffer_mime = 3 CODE: { - static magic_t cookie; - - if (!cookie) - { - cookie = magic_open (MAGIC_SYMLINK); + STRLEN len; + char *data = SvPVbyte (path_or_data, len); - if (cookie) - magic_load (cookie, 0); - else - XSRETURN_UNDEF; + if (!magic_cookie[0]) + { + magic_cookie[0] = magic_open (MAGIC_SYMLINK); + magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE); + magic_load (magic_cookie[0], 0); + magic_load (magic_cookie[1], 0); } - RETVAL = magic_file (cookie, path); -} - OUTPUT: - RETVAL - -const char * -magic_mime (octet_string path) - CODE: -{ - static magic_t cookie; + perlinterp_release (); - if (!cookie) - { - cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK); + RETVAL = ix & 2 + ? magic_buffer (magic_cookie[ix & 1], data, len) + : magic_file (magic_cookie[ix & 1], data); - if (cookie) - magic_load (cookie, 0); - else - XSRETURN_UNDEF; - } - - perlinterp_release (); - RETVAL = magic_file (cookie, path); perlinterp_acquire (); } OUTPUT: @@ -312,24 +365,146 @@ OUTPUT: RETVAL +const char * +filetype (SV *image_data) + CODE: +{ + STRLEN data_len; + U8 *data = SvPVbyte (image_data, data_len); + + if (data_len >= 20 + && data[0] == 0xff + && data[1] == 0xd8 + && data[2] == 0xff) + RETVAL = "jpg"; + else if (data_len >= 12 + && data[ 0] == (U8)'R' + && data[ 1] == (U8)'I' + && data[ 2] == (U8)'F' + && data[ 3] == (U8)'F' + && data[ 8] == (U8)'W' + && data[ 9] == (U8)'E' + && data[10] == (U8)'B' + && data[11] == (U8)'P') + RETVAL = "webp"; + else if (data_len >= 16 + && data[ 0] == 0x89 + && data[ 1] == (U8)'P' + && data[ 2] == (U8)'N' + && data[ 3] == (U8)'G' + && data[ 4] == 0x0d + && data[ 5] == 0x0a + && data[ 6] == 0x1a + && data[ 7] == 0x0a) + RETVAL = "png"; + else + RETVAL = "other"; +} + OUTPUT: + RETVAL + GdkPixbuf_noinc * -load_jpeg (SV *path, int thumbnail=0) +decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) + CODE: +{ +#if WEBP + STRLEN data_size; + int alpha; + WebPData data; + WebPDemuxer *demux; + WebPIterator iter; + WebPDecoderConfig config; + int inw, inh; + + data.bytes = (uint8_t *)SvPVbyte (image_data, data_size); + data.size = data_size; + + perlinterp_release (); + + RETVAL = 0; + + if (!(demux = WebPDemux (&data))) + goto err_demux; + + if (!WebPDemuxGetFrame (demux, 1, &iter)) + goto err_iter; + + if (!WebPInitDecoderConfig (&config)) + goto err_iter; + + config.options.use_threads = 1; + + if (WebPGetFeatures (iter.fragment.bytes, iter.fragment.size, &config.input) != VP8_STATUS_OK) + goto err_iter; + + 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_iter; + + 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 (iter.fragment.bytes, iter.fragment.size, &config) != VP8_STATUS_OK) + { + g_object_unref (RETVAL); + RETVAL = 0; + goto err_iter; + } + + err_iter: + WebPDemuxReleaseIterator (&iter); + err_demux: + WebPDemuxDelete (demux); + + perlinterp_acquire (); +#else + croak ("load_webp: webp not enabled at compile time"); +#endif +} + OUTPUT: + RETVAL + +GdkPixbuf_noinc * +decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) CODE: { struct jpeg_decompress_struct cinfo; struct jpg_err_mgr jerr; - guchar *data; int rs; - FILE *fp; volatile GdkPixbuf *pb = 0; + STRLEN data_len; + guchar *data = SvPVbyte (image_data, data_len); RETVAL = 0; - fp = fopen (SvPVbyte_nolen (path), "rb"); - - if (!fp) - XSRETURN_UNDEF; - perlinterp_release (); cinfo.err = jpeg_std_error (&jerr.err); @@ -339,7 +514,6 @@ if ((rs = setjmp (jerr.setjmp_buffer))) { - fclose (fp); jpeg_destroy_decompress (&cinfo); if (pb) @@ -350,8 +524,8 @@ } jpeg_create_decompress (&cinfo); + cv_jpeg_mem_src (&cinfo, data, data_len); - jpeg_stdio_src (&cinfo, fp); jpeg_read_header (&cinfo, TRUE); cinfo.dct_method = JDCT_DEFAULT; @@ -371,24 +545,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) @@ -404,8 +584,36 @@ 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 (); }