--- CV/CV.xs 2017/12/23 03:30:28 1.50 +++ CV/CV.xs 2017/12/23 04:11:49 1.51 @@ -36,6 +36,8 @@ typedef char *octet_string; +static magic_t magic_cookie[2]; /* !mime, mime */ + struct jpg_err_mgr { struct jpeg_error_mgr err; @@ -221,44 +223,29 @@ 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; + STRLEN len; + char *data = SvPVbyte (path_or_data, len); - if (!cookie) - { - cookie = magic_open (MAGIC_SYMLINK); + perlinterp_release (); - 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); } - 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 | MAGIC_SYMLINK); - - 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); - perlinterp_release (); - RETVAL = magic_file (cookie, path); perlinterp_acquire (); } OUTPUT: @@ -324,14 +311,17 @@ RETVAL GdkPixbuf_noinc * -load_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) +decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) CODE: { #if WEBP + guchar *data; STRLEN data_size; - guchar *data = SvPVbyte (image_data, data_size); int alpha; WebPDecoderConfig config; + int inw, inh; + + data = SvPVbyte (image_data, data_size); perlinterp_release (); @@ -342,8 +332,19 @@ 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; @@ -353,13 +354,10 @@ } else { - iw = config.input.width; - ih = config.input.height; + iw = inw; + ih = inh; } - if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK) - goto err; - alpha = config.input.has_alpha; RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih);