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

Comparing CV/CV.xs (file contents):
Revision 1.50 by root, Sat Dec 23 03:30:28 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
33#define LINELENGTH 240 36#define LINELENGTH 240
34 37
35#define ELLIPSIS "\xe2\x80\xa6" 38#define ELLIPSIS "\xe2\x80\xa6"
36 39
37typedef char *octet_string; 40typedef char *octet_string;
41
42static magic_t magic_cookie[2]; /* !mime, mime */
38 43
39struct jpg_err_mgr 44struct jpg_err_mgr
40{ 45{
41 struct jpeg_error_mgr err; 46 struct jpeg_error_mgr err;
42 jmp_buf setjmp_buffer; 47 jmp_buf setjmp_buffer;
197 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 202 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
198} 203}
199 204
200///////////////////////////////////////////////////////////////////////////// 205/////////////////////////////////////////////////////////////////////////////
201 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
202MODULE = Gtk2::CV PACKAGE = Gtk2::CV 255MODULE = Gtk2::CV PACKAGE = Gtk2::CV
203 256
204PROTOTYPES: ENABLE 257PROTOTYPES: ENABLE
205 258
206# missing function in perl. really :) 259# missing function in perl. really :)
219 272
220 OUTPUT: 273 OUTPUT:
221 RETVAL 274 RETVAL
222 275
223const char * 276const char *
224magic (octet_string path) 277magic (SV *path_or_data)
278 ALIAS:
279 magic = 0
280 magic_mime = 1
281 magic_buffer = 2
282 magic_buffer_mime = 3
225 CODE: 283 CODE:
226{ 284{
227 static magic_t cookie; 285 STRLEN len;
286 char *data = SvPVbyte (path_or_data, len);
228 287
229 if (!cookie) 288 if (!magic_cookie[0])
230 { 289 {
231 cookie = magic_open (MAGIC_SYMLINK); 290 magic_cookie[0] = magic_open (MAGIC_SYMLINK);
232 291 magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE);
233 if (cookie)
234 magic_load (cookie, 0); 292 magic_load (magic_cookie[0], 0);
235 else
236 XSRETURN_UNDEF;
237 }
238
239 RETVAL = magic_file (cookie, path);
240}
241 OUTPUT:
242 RETVAL
243
244const char *
245magic_mime (octet_string path)
246 CODE:
247{
248 static magic_t cookie;
249
250 if (!cookie)
251 {
252 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
253
254 if (cookie)
255 magic_load (cookie, 0); 293 magic_load (magic_cookie[1], 0);
256 else
257 XSRETURN_UNDEF;
258 } 294 }
259 295
260 perlinterp_release (); 296 perlinterp_release ();
261 RETVAL = magic_file (cookie, path); 297
298 RETVAL = ix & 2
299 ? magic_buffer (magic_cookie[ix & 1], data, len)
300 : magic_file (magic_cookie[ix & 1], data);
301
262 perlinterp_acquire (); 302 perlinterp_acquire ();
263} 303}
264 OUTPUT: 304 OUTPUT:
265 RETVAL 305 RETVAL
266 306
321 : angle); 361 : angle);
322 perlinterp_acquire (); 362 perlinterp_acquire ();
323 OUTPUT: 363 OUTPUT:
324 RETVAL 364 RETVAL
325 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
326GdkPixbuf_noinc * 404GdkPixbuf_noinc *
327load_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)
328 CODE: 406 CODE:
329{ 407{
330#if WEBP 408#if WEBP
331 STRLEN data_size; 409 STRLEN data_size;
332 guchar *data = SvPVbyte (image_data, data_size);
333 int alpha; 410 int alpha;
411 WebPData data;
412 WebPDemuxer *demux;
413 WebPIterator iter;
334 WebPDecoderConfig config; 414 WebPDecoderConfig config;
415 int inw, inh;
416
417 data.bytes = (uint8_t *)SvPVbyte (image_data, data_size);
418 data.size = data_size;
335 419
336 perlinterp_release (); 420 perlinterp_release ();
337 421
338 RETVAL = 0; 422 RETVAL = 0;
339 423
424 if (!(demux = WebPDemux (&data)))
425 goto err_demux;
426
427 if (!WebPDemuxGetFrame (demux, 1, &iter))
428 goto err_iter;
429
340 if (!WebPInitDecoderConfig (&config)) 430 if (!WebPInitDecoderConfig (&config))
341 goto err; 431 goto err_iter;
342 432
343 config.options.use_threads = 1; 433 config.options.use_threads = 1;
434
435 if (WebPGetFeatures (iter.fragment.bytes, iter.fragment.size, &config.input) != VP8_STATUS_OK)
436 goto err_iter;
437
438 inw = config.input.width;
439 inh = config.input.height;
344 440
345 if (thumbnail) 441 if (thumbnail)
346 { 442 {
443 if (inw * ih > inh * iw)
444 ih = (iw * inh + inw - 1) / inw;
445 else
446 iw = (ih * inw + inh - 1) / inh;
447
347 config.options.bypass_filtering = 1; 448 config.options.bypass_filtering = 1;
348 config.options.no_fancy_upsampling = 1; 449 config.options.no_fancy_upsampling = 1;
349 450
350 config.options.use_scaling = 1; 451 config.options.use_scaling = 1;
351 config.options.scaled_width = iw; 452 config.options.scaled_width = iw;
352 config.options.scaled_height = ih; 453 config.options.scaled_height = ih;
353 } 454 }
354 else 455 else
355 { 456 {
356 iw = config.input.width; 457 iw = inw;
357 ih = config.input.height; 458 ih = inh;
358 } 459 }
359 460
360 if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK)
361 goto err;
362
363 alpha = config.input.has_alpha; 461 alpha = !!config.input.has_alpha;
364 462
365 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih); 463 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, alpha, 8, iw, ih);
366 if (!RETVAL) 464 if (!RETVAL)
367 goto err; 465 goto err_iter;
368 466
369 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB; 467 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB;
370 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL); 468 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL);
371 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL); 469 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL);
372 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL); 470 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL);
373 config.output.is_external_memory = 1; 471 config.output.is_external_memory = 1;
374 472
375 if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK) 473 if (WebPDecode (iter.fragment.bytes, iter.fragment.size, &config) != VP8_STATUS_OK)
376 { 474 {
377 g_object_unref (RETVAL); 475 g_object_unref (RETVAL);
378 RETVAL = 0; 476 RETVAL = 0;
379 goto err; 477 goto err_iter;
380 } 478 }
381 479
382 err: 480 err_iter:
481 WebPDemuxReleaseIterator (&iter);
482 err_demux:
483 WebPDemuxDelete (demux);
484
383 perlinterp_acquire (); 485 perlinterp_acquire ();
384#else 486#else
385 croak ("load_webp: webp not enabled at compile time"); 487 croak ("load_webp: webp not enabled at compile time");
386#endif 488#endif
387} 489}
388 OUTPUT: 490 OUTPUT:
389 RETVAL 491 RETVAL
390 492
391GdkPixbuf_noinc * 493GdkPixbuf_noinc *
392load_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)
393 CODE: 495 CODE:
394{ 496{
395 struct jpeg_decompress_struct cinfo; 497 struct jpeg_decompress_struct cinfo;
396 struct jpg_err_mgr jerr; 498 struct jpg_err_mgr jerr;
397 guchar *data;
398 int rs; 499 int rs;
399 FILE *fp;
400 volatile GdkPixbuf *pb = 0; 500 volatile GdkPixbuf *pb = 0;
501 STRLEN data_len;
502 guchar *data = SvPVbyte (image_data, data_len);
401 503
402 RETVAL = 0; 504 RETVAL = 0;
403
404 fp = fopen (SvPVbyte_nolen (path), "rb");
405
406 if (!fp)
407 XSRETURN_UNDEF;
408 505
409 perlinterp_release (); 506 perlinterp_release ();
410 507
411 cinfo.err = jpeg_std_error (&jerr.err); 508 cinfo.err = jpeg_std_error (&jerr.err);
412 509
413 jerr.err.error_exit = cv_error_exit; 510 jerr.err.error_exit = cv_error_exit;
414 jerr.err.output_message = cv_error_output; 511 jerr.err.output_message = cv_error_output;
415 512
416 if ((rs = setjmp (jerr.setjmp_buffer))) 513 if ((rs = setjmp (jerr.setjmp_buffer)))
417 { 514 {
418 fclose (fp);
419 jpeg_destroy_decompress (&cinfo); 515 jpeg_destroy_decompress (&cinfo);
420 516
421 if (pb) 517 if (pb)
422 g_object_unref ((gpointer)pb); 518 g_object_unref ((gpointer)pb);
423 519
424 perlinterp_acquire (); 520 perlinterp_acquire ();
425 XSRETURN_UNDEF; 521 XSRETURN_UNDEF;
426 } 522 }
427 523
428 jpeg_create_decompress (&cinfo); 524 jpeg_create_decompress (&cinfo);
525 cv_jpeg_mem_src (&cinfo, data, data_len);
429 526
430 jpeg_stdio_src (&cinfo, fp);
431 jpeg_read_header (&cinfo, TRUE); 527 jpeg_read_header (&cinfo, TRUE);
432 528
433 cinfo.dct_method = JDCT_DEFAULT; 529 cinfo.dct_method = JDCT_DEFAULT;
434 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 */
435 cinfo.do_block_smoothing = FALSE; 531 cinfo.do_block_smoothing = FALSE;
514 data += 4; 610 data += 4;
515 } 611 }
516 } 612 }
517 613
518 jpeg_finish_decompress (&cinfo); 614 jpeg_finish_decompress (&cinfo);
519 fclose (fp);
520 jpeg_destroy_decompress (&cinfo); 615 jpeg_destroy_decompress (&cinfo);
521 perlinterp_acquire (); 616 perlinterp_acquire ();
522} 617}
523 OUTPUT: 618 OUTPUT:
524 RETVAL 619 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines