--- CV/CV.xs 2015/07/04 05:31:17 1.46 +++ CV/CV.xs 2017/12/23 03:30:28 1.50 @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -18,6 +19,10 @@ #include +#if WEBP +#include +#endif + #include "perlmulticore.h" #define IW 80 /* MUST match Schnauzer.pm! */ @@ -40,12 +45,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 +163,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; @@ -313,7 +324,72 @@ RETVAL GdkPixbuf_noinc * -load_jpeg (SV *path, int thumbnail=0) +load_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) + CODE: +{ +#if WEBP + STRLEN data_size; + guchar *data = SvPVbyte (image_data, data_size); + int alpha; + WebPDecoderConfig config; + + perlinterp_release (); + + RETVAL = 0; + + if (!WebPInitDecoderConfig (&config)) + goto err; + + config.options.use_threads = 1; + + if (thumbnail) + { + 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 = config.input.width; + ih = config.input.height; + } + + 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); + 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, int iw = 0, int ih = 0) CODE: { struct jpeg_decompress_struct cinfo; @@ -371,24 +447,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,6 +486,35 @@ 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);