1 |
frodo |
10 |
/* |
2 |
|
|
parse_image.c - Part of psiconv, a PSION 5 file formats converter |
3 |
frodo |
63 |
Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
4 |
frodo |
10 |
|
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 |
7 |
|
|
the Free Software Foundation; either version 2 of the License, or |
8 |
|
|
(at your option) any later version. |
9 |
|
|
|
10 |
|
|
This program is distributed in the hope that it will be useful, |
11 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
|
|
GNU General Public License for more details. |
14 |
|
|
|
15 |
|
|
You should have received a copy of the GNU General Public License |
16 |
|
|
along with this program; if not, write to the Free Software |
17 |
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 |
|
|
*/ |
19 |
|
|
|
20 |
|
|
#include "config.h" |
21 |
frodo |
71 |
#include "compat.h" |
22 |
|
|
|
23 |
frodo |
10 |
#include <stdlib.h> |
24 |
|
|
|
25 |
|
|
#include "parse_routines.h" |
26 |
frodo |
71 |
#include "error.h" |
27 |
frodo |
10 |
|
28 |
frodo |
142 |
#ifdef DMALLOC |
29 |
|
|
#include <dmalloc.h> |
30 |
|
|
#endif |
31 |
|
|
|
32 |
|
|
|
33 |
frodo |
168 |
int psiconv_parse_jumptable_section(const psiconv_config config, |
34 |
|
|
const psiconv_buffer buf,int lev, |
35 |
frodo |
42 |
psiconv_u32 off, int *length, |
36 |
|
|
psiconv_jumptable_section *result) |
37 |
frodo |
12 |
{ |
38 |
|
|
int res = 0; |
39 |
|
|
int len = 0; |
40 |
|
|
psiconv_u32 listlen,temp; |
41 |
|
|
int i; |
42 |
|
|
|
43 |
frodo |
168 |
psiconv_progress(config,lev+1,off+len,"Going to read the jumptable section"); |
44 |
frodo |
64 |
if (!((*result) = psiconv_list_new(sizeof(psiconv_u32)))) |
45 |
|
|
goto ERROR1; |
46 |
frodo |
12 |
|
47 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the list length"); |
48 |
|
|
listlen = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
49 |
frodo |
65 |
if (res) |
50 |
frodo |
64 |
goto ERROR2; |
51 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"List length: %08x",listlen); |
52 |
frodo |
12 |
len += 4; |
53 |
|
|
|
54 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the list"); |
55 |
frodo |
12 |
for (i = 0; i < listlen; i++) { |
56 |
frodo |
168 |
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
57 |
frodo |
65 |
if (res) |
58 |
frodo |
64 |
goto ERROR2; |
59 |
|
|
if ((res = psiconv_list_add(*result,&temp))) |
60 |
|
|
goto ERROR2; |
61 |
frodo |
168 |
psiconv_debug(config,lev+3,off+len,"Offset: %08x",temp); |
62 |
frodo |
12 |
len += 4; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
if (length) |
66 |
|
|
*length = len; |
67 |
|
|
|
68 |
frodo |
168 |
psiconv_progress(config,lev+1,off+len-1,"End of jumptable section " |
69 |
frodo |
13 |
"(total length: %08x)", len); |
70 |
frodo |
12 |
|
71 |
frodo |
64 |
return 0; |
72 |
|
|
|
73 |
|
|
ERROR2: |
74 |
|
|
psiconv_list_free(*result); |
75 |
|
|
ERROR1: |
76 |
frodo |
168 |
psiconv_warn(config,lev+1,off,"Reading of Jumptable Section failed"); |
77 |
frodo |
64 |
if (length) |
78 |
|
|
*length = 0; |
79 |
|
|
if (!res) |
80 |
|
|
return -PSICONV_E_NOMEM; |
81 |
|
|
else |
82 |
|
|
return res; |
83 |
frodo |
12 |
} |
84 |
|
|
|
85 |
frodo |
168 |
static int decode_byte(const psiconv_config config, int lev, psiconv_u32 off, |
86 |
frodo |
97 |
psiconv_paint_data_section data, psiconv_u32 *pixelnr, |
87 |
frodo |
25 |
psiconv_u8 byte, int bits_per_pixel, int linelen, |
88 |
|
|
int *linepos,int picsize) |
89 |
|
|
{ |
90 |
|
|
int mask = (bits_per_pixel << 1) -1; |
91 |
|
|
int i; |
92 |
|
|
if (*linepos < (data->xsize + (8/bits_per_pixel) - 1) / (8/bits_per_pixel)) |
93 |
|
|
for (i = 0; i < 8/bits_per_pixel; i ++) { |
94 |
|
|
if ((i != 0) && ((*pixelnr % (data->xsize)) == 0)) { |
95 |
frodo |
168 |
psiconv_debug(config,lev+1,off,"Skipping padding: %02x",byte); |
96 |
frodo |
25 |
i = 8; |
97 |
|
|
} else if (*pixelnr >= picsize) { |
98 |
frodo |
168 |
psiconv_warn(config,lev+1,off,"Corrupted picture data!"); |
99 |
|
|
psiconv_debug(config,lev+1,off,"Trying to write a pixel too far"); |
100 |
frodo |
25 |
return -1; |
101 |
|
|
} else { |
102 |
|
|
data->red[*pixelnr] = data->green[*pixelnr] = data->blue[*pixelnr] = |
103 |
|
|
((float) (byte & mask)) / ((1 << bits_per_pixel) -1); |
104 |
frodo |
168 |
psiconv_debug(config,lev+1,off,"Pixel %04x: (%04x,%04x) value %02x, color %f", |
105 |
frodo |
64 |
*pixelnr,*pixelnr % data->xsize, |
106 |
|
|
*pixelnr / data->xsize, byte&mask, data->red[*pixelnr]); |
107 |
frodo |
25 |
byte = byte >> bits_per_pixel; |
108 |
|
|
(*pixelnr) ++; |
109 |
|
|
} |
110 |
|
|
} |
111 |
|
|
else |
112 |
frodo |
168 |
psiconv_debug(config,lev+1,off,"Skipping padding byte"); |
113 |
frodo |
25 |
(*linepos) ++; |
114 |
|
|
if (*linepos == linelen) |
115 |
|
|
*linepos = 0; |
116 |
|
|
return 0; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
|
120 |
frodo |
168 |
int psiconv_parse_paint_data_section(const psiconv_config config, |
121 |
|
|
const psiconv_buffer buf,int lev, |
122 |
frodo |
45 |
psiconv_u32 off, int *length,int isclipart, |
123 |
frodo |
10 |
psiconv_paint_data_section *result) |
124 |
|
|
{ |
125 |
|
|
int res = 0; |
126 |
|
|
int len = 0; |
127 |
frodo |
13 |
psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; |
128 |
frodo |
11 |
psiconv_u8 marker; |
129 |
frodo |
25 |
int i,leng; |
130 |
|
|
psiconv_u32 bits_per_pixel,compression; |
131 |
|
|
int linepos = 0; |
132 |
frodo |
10 |
|
133 |
frodo |
168 |
psiconv_progress(config,lev+1,off,"Going to read a paint data section"); |
134 |
frodo |
64 |
if (!((*result) = malloc(sizeof(**result)))) |
135 |
|
|
goto ERROR1; |
136 |
frodo |
10 |
|
137 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read section size"); |
138 |
|
|
size = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
139 |
frodo |
64 |
if (res) |
140 |
|
|
goto ERROR2; |
141 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Section size: %08x",size); |
142 |
frodo |
10 |
len += 4; |
143 |
|
|
|
144 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read pixel data offset"); |
145 |
|
|
offset = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
146 |
frodo |
64 |
if (res) |
147 |
|
|
goto ERROR2; |
148 |
frodo |
13 |
if (offset != 0x28) { |
149 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
150 |
frodo |
10 |
"Paint data section data offset has unexpected value"); |
151 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len, |
152 |
frodo |
10 |
"Data offset: read %08x, expected %08x",offset,0x28); |
153 |
frodo |
12 |
res = -1; |
154 |
frodo |
10 |
} |
155 |
|
|
len += 4; |
156 |
|
|
|
157 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read picture X size"); |
158 |
|
|
(*result)->xsize = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
159 |
frodo |
64 |
if (res) |
160 |
|
|
goto ERROR2; |
161 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
162 |
frodo |
10 |
len += 4; |
163 |
|
|
|
164 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read picture Y size"); |
165 |
|
|
(*result)->ysize = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
166 |
frodo |
64 |
if (res) |
167 |
|
|
goto ERROR2; |
168 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
169 |
frodo |
10 |
len += 4; |
170 |
|
|
|
171 |
frodo |
25 |
picsize = (*result)->ysize * (*result)->xsize; |
172 |
frodo |
10 |
|
173 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the real picture x size"); |
174 |
|
|
(*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
175 |
frodo |
64 |
if (res) |
176 |
|
|
goto ERROR2; |
177 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize); |
178 |
frodo |
25 |
len += leng; |
179 |
|
|
|
180 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the real picture y size"); |
181 |
|
|
(*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
182 |
frodo |
64 |
if (res) |
183 |
|
|
goto ERROR2; |
184 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize); |
185 |
frodo |
25 |
len += leng; |
186 |
|
|
|
187 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the number of bits per pixel"); |
188 |
|
|
bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
189 |
frodo |
64 |
if (res) |
190 |
|
|
goto ERROR2; |
191 |
frodo |
25 |
if (bits_per_pixel > 8) { |
192 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len,"Picture has too many colors"); |
193 |
|
|
psiconv_debug(config,lev+2,off+len,"Read %d colorbits",bits_per_pixel); |
194 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
195 |
|
|
goto ERROR2; |
196 |
frodo |
25 |
} |
197 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); |
198 |
frodo |
25 |
len += 4; |
199 |
|
|
|
200 |
|
|
for (i = 0 ; i < 2; i++) { |
201 |
frodo |
168 |
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
202 |
frodo |
64 |
if (res) |
203 |
|
|
goto ERROR2; |
204 |
frodo |
25 |
if (temp != 00) { |
205 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
206 |
frodo |
64 |
"Paint data section prologue has unknown values (ignored)"); |
207 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len, |
208 |
frodo |
25 |
"offset %02x: read %08x, expected %08x",i,temp, 0x00); |
209 |
frodo |
10 |
} |
210 |
|
|
len += 4; |
211 |
|
|
} |
212 |
frodo |
25 |
|
213 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len, |
214 |
frodo |
25 |
"Going to read whether RLE compression is used"); |
215 |
frodo |
168 |
compression=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
216 |
frodo |
64 |
if (res) |
217 |
|
|
goto ERROR2; |
218 |
frodo |
25 |
if (compression > 1) { |
219 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " |
220 |
frodo |
25 |
"compression type, assuming RLE"); |
221 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); |
222 |
frodo |
25 |
compression = 1; |
223 |
|
|
} |
224 |
frodo |
175 |
psiconv_debug(config,lev+2,off+len,"Compression: %s",compression?"RLE8":"none"); |
225 |
frodo |
45 |
len += 4; |
226 |
frodo |
10 |
|
227 |
frodo |
45 |
if (isclipart) { |
228 |
frodo |
168 |
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); |
230 |
frodo |
64 |
if (res) |
231 |
|
|
goto ERROR2; |
232 |
frodo |
45 |
if (temp != 0xffffffff) { |
233 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
234 |
frodo |
64 |
"Paint data section prologue has unknown values (ignoring)"); |
235 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len, |
236 |
frodo |
45 |
"offset %02x: read %08x, expected %08x",i,temp, 0xffffffff); |
237 |
|
|
} |
238 |
|
|
len += 4; |
239 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long"); |
240 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
241 |
frodo |
64 |
if (res) |
242 |
|
|
goto ERROR2; |
243 |
frodo |
45 |
if (temp != 0x44) { |
244 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
245 |
frodo |
64 |
"Paint data section prologue has unknown values (ignoring)"); |
246 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len, |
247 |
frodo |
45 |
"offset %02x: read %08x, expected %08x",i,temp, 0x44); |
248 |
|
|
} |
249 |
|
|
len += 4; |
250 |
|
|
} |
251 |
|
|
|
252 |
frodo |
64 |
if (!((*result)->red = malloc(sizeof(float) * picsize))) |
253 |
|
|
goto ERROR2; |
254 |
|
|
if (!((*result)->green = malloc(sizeof(float) * picsize))) |
255 |
|
|
goto ERROR3; |
256 |
|
|
if (!((*result)->blue = malloc(sizeof(float) * picsize))) |
257 |
|
|
goto ERROR4; |
258 |
frodo |
10 |
len = offset; |
259 |
|
|
datasize = size - len; |
260 |
frodo |
45 |
if (isclipart) |
261 |
|
|
len += 8; |
262 |
frodo |
10 |
|
263 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the pixel data"); |
264 |
frodo |
10 |
pixelnr = 0; |
265 |
|
|
datanr = 0; |
266 |
frodo |
25 |
if (!compression) { |
267 |
|
|
linelen = datasize / (*result)->ysize; |
268 |
frodo |
168 |
psiconv_debug(config,lev+3,off+len,"Line length: %04x bytes",linelen); |
269 |
frodo |
25 |
while((datanr < datasize)) { |
270 |
frodo |
168 |
temp = psiconv_read_u8(config,buf,lev+2,off+len+datanr,&res); |
271 |
frodo |
64 |
if (res) |
272 |
|
|
goto ERROR5; |
273 |
frodo |
168 |
if (decode_byte(config,lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel, |
274 |
frodo |
25 |
linelen,&linepos,picsize)) { |
275 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
276 |
frodo |
25 |
break; |
277 |
|
|
} |
278 |
|
|
datanr++; |
279 |
|
|
} |
280 |
|
|
} else { |
281 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"First pass: determining line length"); |
282 |
frodo |
25 |
datanr = 0; |
283 |
|
|
i = 0; |
284 |
|
|
while (datanr < datasize) { |
285 |
frodo |
168 |
marker = psiconv_read_u8(config,buf,lev+3,off+len+datanr,&res); |
286 |
frodo |
64 |
if (res) |
287 |
|
|
goto ERROR5; |
288 |
frodo |
25 |
if (marker >= 0x80) { |
289 |
|
|
datanr += 0x100 - marker + 1; |
290 |
|
|
i += 0x100 - marker; |
291 |
frodo |
10 |
} else { |
292 |
frodo |
25 |
datanr += 2; |
293 |
|
|
i += marker + 1; |
294 |
|
|
} |
295 |
|
|
} |
296 |
|
|
linelen = i / (*result)->ysize; |
297 |
|
|
datanr=0; |
298 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Linelen: %04x bytes",linelen); |
299 |
frodo |
25 |
while((datanr < datasize)) { |
300 |
frodo |
168 |
marker = psiconv_read_u8(config,buf,lev+3,off+len+datanr,&res); |
301 |
frodo |
64 |
if (res) |
302 |
|
|
goto ERROR5; |
303 |
frodo |
168 |
psiconv_debug(config,lev+3,off+len+datanr, |
304 |
frodo |
25 |
"Pixelnr %08x, Datanr %08x: Read marker %02x", |
305 |
|
|
pixelnr,datanr,marker); |
306 |
|
|
datanr ++; |
307 |
|
|
if (marker >= 0x80) { |
308 |
|
|
/* 0x100 - marker bytes of data follow */ |
309 |
|
|
for (i = 0; i < 0x100-marker; i++,datanr++) { |
310 |
|
|
if (datanr >= datasize) { |
311 |
frodo |
168 |
psiconv_warn(config,lev+3,off+len+datanr,"Corrupted picture data"); |
312 |
|
|
psiconv_debug(config,lev+3,off+len+datanr, |
313 |
frodo |
15 |
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
314 |
|
|
"Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
315 |
|
|
datanr,marker); |
316 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
317 |
frodo |
25 |
break; |
318 |
|
|
} |
319 |
frodo |
168 |
temp = psiconv_read_u8(config,buf,lev+2,off+len+datanr,&res); |
320 |
frodo |
64 |
if (res) |
321 |
|
|
goto ERROR5; |
322 |
frodo |
168 |
if (decode_byte(config,lev+2,off+len+datanr,*result,&pixelnr,temp, |
323 |
frodo |
25 |
bits_per_pixel,linelen,&linepos,picsize)) { |
324 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
325 |
frodo |
25 |
break; |
326 |
frodo |
15 |
} |
327 |
frodo |
25 |
} |
328 |
|
|
} else { |
329 |
|
|
if (datanr >= datasize) { |
330 |
frodo |
168 |
psiconv_warn(config,lev+3,off+len+datanr,"Corrupted picture data"); |
331 |
|
|
psiconv_debug(config,lev+3,off+len+datanr, |
332 |
frodo |
25 |
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
333 |
|
|
"Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
334 |
|
|
datanr,marker); |
335 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
336 |
frodo |
25 |
} else { |
337 |
frodo |
168 |
temp = psiconv_read_u8(config,buf,lev+3,off+len+datanr,&res); |
338 |
frodo |
64 |
if (res) |
339 |
|
|
goto ERROR5; |
340 |
frodo |
25 |
for (i = 0; i <= marker; i++) { |
341 |
frodo |
168 |
if (decode_byte(config,lev+2,off+len+datanr,*result,&pixelnr,temp, |
342 |
frodo |
25 |
bits_per_pixel,linelen,&linepos,picsize)) { |
343 |
frodo |
64 |
res = -PSICONV_E_PARSE; |
344 |
frodo |
25 |
break; |
345 |
|
|
} |
346 |
|
|
} |
347 |
|
|
datanr ++; |
348 |
|
|
} |
349 |
frodo |
10 |
} |
350 |
|
|
} |
351 |
|
|
} |
352 |
frodo |
25 |
|
353 |
|
|
if (linepos >= ((*result)->xsize + (8/bits_per_pixel) - 1) / |
354 |
|
|
(8/bits_per_pixel)) |
355 |
|
|
datanr += (linelen - linepos); |
356 |
|
|
|
357 |
frodo |
64 |
if (res || (datanr != datasize) || (pixelnr != picsize)) { |
358 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len,"Corrupted picture data!"); |
359 |
|
|
psiconv_debug(config,lev+3,off+len+datanr, |
360 |
frodo |
12 |
"Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
361 |
|
|
"Datanr: %08x",picsize,datasize,pixelnr,datanr); |
362 |
frodo |
64 |
goto ERROR5; |
363 |
frodo |
12 |
} |
364 |
|
|
|
365 |
frodo |
10 |
len += datanr; |
366 |
|
|
|
367 |
|
|
if (length) |
368 |
|
|
*length = len; |
369 |
|
|
|
370 |
frodo |
168 |
psiconv_progress(config,lev+1,off+len-1,"End of paint data section " |
371 |
frodo |
13 |
"(total length: %08x)", len); |
372 |
frodo |
10 |
|
373 |
|
|
return res; |
374 |
frodo |
64 |
|
375 |
|
|
ERROR5: |
376 |
|
|
free((*result)->blue); |
377 |
|
|
ERROR4: |
378 |
|
|
free((*result)->green); |
379 |
|
|
ERROR3: |
380 |
|
|
free((*result)->red); |
381 |
|
|
ERROR2: |
382 |
|
|
free (*result); |
383 |
|
|
ERROR1: |
384 |
frodo |
168 |
psiconv_warn(config,lev+1,off,"Reading of Paint Data Section failed"); |
385 |
frodo |
64 |
if (length) |
386 |
|
|
*length = 0; |
387 |
|
|
if (!res) |
388 |
|
|
return -PSICONV_E_NOMEM; |
389 |
|
|
else |
390 |
|
|
return res; |
391 |
frodo |
10 |
} |
392 |
|
|
|
393 |
frodo |
168 |
int psiconv_parse_sketch_section(const psiconv_config config, |
394 |
|
|
const psiconv_buffer buf, int lev, |
395 |
frodo |
163 |
psiconv_u32 off, int *length, |
396 |
frodo |
24 |
psiconv_sketch_section *result) |
397 |
|
|
{ |
398 |
|
|
int res=0; |
399 |
|
|
int len=0; |
400 |
|
|
psiconv_u32 temp; |
401 |
|
|
int leng; |
402 |
|
|
|
403 |
frodo |
168 |
psiconv_progress(config,lev+1,off,"Going to read the sketch section"); |
404 |
frodo |
64 |
if (!(*result = malloc(sizeof(**result)))) |
405 |
|
|
goto ERROR1; |
406 |
frodo |
24 |
|
407 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the displayed hor. size"); |
408 |
|
|
(*result)->displayed_xsize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
409 |
frodo |
163 |
if (res) |
410 |
|
|
goto ERROR2; |
411 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Displayed hor. size: %04x", |
412 |
frodo |
163 |
(*result)->displayed_xsize); |
413 |
|
|
len += 0x02; |
414 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read displayed ver. size"); |
415 |
|
|
(*result)->displayed_ysize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
416 |
frodo |
163 |
if (res) |
417 |
|
|
goto ERROR2; |
418 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Displayed ver. size: %04x", |
419 |
frodo |
163 |
(*result)->displayed_ysize); |
420 |
|
|
len += 0x02; |
421 |
frodo |
24 |
|
422 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the data hor. offset"); |
423 |
|
|
(*result)->picture_data_x_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
424 |
frodo |
163 |
&res); |
425 |
|
|
if (res) |
426 |
|
|
goto ERROR2; |
427 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Data hor. offset: %04x", |
428 |
frodo |
163 |
(*result)->picture_data_x_offset); |
429 |
|
|
len += 0x02; |
430 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the data ver. offset"); |
431 |
|
|
(*result)->picture_data_y_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
432 |
frodo |
163 |
&res); |
433 |
|
|
if (res) |
434 |
|
|
goto ERROR2; |
435 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Data ver. offset: %04x", |
436 |
frodo |
163 |
(*result)->picture_data_y_offset); |
437 |
|
|
len += 0x02; |
438 |
|
|
|
439 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the displayed hor. offset"); |
440 |
|
|
(*result)->displayed_size_x_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
441 |
frodo |
163 |
&res); |
442 |
|
|
if (res) |
443 |
|
|
goto ERROR2; |
444 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Displayed hor. offset: %04x", |
445 |
frodo |
163 |
(*result)->displayed_size_x_offset); |
446 |
|
|
len += 0x02; |
447 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the displayed ver. offset"); |
448 |
|
|
(*result)->displayed_size_y_offset = psiconv_read_u16(config,buf,lev+2,off + len, |
449 |
frodo |
163 |
&res); |
450 |
|
|
if (res) |
451 |
|
|
goto ERROR2; |
452 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Displayed ver. offset: %04x", |
453 |
frodo |
163 |
(*result)->displayed_size_y_offset); |
454 |
|
|
len += 0x02; |
455 |
|
|
|
456 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the form hor. size"); |
457 |
|
|
(*result)->form_xsize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
458 |
frodo |
163 |
if (res) |
459 |
|
|
goto ERROR2; |
460 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Form hor. size: %04x", |
461 |
frodo |
163 |
(*result)->form_xsize); |
462 |
|
|
len += 0x02; |
463 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read form ver. size"); |
464 |
|
|
(*result)->form_ysize = psiconv_read_u16(config,buf,lev+2,off + len,&res); |
465 |
frodo |
163 |
if (res) |
466 |
|
|
goto ERROR2; |
467 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Form ver. size: %04x", |
468 |
frodo |
163 |
(*result)->form_ysize); |
469 |
|
|
len += 0x02; |
470 |
|
|
|
471 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to skip 1 word of zeros"); |
472 |
|
|
temp = psiconv_read_u16(config,buf,lev+2,off+len,&res); |
473 |
frodo |
163 |
if (res) |
474 |
|
|
goto ERROR2; |
475 |
|
|
if (temp != 0) { |
476 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
477 |
frodo |
163 |
"Unexpected value in sketch section preamble (ignored)"); |
478 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read %04x, expected %04x", |
479 |
frodo |
163 |
temp,0); |
480 |
frodo |
24 |
} |
481 |
frodo |
163 |
off += 0x02; |
482 |
frodo |
24 |
|
483 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the picture data"); |
484 |
|
|
if ((res = psiconv_parse_paint_data_section(config,buf,lev+2,off+len,&leng,0, |
485 |
frodo |
64 |
&((*result)->picture)))) |
486 |
|
|
goto ERROR2; |
487 |
frodo |
24 |
off += leng; |
488 |
|
|
|
489 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the hor. magnification"); |
490 |
|
|
(*result)->magnification_x = psiconv_read_u16(config,buf,lev+2,off+len,&res)/1000.0; |
491 |
frodo |
64 |
if (res) |
492 |
|
|
goto ERROR3; |
493 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Form hor. magnification: %f", |
494 |
frodo |
24 |
(*result)->magnification_x); |
495 |
|
|
len += 0x02; |
496 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the ver. magnification"); |
497 |
|
|
(*result)->magnification_y = psiconv_read_u16(config,buf,lev+2,off+len,&res)/1000.0; |
498 |
frodo |
64 |
if (res) |
499 |
|
|
goto ERROR3; |
500 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Form ver. magnification: %f", |
501 |
frodo |
24 |
(*result)->magnification_y); |
502 |
|
|
len += 0x02; |
503 |
|
|
|
504 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the left cut"); |
505 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
506 |
frodo |
64 |
if (res) |
507 |
|
|
goto ERROR3; |
508 |
frodo |
163 |
(*result)->cut_left = (temp * 6.0) / (*result)->displayed_xsize; |
509 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Left cut: raw %08x, real: %f", |
510 |
frodo |
24 |
temp,(*result)->cut_left); |
511 |
|
|
len += 0x04; |
512 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the right cut"); |
513 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
514 |
frodo |
64 |
if (res) |
515 |
|
|
goto ERROR3; |
516 |
frodo |
163 |
(*result)->cut_right = (temp * 6.0) / (*result)->displayed_xsize; |
517 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Right cut: raw %08x, real: %f", |
518 |
frodo |
24 |
temp,(*result)->cut_right); |
519 |
|
|
len += 0x04; |
520 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the top cut"); |
521 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
522 |
frodo |
64 |
if (res) |
523 |
|
|
goto ERROR3; |
524 |
frodo |
163 |
(*result)->cut_top = (temp * 6.0) / (*result)->displayed_ysize; |
525 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Top cut: raw %08x, real: %f", |
526 |
frodo |
24 |
temp,(*result)->cut_top); |
527 |
|
|
len += 0x04; |
528 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the bottom cut"); |
529 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off + len,&res); |
530 |
frodo |
64 |
if (res) |
531 |
|
|
goto ERROR3; |
532 |
frodo |
163 |
(*result)->cut_bottom = (temp * 6.0) / (*result)->displayed_ysize; |
533 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Bottom cut: raw %08x, real: %f", |
534 |
frodo |
24 |
temp,(*result)->cut_bottom); |
535 |
|
|
len += 0x04; |
536 |
|
|
|
537 |
|
|
if (length) |
538 |
|
|
*length = len; |
539 |
|
|
|
540 |
frodo |
168 |
psiconv_progress(config,lev,off+len-1, |
541 |
frodo |
24 |
"End of sketch section (total length: %08x)", len); |
542 |
|
|
|
543 |
|
|
return res; |
544 |
frodo |
64 |
ERROR3: |
545 |
|
|
psiconv_free_paint_data_section((*result)->picture); |
546 |
|
|
ERROR2: |
547 |
|
|
free (*result); |
548 |
|
|
ERROR1: |
549 |
frodo |
168 |
psiconv_warn(config,lev+1,off,"Reading of Sketch Section failed"); |
550 |
frodo |
64 |
if (length) |
551 |
|
|
*length = 0; |
552 |
|
|
if (!res) |
553 |
|
|
return -PSICONV_E_NOMEM; |
554 |
|
|
else |
555 |
|
|
return res; |
556 |
frodo |
24 |
} |
557 |
|
|
|
558 |
frodo |
43 |
|
559 |
frodo |
168 |
int psiconv_parse_clipart_section(const psiconv_config config, |
560 |
|
|
const psiconv_buffer buf,int lev, |
561 |
frodo |
43 |
psiconv_u32 off, int *length, |
562 |
|
|
psiconv_clipart_section *result) |
563 |
|
|
{ |
564 |
|
|
int res=0; |
565 |
|
|
int len=0; |
566 |
|
|
int leng; |
567 |
|
|
psiconv_u32 temp; |
568 |
|
|
|
569 |
frodo |
168 |
psiconv_progress(config,lev+1,off+len,"Going to read the clipart section"); |
570 |
frodo |
64 |
if (!(*result = malloc(sizeof(**result)))) |
571 |
|
|
goto ERROR1; |
572 |
frodo |
43 |
|
573 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the section ID"); |
574 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
575 |
frodo |
64 |
if (res) |
576 |
|
|
goto ERROR2; |
577 |
frodo |
43 |
if (temp != PSICONV_ID_CLIPART_ITEM) { |
578 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
579 |
frodo |
64 |
"Unexpected value in clipart section preamble (ignored)"); |
580 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, |
581 |
frodo |
43 |
PSICONV_ID_CLIPART_ITEM); |
582 |
|
|
} else |
583 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Clipart ID: %08x", temp); |
584 |
frodo |
43 |
off += 4; |
585 |
|
|
|
586 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); |
587 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
588 |
frodo |
64 |
if (res) |
589 |
|
|
goto ERROR2; |
590 |
frodo |
43 |
if (temp != 0x02) { |
591 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
592 |
frodo |
64 |
"Unexpected value in clipart section preamble (ignored)"); |
593 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, |
594 |
frodo |
43 |
0x02); |
595 |
|
|
} else |
596 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"First unknown long: %08x", temp); |
597 |
frodo |
43 |
off += 4; |
598 |
|
|
|
599 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long"); |
600 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
601 |
frodo |
64 |
if (res) |
602 |
|
|
goto ERROR2; |
603 |
frodo |
43 |
if (temp != 0) { |
604 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
605 |
frodo |
64 |
"Unexpected value in clipart section preamble (ignored)"); |
606 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
607 |
frodo |
43 |
} else |
608 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Second unknown long: %08x", temp); |
609 |
frodo |
43 |
off += 4; |
610 |
|
|
|
611 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read a third unknown long"); |
612 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
613 |
frodo |
64 |
if (res) |
614 |
|
|
goto ERROR2; |
615 |
frodo |
43 |
if (temp != 0) { |
616 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
617 |
frodo |
64 |
"Unexpected value in clipart section preamble (ignored)"); |
618 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
619 |
frodo |
43 |
} else |
620 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Third unknown long: %08x", temp); |
621 |
frodo |
43 |
off += 4; |
622 |
|
|
|
623 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read a fourth unknown long"); |
624 |
|
|
temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
625 |
frodo |
64 |
if (res) |
626 |
|
|
goto ERROR2; |
627 |
frodo |
43 |
if ((temp != 0x0c) && (temp != 0x08)) { |
628 |
frodo |
168 |
psiconv_warn(config,lev+2,off+len, |
629 |
frodo |
64 |
"Unexpected value in clipart section preamble (ignored)"); |
630 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x or %08x",temp, |
631 |
frodo |
43 |
0x0c, 0x08); |
632 |
|
|
} else |
633 |
frodo |
168 |
psiconv_debug(config,lev+2,off+len,"Fourth unknown long: %08x", temp); |
634 |
frodo |
43 |
off += 4; |
635 |
|
|
|
636 |
frodo |
168 |
psiconv_progress(config,lev+2,off+len,"Going to read the Paint Data Section"); |
637 |
|
|
if ((res = psiconv_parse_paint_data_section(config,buf,lev+2,off+len,&leng,1, |
638 |
frodo |
64 |
&((*result)->picture)))) |
639 |
|
|
goto ERROR2; |
640 |
frodo |
43 |
len += leng; |
641 |
|
|
|
642 |
|
|
if (length) |
643 |
|
|
*length = len; |
644 |
|
|
|
645 |
frodo |
168 |
psiconv_progress(config,lev,off+len-1, |
646 |
frodo |
43 |
"End of clipart section (total length: %08x)", len); |
647 |
frodo |
64 |
return 0; |
648 |
frodo |
43 |
|
649 |
frodo |
64 |
ERROR2: |
650 |
|
|
free (*result); |
651 |
|
|
ERROR1: |
652 |
frodo |
168 |
psiconv_warn(config,lev+1,off,"Reading of Font failed"); |
653 |
frodo |
64 |
if (length) |
654 |
|
|
*length = 0; |
655 |
|
|
if (!res) |
656 |
|
|
return -PSICONV_E_NOMEM; |
657 |
|
|
else |
658 |
|
|
return res; |
659 |
frodo |
43 |
} |