ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/CV/CV.xs
(Generate patch)

Comparing CV/CV.xs (file contents):
Revision 1.51 by root, Sat Dec 23 04:11:49 2017 UTC vs.
Revision 1.56 by root, Wed Dec 27 17:48:16 2017 UTC

7#include <math.h> 7#include <math.h>
8 8
9#include <magic.h> 9#include <magic.h>
10 10
11#include <jpeglib.h> 11#include <jpeglib.h>
12#include <jerror.h>
13
12#include <glib.h> 14#include <glib.h>
13#include <gtk/gtk.h> 15#include <gtk/gtk.h>
14#include <gdk/gdkx.h> 16#include <gdk/gdkx.h>
15#include <gdk-pixbuf/gdk-pixbuf.h> 17#include <gdk-pixbuf/gdk-pixbuf.h>
16 18
18#include <gtk2perl.h> 20#include <gtk2perl.h>
19 21
20#include <assert.h> 22#include <assert.h>
21 23
22#if WEBP 24#if WEBP
25#include <webp/demux.h>
23#include <webp/decode.h> 26#include <webp/decode.h>
24#endif 27#endif
25 28
26#include "perlmulticore.h" 29#include "perlmulticore.h"
27 30
198 201
199 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 202 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
200} 203}
201 204
202///////////////////////////////////////////////////////////////////////////// 205/////////////////////////////////////////////////////////////////////////////
206// memory source for libjpeg
207
208static void cv_ms_init (j_decompress_ptr cinfo)
209{
210}
211
212static void cv_ms_term (j_decompress_ptr cinfo)
213{
214}
215
216static boolean cv_ms_fill (j_decompress_ptr cinfo)
217{
218 ERREXIT (cinfo, JERR_INPUT_EMPTY);
219
220 return TRUE;
221}
222
223static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes)
224{
225 if (num_bytes > 0)
226 {
227 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src;
228
229 src->next_input_byte += num_bytes;
230 src->bytes_in_buffer -= num_bytes;
231 }
232}
233
234static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen)
235{
236 struct jpeg_source_mgr *src;
237
238 if (!cinfo->src)
239 cinfo->src = (struct jpeg_source_mgr *)
240 (*cinfo->mem->alloc_small) (
241 (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr)
242 );
243
244 src = (struct jpeg_source_mgr *)cinfo->src;
245 src->init_source = cv_ms_init;
246 src->fill_input_buffer = cv_ms_fill;
247 src->skip_input_data = cv_ms_skip;
248 src->resync_to_restart = jpeg_resync_to_restart;
249 src->term_source = cv_ms_term;
250 src->next_input_byte = (JOCTET *)buf;
251 src->bytes_in_buffer = buflen;
252}
253
254/////////////////////////////////////////////////////////////////////////////
203 255
204MODULE = Gtk2::CV PACKAGE = Gtk2::CV 256MODULE = Gtk2::CV PACKAGE = Gtk2::CV
205 257
206PROTOTYPES: ENABLE 258PROTOTYPES: ENABLE
207 259
260# calculate the common prefix length of two strings
208# missing function in perl. really :) 261# missing function in perl. really :)
209int 262int
210common_prefix_length (a, b) 263common_prefix_length (a, b)
211 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg); 264 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
212 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg); 265 unsigned char *b = (unsigned char *)SvPVutf8_nolen ($arg);
232 CODE: 285 CODE:
233{ 286{
234 STRLEN len; 287 STRLEN len;
235 char *data = SvPVbyte (path_or_data, len); 288 char *data = SvPVbyte (path_or_data, len);
236 289
237 perlinterp_release ();
238
239 if (!magic_cookie[0]) 290 if (!magic_cookie[0])
240 { 291 {
241 magic_cookie[0] = magic_open (MAGIC_SYMLINK); 292 magic_cookie[0] = magic_open (MAGIC_SYMLINK);
242 magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME); 293 magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE);
294 magic_load (magic_cookie[0], 0);
295 magic_load (magic_cookie[1], 0);
243 } 296 }
297
298 perlinterp_release ();
244 299
245 RETVAL = ix & 2 300 RETVAL = ix & 2
246 ? magic_buffer (magic_cookie[ix], data, len) 301 ? magic_buffer (magic_cookie[ix & 1], data, len)
247 : magic_file (magic_cookie[ix], data); 302 : magic_file (magic_cookie[ix & 1], data);
248 303
249 perlinterp_acquire (); 304 perlinterp_acquire ();
250} 305}
251 OUTPUT: 306 OUTPUT:
252 RETVAL 307 RETVAL
308 : angle); 363 : angle);
309 perlinterp_acquire (); 364 perlinterp_acquire ();
310 OUTPUT: 365 OUTPUT:
311 RETVAL 366 RETVAL
312 367
368const char *
369filetype (SV *image_data)
370 CODE:
371{
372 STRLEN data_len;
373 U8 *data = SvPVbyte (image_data, data_len);
374
375 if (data_len >= 20
376 && data[0] == 0xff
377 && data[1] == 0xd8
378 && data[2] == 0xff)
379 RETVAL = "jpg";
380 else if (data_len >= 12
381 && data[ 0] == (U8)'R'
382 && data[ 1] == (U8)'I'
383 && data[ 2] == (U8)'F'
384 && data[ 3] == (U8)'F'
385 && data[ 8] == (U8)'W'
386 && data[ 9] == (U8)'E'
387 && data[10] == (U8)'B'
388 && data[11] == (U8)'P')
389 RETVAL = "webp";
390 else if (data_len >= 16
391 && data[ 0] == 0x89
392 && data[ 1] == (U8)'P'
393 && data[ 2] == (U8)'N'
394 && data[ 3] == (U8)'G'
395 && data[ 4] == 0x0d
396 && data[ 5] == 0x0a
397 && data[ 6] == 0x1a
398 && data[ 7] == 0x0a)
399 RETVAL = "png";
400 else
401 RETVAL = "other";
402}
403 OUTPUT:
404 RETVAL
405
313GdkPixbuf_noinc * 406GdkPixbuf_noinc *
314decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) 407decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
315 CODE: 408 CODE:
316{ 409{
317#if WEBP 410#if WEBP
318 guchar *data;
319 STRLEN data_size; 411 STRLEN data_size;
320 int alpha; 412 int alpha;
413 WebPData data;
414 WebPDemuxer *demux;
415 WebPIterator iter;
321 WebPDecoderConfig config; 416 WebPDecoderConfig config;
322 int inw, inh; 417 int inw, inh;
323 418
324 data = SvPVbyte (image_data, data_size); 419 data.bytes = (uint8_t *)SvPVbyte (image_data, data_size);
420 data.size = data_size;
325 421
326 perlinterp_release (); 422 perlinterp_release ();
327 423
328 RETVAL = 0; 424 RETVAL = 0;
329 425
426 if (!(demux = WebPDemux (&data)))
427 goto err_demux;
428
429 if (!WebPDemuxGetFrame (demux, 1, &iter))
430 goto err_iter;
431
330 if (!WebPInitDecoderConfig (&config)) 432 if (!WebPInitDecoderConfig (&config))
331 goto err; 433 goto err_iter;
332 434
333 config.options.use_threads = 1; 435 config.options.use_threads = 1;
334 436
335 if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK) 437 if (WebPGetFeatures (iter.fragment.bytes, iter.fragment.size, &config.input) != VP8_STATUS_OK)
336 goto err; 438 goto err_iter;
337 439
338 inw = config.input.width; 440 inw = config.input.width;
339 inh = config.input.height; 441 inh = config.input.height;
340 442
341 if (thumbnail) 443 if (thumbnail)
356 { 458 {
357 iw = inw; 459 iw = inw;
358 ih = inh; 460 ih = inh;
359 } 461 }
360 462
361 alpha = config.input.has_alpha; 463 alpha = !!config.input.has_alpha;
362 464
363 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih); 465 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, iw, ih);
364 if (!RETVAL) 466 if (!RETVAL)
365 goto err; 467 goto err_iter;
366 468
367 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB; 469 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB;
368 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL); 470 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL);
369 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL); 471 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL);
370 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL); 472 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL);
371 config.output.is_external_memory = 1; 473 config.output.is_external_memory = 1;
372 474
373 if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK) 475 if (WebPDecode (iter.fragment.bytes, iter.fragment.size, &config) != VP8_STATUS_OK)
374 { 476 {
375 g_object_unref (RETVAL); 477 g_object_unref (RETVAL);
376 RETVAL = 0; 478 RETVAL = 0;
377 goto err; 479 goto err_iter;
378 } 480 }
379 481
380 err: 482 err_iter:
483 WebPDemuxReleaseIterator (&iter);
484 err_demux:
485 WebPDemuxDelete (demux);
486
381 perlinterp_acquire (); 487 perlinterp_acquire ();
382#else 488#else
383 croak ("load_webp: webp not enabled at compile time"); 489 croak ("load_webp: webp not enabled at compile time");
384#endif 490#endif
385} 491}
386 OUTPUT: 492 OUTPUT:
387 RETVAL 493 RETVAL
388 494
389GdkPixbuf_noinc * 495GdkPixbuf_noinc *
390load_jpeg (SV *path, int thumbnail = 0, int iw = 0, int ih = 0) 496decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
391 CODE: 497 CODE:
392{ 498{
393 struct jpeg_decompress_struct cinfo; 499 struct jpeg_decompress_struct cinfo;
394 struct jpg_err_mgr jerr; 500 struct jpg_err_mgr jerr;
395 guchar *data;
396 int rs; 501 int rs;
397 FILE *fp;
398 volatile GdkPixbuf *pb = 0; 502 volatile GdkPixbuf *pb = 0;
503 STRLEN data_len;
504 guchar *data = SvPVbyte (image_data, data_len);
399 505
400 RETVAL = 0; 506 RETVAL = 0;
401
402 fp = fopen (SvPVbyte_nolen (path), "rb");
403
404 if (!fp)
405 XSRETURN_UNDEF;
406 507
407 perlinterp_release (); 508 perlinterp_release ();
408 509
409 cinfo.err = jpeg_std_error (&jerr.err); 510 cinfo.err = jpeg_std_error (&jerr.err);
410 511
411 jerr.err.error_exit = cv_error_exit; 512 jerr.err.error_exit = cv_error_exit;
412 jerr.err.output_message = cv_error_output; 513 jerr.err.output_message = cv_error_output;
413 514
414 if ((rs = setjmp (jerr.setjmp_buffer))) 515 if ((rs = setjmp (jerr.setjmp_buffer)))
415 { 516 {
416 fclose (fp);
417 jpeg_destroy_decompress (&cinfo); 517 jpeg_destroy_decompress (&cinfo);
418 518
419 if (pb) 519 if (pb)
420 g_object_unref ((gpointer)pb); 520 g_object_unref ((gpointer)pb);
421 521
422 perlinterp_acquire (); 522 perlinterp_acquire ();
423 XSRETURN_UNDEF; 523 XSRETURN_UNDEF;
424 } 524 }
425 525
426 jpeg_create_decompress (&cinfo); 526 jpeg_create_decompress (&cinfo);
527 cv_jpeg_mem_src (&cinfo, data, data_len);
427 528
428 jpeg_stdio_src (&cinfo, fp);
429 jpeg_read_header (&cinfo, TRUE); 529 jpeg_read_header (&cinfo, TRUE);
430 530
431 cinfo.dct_method = JDCT_DEFAULT; 531 cinfo.dct_method = JDCT_DEFAULT;
432 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ 532 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
433 cinfo.do_block_smoothing = FALSE; 533 cinfo.do_block_smoothing = FALSE;
512 data += 4; 612 data += 4;
513 } 613 }
514 } 614 }
515 615
516 jpeg_finish_decompress (&cinfo); 616 jpeg_finish_decompress (&cinfo);
517 fclose (fp);
518 jpeg_destroy_decompress (&cinfo); 617 jpeg_destroy_decompress (&cinfo);
519 perlinterp_acquire (); 618 perlinterp_acquire ();
520} 619}
521 OUTPUT: 620 OUTPUT:
522 RETVAL 621 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines