… | |
… | |
22 | |
22 | |
23 | #include <stdlib.h> |
23 | #include <stdlib.h> |
24 | |
24 | |
25 | #include "parse_routines.h" |
25 | #include "parse_routines.h" |
26 | #include "error.h" |
26 | #include "error.h" |
|
|
27 | #include "image.h" |
27 | |
28 | |
28 | #ifdef DMALLOC |
29 | #ifdef DMALLOC |
29 | #include <dmalloc.h> |
30 | #include <dmalloc.h> |
30 | #endif |
31 | #endif |
|
|
32 | |
|
|
33 | static int psiconv_decode_rle8 (const psiconv_config config, int lev, |
|
|
34 | psiconv_u32 off, |
|
|
35 | const psiconv_pixel_bytes encoded, |
|
|
36 | psiconv_pixel_bytes *decoded); |
|
|
37 | |
|
|
38 | static int psiconv_bytes_to_pixel_data(const psiconv_config config, |
|
|
39 | int lev, psiconv_u32 off, |
|
|
40 | const psiconv_pixel_bytes bytes, |
|
|
41 | psiconv_pixel_ints *pixels, |
|
|
42 | int colordepth, int xsize, int ysize); |
|
|
43 | |
|
|
44 | static int psiconv_pixel_data_to_floats (const psiconv_config config, int lev, |
|
|
45 | psiconv_u32 off, |
|
|
46 | const psiconv_pixel_ints pixels, |
|
|
47 | psiconv_pixel_floats_t *floats, |
|
|
48 | int colordepth, int color, |
|
|
49 | int redbits, int bluebits, int greenbits, |
|
|
50 | const psiconv_pixel_floats_t palet); |
|
|
51 | |
31 | |
52 | |
32 | |
53 | |
33 | int psiconv_parse_jumptable_section(const psiconv_config config, |
54 | int psiconv_parse_jumptable_section(const psiconv_config config, |
34 | const psiconv_buffer buf,int lev, |
55 | const psiconv_buffer buf,int lev, |
35 | psiconv_u32 off, int *length, |
56 | psiconv_u32 off, int *length, |
… | |
… | |
219 | psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " |
240 | psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " |
220 | "compression type, assuming RLE"); |
241 | "compression type, assuming RLE"); |
221 | psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); |
242 | psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); |
222 | compression = 1; |
243 | compression = 1; |
223 | } |
244 | } |
224 | psiconv_debug(config,lev+2,off+len,"Compression: %s",compression?"RLE":"none"); |
245 | psiconv_debug(config,lev+2,off+len,"Compression: %s",compression?"RLE8":"none"); |
225 | len += 4; |
246 | len += 4; |
226 | |
247 | |
227 | if (isclipart) { |
248 | if (isclipart) { |
228 | psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); |
249 | psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); |
229 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
250 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
… | |
… | |
655 | if (!res) |
676 | if (!res) |
656 | return -PSICONV_E_NOMEM; |
677 | return -PSICONV_E_NOMEM; |
657 | else |
678 | else |
658 | return res; |
679 | return res; |
659 | } |
680 | } |
|
|
681 | |
|
|
682 | int psiconv_decode_rle8 (const psiconv_config config, int lev, psiconv_u32 off, |
|
|
683 | const psiconv_pixel_bytes encoded, |
|
|
684 | psiconv_pixel_bytes *decoded) |
|
|
685 | { |
|
|
686 | int res=0; |
|
|
687 | psiconv_u8 *marker,*value; |
|
|
688 | int i,j; |
|
|
689 | |
|
|
690 | psiconv_progress(config,lev+1,off,"Going to decode the RLE8 encoding"); |
|
|
691 | if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
692 | goto ERROR1; |
|
|
693 | |
|
|
694 | for (i = 0; i < psiconv_list_length(encoded);) { |
|
|
695 | psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i); |
|
|
696 | if (!(marker = psiconv_list_get(encoded,i))) |
|
|
697 | goto ERROR2; |
|
|
698 | psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker); |
|
|
699 | if (*marker < 0x80) { |
|
|
700 | psiconv_debug(config,lev+2,off,"Marker: repeat value byte %02x times", |
|
|
701 | *marker+1); |
|
|
702 | psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1); |
|
|
703 | if (!(value = psiconv_list_get(encoded,i+1))) |
|
|
704 | goto ERROR2; |
|
|
705 | psiconv_debug(config,lev+2,off,"Value byte: %02x",*value); |
|
|
706 | psiconv_progress(config,lev+2,off,"Adding %02x bytes %02x", |
|
|
707 | *marker+1,*value); |
|
|
708 | for (j = 0; j < *marker + 1; j++) |
|
|
709 | if ((res = psiconv_list_add(*decoded,value))) |
|
|
710 | goto ERROR2; |
|
|
711 | i += 2; |
|
|
712 | } else { |
|
|
713 | psiconv_debug(config,lev+2,off,"Marker: %02x value bytes follow", |
|
|
714 | 0x100 - *marker); |
|
|
715 | for (j = 0; j < (0x100 - *marker); j++) { |
|
|
716 | psiconv_progress(config,lev+2,off,"Going to read value byte at %04x", |
|
|
717 | i+j+1); |
|
|
718 | if (!(value = psiconv_list_get(encoded,i+j+1))) |
|
|
719 | goto ERROR2; |
|
|
720 | psiconv_debug(config,lev+2,off,"Value: %02x",*value); |
|
|
721 | if ((res = psiconv_list_add(*decoded,value))) |
|
|
722 | goto ERROR2; |
|
|
723 | } |
|
|
724 | i += (0x100 - *marker) + 1; |
|
|
725 | } |
|
|
726 | } |
|
|
727 | psiconv_progress(config,lev,off, |
|
|
728 | "End of RLE8 decoding process"); |
|
|
729 | return 0; |
|
|
730 | |
|
|
731 | ERROR2: |
|
|
732 | psiconv_list_free(*decoded); |
|
|
733 | ERROR1: |
|
|
734 | psiconv_warn(config,lev+1,off,"Decoding of RLE8 failed"); |
|
|
735 | if (!res) |
|
|
736 | return -PSICONV_E_NOMEM; |
|
|
737 | else |
|
|
738 | return res; |
|
|
739 | } |
|
|
740 | |
|
|
741 | int psiconv_bytes_to_pixel_data(const psiconv_config config, |
|
|
742 | int lev, psiconv_u32 off, |
|
|
743 | const psiconv_pixel_bytes bytes, |
|
|
744 | psiconv_pixel_ints *pixels, |
|
|
745 | int colordepth, int xsize, int ysize) |
|
|
746 | { |
|
|
747 | int res=0; |
|
|
748 | int ibits,obits,x,y,bits; |
|
|
749 | psiconv_u8 input; |
|
|
750 | psiconv_u32 nr,output; |
|
|
751 | psiconv_u8 *ientry; |
|
|
752 | |
|
|
753 | psiconv_progress(config,lev+1,off,"Going to convert the bytes to pixels"); |
|
|
754 | if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) |
|
|
755 | goto ERROR1; |
|
|
756 | |
|
|
757 | nr = 0; |
|
|
758 | for (y = 0; y < ysize; y++) { |
|
|
759 | /* New lines will start at longs */ |
|
|
760 | while (nr % 4) |
|
|
761 | nr ++; |
|
|
762 | input = 0; |
|
|
763 | ibits = 0; |
|
|
764 | for (x= 0; x < xsize; x++) { |
|
|
765 | psiconv_progress(config,lev+2,off, |
|
|
766 | "Processing pixel at (x,y) = (%04x,%04x)",x,y); |
|
|
767 | output = 0; |
|
|
768 | obits = 0; |
|
|
769 | while (obits < colordepth) { |
|
|
770 | if (ibits == 0) { |
|
|
771 | psiconv_progress(config,lev+3,off, |
|
|
772 | "Going to read byte %08x",nr); |
|
|
773 | if (!(ientry = psiconv_list_get(bytes,nr))) |
|
|
774 | goto ERROR2; |
|
|
775 | psiconv_debug(config,lev+3,off,"Byte value: %02x",*ientry); |
|
|
776 | input = *ientry; |
|
|
777 | ibits = 8; |
|
|
778 | nr ++; |
|
|
779 | } |
|
|
780 | bits = ibits + obits > colordepth?colordepth-obits:ibits; |
|
|
781 | output = output << bits; |
|
|
782 | output |= input & ((1 << bits) - 1); |
|
|
783 | input = input >> bits; |
|
|
784 | ibits -= bits; |
|
|
785 | obits += bits; |
|
|
786 | } |
|
|
787 | psiconv_debug(config,lev+2,off,"Pixel value: %08x",output); |
|
|
788 | if ((res = psiconv_list_add(*pixels,&output))) |
|
|
789 | goto ERROR2; |
|
|
790 | } |
|
|
791 | } |
|
|
792 | |
|
|
793 | psiconv_progress(config,lev,off, |
|
|
794 | "Converting bytes to pixels completed"); |
|
|
795 | return 0; |
|
|
796 | |
|
|
797 | |
|
|
798 | ERROR2: |
|
|
799 | psiconv_list_free(*pixels); |
|
|
800 | ERROR1: |
|
|
801 | psiconv_warn(config,lev+1,off,"Converting bytes to pixels failed"); |
|
|
802 | if (!res) |
|
|
803 | return -PSICONV_E_NOMEM; |
|
|
804 | else |
|
|
805 | return res; |
|
|
806 | } |
|
|
807 | |
|
|
808 | int psiconv_pixel_data_to_floats (const psiconv_config config, int lev, |
|
|
809 | psiconv_u32 off, |
|
|
810 | const psiconv_pixel_ints pixels, |
|
|
811 | psiconv_pixel_floats_t *floats, |
|
|
812 | int colordepth, int color, |
|
|
813 | int redbits, int bluebits, int greenbits, |
|
|
814 | const psiconv_pixel_floats_t palet) |
|
|
815 | { |
|
|
816 | int res = 0; |
|
|
817 | psiconv_u32 i; |
|
|
818 | psiconv_u32 *pixel; |
|
|
819 | |
|
|
820 | psiconv_progress(config,lev+1,off,"Going to convert pixels to floats"); |
|
|
821 | if (!((*floats).red = malloc(psiconv_list_length(pixels) * |
|
|
822 | sizeof(*(*floats).red)))) |
|
|
823 | goto ERROR1; |
|
|
824 | if (!((*floats).green = malloc(psiconv_list_length(pixels) * |
|
|
825 | sizeof(*(*floats).green)))) |
|
|
826 | goto ERROR2; |
|
|
827 | if (!((*floats).blue = malloc(psiconv_list_length(pixels) * |
|
|
828 | sizeof(*(*floats).blue)))) |
|
|
829 | goto ERROR3; |
|
|
830 | (*floats).length = psiconv_list_length(pixels); |
|
|
831 | |
|
|
832 | for (i = 0; i < psiconv_list_length(pixels); i++) { |
|
|
833 | if (!(pixel = psiconv_list_get(pixels,i))) |
|
|
834 | goto ERROR4; |
|
|
835 | psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel); |
|
|
836 | if (!palet.length) { |
|
|
837 | if (color) { |
|
|
838 | (*floats).blue[i] = (*pixel & ((1 << bluebits) - 1)) / |
|
|
839 | (1 << bluebits); |
|
|
840 | (*floats).green[i] = ((*pixel >> bluebits) & ((1 << greenbits) - 1)) / |
|
|
841 | (1 << greenbits); |
|
|
842 | (*floats).red[i] = ((*pixel >> (bluebits+greenbits)) & |
|
|
843 | ((1 << redbits) - 1)) / (1 << redbits); |
|
|
844 | } else { |
|
|
845 | (*floats).red[i] = (*floats).green[i] = |
|
|
846 | (*floats).blue[i] = *pixel / (1 << colordepth); |
|
|
847 | } |
|
|
848 | } else { |
|
|
849 | if (*pixel >= palet.length) { |
|
|
850 | psiconv_warn(config,lev+2,off, |
|
|
851 | "Invalid palet color found (using color 0x00)"); |
|
|
852 | (*floats).red[i] = palet.red[0]; |
|
|
853 | (*floats).green[i] = palet.green[0]; |
|
|
854 | (*floats).blue[i] = palet.blue[0]; |
|
|
855 | } else { |
|
|
856 | (*floats).red[i] = palet.red[*pixel]; |
|
|
857 | (*floats).green[i] = palet.green[*pixel]; |
|
|
858 | (*floats).blue[i] = palet.blue[*pixel]; |
|
|
859 | } |
|
|
860 | } |
|
|
861 | psiconv_debug(config,lev+2,off, "Pixel: Red (%f), green (%f), blue (%f)", |
|
|
862 | (*floats).red[i],(*floats).green[i],(*floats).blue[i]); |
|
|
863 | } |
|
|
864 | psiconv_progress(config,lev+1,off,"Finished converting pixels to floats"); |
|
|
865 | return 0; |
|
|
866 | |
|
|
867 | ERROR4: |
|
|
868 | free((*floats).blue); |
|
|
869 | ERROR3: |
|
|
870 | free((*floats).green); |
|
|
871 | ERROR2: |
|
|
872 | free((*floats).red); |
|
|
873 | ERROR1: |
|
|
874 | psiconv_warn(config,lev+1,off,"Converting pixels to floats failed"); |
|
|
875 | if (!res) |
|
|
876 | return -PSICONV_E_NOMEM; |
|
|
877 | else |
|
|
878 | return res; |
|
|
879 | } |
|
|
880 | |
|
|
881 | |
|
|
882 | |