… | |
… | |
303 | |
303 | |
304 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
304 | MODULE = Gtk2::CV PACKAGE = Gtk2::CV |
305 | |
305 | |
306 | PROTOTYPES: ENABLE |
306 | PROTOTYPES: ENABLE |
307 | |
307 | |
|
|
308 | void |
|
|
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 :) |
310 | int |
313 | int |
311 | common_prefix_length (a, b) |
314 | common_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"; |
|
|
460 | else if (data_len >= 13 |
|
|
461 | && data[0] == 'G' |
|
|
462 | && data[1] == 'I' |
|
|
463 | && data[2] == 'F' |
|
|
464 | && data[3] == '8' |
|
|
465 | //&& (data[4] == '7' || data[4] == '9') |
|
|
466 | && data[5] == 'a') |
|
|
467 | { |
|
|
468 | RETVAL = "image/gif"; |
|
|
469 | |
|
|
470 | // now see if its animated - we require the netscape application header for this |
|
|
471 | int ofs = 13; |
|
|
472 | |
|
|
473 | if (data[10] & 0x80) |
|
|
474 | ofs += (1 << ((data[10] & 7) + 1)) * 3; |
|
|
475 | |
|
|
476 | if (data_len >= ofs + 2 + 1 + 11) |
|
|
477 | { |
|
|
478 | |
|
|
479 | // skip a graphic control extension block. we assume |
|
|
480 | // there is at most one such block - while the NAB |
|
|
481 | // has to come firstz, some files do not obey this |
|
|
482 | if (data[ofs] == 0x21 && data[ofs + 1] == 0xf9) |
|
|
483 | ofs += 3 + data[ofs + 2] + 1; |
|
|
484 | |
|
|
485 | if (data_len >= ofs + 2 + 1 + 11) |
|
|
486 | if (!memcmp (data + ofs, "\x21\xff\x0bNETSCAPE2.0", sizeof ("\x21\xff\x0bNETSCAPE2.0") - 1)) |
|
|
487 | RETVAL = "video/gif"; |
|
|
488 | } |
|
|
489 | } |
|
|
490 | else if (data_len >= 0x8000 + 6 |
|
|
491 | && data[0x8000+0] == 0 |
|
|
492 | && data[0x8000+1] == (U8)'B' |
|
|
493 | && data[0x8000+2] == (U8)'E' |
|
|
494 | && data[0x8000+3] == (U8)'A' |
|
|
495 | && data[0x8000+4] == (U8)'0' |
|
|
496 | && data[0x8000+5] == (U8)'1') |
|
|
497 | RETVAL = "video/iso-bluray"; |
|
|
498 | |
456 | else |
499 | else |
457 | XSRETURN_UNDEF; |
500 | XSRETURN_UNDEF; |
458 | } |
501 | } |
459 | OUTPUT: |
502 | OUTPUT: |
460 | RETVAL |
503 | RETVAL |
… | |
… | |
551 | GdkPixbuf_noinc * |
594 | GdkPixbuf_noinc * |
552 | decode_jxl (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
595 | decode_jxl (SV *image_data, int thumbnail = 0, int iw = 0, int ih = 0) |
553 | CODE: |
596 | CODE: |
554 | { |
597 | { |
555 | #if JXL |
598 | #if JXL |
556 | JxlDecoder *dec = JxlDecoderCreate (0); |
599 | JxlDecoder *dec; |
557 | JxlBasicInfo info; |
600 | JxlBasicInfo info; |
558 | const uint8_t *next_in = (uint8_t *)SvPVbyte_nolen (image_data); |
601 | const uint8_t *next_in = (uint8_t *)SvPVbyte_nolen (image_data); |
559 | size_t avail_in = SvCUR (image_data); |
602 | size_t avail_in = SvCUR (image_data); |
560 | const char *error = 0; |
603 | const char *error = 0; |
|
|
604 | JxlDecoderStatus status; |
|
|
605 | static void *runner_cache; |
561 | void *runner = 0; |
606 | void *runner = 0; |
562 | struct bmff_box box; |
607 | |
|
|
608 | RETVAL = 0; |
|
|
609 | |
|
|
610 | if (runner_cache) |
|
|
611 | runner = runner_cache; |
|
|
612 | else |
|
|
613 | runner = JxlThreadParallelRunnerCreate (0, JxlThreadParallelRunnerDefaultNumWorkerThreads ()); |
|
|
614 | |
|
|
615 | runner_cache = 0; |
563 | |
616 | |
564 | perlinterp_release (); |
617 | perlinterp_release (); |
565 | |
618 | |
566 | RETVAL = 0; |
619 | dec = JxlDecoderCreate (0); |
567 | |
620 | |
568 | error = "JxlDecoderCreate failed"; |
621 | error = "JxlDecoderCreate failed"; |
569 | if (!dec) |
622 | if (!dec) |
570 | goto done; |
623 | goto done; |
571 | |
624 | |
572 | runner = JxlThreadParallelRunnerCreate (0, JxlThreadParallelRunnerDefaultNumWorkerThreads ()); |
625 | status = JxlDecoderSetParallelRunner (dec, JxlThreadParallelRunner, runner); |
573 | |
|
|
574 | error = "JxlDecoderSetParallelRunner failed"; |
626 | error = "JxlDecoderSetParallelRunner failed"; |
575 | if (JxlDecoderSetParallelRunner (dec, JxlThreadParallelRunner, runner) != JXL_DEC_SUCCESS) |
627 | if (status != JXL_DEC_SUCCESS) |
576 | goto done; |
628 | goto done; |
577 | |
629 | |
578 | error = "JxlDecoderSubscribeEvents failed"; |
630 | error = "JxlDecoderSubscribeEvents failed"; |
579 | if (JxlDecoderSubscribeEvents (dec, JXL_DEC_FULL_IMAGE | JXL_DEC_BASIC_INFO) != JXL_DEC_SUCCESS) |
631 | status = JxlDecoderSubscribeEvents (dec, JXL_DEC_BASIC_INFO | JXL_DEC_FULL_IMAGE); |
|
|
632 | if (status != JXL_DEC_SUCCESS) |
580 | goto done; |
633 | goto done; |
581 | |
634 | |
|
|
635 | status = JxlDecoderSetInput (dec, next_in, avail_in); |
582 | error = "JxlDecoderSetInput failed"; |
636 | error = "JxlDecoderSetInput failed"; |
583 | if (JxlDecoderSetInput (dec, next_in, avail_in) != JXL_DEC_SUCCESS) |
637 | if (status != JXL_DEC_SUCCESS) |
584 | goto done; |
638 | goto done; |
585 | |
639 | |
586 | for (;;) |
640 | for (;;) |
587 | { |
641 | { |
588 | JxlDecoderStatus status = JxlDecoderProcessInput (dec); |
642 | status = JxlDecoderProcessInput (dec); |
589 | |
643 | |
590 | printf ("status %d\n",status); |
|
|
591 | |
|
|
592 | switch (status) |
644 | switch (status) |
593 | { |
645 | { |
|
|
646 | case JXL_DEC_FULL_IMAGE: |
|
|
647 | error = 0; |
|
|
648 | goto done; |
|
|
649 | |
594 | case JXL_DEC_ERROR: |
650 | case JXL_DEC_ERROR: |
595 | error = "JxlDecoderProcessInput failed"; |
651 | error = "JxlDecoderProcessInput failed"; |
596 | goto done; |
652 | goto done; |
597 | |
653 | |
598 | case JXL_DEC_NEED_MORE_INPUT: |
654 | case JXL_DEC_NEED_MORE_INPUT: |
599 | error = "incomplete file"; |
655 | error = "incomplete file"; |
600 | goto done; |
656 | goto done; |
601 | |
657 | |
602 | case JXL_DEC_SUCCESS: |
658 | case JXL_DEC_SUCCESS: |
|
|
659 | error = "incomplete decode"; |
603 | goto done; |
660 | goto done; |
604 | |
661 | |
605 | case JXL_DEC_NEED_IMAGE_OUT_BUFFER: |
662 | case JXL_DEC_BASIC_INFO: |
606 | { |
663 | { |
|
|
664 | status = JxlDecoderGetBasicInfo (dec, &info); |
607 | error = "JxlDecoderGetBasicInfo failed"; |
665 | error = "JxlDecoderGetBasicInfo failed"; |
608 | if (JxlDecoderGetBasicInfo (dec, &info) != JXL_DEC_SUCCESS) |
666 | if (status != JXL_DEC_SUCCESS) |
609 | goto done; |
667 | goto done; |
610 | |
668 | |
611 | RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!info.alpha_bits, 8, info.xsize, info.ysize); |
669 | RETVAL = gdk_pixbuf_new (GDK_COLORSPACE_RGB, !!info.alpha_bits, 8, info.xsize, info.ysize); |
612 | error = "unable to allocate pixbuf"; |
670 | error = "unable to allocate pixbuf"; |
613 | if (!RETVAL) |
671 | if (!RETVAL) |
… | |
… | |
618 | JXL_TYPE_UINT8, |
676 | JXL_TYPE_UINT8, |
619 | JXL_NATIVE_ENDIAN, |
677 | JXL_NATIVE_ENDIAN, |
620 | gdk_pixbuf_get_rowstride (RETVAL) |
678 | gdk_pixbuf_get_rowstride (RETVAL) |
621 | }; |
679 | }; |
622 | |
680 | |
|
|
681 | // cannot use gdk_pixbuf_get_byte_length because that does |
|
|
682 | // not return the size of the buffer, but the size of the buffer without |
|
|
683 | // the last padding bytes. the internal buffer is rowstride * ysize, |
|
|
684 | // and this is what the jxl decoder needs. none of this is documented |
|
|
685 | // in either library, of course. |
|
|
686 | |
|
|
687 | status = JxlDecoderSetImageOutBuffer ( |
|
|
688 | dec, |
|
|
689 | &format, |
|
|
690 | gdk_pixbuf_get_pixels (RETVAL), |
|
|
691 | gdk_pixbuf_get_rowstride (RETVAL) * info.ysize |
|
|
692 | ); |
623 | error = "JxlDecoderSetImageOutBuffer failed"; |
693 | error = "JxlDecoderSetImageOutBuffer failed"; |
624 | if (JxlDecoderSetImageOutBuffer ( |
|
|
625 | dec, |
|
|
626 | &format, |
|
|
627 | gdk_pixbuf_get_pixels (RETVAL), |
|
|
628 | gdk_pixbuf_get_byte_length (RETVAL) |
|
|
629 | ) != JXL_DEC_SUCCESS) |
694 | if (status != JXL_DEC_SUCCESS) |
630 | goto done; |
695 | goto done; |
631 | } |
696 | } |
632 | break; |
697 | break; |
633 | |
698 | |
634 | default: |
699 | default: |
… | |
… | |
639 | |
704 | |
640 | done: |
705 | done: |
641 | if (dec) |
706 | if (dec) |
642 | JxlDecoderDestroy (dec); |
707 | JxlDecoderDestroy (dec); |
643 | |
708 | |
|
|
709 | perlinterp_acquire (); |
|
|
710 | |
644 | if (runner) |
711 | if (runner_cache) |
645 | JxlThreadParallelRunnerDestroy (runner); |
712 | JxlThreadParallelRunnerDestroy (runner); |
646 | |
713 | |
647 | perlinterp_acquire (); |
714 | runner_cache = runner; |
648 | |
715 | |
649 | if (error) |
716 | if (error) |
650 | { |
717 | { |
651 | if (RETVAL) |
718 | if (RETVAL) |
652 | g_object_unref (RETVAL); |
719 | g_object_unref (RETVAL); |
653 | |
720 | |
654 | croak ("load_jxl: %s", error); |
721 | croak ("load_jxl: %s (status %d)", error, status); |
655 | } |
722 | } |
656 | #else |
723 | #else |
657 | croak ("load_jxl: jpeg-xl not enabled at compile time"); |
724 | croak ("load_jxl: jpeg-xl not enabled at compile time"); |
658 | #endif |
725 | #endif |
659 | } |
726 | } |
… | |
… | |
851 | CODE: |
918 | CODE: |
852 | { |
919 | { |
853 | STRLEN plen; |
920 | STRLEN plen; |
854 | U8 *path = (U8 *)SvPV (pathsv, plen); |
921 | U8 *path = (U8 *)SvPV (pathsv, plen); |
855 | U8 *pend = path + plen; |
922 | U8 *pend = path + plen; |
856 | U8 dst [plen * 6 * 3], *dstp = dst; |
923 | U8 dst [plen * 8 * 3], *dstp = dst; |
857 | |
924 | |
858 | while (path < pend) |
925 | while (path < pend) |
859 | { |
926 | { |
860 | U8 ch = *path; |
927 | U8 ch = *path; |
861 | |
928 | |
… | |
… | |
863 | *dstp++ = *path++; |
930 | *dstp++ = *path++; |
864 | else if (ch >= 'A' && ch <= 'Z') |
931 | else if (ch >= 'A' && ch <= 'Z') |
865 | *dstp++ = *path++ + ('a' - 'A'); |
932 | *dstp++ = *path++ + ('a' - 'A'); |
866 | else if (ch >= '0' && ch <= '9') |
933 | else if (ch >= '0' && ch <= '9') |
867 | { |
934 | { |
|
|
935 | /* version sort, up to 8 digits */ |
868 | STRLEN el, nl = 0; |
936 | STRLEN el, nl = 0; |
869 | while (*path >= '0' && *path <= '9' && path < pend) |
937 | while (*path >= '0' && *path <= '9' && path < pend) |
870 | path++, nl++; |
938 | path++, nl++; |
871 | |
939 | |
872 | for (el = nl; el < 6; el++) |
940 | for (el = nl; el < 8; el++) |
873 | *dstp++ = '0'; |
941 | *dstp++ = '0'; |
874 | |
942 | |
875 | memcpy (dstp, path - nl, nl); |
943 | memcpy (dstp, path - nl, nl); |
876 | dstp += nl; |
944 | dstp += nl; |
877 | } |
945 | } |