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

Comparing CV/CV.xs (file contents):
Revision 1.46 by root, Sat Jul 4 05:31:17 2015 UTC vs.
Revision 1.53 by root, Sat Dec 23 05:27:04 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>
16#include <gdk/gdkx.h>
14#include <gdk-pixbuf/gdk-pixbuf.h> 17#include <gdk-pixbuf/gdk-pixbuf.h>
15 18
16#include <gperl.h> 19#include <gperl.h>
17#include <gtk2perl.h> 20#include <gtk2perl.h>
18 21
19#include <assert.h> 22#include <assert.h>
20 23
24#if WEBP
25#include <webp/decode.h>
26#endif
27
21#include "perlmulticore.h" 28#include "perlmulticore.h"
22 29
23#define IW 80 /* MUST match Schnauzer.pm! */ 30#define IW 80 /* MUST match Schnauzer.pm! */
24#define IH 60 /* MUST match Schnauzer.pm! */ 31#define IH 60 /* MUST match Schnauzer.pm! */
25 32
28#define LINELENGTH 240 35#define LINELENGTH 240
29 36
30#define ELLIPSIS "\xe2\x80\xa6" 37#define ELLIPSIS "\xe2\x80\xa6"
31 38
32typedef char *octet_string; 39typedef char *octet_string;
40
41static magic_t magic_cookie[2]; /* !mime, mime */
33 42
34struct jpg_err_mgr 43struct jpg_err_mgr
35{ 44{
36 struct jpeg_error_mgr err; 45 struct jpeg_error_mgr err;
37 jmp_buf setjmp_buffer; 46 jmp_buf setjmp_buffer;
38}; 47};
39 48
40static void 49static void
41cv_error_exit (j_common_ptr cinfo) 50cv_error_exit (j_common_ptr cinfo)
42{ 51{
52 cinfo->err->output_message (cinfo);
43 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99); 53 longjmp (((struct jpg_err_mgr *)cinfo->err)->setjmp_buffer, 99);
44} 54}
45 55
46static void 56static void
47cv_error_output (j_common_ptr cinfo) 57cv_error_output (j_common_ptr cinfo)
48{ 58{
59 char msg[JMSG_LENGTH_MAX];
60
61 cinfo->err->format_message (cinfo, msg);
62
63 fprintf (stderr, "JPEG decoding error: %s\n", msg);
49 return; 64 return;
50} 65}
51 66
52static void 67static void
53rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b, 68rgb_to_hsv (unsigned int r, unsigned int g, unsigned int b,
150 if (!--a85_cnt) 165 if (!--a85_cnt)
151 { 166 {
152 a85_cnt = 4; 167 a85_cnt = 4;
153 if (a85_val) 168 if (a85_val)
154 { 169 {
155 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85; 170 a85_ptr[4] = (a85_val % 85) + 33; a85_val /= 85;
156 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85; 171 a85_ptr[3] = (a85_val % 85) + 33; a85_val /= 85;
157 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85; 172 a85_ptr[2] = (a85_val % 85) + 33; a85_val /= 85;
158 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85; 173 a85_ptr[1] = (a85_val % 85) + 33; a85_val /= 85;
159 a85_ptr[0] = (a85_val ) + 33; 174 a85_ptr[0] = (a85_val ) + 33;
160 175
161 a85_ptr += 5; 176 a85_ptr += 5;
162 } 177 }
186 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf); 201 PerlIO_write (fp, a85_buf, a85_ptr - a85_buf);
187} 202}
188 203
189///////////////////////////////////////////////////////////////////////////// 204/////////////////////////////////////////////////////////////////////////////
190 205
206static void cv_ms_init (j_decompress_ptr cinfo)
207{
208}
209
210static void cv_ms_term (j_decompress_ptr cinfo)
211{
212}
213
214static boolean cv_ms_fill (j_decompress_ptr cinfo)
215{
216 ERREXIT (cinfo, JERR_INPUT_EMPTY);
217
218 return TRUE;
219}
220
221static void cv_ms_skip (j_decompress_ptr cinfo, long num_bytes)
222{
223 if (num_bytes > 0)
224 {
225 struct jpeg_source_mgr *src = (struct jpeg_source_mgr *)cinfo->src;
226
227 src->next_input_byte += num_bytes;
228 src->bytes_in_buffer -= num_bytes;
229 }
230}
231
232static void cv_jpeg_mem_src (j_decompress_ptr cinfo, void *buf, size_t buflen)
233{
234 struct jpeg_source_mgr *src;
235
236 if (!cinfo->src)
237 cinfo->src = (struct jpeg_source_mgr *)
238 (*cinfo->mem->alloc_small) (
239 (j_common_ptr) cinfo, JPOOL_PERMANENT, sizeof (struct jpeg_source_mgr)
240 );
241
242 src = (struct jpeg_source_mgr *)cinfo->src;
243 src->init_source = cv_ms_init;
244 src->fill_input_buffer = cv_ms_fill;
245 src->skip_input_data = cv_ms_skip;
246 src->resync_to_restart = jpeg_resync_to_restart;
247 src->term_source = cv_ms_term;
248 src->next_input_byte = (JOCTET *)buf;
249 src->bytes_in_buffer = buflen;
250}
251
252/////////////////////////////////////////////////////////////////////////////
253
191MODULE = Gtk2::CV PACKAGE = Gtk2::CV 254MODULE = Gtk2::CV PACKAGE = Gtk2::CV
192 255
193PROTOTYPES: ENABLE 256PROTOTYPES: ENABLE
194 257
195# missing function in perl. really :) 258# missing function in perl. really :)
208 271
209 OUTPUT: 272 OUTPUT:
210 RETVAL 273 RETVAL
211 274
212const char * 275const char *
213magic (octet_string path) 276magic (SV *path_or_data)
277 ALIAS:
278 magic = 0
279 magic_mime = 1
280 magic_buffer = 2
281 magic_buffer_mime = 3
214 CODE: 282 CODE:
215{ 283{
216 static magic_t cookie; 284 STRLEN len;
285 char *data = SvPVbyte (path_or_data, len);
217 286
218 if (!cookie) 287 if (!magic_cookie[0])
219 { 288 {
220 cookie = magic_open (MAGIC_SYMLINK); 289 magic_cookie[0] = magic_open (MAGIC_SYMLINK);
221 290 magic_cookie[1] = magic_open (MAGIC_SYMLINK | MAGIC_MIME_TYPE);
222 if (cookie)
223 magic_load (cookie, 0); 291 magic_load (magic_cookie[0], 0);
224 else
225 XSRETURN_UNDEF;
226 }
227
228 RETVAL = magic_file (cookie, path);
229}
230 OUTPUT:
231 RETVAL
232
233const char *
234magic_mime (octet_string path)
235 CODE:
236{
237 static magic_t cookie;
238
239 if (!cookie)
240 {
241 cookie = magic_open (MAGIC_MIME | MAGIC_SYMLINK);
242
243 if (cookie)
244 magic_load (cookie, 0); 292 magic_load (magic_cookie[1], 0);
245 else
246 XSRETURN_UNDEF;
247 } 293 }
248 294
249 perlinterp_release (); 295 perlinterp_release ();
250 RETVAL = magic_file (cookie, path); 296
297 RETVAL = ix & 2
298 ? magic_buffer (magic_cookie[ix & 1], data, len)
299 : magic_file (magic_cookie[ix & 1], data);
300
251 perlinterp_acquire (); 301 perlinterp_acquire ();
252} 302}
253 OUTPUT: 303 OUTPUT:
254 RETVAL 304 RETVAL
255 305
310 : angle); 360 : angle);
311 perlinterp_acquire (); 361 perlinterp_acquire ();
312 OUTPUT: 362 OUTPUT:
313 RETVAL 363 RETVAL
314 364
365const char *
366filetype (SV *image_data)
367 CODE:
368{
369 STRLEN data_len;
370 U8 *data = SvPVbyte (image_data, data_len);
371
372 if (data_len >= 20
373 && data[0] == 0xff
374 && data[1] == 0xd8
375 && data[2] == 0xff)
376 RETVAL = "jpg";
377 else if (data_len >= 12
378 && data[ 0] == (U8)'R'
379 && data[ 1] == (U8)'I'
380 && data[ 2] == (U8)'F'
381 && data[ 3] == (U8)'F'
382 && data[ 8] == (U8)'W'
383 && data[ 9] == (U8)'E'
384 && data[10] == (U8)'B'
385 && data[11] == (U8)'P')
386 RETVAL = "webp";
387 else if (data_len >= 16
388 && data[ 0] == 0x89
389 && data[ 1] == (U8)'P'
390 && data[ 2] == (U8)'N'
391 && data[ 3] == (U8)'G'
392 && data[ 4] == 0x0d
393 && data[ 5] == 0x0a
394 && data[ 6] == 0x1a
395 && data[ 7] == 0x0a)
396 RETVAL = "png";
397 else
398 RETVAL = "other";
399}
400 OUTPUT:
401 RETVAL
402
315GdkPixbuf_noinc * 403GdkPixbuf_noinc *
316load_jpeg (SV *path, int thumbnail=0) 404decode_webp (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
405 CODE:
406{
407#if WEBP
408 guchar *data;
409 STRLEN data_size;
410 int alpha;
411 WebPDecoderConfig config;
412 int inw, inh;
413
414 data = SvPVbyte (image_data, data_size);
415
416 perlinterp_release ();
417
418 RETVAL = 0;
419
420 if (!WebPInitDecoderConfig (&config))
421 goto err;
422
423 config.options.use_threads = 1;
424
425 if (WebPGetFeatures (data, data_size, &config.input) != VP8_STATUS_OK)
426 goto err;
427
428 inw = config.input.width;
429 inh = config.input.height;
430
431 if (thumbnail)
432 {
433 if (inw * ih > inh * iw)
434 ih = (iw * inh + inw - 1) / inw;
435 else
436 iw = (ih * inw + inh - 1) / inh;
437
438 config.options.bypass_filtering = 1;
439 config.options.no_fancy_upsampling = 1;
440
441 config.options.use_scaling = 1;
442 config.options.scaled_width = iw;
443 config.options.scaled_height = ih;
444 }
445 else
446 {
447 iw = inw;
448 ih = inh;
449 }
450
451 alpha = config.input.has_alpha;
452
453 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!alpha, 8, iw, ih);
454 if (!RETVAL)
455 goto err;
456
457 config.output.colorspace = alpha ? MODE_RGBA : MODE_RGB;
458 config.output.u.RGBA.rgba = gdk_pixbuf_get_pixels (RETVAL);
459 config.output.u.RGBA.stride = gdk_pixbuf_get_rowstride (RETVAL);
460 config.output.u.RGBA.size = gdk_pixbuf_get_byte_length (RETVAL);
461 config.output.is_external_memory = 1;
462
463 if (WebPDecode (data, data_size, &config) != VP8_STATUS_OK)
464 {
465 g_object_unref (RETVAL);
466 RETVAL = 0;
467 goto err;
468 }
469
470 err:
471 perlinterp_acquire ();
472#else
473 croak ("load_webp: webp not enabled at compile time");
474#endif
475}
476 OUTPUT:
477 RETVAL
478
479GdkPixbuf_noinc *
480decode_jpeg (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
317 CODE: 481 CODE:
318{ 482{
319 struct jpeg_decompress_struct cinfo; 483 struct jpeg_decompress_struct cinfo;
320 struct jpg_err_mgr jerr; 484 struct jpg_err_mgr jerr;
321 guchar *data;
322 int rs; 485 int rs;
323 FILE *fp;
324 volatile GdkPixbuf *pb = 0; 486 volatile GdkPixbuf *pb = 0;
487 STRLEN data_len;
488 guchar *data = SvPVbyte (image_data, data_len);
325 489
326 RETVAL = 0; 490 RETVAL = 0;
327
328 fp = fopen (SvPVbyte_nolen (path), "rb");
329
330 if (!fp)
331 XSRETURN_UNDEF;
332 491
333 perlinterp_release (); 492 perlinterp_release ();
334 493
335 cinfo.err = jpeg_std_error (&jerr.err); 494 cinfo.err = jpeg_std_error (&jerr.err);
336 495
337 jerr.err.error_exit = cv_error_exit; 496 jerr.err.error_exit = cv_error_exit;
338 jerr.err.output_message = cv_error_output; 497 jerr.err.output_message = cv_error_output;
339 498
340 if ((rs = setjmp (jerr.setjmp_buffer))) 499 if ((rs = setjmp (jerr.setjmp_buffer)))
341 { 500 {
342 fclose (fp);
343 jpeg_destroy_decompress (&cinfo); 501 jpeg_destroy_decompress (&cinfo);
344 502
345 if (pb) 503 if (pb)
346 g_object_unref ((gpointer)pb); 504 g_object_unref ((gpointer)pb);
347 505
348 perlinterp_acquire (); 506 perlinterp_acquire ();
349 XSRETURN_UNDEF; 507 XSRETURN_UNDEF;
350 } 508 }
351 509
352 jpeg_create_decompress (&cinfo); 510 jpeg_create_decompress (&cinfo);
511 cv_jpeg_mem_src (&cinfo, data, data_len);
353 512
354 jpeg_stdio_src (&cinfo, fp);
355 jpeg_read_header (&cinfo, TRUE); 513 jpeg_read_header (&cinfo, TRUE);
356 514
357 cinfo.dct_method = JDCT_DEFAULT; 515 cinfo.dct_method = JDCT_DEFAULT;
358 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */ 516 cinfo.do_fancy_upsampling = FALSE; /* worse quality, but nobody compained so far, and gdk-pixbuf does the same */
359 cinfo.do_block_smoothing = FALSE; 517 cinfo.do_block_smoothing = FALSE;
369 { 527 {
370 cinfo.dct_method = JDCT_FASTEST; 528 cinfo.dct_method = JDCT_FASTEST;
371 cinfo.do_fancy_upsampling = FALSE; 529 cinfo.do_fancy_upsampling = FALSE;
372 530
373 while (cinfo.scale_denom < 8 531 while (cinfo.scale_denom < 8
374 && cinfo.output_width >= IW*4 532 && cinfo.output_width >= iw*4
375 && cinfo.output_height >= IH*4) 533 && cinfo.output_height >= ih*4)
376 { 534 {
377 cinfo.scale_denom <<= 1; 535 cinfo.scale_denom <<= 1;
378 jpeg_calc_output_dimensions (&cinfo); 536 jpeg_calc_output_dimensions (&cinfo);
379 } 537 }
380 } 538 }
381 539
540 if (cinfo.output_components != 3)
541 longjmp (jerr.setjmp_buffer, 3);
542
543 if (cinfo.jpeg_color_space == JCS_YCCK || cinfo.jpeg_color_space == JCS_CMYK)
544 {
545 cinfo.out_color_space = JCS_CMYK;
546 cinfo.output_components = 4;
547 }
548
382 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, 0, 8, cinfo.output_width, cinfo.output_height); 549 pb = RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, cinfo.output_components == 4, 8, cinfo.output_width, cinfo.output_height);
383 if (!RETVAL) 550 if (!RETVAL)
384 longjmp (jerr.setjmp_buffer, 2); 551 longjmp (jerr.setjmp_buffer, 2);
385 552
386 data = gdk_pixbuf_get_pixels (RETVAL); 553 data = gdk_pixbuf_get_pixels (RETVAL);
387 rs = gdk_pixbuf_get_rowstride (RETVAL); 554 rs = gdk_pixbuf_get_rowstride (RETVAL);
388
389 if (cinfo.output_components != 3)
390 longjmp (jerr.setjmp_buffer, 3);
391 555
392 jpeg_start_decompress (&cinfo); 556 jpeg_start_decompress (&cinfo);
393 557
394 while (cinfo.output_scanline < cinfo.output_height) 558 while (cinfo.output_scanline < cinfo.output_height)
395 { 559 {
402 rp [3] = (guchar *)rp [2] + rs; 566 rp [3] = (guchar *)rp [2] + rs;
403 567
404 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4); 568 jpeg_read_scanlines (&cinfo, rp, remaining < 4 ? remaining : 4);
405 } 569 }
406 570
571 if (cinfo.out_color_space == JCS_CMYK)
572 {
573 guchar *end = data + cinfo.output_height * rs;
574
575 while (data < end)
576 {
577 U32 c = data [0];
578 U32 m = data [1];
579 U32 y = data [2];
580 U32 k = data [3];
581
582 if (0)
583 if (cinfo.Adobe_transform == 2)
584 {
585 c ^= 0xff;
586 m ^= 0xff;
587 y ^= 0xff;
588 k ^= 0xff;
589 }
590
591 data [0] = (c * k + 0x80) / 0xff;
592 data [1] = (m * k + 0x80) / 0xff;
593 data [2] = (y * k + 0x80) / 0xff;
594 data [3] = 0xff;
595
596 data += 4;
597 }
598 }
599
407 jpeg_finish_decompress (&cinfo); 600 jpeg_finish_decompress (&cinfo);
408 fclose (fp);
409 jpeg_destroy_decompress (&cinfo); 601 jpeg_destroy_decompress (&cinfo);
410 perlinterp_acquire (); 602 perlinterp_acquire ();
411} 603}
412 OUTPUT: 604 OUTPUT:
413 RETVAL 605 RETVAL

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines