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

Comparing CV/CV.xs (file contents):
Revision 1.62 by root, Sun Jul 25 11:32:46 2021 UTC vs.
Revision 1.66 by root, Tue Mar 21 00:06:49 2023 UTC

303 303
304MODULE = Gtk2::CV PACKAGE = Gtk2::CV 304MODULE = Gtk2::CV PACKAGE = Gtk2::CV
305 305
306PROTOTYPES: ENABLE 306PROTOTYPES: ENABLE
307 307
308void
309_exit (int code)
310
308# calculate the common prefix length of two strings 311# calculate the common prefix length of two strings
309# missing function in perl. really :) 312# missing function in perl. really :)
310int 313int
311common_prefix_length (a, b) 314common_prefix_length (a, b)
312 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg); 315 unsigned char *a = (unsigned char *)SvPVutf8_nolen ($arg);
418 CODE: 421 CODE:
419{ 422{
420 STRLEN data_len; 423 STRLEN data_len;
421 U8 *data = SvPVbyte (image_data, data_len); 424 U8 *data = SvPVbyte (image_data, data_len);
422 static const unsigned char jxl_header[] = { 425 static const unsigned char jxl_header[] = {
423 0, 0, 0, 0x0c, 0x4a, 0x58, 0x4c, 0x20, 426 0, 0, 0, 0x0c, 0x4a, 0x58, 0x4c, 0x20, 0x0d, 0xa, 0x87, 0x0a
424 0x0d, 0xa, 0x87, 0x0a, 0, 0, 0, 0x14,
425 0x66, 0x74, 0x79, 0x70, 0x6a, 0x78, 0x6c, 0x20,
426 0, 0, 0, 0, 0x6a, 0x78, 0x6c, 0x20,
427 }; 427 };
428 428
429 if (data_len >= 20 429 if (data_len >= 20
430 && data[0] == 0xff 430 && data[0] == 0xff
431 && data[1] == 0xd8 431 && data[1] == 0xd8
450 && data[ 5] == 0x0a 450 && data[ 5] == 0x0a
451 && data[ 6] == 0x1a 451 && data[ 6] == 0x1a
452 && data[ 7] == 0x0a) 452 && data[ 7] == 0x0a)
453 RETVAL = "image/png"; 453 RETVAL = "image/png";
454 else if (data_len >= sizeof (jxl_header) && memcmp (data, jxl_header, sizeof (jxl_header)) == 0) 454 else if (data_len >= sizeof (jxl_header) && memcmp (data, jxl_header, sizeof (jxl_header)) == 0)
455 RETVAL = "image/jxl"; // todo: might want to use JxlSignatureCheck
456 else if (data_len >= 2
457 && data[0] == 0xff
458 && data[1] == 0x0a)
455 RETVAL = "image/jxl"; 459 RETVAL = "image/jxl";
456 else if (data_len >= 13 460 else if (data_len >= 13
457 && data[0] == 'G' 461 && data[0] == 'G'
458 && data[1] == 'I' 462 && data[1] == 'I'
459 && data[2] == 'F' 463 && data[2] == 'F'
582GdkPixbuf_noinc * 586GdkPixbuf_noinc *
583decode_jxl (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) 587decode_jxl (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0)
584 CODE: 588 CODE:
585{ 589{
586#if JXL 590#if JXL
587 JxlDecoder *dec = JxlDecoderCreate (0); 591 JxlDecoder *dec;
588 JxlBasicInfo info; 592 JxlBasicInfo info;
589 const uint8_t *next_in = (uint8_t *)SvPVbyte_nolen (image_data); 593 const uint8_t *next_in = (uint8_t *)SvPVbyte_nolen (image_data);
590 size_t avail_in = SvCUR (image_data); 594 size_t avail_in = SvCUR (image_data);
591 const char *error = 0; 595 const char *error = 0;
596 JxlDecoderStatus status;
597 static void *runner_cache;
592 void *runner = 0; 598 void *runner = 0;
593 struct bmff_box box; 599
600 RETVAL = 0;
601
602 if (runner_cache)
603 runner = runner_cache;
604 else
605 runner = JxlThreadParallelRunnerCreate (0, JxlThreadParallelRunnerDefaultNumWorkerThreads ());
606
607 runner_cache = 0;
594 608
595 perlinterp_release (); 609 perlinterp_release ();
596 610
597 RETVAL = 0; 611 dec = JxlDecoderCreate (0);
598 612
599 error = "JxlDecoderCreate failed"; 613 error = "JxlDecoderCreate failed";
600 if (!dec) 614 if (!dec)
601 goto done; 615 goto done;
602 616
603 runner = JxlThreadParallelRunnerCreate (0, JxlThreadParallelRunnerDefaultNumWorkerThreads ()); 617 status = JxlDecoderSetParallelRunner (dec, JxlThreadParallelRunner, runner);
604
605 error = "JxlDecoderSetParallelRunner failed"; 618 error = "JxlDecoderSetParallelRunner failed";
606 if (JxlDecoderSetParallelRunner (dec, JxlThreadParallelRunner, runner) != JXL_DEC_SUCCESS) 619 if (status != JXL_DEC_SUCCESS)
607 goto done; 620 goto done;
608 621
609 error = "JxlDecoderSubscribeEvents failed"; 622 error = "JxlDecoderSubscribeEvents failed";
610 if (JxlDecoderSubscribeEvents (dec, JXL_DEC_FULL_IMAGE | JXL_DEC_BASIC_INFO) != JXL_DEC_SUCCESS) 623 status = JxlDecoderSubscribeEvents (dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE);
624 if (status != JXL_DEC_SUCCESS)
611 goto done; 625 goto done;
612 626
627 status = JxlDecoderSetInput (dec, next_in, avail_in);
613 error = "JxlDecoderSetInput failed"; 628 error = "JxlDecoderSetInput failed";
614 if (JxlDecoderSetInput (dec, next_in, avail_in) != JXL_DEC_SUCCESS) 629 if (status != JXL_DEC_SUCCESS)
615 goto done; 630 goto done;
616 631
617 for (;;) 632 for (;;)
618 { 633 {
619 JxlDecoderStatus status = JxlDecoderProcessInput (dec); 634 status = JxlDecoderProcessInput (dec);
620 635
621 printf ("status %d\n",status);
622
623 switch (status) 636 switch (status)
624 { 637 {
638 case JXL_DEC_FULL_IMAGE:
639 error = 0;
640 goto done;
641
625 case JXL_DEC_ERROR: 642 case JXL_DEC_ERROR:
626 error = "JxlDecoderProcessInput failed"; 643 error = "JxlDecoderProcessInput failed";
627 goto done; 644 goto done;
628 645
629 case JXL_DEC_NEED_MORE_INPUT: 646 case JXL_DEC_NEED_MORE_INPUT:
630 error = "incomplete file"; 647 error = "incomplete file";
631 goto done; 648 goto done;
632 649
633 case JXL_DEC_SUCCESS: 650 case JXL_DEC_SUCCESS:
651 error = "incomplete decode";
634 goto done; 652 goto done;
635 653
636 case JXL_DEC_NEED_IMAGE_OUT_BUFFER: 654 case JXL_DEC_BASIC_INFO:
637 { 655 {
656 status = JxlDecoderGetBasicInfo (dec, &info);
638 error = "JxlDecoderGetBasicInfo failed"; 657 error = "JxlDecoderGetBasicInfo failed";
639 if (JxlDecoderGetBasicInfo (dec, &info) != JXL_DEC_SUCCESS) 658 if (status != JXL_DEC_SUCCESS)
640 goto done; 659 goto done;
641 660
642 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!info.alpha_bits, 8, info.xsize, info.ysize); 661 RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!info.alpha_bits, 8, info.xsize, info.ysize);
643 error = "unable to allocate pixbuf"; 662 error = "unable to allocate pixbuf";
644 if (!RETVAL) 663 if (!RETVAL)
649 JXL_TYPE_UINT8, 668 JXL_TYPE_UINT8,
650 JXL_NATIVE_ENDIAN, 669 JXL_NATIVE_ENDIAN,
651 gdk_pixbuf_get_rowstride (RETVAL) 670 gdk_pixbuf_get_rowstride (RETVAL)
652 }; 671 };
653 672
673 // cannot use gdk_pixbuf_get_byte_length because that does
674 // not return the size of the buffer, but the size of the buffer without
675 // the last padding bytes. the internal buffer is rowstride * ysize,
676 // and this is what the jxl decoder needs. none of this is documented
677 // in either library, of course.
678
679 status = JxlDecoderSetImageOutBuffer (
680 dec,
681 &format,
682 gdk_pixbuf_get_pixels (RETVAL),
683 gdk_pixbuf_get_rowstride (RETVAL) * info.ysize
684 );
654 error = "JxlDecoderSetImageOutBuffer failed"; 685 error = "JxlDecoderSetImageOutBuffer failed";
655 if (JxlDecoderSetImageOutBuffer (
656 dec,
657 &format,
658 gdk_pixbuf_get_pixels (RETVAL),
659 gdk_pixbuf_get_byte_length (RETVAL)
660 ) != JXL_DEC_SUCCESS) 686 if (status != JXL_DEC_SUCCESS)
661 goto done; 687 goto done;
662 } 688 }
663 break; 689 break;
664 690
665 default: 691 default:
670 696
671 done: 697 done:
672 if (dec) 698 if (dec)
673 JxlDecoderDestroy (dec); 699 JxlDecoderDestroy (dec);
674 700
701 perlinterp_acquire ();
702
675 if (runner) 703 if (runner_cache)
676 JxlThreadParallelRunnerDestroy (runner); 704 JxlThreadParallelRunnerDestroy (runner);
677 705
678 perlinterp_acquire (); 706 runner_cache = runner;
679 707
680 if (error) 708 if (error)
681 { 709 {
682 if (RETVAL) 710 if (RETVAL)
683 g_object_unref (RETVAL); 711 g_object_unref (RETVAL);
684 712
685 croak ("load_jxl: %s", error); 713 croak ("load_jxl: %s (status %d)", error, status);
686 } 714 }
687#else 715#else
688 croak ("load_jxl: jpeg-xl not enabled at compile time"); 716 croak ("load_jxl: jpeg-xl not enabled at compile time");
689#endif 717#endif
690} 718}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines