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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines