1 | /* |
1 | /* |
2 | parse_image.c - Part of psiconv, a PSION 5 file formats converter |
2 | parse_image.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
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 | |
|
|
29 | #ifdef DMALLOC |
|
|
30 | #include <dmalloc.h> |
|
|
31 | #endif |
|
|
32 | |
|
|
33 | /* Extreme debugging info */ |
|
|
34 | #undef LOUD |
|
|
35 | |
|
|
36 | static int psiconv_decode_rle8 (const psiconv_config config, int lev, |
|
|
37 | psiconv_u32 off, |
|
|
38 | const psiconv_pixel_bytes encoded, |
|
|
39 | psiconv_pixel_bytes *decoded); |
|
|
40 | |
|
|
41 | static int psiconv_decode_rle12 (const psiconv_config config, int lev, |
|
|
42 | psiconv_u32 off, |
|
|
43 | const psiconv_pixel_bytes encoded, |
|
|
44 | psiconv_pixel_bytes *decoded); |
|
|
45 | |
|
|
46 | static int psiconv_decode_rle16 (const psiconv_config config, int lev, |
|
|
47 | psiconv_u32 off, |
|
|
48 | const psiconv_pixel_bytes encoded, |
|
|
49 | psiconv_pixel_bytes *decoded); |
|
|
50 | |
|
|
51 | static int psiconv_decode_rle24 (const psiconv_config config, int lev, |
|
|
52 | psiconv_u32 off, |
|
|
53 | const psiconv_pixel_bytes encoded, |
|
|
54 | psiconv_pixel_bytes *decoded); |
|
|
55 | |
|
|
56 | static int psiconv_bytes_to_pixel_data(const psiconv_config config, |
|
|
57 | int lev, psiconv_u32 off, |
|
|
58 | const psiconv_pixel_bytes bytes, |
|
|
59 | psiconv_pixel_ints *pixels, |
|
|
60 | int colordepth, int xsize, int ysize); |
|
|
61 | |
|
|
62 | static int psiconv_pixel_data_to_floats (const psiconv_config config, int lev, |
|
|
63 | psiconv_u32 off, |
|
|
64 | const psiconv_pixel_ints pixels, |
|
|
65 | psiconv_pixel_floats_t *floats, |
|
|
66 | int colordepth, int color, |
|
|
67 | int redbits, int bluebits, int greenbits, |
|
|
68 | const psiconv_pixel_floats_t palet); |
|
|
69 | |
|
|
70 | |
|
|
71 | |
28 | int psiconv_parse_jumptable_section(const psiconv_buffer buf,int lev, |
72 | int psiconv_parse_jumptable_section(const psiconv_config config, |
|
|
73 | const psiconv_buffer buf,int lev, |
29 | psiconv_u32 off, int *length, |
74 | psiconv_u32 off, int *length, |
30 | psiconv_jumptable_section *result) |
75 | psiconv_jumptable_section *result) |
31 | { |
76 | { |
32 | int res = 0; |
77 | int res = 0; |
33 | int len = 0; |
78 | int len = 0; |
34 | psiconv_u32 listlen,temp; |
79 | psiconv_u32 listlen,temp; |
35 | int i; |
80 | int i; |
36 | |
81 | |
37 | psiconv_progress(lev+1,off+len,"Going to read the jumptable section"); |
82 | psiconv_progress(config,lev+1,off+len,"Going to read the jumptable section"); |
38 | if (!((*result) = psiconv_list_new(sizeof(psiconv_u32)))) |
83 | if (!((*result) = psiconv_list_new(sizeof(psiconv_u32)))) |
39 | goto ERROR1; |
84 | goto ERROR1; |
40 | |
85 | |
41 | psiconv_progress(lev+2,off+len,"Going to read the list length"); |
86 | psiconv_progress(config,lev+2,off+len,"Going to read the list length"); |
42 | listlen = psiconv_read_u32(buf,lev+2,off+len,&res); |
87 | listlen = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
43 | if (res) |
88 | if (res) |
44 | goto ERROR2; |
89 | goto ERROR2; |
45 | psiconv_debug(lev+2,off+len,"List length: %08x",listlen); |
90 | psiconv_debug(config,lev+2,off+len,"List length: %08x",listlen); |
46 | len += 4; |
91 | len += 4; |
47 | |
92 | |
48 | psiconv_progress(lev+2,off+len,"Going to read the list"); |
93 | psiconv_progress(config,lev+2,off+len,"Going to read the list"); |
49 | for (i = 0; i < listlen; i++) { |
94 | for (i = 0; i < listlen; i++) { |
50 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
95 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
51 | if (res) |
96 | if (res) |
52 | goto ERROR2; |
97 | goto ERROR2; |
53 | if ((res = psiconv_list_add(*result,&temp))) |
98 | if ((res = psiconv_list_add(*result,&temp))) |
54 | goto ERROR2; |
99 | goto ERROR2; |
55 | psiconv_debug(lev+3,off+len,"Offset: %08x",temp); |
100 | psiconv_debug(config,lev+3,off+len,"Offset: %08x",temp); |
56 | len += 4; |
101 | len += 4; |
57 | } |
102 | } |
58 | |
103 | |
59 | if (length) |
104 | if (length) |
60 | *length = len; |
105 | *length = len; |
61 | |
106 | |
62 | psiconv_progress(lev+1,off+len-1,"End of jumptable section " |
107 | psiconv_progress(config,lev+1,off+len-1,"End of jumptable section " |
63 | "(total length: %08x)", len); |
108 | "(total length: %08x)", len); |
64 | |
109 | |
65 | return 0; |
110 | return 0; |
66 | |
111 | |
67 | ERROR2: |
112 | ERROR2: |
68 | psiconv_list_free(*result); |
113 | psiconv_list_free(*result); |
69 | ERROR1: |
114 | ERROR1: |
70 | psiconv_warn(lev+1,off,"Reading of Jumptable Section failed"); |
115 | psiconv_error(config,lev+1,off,"Reading of Jumptable Section failed"); |
71 | if (length) |
116 | if (length) |
72 | *length = 0; |
117 | *length = 0; |
73 | if (!res) |
118 | if (!res) |
74 | return -PSICONV_E_NOMEM; |
119 | return -PSICONV_E_NOMEM; |
75 | else |
120 | else |
76 | return res; |
121 | return res; |
77 | } |
122 | } |
78 | |
123 | |
79 | static int decode_byte(int lev, psiconv_u32 off, |
|
|
80 | psiconv_paint_data_section data, psiconv_u32 *pixelnr, |
|
|
81 | psiconv_u8 byte, int bits_per_pixel, int linelen, |
|
|
82 | int *linepos,int picsize) |
|
|
83 | { |
|
|
84 | int mask = (bits_per_pixel << 1) -1; |
|
|
85 | int i; |
|
|
86 | if (*linepos < (data->xsize + (8/bits_per_pixel) - 1) / (8/bits_per_pixel)) |
|
|
87 | for (i = 0; i < 8/bits_per_pixel; i ++) { |
|
|
88 | if ((i != 0) && ((*pixelnr % (data->xsize)) == 0)) { |
|
|
89 | psiconv_debug(lev+1,off,"Skipping padding: %02x",byte); |
|
|
90 | i = 8; |
|
|
91 | } else if (*pixelnr >= picsize) { |
|
|
92 | psiconv_warn(lev+1,off,"Corrupted picture data!"); |
|
|
93 | psiconv_debug(lev+1,off,"Trying to write a pixel too far"); |
|
|
94 | return -1; |
|
|
95 | } else { |
|
|
96 | data->red[*pixelnr] = data->green[*pixelnr] = data->blue[*pixelnr] = |
|
|
97 | ((float) (byte & mask)) / ((1 << bits_per_pixel) -1); |
|
|
98 | psiconv_debug(lev+1,off,"Pixel %04x: (%04x,%04x) value %02x, color %f", |
|
|
99 | *pixelnr,*pixelnr % data->xsize, |
|
|
100 | *pixelnr / data->xsize, byte&mask, data->red[*pixelnr]); |
|
|
101 | byte = byte >> bits_per_pixel; |
|
|
102 | (*pixelnr) ++; |
|
|
103 | } |
|
|
104 | } |
|
|
105 | else |
|
|
106 | psiconv_debug(lev+1,off,"Skipping padding byte"); |
|
|
107 | (*linepos) ++; |
|
|
108 | if (*linepos == linelen) |
|
|
109 | *linepos = 0; |
|
|
110 | return 0; |
|
|
111 | } |
|
|
112 | |
|
|
113 | |
|
|
114 | int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, |
124 | int psiconv_parse_paint_data_section(const psiconv_config config, |
|
|
125 | const psiconv_buffer buf,int lev, |
115 | psiconv_u32 off, int *length,int isclipart, |
126 | psiconv_u32 off, int *length,int isclipart, |
116 | psiconv_paint_data_section *result) |
127 | psiconv_paint_data_section *result) |
117 | { |
128 | { |
118 | int res = 0; |
129 | int res = 0; |
119 | int len = 0; |
130 | int len = 0; |
120 | psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; |
131 | psiconv_u32 size,offset,picsize,temp,datasize,color, |
|
|
132 | redbits,bluebits,greenbits; |
121 | psiconv_u8 marker; |
133 | psiconv_u8 byte; |
122 | int i,leng; |
134 | int leng,i; |
123 | psiconv_u32 bits_per_pixel,compression; |
135 | psiconv_u32 bits_per_pixel,compression; |
124 | int linepos = 0; |
136 | psiconv_pixel_bytes bytes,decoded; |
|
|
137 | psiconv_pixel_ints pixels; |
|
|
138 | psiconv_pixel_floats_t floats,palet; |
125 | |
139 | |
126 | psiconv_progress(lev+1,off,"Going to read a paint data section"); |
140 | psiconv_progress(config,lev+1,off,"Going to read a paint data section"); |
127 | if (!((*result) = malloc(sizeof(**result)))) |
141 | if (!((*result) = malloc(sizeof(**result)))) |
128 | goto ERROR1; |
142 | goto ERROR1; |
129 | |
143 | |
|
|
144 | if (!(bytes = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
145 | goto ERROR2; |
|
|
146 | |
130 | psiconv_progress(lev+2,off+len,"Going to read section size"); |
147 | psiconv_progress(config,lev+2,off+len,"Going to read section size"); |
131 | size = psiconv_read_u32(buf,lev+2,off+len,&res); |
148 | size = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
132 | if (res) |
149 | if (res) |
133 | goto ERROR2; |
150 | goto ERROR3; |
134 | psiconv_debug(lev+2,off+len,"Section size: %08x",size); |
151 | psiconv_debug(config,lev+2,off+len,"Section size: %08x",size); |
135 | len += 4; |
152 | len += 4; |
136 | |
153 | |
137 | psiconv_progress(lev+2,off+len,"Going to read pixel data offset"); |
154 | psiconv_progress(config,lev+2,off+len,"Going to read pixel data offset"); |
138 | offset = psiconv_read_u32(buf,lev+2,off+len,&res); |
155 | offset = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
139 | if (res) |
156 | if (res) |
140 | goto ERROR2; |
157 | goto ERROR3; |
141 | if (offset != 0x28) { |
158 | if (offset != 0x28) { |
142 | psiconv_warn(lev+2,off+len, |
159 | psiconv_error(config,lev+2,off+len, |
143 | "Paint data section data offset has unexpected value"); |
160 | "Paint data section data offset has unexpected value"); |
144 | psiconv_debug(lev+2,off+len, |
161 | psiconv_debug(config,lev+2,off+len, |
145 | "Data offset: read %08x, expected %08x",offset,0x28); |
162 | "Data offset: read %08x, expected %08x",offset,0x28); |
146 | res = -1; |
163 | res = -1; |
147 | } |
164 | } |
148 | len += 4; |
165 | len += 4; |
149 | |
166 | |
150 | psiconv_progress(lev+2,off+len,"Going to read picture X size"); |
167 | psiconv_progress(config,lev+2,off+len,"Going to read picture X size"); |
151 | (*result)->xsize = psiconv_read_u32(buf,lev+2,off+len,&res); |
168 | (*result)->xsize = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
152 | if (res) |
169 | if (res) |
153 | goto ERROR2; |
170 | goto ERROR3; |
154 | psiconv_debug(lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
171 | psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
155 | len += 4; |
172 | len += 4; |
156 | |
173 | |
157 | psiconv_progress(lev+2,off+len,"Going to read picture Y size"); |
174 | psiconv_progress(config,lev+2,off+len,"Going to read picture Y size"); |
158 | (*result)->ysize = psiconv_read_u32(buf,lev+2,off+len,&res); |
175 | (*result)->ysize = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
159 | if (res) |
176 | if (res) |
160 | goto ERROR2; |
177 | goto ERROR3; |
161 | psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
178 | psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
162 | len += 4; |
179 | len += 4; |
163 | |
180 | |
164 | picsize = (*result)->ysize * (*result)->xsize; |
181 | picsize = (*result)->ysize * (*result)->xsize; |
165 | |
182 | |
166 | psiconv_progress(lev+2,off+len,"Going to read the real picture x size"); |
183 | psiconv_progress(config,lev+2,off+len,"Going to read the real picture x size"); |
167 | (*result)->pic_xsize = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
184 | (*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
168 | if (res) |
185 | if (res) |
169 | goto ERROR2; |
186 | goto ERROR3; |
170 | psiconv_debug(lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize); |
187 | psiconv_debug(config,lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize); |
171 | len += leng; |
188 | len += leng; |
172 | |
189 | |
173 | psiconv_progress(lev+2,off+len,"Going to read the real picture y size"); |
190 | psiconv_progress(config,lev+2,off+len,"Going to read the real picture y size"); |
174 | (*result)->pic_ysize = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
191 | (*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
175 | if (res) |
192 | if (res) |
176 | goto ERROR2; |
193 | goto ERROR3; |
177 | psiconv_debug(lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize); |
194 | psiconv_debug(config,lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize); |
178 | len += leng; |
195 | len += leng; |
179 | |
196 | |
180 | psiconv_progress(lev+2,off+len,"Going to read the number of bits per pixel"); |
197 | psiconv_progress(config,lev+2,off+len,"Going to read the number of bits per pixel"); |
181 | bits_per_pixel=psiconv_read_u32(buf,lev+2,off+len,&res); |
198 | bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
182 | if (res) |
199 | if (res) |
183 | goto ERROR2; |
200 | goto ERROR3; |
184 | if (bits_per_pixel > 8) { |
|
|
185 | psiconv_warn(lev+2,off+len,"Picture has too many colors"); |
|
|
186 | psiconv_debug(lev+2,off+len,"Read %d colorbits",bits_per_pixel); |
|
|
187 | res = -PSICONV_E_PARSE; |
|
|
188 | goto ERROR2; |
|
|
189 | } |
|
|
190 | psiconv_debug(lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); |
201 | psiconv_debug(config,lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); |
191 | len += 4; |
202 | len += 4; |
192 | |
203 | |
193 | for (i = 0 ; i < 2; i++) { |
204 | psiconv_progress(config,lev+2,off+len, |
|
|
205 | "Going to read whether this is a colour or greyscale picture"); |
|
|
206 | color = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
|
|
207 | if (res) |
|
|
208 | goto ERROR3; |
|
|
209 | if ((color != 0) && (color != 1)) { |
|
|
210 | psiconv_warn(config,lev+2,off+len, |
|
|
211 | "Paint data section unknown color type (ignored)"); |
|
|
212 | psiconv_debug(config,lev+2,off+len, |
|
|
213 | "Color: read %08x, expected %08x or %08x",color,0,1); |
|
|
214 | color = 1; |
|
|
215 | } else { |
|
|
216 | psiconv_debug(config,lev+2,off+len,"Color: %08x (%s picture)", |
|
|
217 | color,(color?"color":"greyscale")); |
|
|
218 | } |
|
|
219 | len += 4; |
|
|
220 | |
|
|
221 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
|
|
222 | if (res) |
|
|
223 | goto ERROR3; |
|
|
224 | if (temp != 00) { |
|
|
225 | psiconv_warn(config,lev+2,off+len, |
|
|
226 | "Paint data section prologue has unknown values (ignored)"); |
|
|
227 | psiconv_debug(config,lev+2,off+len, |
|
|
228 | "read %08x, expected %08x",temp, 0x00); |
|
|
229 | } |
|
|
230 | len += 4; |
|
|
231 | |
|
|
232 | psiconv_progress(config,lev+2,off+len, |
|
|
233 | "Going to read whether RLE compression is used"); |
|
|
234 | compression=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
|
|
235 | if (res) |
|
|
236 | goto ERROR3; |
|
|
237 | if (compression > 4) { |
|
|
238 | psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " |
|
|
239 | "compression type, assuming RLE"); |
|
|
240 | psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); |
|
|
241 | compression = 0; |
|
|
242 | } |
|
|
243 | psiconv_debug(config,lev+2,off+len,"Compression: %s", |
|
|
244 | compression == 4?"RLE24":compression == 3?"RLE16": |
|
|
245 | compression == 2?"RLE12":compression == 1?"RLE8":"none"); |
|
|
246 | len += 4; |
|
|
247 | |
|
|
248 | if (isclipart) { |
|
|
249 | psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); |
194 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
250 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
195 | if (res) |
251 | if (res) |
196 | goto ERROR2; |
252 | goto ERROR3; |
197 | if (temp != 00) { |
253 | if (temp != 0xffffffff) { |
198 | psiconv_warn(lev+2,off+len, |
254 | psiconv_warn(config,lev+2,off+len, |
199 | "Paint data section prologue has unknown values (ignored)"); |
255 | "Paint data section prologue has unknown values (ignoring)"); |
200 | psiconv_debug(lev+2,off+len, |
256 | psiconv_debug(config,lev+2,off+len, |
201 | "offset %02x: read %08x, expected %08x",i,temp, 0x00); |
257 | "Read %08x, expected %08x",temp, 0xffffffff); |
202 | } |
258 | } |
203 | len += 4; |
259 | len += 4; |
204 | } |
|
|
205 | |
|
|
206 | psiconv_progress(lev+2,off+len, |
|
|
207 | "Going to read whether RLE compression is used"); |
|
|
208 | compression=psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
209 | if (res) |
|
|
210 | goto ERROR2; |
|
|
211 | if (compression > 1) { |
|
|
212 | psiconv_warn(lev+2,off+len,"Paint data section has unknown " |
|
|
213 | "compression type, assuming RLE"); |
|
|
214 | psiconv_debug(lev+2,off+len,"Read compression type %d",compression); |
|
|
215 | compression = 1; |
|
|
216 | } |
|
|
217 | psiconv_debug(lev+2,off+len,"Compression: %s",compression?"RLE":"none"); |
|
|
218 | len += 4; |
|
|
219 | |
|
|
220 | if (isclipart) { |
|
|
221 | psiconv_progress(lev+2,off+len,"Going to read an unknown long"); |
260 | psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long"); |
222 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
261 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
223 | if (res) |
262 | if (res) |
224 | goto ERROR2; |
263 | goto ERROR3; |
225 | if (temp != 0xffffffff) { |
264 | if (temp != 0x44) { |
226 | psiconv_warn(lev+2,off+len, |
265 | psiconv_warn(config,lev+2,off+len, |
227 | "Paint data section prologue has unknown values (ignoring)"); |
266 | "Paint data section prologue has unknown values (ignoring)"); |
228 | psiconv_debug(lev+2,off+len, |
267 | psiconv_debug(config,lev+2,off+len, |
229 | "offset %02x: read %08x, expected %08x",i,temp, 0xffffffff); |
268 | "read %08x, expected %08x",temp, 0x44); |
230 | } |
269 | } |
231 | len += 4; |
270 | len += 4; |
232 | psiconv_progress(lev+2,off+len,"Going to read a second unknown long"); |
|
|
233 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
234 | if (res) |
|
|
235 | goto ERROR2; |
|
|
236 | if (temp != 0x44) { |
|
|
237 | psiconv_warn(lev+2,off+len, |
|
|
238 | "Paint data section prologue has unknown values (ignoring)"); |
|
|
239 | psiconv_debug(lev+2,off+len, |
|
|
240 | "offset %02x: read %08x, expected %08x",i,temp, 0x44); |
|
|
241 | } |
271 | } |
242 | len += 4; |
|
|
243 | } |
|
|
244 | |
272 | |
245 | if (!((*result)->red = malloc(sizeof(float) * picsize))) |
|
|
246 | goto ERROR2; |
|
|
247 | if (!((*result)->green = malloc(sizeof(float) * picsize))) |
|
|
248 | goto ERROR3; |
|
|
249 | if (!((*result)->blue = malloc(sizeof(float) * picsize))) |
|
|
250 | goto ERROR4; |
|
|
251 | len = offset; |
273 | len = offset; |
252 | datasize = size - len; |
274 | datasize = size - len; |
253 | if (isclipart) |
275 | if (isclipart) |
254 | len += 8; |
276 | len += 8; |
255 | |
277 | |
|
|
278 | if (color || (bits_per_pixel != 2)) |
|
|
279 | psiconv_warn(config,lev+2,off+len, |
|
|
280 | "All image types except 2-bit greyscale are experimental!"); |
|
|
281 | |
256 | psiconv_progress(lev+2,off+len,"Going to read the pixel data"); |
282 | psiconv_progress(config,lev+2,off+len,"Going to read the pixel data"); |
257 | pixelnr = 0; |
283 | for (i = 0; i < datasize; i++) { |
258 | datanr = 0; |
|
|
259 | if (!compression) { |
|
|
260 | linelen = datasize / (*result)->ysize; |
|
|
261 | psiconv_debug(lev+3,off+len,"Line length: %04x bytes",linelen); |
|
|
262 | while((datanr < datasize)) { |
|
|
263 | temp = psiconv_read_u8(buf,lev+2,off+len+datanr,&res); |
284 | byte = psiconv_read_u8(config,buf,lev+2,off+len+i,&res); |
|
|
285 | #ifdef LOUD |
|
|
286 | psiconv_debug(config,lev+2,off+len+i, |
|
|
287 | "Pixel byte %04x of %04x has value %02x", |
|
|
288 | i,datasize,byte); |
|
|
289 | #endif |
264 | if (res) |
290 | if (res) |
|
|
291 | goto ERROR3; |
|
|
292 | psiconv_list_add(bytes,&byte); |
|
|
293 | } |
|
|
294 | len += datasize; |
|
|
295 | |
|
|
296 | switch(compression) { |
|
|
297 | case 1: |
|
|
298 | if ((res = psiconv_decode_rle8(config,lev+2,off+len,bytes,&decoded))) |
|
|
299 | goto ERROR3; |
|
|
300 | psiconv_list_free(bytes); |
|
|
301 | bytes = decoded; |
|
|
302 | break; |
|
|
303 | case 2: |
|
|
304 | if ((psiconv_decode_rle12(config,lev+2,off+len,bytes,&decoded))) |
|
|
305 | goto ERROR3; |
|
|
306 | psiconv_list_free(bytes); |
|
|
307 | bytes = decoded; |
|
|
308 | break; |
|
|
309 | case 3: |
|
|
310 | if ((psiconv_decode_rle16(config,lev+2,off+len,bytes,&decoded))) |
265 | goto ERROR5; |
311 | goto ERROR3; |
266 | if (decode_byte(lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel, |
312 | psiconv_list_free(bytes); |
267 | linelen,&linepos,picsize)) { |
313 | bytes = decoded; |
268 | res = -PSICONV_E_PARSE; |
|
|
269 | break; |
314 | break; |
270 | } |
315 | case 4: |
271 | datanr++; |
316 | if ((psiconv_decode_rle24(config,lev+2,off+len,bytes,&decoded))) |
|
|
317 | goto ERROR3; |
|
|
318 | psiconv_list_free(bytes); |
|
|
319 | bytes = decoded; |
|
|
320 | break; |
|
|
321 | } |
|
|
322 | |
|
|
323 | if ((res = psiconv_bytes_to_pixel_data(config,lev+2,off+len,bytes, |
|
|
324 | &pixels,bits_per_pixel, |
|
|
325 | (*result)->xsize,(*result)->ysize))) |
|
|
326 | goto ERROR3; |
|
|
327 | |
|
|
328 | /* Use some heuristics; things may get unexpected around here */ |
|
|
329 | bluebits = redbits = greenbits = 0; |
|
|
330 | palet = psiconv_palet_none; |
|
|
331 | if (color) { |
|
|
332 | if (bits_per_pixel == 4) |
|
|
333 | palet = psiconv_palet_color_4; |
|
|
334 | else if (bits_per_pixel == 8) |
|
|
335 | palet = psiconv_palet_color_8; |
|
|
336 | else { |
|
|
337 | redbits = (bits_per_pixel+2) / 3; |
|
|
338 | greenbits = (bits_per_pixel+2) / 3; |
|
|
339 | bluebits = bits_per_pixel - redbits - greenbits; |
272 | } |
340 | } |
273 | } else { |
|
|
274 | psiconv_progress(lev+2,off+len,"First pass: determining line length"); |
|
|
275 | datanr = 0; |
|
|
276 | i = 0; |
|
|
277 | while (datanr < datasize) { |
|
|
278 | marker = psiconv_read_u8(buf,lev+3,off+len+datanr,&res); |
|
|
279 | if (res) |
|
|
280 | goto ERROR5; |
|
|
281 | if (marker >= 0x80) { |
|
|
282 | datanr += 0x100 - marker + 1; |
|
|
283 | i += 0x100 - marker; |
|
|
284 | } else { |
|
|
285 | datanr += 2; |
|
|
286 | i += marker + 1; |
|
|
287 | } |
|
|
288 | } |
341 | } |
289 | linelen = i / (*result)->ysize; |
342 | if ((res = psiconv_pixel_data_to_floats(config,lev+2,off+len,pixels, |
290 | datanr=0; |
343 | &floats,bits_per_pixel,color, |
291 | psiconv_debug(lev+2,off+len,"Linelen: %04x bytes",linelen); |
344 | redbits,greenbits,bluebits,palet))) |
292 | while((datanr < datasize)) { |
|
|
293 | marker = psiconv_read_u8(buf,lev+3,off+len+datanr,&res); |
|
|
294 | if (res) |
|
|
295 | goto ERROR5; |
|
|
296 | psiconv_debug(lev+3,off+len+datanr, |
|
|
297 | "Pixelnr %08x, Datanr %08x: Read marker %02x", |
|
|
298 | pixelnr,datanr,marker); |
|
|
299 | datanr ++; |
|
|
300 | if (marker >= 0x80) { |
|
|
301 | /* 0x100 - marker bytes of data follow */ |
|
|
302 | for (i = 0; i < 0x100-marker; i++,datanr++) { |
|
|
303 | if (datanr >= datasize) { |
|
|
304 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
305 | psiconv_debug(lev+3,off+len+datanr, |
|
|
306 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
307 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
308 | datanr,marker); |
|
|
309 | res = -PSICONV_E_PARSE; |
|
|
310 | break; |
|
|
311 | } |
|
|
312 | temp = psiconv_read_u8(buf,lev+2,off+len+datanr,&res); |
|
|
313 | if (res) |
|
|
314 | goto ERROR5; |
|
|
315 | if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp, |
|
|
316 | bits_per_pixel,linelen,&linepos,picsize)) { |
|
|
317 | res = -PSICONV_E_PARSE; |
|
|
318 | break; |
|
|
319 | } |
|
|
320 | } |
|
|
321 | } else { |
|
|
322 | if (datanr >= datasize) { |
|
|
323 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
324 | psiconv_debug(lev+3,off+len+datanr, |
|
|
325 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
326 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
327 | datanr,marker); |
|
|
328 | res = -PSICONV_E_PARSE; |
|
|
329 | } else { |
|
|
330 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr,&res); |
|
|
331 | if (res) |
|
|
332 | goto ERROR5; |
|
|
333 | for (i = 0; i <= marker; i++) { |
|
|
334 | if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp, |
|
|
335 | bits_per_pixel,linelen,&linepos,picsize)) { |
|
|
336 | res = -PSICONV_E_PARSE; |
|
|
337 | break; |
|
|
338 | } |
|
|
339 | } |
|
|
340 | datanr ++; |
|
|
341 | } |
|
|
342 | } |
|
|
343 | } |
|
|
344 | } |
|
|
345 | |
|
|
346 | if (linepos >= ((*result)->xsize + (8/bits_per_pixel) - 1) / |
|
|
347 | (8/bits_per_pixel)) |
|
|
348 | datanr += (linelen - linepos); |
|
|
349 | |
|
|
350 | if (res || (datanr != datasize) || (pixelnr != picsize)) { |
|
|
351 | psiconv_warn(lev+2,off+len,"Corrupted picture data!"); |
|
|
352 | psiconv_debug(lev+3,off+len+datanr, |
|
|
353 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
354 | "Datanr: %08x",picsize,datasize,pixelnr,datanr); |
|
|
355 | goto ERROR5; |
345 | goto ERROR4; |
356 | } |
346 | |
|
|
347 | (*result)->red = floats.red; |
|
|
348 | (*result)->green = floats.green; |
|
|
349 | (*result)->blue = floats.blue; |
357 | |
350 | |
358 | len += datanr; |
351 | psiconv_list_free(bytes); |
|
|
352 | psiconv_list_free(pixels); |
|
|
353 | |
359 | |
354 | |
360 | if (length) |
355 | if (length) |
361 | *length = len; |
356 | *length = len; |
362 | |
357 | |
363 | psiconv_progress(lev+1,off+len-1,"End of paint data section " |
358 | psiconv_progress(config,lev,off+len-1, |
364 | "(total length: %08x)", len); |
359 | "End of Paint Data Section (total length: %08x)", len); |
365 | |
360 | |
366 | return res; |
361 | return 0; |
367 | |
362 | |
368 | ERROR5: |
|
|
369 | free((*result)->blue); |
|
|
370 | ERROR4: |
363 | ERROR4: |
371 | free((*result)->green); |
364 | psiconv_list_free(pixels); |
372 | ERROR3: |
365 | ERROR3: |
373 | free((*result)->red); |
366 | psiconv_list_free(bytes); |
374 | ERROR2: |
367 | ERROR2: |
375 | free (*result); |
368 | free(*result); |
376 | ERROR1: |
369 | ERROR1: |
377 | psiconv_warn(lev+1,off,"Reading of Paint Data Section failed"); |
370 | psiconv_error(config,lev+1,off,"Reading of Paint Data Section failed"); |
378 | if (length) |
371 | if (length) |
379 | *length = 0; |
372 | *length = 0; |
380 | if (!res) |
373 | if (!res) |
381 | return -PSICONV_E_NOMEM; |
374 | return -PSICONV_E_NOMEM; |
382 | else |
375 | else |
383 | return res; |
376 | return res; |
384 | } |
377 | } |
385 | |
378 | |
386 | int psiconv_parse_sketch_section(const psiconv_buffer buf, int lev, |
379 | int psiconv_parse_sketch_section(const psiconv_config config, |
|
|
380 | const psiconv_buffer buf, int lev, |
387 | psiconv_u32 off, int *length, int is_object, |
381 | psiconv_u32 off, int *length, |
388 | psiconv_sketch_section *result) |
382 | psiconv_sketch_section *result) |
389 | { |
383 | { |
390 | int res=0; |
384 | int res=0; |
391 | int len=0; |
385 | int len=0; |
392 | psiconv_u32 temp; |
386 | psiconv_u32 temp; |
393 | int leng; |
387 | int leng; |
394 | int i; |
|
|
395 | |
388 | |
396 | psiconv_progress(lev+1,off,"Going to read the sketch section"); |
389 | psiconv_progress(config,lev+1,off,"Going to read the sketch section"); |
397 | if (!(*result = malloc(sizeof(**result)))) |
390 | if (!(*result = malloc(sizeof(**result)))) |
398 | goto ERROR1; |
391 | goto ERROR1; |
399 | |
392 | |
400 | if (!is_object) { |
|
|
401 | psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); |
393 | psiconv_progress(config,lev+2,off+len,"Going to read the displayed hor. size"); |
402 | (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); |
394 | (*result)->displayed_xsize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
403 | if (res) |
395 | if (res) |
404 | goto ERROR2; |
396 | goto ERROR2; |
|
|
397 | psiconv_debug(config,lev+2,off+len,"Displayed hor. size: %04x", |
|
|
398 | (*result)->displayed_xsize); |
|
|
399 | len += 0x02; |
|
|
400 | psiconv_progress(config,lev+2,off+len,"Going to read displayed ver. size"); |
|
|
401 | (*result)->displayed_ysize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
|
|
402 | if (res) |
|
|
403 | goto ERROR2; |
|
|
404 | psiconv_debug(config,lev+2,off+len,"Displayed ver. size: %04x", |
|
|
405 | (*result)->displayed_ysize); |
|
|
406 | len += 0x02; |
|
|
407 | |
|
|
408 | psiconv_progress(config,lev+2,off+len,"Going to read the data hor. offset"); |
|
|
409 | (*result)->picture_data_x_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
|
|
410 | &res); |
|
|
411 | if (res) |
|
|
412 | goto ERROR2; |
|
|
413 | psiconv_debug(config,lev+2,off+len,"Data hor. offset: %04x", |
|
|
414 | (*result)->picture_data_x_offset); |
|
|
415 | len += 0x02; |
|
|
416 | psiconv_progress(config,lev+2,off+len,"Going to read the data ver. offset"); |
|
|
417 | (*result)->picture_data_y_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
|
|
418 | &res); |
|
|
419 | if (res) |
|
|
420 | goto ERROR2; |
|
|
421 | psiconv_debug(config,lev+2,off+len,"Data ver. offset: %04x", |
|
|
422 | (*result)->picture_data_y_offset); |
|
|
423 | len += 0x02; |
|
|
424 | |
|
|
425 | psiconv_progress(config,lev+2,off+len,"Going to read the displayed hor. offset"); |
|
|
426 | (*result)->displayed_size_x_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
|
|
427 | &res); |
|
|
428 | if (res) |
|
|
429 | goto ERROR2; |
|
|
430 | psiconv_debug(config,lev+2,off+len,"Displayed hor. offset: %04x", |
|
|
431 | (*result)->displayed_size_x_offset); |
|
|
432 | len += 0x02; |
|
|
433 | psiconv_progress(config,lev+2,off+len,"Going to read the displayed ver. offset"); |
|
|
434 | (*result)->displayed_size_y_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
|
|
435 | &res); |
|
|
436 | if (res) |
|
|
437 | goto ERROR2; |
|
|
438 | psiconv_debug(config,lev+2,off+len,"Displayed ver. offset: %04x", |
|
|
439 | (*result)->displayed_size_y_offset); |
|
|
440 | len += 0x02; |
|
|
441 | |
|
|
442 | psiconv_progress(config,lev+2,off+len,"Going to read the form hor. size"); |
|
|
443 | (*result)->form_xsize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
|
|
444 | if (res) |
|
|
445 | goto ERROR2; |
405 | psiconv_debug(lev+2,off+len,"Form hor. size: %04x", |
446 | psiconv_debug(config,lev+2,off+len,"Form hor. size: %04x", |
406 | (*result)->form_xsize); |
447 | (*result)->form_xsize); |
407 | len += 0x02; |
448 | len += 0x02; |
408 | psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); |
449 | psiconv_progress(config,lev+2,off+len,"Going to read form ver. size"); |
409 | (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); |
450 | (*result)->form_ysize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
410 | if (res) |
451 | if (res) |
411 | goto ERROR2; |
452 | goto ERROR2; |
412 | psiconv_debug(lev+2,off+len,"Form ver. size: %04x", |
453 | psiconv_debug(config,lev+2,off+len,"Form ver. size: %04x", |
413 | (*result)->form_ysize); |
454 | (*result)->form_ysize); |
414 | len += 0x02; |
455 | len += 0x02; |
415 | psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); |
456 | |
416 | (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
457 | psiconv_progress(config,lev+2,off+len,"Going to skip 1 word of zeros"); |
|
|
458 | temp = psiconv_read_u16(config,buf,lev+2,off+len,&res); |
417 | if (res) |
459 | if (res) |
418 | goto ERROR2; |
460 | goto ERROR2; |
419 | psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x", |
|
|
420 | (*result)->picture_x_offset); |
|
|
421 | len += 0x02; |
|
|
422 | psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset"); |
|
|
423 | (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
424 | if (res) |
|
|
425 | goto ERROR2; |
|
|
426 | psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x", |
|
|
427 | (*result)->picture_y_offset); |
|
|
428 | len += 0x02; |
|
|
429 | psiconv_progress(lev+2,off+len,"Going to skip 5 words of zeros"); |
|
|
430 | for (i = 0; i < 5; i++) { |
|
|
431 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
432 | if (res) |
|
|
433 | goto ERROR2; |
|
|
434 | if (temp != 0) { |
|
|
435 | psiconv_warn(lev+2,off+len, |
|
|
436 | "Unexpected value in sketch section preamble (ignored)"); |
|
|
437 | psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i, |
|
|
438 | temp,0); |
|
|
439 | } |
|
|
440 | off += 0x02; |
|
|
441 | } |
|
|
442 | } else { |
|
|
443 | psiconv_progress(lev+2,off+len,"Going to read the displayed hor. size"); |
|
|
444 | (*result)->picture_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
445 | if (res) |
|
|
446 | goto ERROR2; |
|
|
447 | psiconv_debug(lev+2,off+len,"Displayed hor. size: %04x", |
|
|
448 | (*result)->picture_xsize); |
|
|
449 | len += 0x02; |
|
|
450 | psiconv_progress(lev+2,off+len,"Going to read the displayed ver. size"); |
|
|
451 | (*result)->picture_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
452 | if (res) |
|
|
453 | goto ERROR2; |
|
|
454 | psiconv_debug(lev+2,off+len,"Displayed ver. size: %04x", |
|
|
455 | (*result)->picture_ysize); |
|
|
456 | len += 0x02; |
|
|
457 | |
|
|
458 | psiconv_progress(lev+2,off+len,"Going to skip 2 words of zeros"); |
|
|
459 | for (i = 0; i < 2; i++) { |
|
|
460 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
461 | if (res) |
|
|
462 | goto ERROR2; |
|
|
463 | if (temp != 0) { |
|
|
464 | psiconv_warn(lev+2,off+len, |
|
|
465 | "Unexpected value in sketch section preamble (ignored)"); |
|
|
466 | psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i, |
|
|
467 | temp,0); |
|
|
468 | } |
|
|
469 | off += 0x02; |
|
|
470 | } |
|
|
471 | psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); |
|
|
472 | (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
473 | if (res) |
|
|
474 | goto ERROR2; |
|
|
475 | psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x", |
|
|
476 | (*result)->picture_x_offset); |
|
|
477 | len += 0x02; |
|
|
478 | psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset"); |
|
|
479 | (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
480 | if (res) |
|
|
481 | goto ERROR2; |
|
|
482 | psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x", |
|
|
483 | (*result)->picture_y_offset); |
|
|
484 | len += 0x02; |
|
|
485 | psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); |
|
|
486 | (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
487 | if (res) |
|
|
488 | goto ERROR2; |
|
|
489 | psiconv_debug(lev+2,off+len,"Form hor. size: %04x", |
|
|
490 | (*result)->form_xsize); |
|
|
491 | len += 0x02; |
|
|
492 | psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); |
|
|
493 | (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
494 | if (res) |
|
|
495 | goto ERROR2; |
|
|
496 | psiconv_debug(lev+2,off+len,"Form ver. size: %04x", |
|
|
497 | (*result)->form_ysize); |
|
|
498 | len += 0x02; |
|
|
499 | psiconv_progress(lev+2,off+len,"Going to skip 1 zero word"); |
|
|
500 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
501 | if (res) |
|
|
502 | goto ERROR2; |
|
|
503 | if (temp != 0) { |
461 | if (temp != 0) { |
504 | psiconv_warn(lev+2,off+len, |
462 | psiconv_warn(config,lev+2,off+len, |
505 | "Unexpected value in sketch section preamble (ignored)"); |
463 | "Unexpected value in sketch section preamble (ignored)"); |
506 | psiconv_debug(lev+2,off+len,"Read %04x, expected %04x",i, temp,0); |
464 | psiconv_debug(config,lev+2,off+len,"Read %04x, expected %04x", |
|
|
465 | temp,0); |
507 | } |
466 | } |
508 | off += 0x02; |
467 | off += 0x02; |
509 | } |
|
|
510 | |
468 | |
511 | psiconv_progress(lev+2,off+len,"Going to read the picture data"); |
469 | psiconv_progress(config,lev+2,off+len,"Going to read the picture data"); |
512 | if ((res = psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,0, |
470 | if ((res = psiconv_parse_paint_data_section(config,buf,lev+2,off+len,&leng,0, |
513 | &((*result)->picture)))) |
471 | &((*result)->picture)))) |
514 | goto ERROR2; |
472 | goto ERROR2; |
515 | off += leng; |
473 | off += leng; |
516 | if (!is_object) { |
|
|
517 | (*result)->picture_xsize = (*result)->picture->xsize; |
|
|
518 | (*result)->picture_ysize = (*result)->picture->ysize; |
|
|
519 | } |
|
|
520 | |
474 | |
521 | psiconv_progress(lev+2,off+len,"Going to read the hor. magnification"); |
475 | psiconv_progress(config,lev+2,off+len,"Going to read the hor. magnification"); |
522 | (*result)->magnification_x = psiconv_read_u16(buf,lev+2,off+len,&res)/1000.0; |
476 | (*result)->magnification_x = psiconv_read_u16(config,buf,lev+2,off+len,&res)/1000.0; |
523 | if (res) |
477 | if (res) |
524 | goto ERROR3; |
478 | goto ERROR3; |
525 | psiconv_debug(lev+2,off+len,"Form hor. magnification: %f", |
479 | psiconv_debug(config,lev+2,off+len,"Form hor. magnification: %f", |
526 | (*result)->magnification_x); |
480 | (*result)->magnification_x); |
527 | len += 0x02; |
481 | len += 0x02; |
528 | psiconv_progress(lev+2,off+len,"Going to read the ver. magnification"); |
482 | psiconv_progress(config,lev+2,off+len,"Going to read the ver. magnification"); |
529 | (*result)->magnification_y = psiconv_read_u16(buf,lev+2,off+len,&res)/1000.0; |
483 | (*result)->magnification_y = psiconv_read_u16(config,buf,lev+2,off+len,&res)/1000.0; |
530 | if (res) |
484 | if (res) |
531 | goto ERROR3; |
485 | goto ERROR3; |
532 | psiconv_debug(lev+2,off+len,"Form ver. magnification: %f", |
486 | psiconv_debug(config,lev+2,off+len,"Form ver. magnification: %f", |
533 | (*result)->magnification_y); |
487 | (*result)->magnification_y); |
534 | len += 0x02; |
488 | len += 0x02; |
535 | |
489 | |
536 | psiconv_progress(lev+2,off+len,"Going to read the left cut"); |
490 | psiconv_progress(config,lev+2,off+len,"Going to read the left cut"); |
537 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
491 | temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
538 | if (res) |
492 | if (res) |
539 | goto ERROR3; |
493 | goto ERROR3; |
540 | (*result)->cut_left = (temp * 6.0) / (*result)->picture_xsize; |
494 | (*result)->cut_left = (temp * 6.0) / (*result)->displayed_xsize; |
541 | psiconv_debug(lev+2,off+len,"Left cut: raw %08x, real: %f", |
495 | psiconv_debug(config,lev+2,off+len,"Left cut: raw %08x, real: %f", |
542 | temp,(*result)->cut_left); |
496 | temp,(*result)->cut_left); |
543 | len += 0x04; |
497 | len += 0x04; |
544 | psiconv_progress(lev+2,off+len,"Going to read the right cut"); |
498 | psiconv_progress(config,lev+2,off+len,"Going to read the right cut"); |
545 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
499 | temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
546 | if (res) |
500 | if (res) |
547 | goto ERROR3; |
501 | goto ERROR3; |
548 | (*result)->cut_right = (temp * 6.0) / (*result)->picture_xsize; |
502 | (*result)->cut_right = (temp * 6.0) / (*result)->displayed_xsize; |
549 | psiconv_debug(lev+2,off+len,"Right cut: raw %08x, real: %f", |
503 | psiconv_debug(config,lev+2,off+len,"Right cut: raw %08x, real: %f", |
550 | temp,(*result)->cut_right); |
504 | temp,(*result)->cut_right); |
551 | len += 0x04; |
505 | len += 0x04; |
552 | psiconv_progress(lev+2,off+len,"Going to read the top cut"); |
506 | psiconv_progress(config,lev+2,off+len,"Going to read the top cut"); |
553 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
507 | temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
554 | if (res) |
508 | if (res) |
555 | goto ERROR3; |
509 | goto ERROR3; |
556 | (*result)->cut_top = (temp * 6.0) / (*result)->picture_ysize; |
510 | (*result)->cut_top = (temp * 6.0) / (*result)->displayed_ysize; |
557 | psiconv_debug(lev+2,off+len,"Top cut: raw %08x, real: %f", |
511 | psiconv_debug(config,lev+2,off+len,"Top cut: raw %08x, real: %f", |
558 | temp,(*result)->cut_top); |
512 | temp,(*result)->cut_top); |
559 | len += 0x04; |
513 | len += 0x04; |
560 | psiconv_progress(lev+2,off+len,"Going to read the bottom cut"); |
514 | psiconv_progress(config,lev+2,off+len,"Going to read the bottom cut"); |
561 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
515 | temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
562 | if (res) |
516 | if (res) |
563 | goto ERROR3; |
517 | goto ERROR3; |
564 | (*result)->cut_bottom = (temp * 6.0) / (*result)->picture_ysize; |
518 | (*result)->cut_bottom = (temp * 6.0) / (*result)->displayed_ysize; |
565 | psiconv_debug(lev+2,off+len,"Bottom cut: raw %08x, real: %f", |
519 | psiconv_debug(config,lev+2,off+len,"Bottom cut: raw %08x, real: %f", |
566 | temp,(*result)->cut_bottom); |
520 | temp,(*result)->cut_bottom); |
567 | len += 0x04; |
521 | len += 0x04; |
568 | |
522 | |
569 | if (length) |
523 | if (length) |
570 | *length = len; |
524 | *length = len; |
571 | |
525 | |
572 | psiconv_progress(lev,off+len-1, |
526 | psiconv_progress(config,lev,off+len-1, |
573 | "End of sketch section (total length: %08x)", len); |
527 | "End of sketch section (total length: %08x)", len); |
574 | |
528 | |
575 | return res; |
529 | return res; |
576 | ERROR3: |
530 | ERROR3: |
577 | psiconv_free_paint_data_section((*result)->picture); |
531 | psiconv_free_paint_data_section((*result)->picture); |
578 | ERROR2: |
532 | ERROR2: |
579 | free (*result); |
533 | free (*result); |
580 | ERROR1: |
534 | ERROR1: |
581 | psiconv_warn(lev+1,off,"Reading of Sketch Section failed"); |
535 | psiconv_error(config,lev+1,off,"Reading of Sketch Section failed"); |
582 | if (length) |
536 | if (length) |
583 | *length = 0; |
537 | *length = 0; |
584 | if (!res) |
538 | if (!res) |
585 | return -PSICONV_E_NOMEM; |
539 | return -PSICONV_E_NOMEM; |
586 | else |
540 | else |
587 | return res; |
541 | return res; |
588 | } |
542 | } |
589 | |
543 | |
590 | |
544 | |
591 | int psiconv_parse_clipart_section(const psiconv_buffer buf,int lev, |
545 | int psiconv_parse_clipart_section(const psiconv_config config, |
|
|
546 | const psiconv_buffer buf,int lev, |
592 | psiconv_u32 off, int *length, |
547 | psiconv_u32 off, int *length, |
593 | psiconv_clipart_section *result) |
548 | psiconv_clipart_section *result) |
594 | { |
549 | { |
595 | int res=0; |
550 | int res=0; |
596 | int len=0; |
551 | int len=0; |
597 | int leng; |
552 | int leng; |
598 | psiconv_u32 temp; |
553 | psiconv_u32 temp; |
599 | |
554 | |
600 | psiconv_progress(lev+1,off+len,"Going to read the clipart section"); |
555 | psiconv_progress(config,lev+1,off+len,"Going to read the clipart section"); |
601 | if (!(*result = malloc(sizeof(**result)))) |
556 | if (!(*result = malloc(sizeof(**result)))) |
602 | goto ERROR1; |
557 | goto ERROR1; |
603 | |
558 | |
604 | psiconv_progress(lev+2,off+len,"Going to read the section ID"); |
559 | psiconv_progress(config,lev+2,off+len,"Going to read the section ID"); |
605 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
560 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
606 | if (res) |
561 | if (res) |
607 | goto ERROR2; |
562 | goto ERROR2; |
608 | if (temp != PSICONV_ID_CLIPART_ITEM) { |
563 | if (temp != PSICONV_ID_CLIPART_ITEM) { |
609 | psiconv_warn(lev+2,off+len, |
564 | psiconv_warn(config,lev+2,off+len, |
610 | "Unexpected value in clipart section preamble (ignored)"); |
565 | "Unexpected value in clipart section preamble (ignored)"); |
611 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, |
566 | psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, |
612 | PSICONV_ID_CLIPART_ITEM); |
567 | PSICONV_ID_CLIPART_ITEM); |
613 | } else |
568 | } else |
614 | psiconv_debug(lev+2,off+len,"Clipart ID: %08x", temp); |
569 | psiconv_debug(config,lev+2,off+len,"Clipart ID: %08x", temp); |
615 | off += 4; |
570 | off += 4; |
616 | |
571 | |
617 | psiconv_progress(lev+2,off+len,"Going to read an unknown long"); |
572 | psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); |
618 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
573 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
619 | if (res) |
574 | if (res) |
620 | goto ERROR2; |
575 | goto ERROR2; |
621 | if (temp != 0x02) { |
576 | if (temp != 0x02) { |
622 | psiconv_warn(lev+2,off+len, |
577 | psiconv_warn(config,lev+2,off+len, |
623 | "Unexpected value in clipart section preamble (ignored)"); |
578 | "Unexpected value in clipart section preamble (ignored)"); |
624 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, |
579 | psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, |
625 | 0x02); |
580 | 0x02); |
626 | } else |
581 | } else |
627 | psiconv_debug(lev+2,off+len,"First unknown long: %08x", temp); |
582 | psiconv_debug(config,lev+2,off+len,"First unknown long: %08x", temp); |
628 | off += 4; |
583 | off += 4; |
629 | |
584 | |
630 | psiconv_progress(lev+2,off+len,"Going to read a second unknown long"); |
585 | psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long"); |
631 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
586 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
632 | if (res) |
587 | if (res) |
633 | goto ERROR2; |
588 | goto ERROR2; |
634 | if (temp != 0) { |
589 | if (temp != 0) { |
635 | psiconv_warn(lev+2,off+len, |
590 | psiconv_warn(config,lev+2,off+len, |
636 | "Unexpected value in clipart section preamble (ignored)"); |
591 | "Unexpected value in clipart section preamble (ignored)"); |
637 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
592 | psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
638 | } else |
593 | } else |
639 | psiconv_debug(lev+2,off+len,"Second unknown long: %08x", temp); |
594 | psiconv_debug(config,lev+2,off+len,"Second unknown long: %08x", temp); |
640 | off += 4; |
595 | off += 4; |
641 | |
596 | |
642 | psiconv_progress(lev+2,off+len,"Going to read a third unknown long"); |
597 | psiconv_progress(config,lev+2,off+len,"Going to read a third unknown long"); |
643 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
598 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
644 | if (res) |
599 | if (res) |
645 | goto ERROR2; |
600 | goto ERROR2; |
646 | if (temp != 0) { |
601 | if (temp != 0) { |
647 | psiconv_warn(lev+2,off+len, |
602 | psiconv_warn(config,lev+2,off+len, |
648 | "Unexpected value in clipart section preamble (ignored)"); |
603 | "Unexpected value in clipart section preamble (ignored)"); |
649 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
604 | psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
650 | } else |
605 | } else |
651 | psiconv_debug(lev+2,off+len,"Third unknown long: %08x", temp); |
606 | psiconv_debug(config,lev+2,off+len,"Third unknown long: %08x", temp); |
652 | off += 4; |
607 | off += 4; |
653 | |
608 | |
654 | psiconv_progress(lev+2,off+len,"Going to read a fourth unknown long"); |
609 | psiconv_progress(config,lev+2,off+len,"Going to read a fourth unknown long"); |
655 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
610 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
656 | if (res) |
611 | if (res) |
657 | goto ERROR2; |
612 | goto ERROR2; |
658 | if ((temp != 0x0c) && (temp != 0x08)) { |
613 | if ((temp != 0x0c) && (temp != 0x08)) { |
659 | psiconv_warn(lev+2,off+len, |
614 | psiconv_warn(config,lev+2,off+len, |
660 | "Unexpected value in clipart section preamble (ignored)"); |
615 | "Unexpected value in clipart section preamble (ignored)"); |
661 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x or %08x",temp, |
616 | psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x or %08x",temp, |
662 | 0x0c, 0x08); |
617 | 0x0c, 0x08); |
663 | } else |
618 | } else |
664 | psiconv_debug(lev+2,off+len,"Fourth unknown long: %08x", temp); |
619 | psiconv_debug(config,lev+2,off+len,"Fourth unknown long: %08x", temp); |
665 | off += 4; |
620 | off += 4; |
666 | |
621 | |
667 | psiconv_progress(lev+2,off+len,"Going to read the Paint Data Section"); |
622 | psiconv_progress(config,lev+2,off+len,"Going to read the Paint Data Section"); |
668 | if ((res = psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,1, |
623 | if ((res = psiconv_parse_paint_data_section(config,buf,lev+2,off+len,&leng,1, |
669 | &((*result)->picture)))) |
624 | &((*result)->picture)))) |
670 | goto ERROR2; |
625 | goto ERROR2; |
671 | len += leng; |
626 | len += leng; |
672 | |
627 | |
673 | if (length) |
628 | if (length) |
674 | *length = len; |
629 | *length = len; |
675 | |
630 | |
676 | psiconv_progress(lev,off+len-1, |
631 | psiconv_progress(config,lev,off+len-1, |
677 | "End of clipart section (total length: %08x)", len); |
632 | "End of clipart section (total length: %08x)", len); |
678 | return 0; |
633 | return 0; |
679 | |
634 | |
680 | ERROR2: |
635 | ERROR2: |
681 | free (*result); |
636 | free (*result); |
682 | ERROR1: |
637 | ERROR1: |
683 | psiconv_warn(lev+1,off,"Reading of Font failed"); |
638 | psiconv_error(config,lev+1,off,"Reading of Font failed"); |
684 | if (length) |
639 | if (length) |
685 | *length = 0; |
640 | *length = 0; |
686 | if (!res) |
641 | if (!res) |
687 | return -PSICONV_E_NOMEM; |
642 | return -PSICONV_E_NOMEM; |
688 | else |
643 | else |
689 | return res; |
644 | return res; |
690 | } |
645 | } |
|
|
646 | |
|
|
647 | int psiconv_decode_rle8 (const psiconv_config config, int lev, psiconv_u32 off, |
|
|
648 | const psiconv_pixel_bytes encoded, |
|
|
649 | psiconv_pixel_bytes *decoded) |
|
|
650 | { |
|
|
651 | int res=0; |
|
|
652 | psiconv_u8 *marker,*value; |
|
|
653 | int i,j; |
|
|
654 | |
|
|
655 | psiconv_progress(config,lev+1,off,"Going to decode the RLE8 encoding"); |
|
|
656 | if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
657 | goto ERROR1; |
|
|
658 | |
|
|
659 | for (i = 0; i < psiconv_list_length(encoded);) { |
|
|
660 | #ifdef LOUD |
|
|
661 | psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i); |
|
|
662 | #endif |
|
|
663 | if (!(marker = psiconv_list_get(encoded,i))) |
|
|
664 | goto ERROR2; |
|
|
665 | #ifdef LOUD |
|
|
666 | psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker); |
|
|
667 | #endif |
|
|
668 | if (*marker < 0x80) { |
|
|
669 | #ifdef LOUD |
|
|
670 | psiconv_debug(config,lev+2,off,"Marker: repeat value byte %02x times", |
|
|
671 | *marker+1); */ |
|
|
672 | psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1); |
|
|
673 | #endif |
|
|
674 | if (!(value = psiconv_list_get(encoded,i+1))) |
|
|
675 | goto ERROR2; |
|
|
676 | #ifdef LOUD |
|
|
677 | psiconv_debug(config,lev+2,off,"Value byte: %02x",*value); |
|
|
678 | psiconv_progress(config,lev+2,off,"Adding %02x pixels %02x", |
|
|
679 | *marker+1,*value); |
|
|
680 | #endif |
|
|
681 | for (j = 0; j < *marker + 1; j++) |
|
|
682 | if ((res = psiconv_list_add(*decoded,value))) |
|
|
683 | goto ERROR2; |
|
|
684 | i += 2; |
|
|
685 | } else { |
|
|
686 | #ifdef LOUD |
|
|
687 | psiconv_debug(config,lev+2,off,"Marker: %02x value bytes follow", |
|
|
688 | 0x100 - *marker); |
|
|
689 | #endif |
|
|
690 | for (j = 0; j < (0x100 - *marker); j++) { |
|
|
691 | #ifdef LOUD |
|
|
692 | psiconv_progress(config,lev+2,off,"Going to read value byte at %04x", |
|
|
693 | i+j+1); |
|
|
694 | #endif |
|
|
695 | if (!(value = psiconv_list_get(encoded,i+j+1))) |
|
|
696 | goto ERROR2; |
|
|
697 | #ifdef LOUD |
|
|
698 | psiconv_debug(config,lev+2,off,"Value: %02x",*value); |
|
|
699 | #endif |
|
|
700 | if ((res = psiconv_list_add(*decoded,value))) |
|
|
701 | goto ERROR2; |
|
|
702 | } |
|
|
703 | i += (0x100 - *marker) + 1; |
|
|
704 | } |
|
|
705 | } |
|
|
706 | psiconv_progress(config,lev,off, |
|
|
707 | "End of RLE8 decoding process"); |
|
|
708 | return 0; |
|
|
709 | |
|
|
710 | ERROR2: |
|
|
711 | psiconv_list_free(*decoded); |
|
|
712 | ERROR1: |
|
|
713 | psiconv_error(config,lev+1,off,"Decoding of RLE8 failed"); |
|
|
714 | if (!res) |
|
|
715 | return -PSICONV_E_NOMEM; |
|
|
716 | else |
|
|
717 | return res; |
|
|
718 | } |
|
|
719 | |
|
|
720 | int psiconv_decode_rle12 (const psiconv_config config, int lev, psiconv_u32 off, |
|
|
721 | const psiconv_pixel_bytes encoded, |
|
|
722 | psiconv_pixel_bytes *decoded) |
|
|
723 | { |
|
|
724 | int res=0; |
|
|
725 | psiconv_u8 *value0,*value1; |
|
|
726 | psiconv_u32 value,repeat; |
|
|
727 | int i,j; |
|
|
728 | |
|
|
729 | psiconv_progress(config,lev+1,off,"Going to decode the RLE12 encoding"); |
|
|
730 | if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
731 | goto ERROR1; |
|
|
732 | |
|
|
733 | for (i = 0; i < psiconv_list_length(encoded);) { |
|
|
734 | psiconv_progress(config,lev+2,off,"Going to read data word at %04x",i); |
|
|
735 | if (!(value0 = psiconv_list_get(encoded,i))) |
|
|
736 | goto ERROR2; |
|
|
737 | if (!(value1 = psiconv_list_get(encoded,i+1))) |
|
|
738 | goto ERROR2; |
|
|
739 | psiconv_debug(config,lev+2,off,"Data Word: %04x",*value0 + (*value1 << 8)); |
|
|
740 | value = *value0 + ((*value1 & 0x0f) << 8); |
|
|
741 | repeat = (*value1 >> 4) + 1; |
|
|
742 | psiconv_progress(config,lev+2,off,"Adding %02x pixels %03x", |
|
|
743 | repeat,value); |
|
|
744 | for (j = 0; j < repeat; j ++) |
|
|
745 | if ((res = psiconv_list_add(*decoded,&value))) |
|
|
746 | goto ERROR2; |
|
|
747 | i += 2; |
|
|
748 | } |
|
|
749 | psiconv_progress(config,lev,off, |
|
|
750 | "End of RLE12 decoding process"); |
|
|
751 | return 0; |
|
|
752 | |
|
|
753 | ERROR2: |
|
|
754 | psiconv_list_free(*decoded); |
|
|
755 | ERROR1: |
|
|
756 | psiconv_error(config,lev+1,off,"Decoding of RLE12 failed"); |
|
|
757 | if (!res) |
|
|
758 | return -PSICONV_E_NOMEM; |
|
|
759 | else |
|
|
760 | return res; |
|
|
761 | } |
|
|
762 | |
|
|
763 | int psiconv_decode_rle16 (const psiconv_config config, int lev, psiconv_u32 off, |
|
|
764 | const psiconv_pixel_bytes encoded, |
|
|
765 | psiconv_pixel_bytes *decoded) |
|
|
766 | { |
|
|
767 | int res=0; |
|
|
768 | psiconv_u8 *marker,*value0,*value1; |
|
|
769 | psiconv_u32 value; |
|
|
770 | int i,j; |
|
|
771 | |
|
|
772 | psiconv_progress(config,lev+1,off,"Going to decode the RLE16 encoding"); |
|
|
773 | if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
774 | goto ERROR1; |
|
|
775 | |
|
|
776 | for (i = 0; i < psiconv_list_length(encoded);) { |
|
|
777 | psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i); |
|
|
778 | if (!(marker = psiconv_list_get(encoded,i))) |
|
|
779 | goto ERROR2; |
|
|
780 | psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker); |
|
|
781 | if (*marker < 0x80) { |
|
|
782 | psiconv_debug(config,lev+2,off,"Marker: repeat value word %02x times", |
|
|
783 | *marker+1); |
|
|
784 | psiconv_progress(config,lev+2,off,"Going to read value word at %04x",i+1); |
|
|
785 | if (!(value0 = psiconv_list_get(encoded,i+1))) |
|
|
786 | goto ERROR2; |
|
|
787 | if (!(value1 = psiconv_list_get(encoded,i+2))) |
|
|
788 | goto ERROR2; |
|
|
789 | value = *value0 + (*value1 << 8); |
|
|
790 | psiconv_debug(config,lev+2,off,"Value word: %02x",value); |
|
|
791 | psiconv_progress(config,lev+2,off,"Adding %02x pixels %04x", |
|
|
792 | *marker+1,value); |
|
|
793 | for (j = 0; j < *marker + 1; j++) |
|
|
794 | if ((res = psiconv_list_add(*decoded,&value))) |
|
|
795 | goto ERROR2; |
|
|
796 | i += 3; |
|
|
797 | } else { |
|
|
798 | psiconv_debug(config,lev+2,off,"Marker: %02x value words follow", |
|
|
799 | 0x100 - *marker); |
|
|
800 | for (j = 0; j < (0x100 - *marker); j++) { |
|
|
801 | psiconv_progress(config,lev+2,off,"Going to read value word at %04x", |
|
|
802 | i+j*2+1); |
|
|
803 | if (!(value0 = psiconv_list_get(encoded,i+j*2+1))) |
|
|
804 | goto ERROR2; |
|
|
805 | if (!(value1 = psiconv_list_get(encoded,i+j*2+2))) |
|
|
806 | goto ERROR2; |
|
|
807 | value = *value0 + (*value1 << 8); |
|
|
808 | psiconv_debug(config,lev+2,off,"Value: %04x",value); |
|
|
809 | if ((res = psiconv_list_add(*decoded,&value))) |
|
|
810 | goto ERROR2; |
|
|
811 | } |
|
|
812 | i += (0x100 - *marker)*2 + 1; |
|
|
813 | } |
|
|
814 | } |
|
|
815 | psiconv_progress(config,lev,off, |
|
|
816 | "End of RLE16 decoding process"); |
|
|
817 | return 0; |
|
|
818 | |
|
|
819 | ERROR2: |
|
|
820 | psiconv_list_free(*decoded); |
|
|
821 | ERROR1: |
|
|
822 | psiconv_error(config,lev+1,off,"Decoding of RLE16 failed"); |
|
|
823 | if (!res) |
|
|
824 | return -PSICONV_E_NOMEM; |
|
|
825 | else |
|
|
826 | return res; |
|
|
827 | } |
|
|
828 | |
|
|
829 | int psiconv_decode_rle24 (const psiconv_config config, int lev, psiconv_u32 off, |
|
|
830 | const psiconv_pixel_bytes encoded, |
|
|
831 | psiconv_pixel_bytes *decoded) |
|
|
832 | { |
|
|
833 | int res=0; |
|
|
834 | psiconv_u8 *marker,*value0,*value1,*value2; |
|
|
835 | psiconv_u32 value; |
|
|
836 | int i,j; |
|
|
837 | |
|
|
838 | psiconv_progress(config,lev+1,off,"Going to decode the RLE24 encoding"); |
|
|
839 | if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
840 | goto ERROR1; |
|
|
841 | |
|
|
842 | for (i = 0; i < psiconv_list_length(encoded);) { |
|
|
843 | psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i); |
|
|
844 | if (!(marker = psiconv_list_get(encoded,i))) |
|
|
845 | goto ERROR2; |
|
|
846 | psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker); |
|
|
847 | if (*marker < 0x80) { |
|
|
848 | psiconv_debug(config,lev+2,off,"Marker: repeat value byte triplet %02x times", |
|
|
849 | *marker+1); |
|
|
850 | psiconv_progress(config,lev+2,off,"Going to read value byte triplet at %04x",i+1); |
|
|
851 | if (!(value0 = psiconv_list_get(encoded,i+1))) |
|
|
852 | goto ERROR2; |
|
|
853 | if (!(value1 = psiconv_list_get(encoded,i+2))) |
|
|
854 | goto ERROR2; |
|
|
855 | if (!(value2 = psiconv_list_get(encoded,i+3))) |
|
|
856 | goto ERROR2; |
|
|
857 | value = *value0 + (*value1 << 8) + (*value2 << 16); |
|
|
858 | psiconv_debug(config,lev+2,off,"Value byte triplet: %06x",value); |
|
|
859 | psiconv_progress(config,lev+2,off,"Adding %02x pixels %06x", |
|
|
860 | *marker+1,value); |
|
|
861 | for (j = 0; j < *marker + 1; j++) |
|
|
862 | if ((res = psiconv_list_add(*decoded,&value))) |
|
|
863 | goto ERROR2; |
|
|
864 | i += 4; |
|
|
865 | } else { |
|
|
866 | psiconv_debug(config,lev+2,off,"Marker: %02x value byte triplets follow", |
|
|
867 | 0x100 - *marker); |
|
|
868 | for (j = 0; j < (0x100 - *marker); j++) { |
|
|
869 | psiconv_progress(config,lev+2,off,"Going to read value byte triplets at %04x", |
|
|
870 | i+j*3+1); |
|
|
871 | if (!(value0 = psiconv_list_get(encoded,i+j*3+1))) |
|
|
872 | goto ERROR2; |
|
|
873 | if (!(value1 = psiconv_list_get(encoded,i+j*3+2))) |
|
|
874 | goto ERROR2; |
|
|
875 | if (!(value2 = psiconv_list_get(encoded,i+j*3+3))) |
|
|
876 | goto ERROR2; |
|
|
877 | value = *value0 + (*value1 << 8) + (*value2 << 16); |
|
|
878 | psiconv_debug(config,lev+2,off,"Value: %06x",value); |
|
|
879 | if ((res = psiconv_list_add(*decoded,&value))) |
|
|
880 | goto ERROR2; |
|
|
881 | } |
|
|
882 | i += (0x100 - *marker)*3 + 1; |
|
|
883 | } |
|
|
884 | } |
|
|
885 | psiconv_progress(config,lev,off, |
|
|
886 | "End of RLE24 decoding process"); |
|
|
887 | return 0; |
|
|
888 | |
|
|
889 | ERROR2: |
|
|
890 | psiconv_list_free(*decoded); |
|
|
891 | ERROR1: |
|
|
892 | psiconv_error(config,lev+1,off,"Decoding of RLE24 failed"); |
|
|
893 | if (!res) |
|
|
894 | return -PSICONV_E_NOMEM; |
|
|
895 | else |
|
|
896 | return res; |
|
|
897 | } |
|
|
898 | |
|
|
899 | int psiconv_bytes_to_pixel_data(const psiconv_config config, |
|
|
900 | int lev, psiconv_u32 off, |
|
|
901 | const psiconv_pixel_bytes bytes, |
|
|
902 | psiconv_pixel_ints *pixels, |
|
|
903 | int colordepth, int xsize, int ysize) |
|
|
904 | { |
|
|
905 | int res=0; |
|
|
906 | int ibits,obits,x,y,bits; |
|
|
907 | psiconv_u8 input; |
|
|
908 | psiconv_u32 nr,output; |
|
|
909 | psiconv_u8 *ientry; |
|
|
910 | |
|
|
911 | psiconv_progress(config,lev+1,off,"Going to convert the bytes to pixels"); |
|
|
912 | if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) |
|
|
913 | goto ERROR1; |
|
|
914 | |
|
|
915 | nr = 0; |
|
|
916 | for (y = 0; y < ysize; y++) { |
|
|
917 | /* New lines will start at longs */ |
|
|
918 | while (nr % 4) |
|
|
919 | nr ++; |
|
|
920 | input = 0; |
|
|
921 | ibits = 0; |
|
|
922 | for (x= 0; x < xsize; x++) { |
|
|
923 | #ifdef LOUD |
|
|
924 | psiconv_progress(config,lev+2,off, |
|
|
925 | "Processing pixel at (x,y) = (%04x,%04x)",x,y); |
|
|
926 | #endif |
|
|
927 | output = 0; |
|
|
928 | obits = 0; |
|
|
929 | while (obits < colordepth) { |
|
|
930 | if (ibits == 0) { |
|
|
931 | #ifdef LOUD |
|
|
932 | psiconv_progress(config,lev+3,off, |
|
|
933 | "Going to read byte %08x",nr); |
|
|
934 | #endif |
|
|
935 | if (!(ientry = psiconv_list_get(bytes,nr))) |
|
|
936 | goto ERROR2; |
|
|
937 | #ifdef LOUD |
|
|
938 | psiconv_debug(config,lev+3,off,"Byte value: %02x",*ientry); |
|
|
939 | #endif |
|
|
940 | input = *ientry; |
|
|
941 | ibits = 8; |
|
|
942 | nr ++; |
|
|
943 | } |
|
|
944 | bits = ibits + obits > colordepth?colordepth-obits:ibits; |
|
|
945 | output = output << bits; |
|
|
946 | output |= input & ((1 << bits) - 1); |
|
|
947 | input = input >> bits; |
|
|
948 | ibits -= bits; |
|
|
949 | obits += bits; |
|
|
950 | } |
|
|
951 | #ifdef LOUD |
|
|
952 | psiconv_debug(config,lev+2,off,"Pixel value: %08x",output); |
|
|
953 | #endif |
|
|
954 | if ((res = psiconv_list_add(*pixels,&output))) |
|
|
955 | goto ERROR2; |
|
|
956 | } |
|
|
957 | } |
|
|
958 | |
|
|
959 | psiconv_progress(config,lev,off, |
|
|
960 | "Converting bytes to pixels completed"); |
|
|
961 | return 0; |
|
|
962 | |
|
|
963 | |
|
|
964 | ERROR2: |
|
|
965 | psiconv_list_free(*pixels); |
|
|
966 | ERROR1: |
|
|
967 | psiconv_error(config,lev+1,off,"Converting bytes to pixels failed"); |
|
|
968 | if (!res) |
|
|
969 | return -PSICONV_E_NOMEM; |
|
|
970 | else |
|
|
971 | return res; |
|
|
972 | } |
|
|
973 | |
|
|
974 | int psiconv_pixel_data_to_floats (const psiconv_config config, int lev, |
|
|
975 | psiconv_u32 off, |
|
|
976 | const psiconv_pixel_ints pixels, |
|
|
977 | psiconv_pixel_floats_t *floats, |
|
|
978 | int colordepth, int color, |
|
|
979 | int redbits, int bluebits, int greenbits, |
|
|
980 | const psiconv_pixel_floats_t palet) |
|
|
981 | { |
|
|
982 | int res = 0; |
|
|
983 | psiconv_u32 i; |
|
|
984 | psiconv_u32 *pixel; |
|
|
985 | |
|
|
986 | psiconv_progress(config,lev+1,off,"Going to convert pixels to floats"); |
|
|
987 | if (!((*floats).red = malloc(psiconv_list_length(pixels) * |
|
|
988 | sizeof(*(*floats).red)))) |
|
|
989 | goto ERROR1; |
|
|
990 | if (!((*floats).green = malloc(psiconv_list_length(pixels) * |
|
|
991 | sizeof(*(*floats).green)))) |
|
|
992 | goto ERROR2; |
|
|
993 | if (!((*floats).blue = malloc(psiconv_list_length(pixels) * |
|
|
994 | sizeof(*(*floats).blue)))) |
|
|
995 | goto ERROR3; |
|
|
996 | (*floats).length = psiconv_list_length(pixels); |
|
|
997 | |
|
|
998 | for (i = 0; i < psiconv_list_length(pixels); i++) { |
|
|
999 | if (!(pixel = psiconv_list_get(pixels,i))) |
|
|
1000 | goto ERROR4; |
|
|
1001 | #ifdef LOUD |
|
|
1002 | psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel); |
|
|
1003 | #endif |
|
|
1004 | if (!palet.length) { |
|
|
1005 | if (color) { |
|
|
1006 | (*floats).blue[i] = ((float) (*pixel & ((1 << bluebits) - 1))) / |
|
|
1007 | (1 << bluebits); |
|
|
1008 | (*floats).green[i] = ((float) ((*pixel >> bluebits) & |
|
|
1009 | ((1 << greenbits) - 1))) / (1 << greenbits); |
|
|
1010 | (*floats).red[i] = ((float) ((*pixel >> (bluebits+greenbits)) & |
|
|
1011 | ((1 << redbits) - 1))) / (1 << redbits); |
|
|
1012 | } else { |
|
|
1013 | (*floats).red[i] = (*floats).green[i] = |
|
|
1014 | (*floats).blue[i] = ((float) *pixel) / |
|
|
1015 | (1 << colordepth); |
|
|
1016 | } |
|
|
1017 | } else { |
|
|
1018 | if (*pixel >= palet.length) { |
|
|
1019 | psiconv_warn(config,lev+2,off, |
|
|
1020 | "Invalid palet color found (using color 0x00)"); |
|
|
1021 | (*floats).red[i] = palet.red[0]; |
|
|
1022 | (*floats).green[i] = palet.green[0]; |
|
|
1023 | (*floats).blue[i] = palet.blue[0]; |
|
|
1024 | } else { |
|
|
1025 | (*floats).red[i] = palet.red[*pixel]; |
|
|
1026 | (*floats).green[i] = palet.green[*pixel]; |
|
|
1027 | (*floats).blue[i] = palet.blue[*pixel]; |
|
|
1028 | } |
|
|
1029 | } |
|
|
1030 | #ifdef LOUD |
|
|
1031 | psiconv_debug(config,lev+2,off, "Pixel: Red (%f), green (%f), blue (%f)", |
|
|
1032 | (*floats).red[i],(*floats).green[i],(*floats).blue[i]); |
|
|
1033 | #endif |
|
|
1034 | } |
|
|
1035 | psiconv_progress(config,lev+1,off,"Finished converting pixels to floats"); |
|
|
1036 | return 0; |
|
|
1037 | |
|
|
1038 | ERROR4: |
|
|
1039 | free((*floats).blue); |
|
|
1040 | ERROR3: |
|
|
1041 | free((*floats).green); |
|
|
1042 | ERROR2: |
|
|
1043 | free((*floats).red); |
|
|
1044 | ERROR1: |
|
|
1045 | psiconv_error(config,lev+1,off,"Converting pixels to floats failed"); |
|
|
1046 | if (!res) |
|
|
1047 | return -PSICONV_E_NOMEM; |
|
|
1048 | else |
|
|
1049 | return res; |
|
|
1050 | } |
|
|
1051 | |
|
|
1052 | |
|
|
1053 | |