… | |
… | |
98 | len += 4; |
98 | len += 4; |
99 | |
99 | |
100 | picsize = (*result)->ysize * (*result)->xsize * 2; |
100 | picsize = (*result)->ysize * (*result)->xsize * 2; |
101 | linelen = (*result)->xsize; |
101 | linelen = (*result)->xsize; |
102 | |
102 | |
103 | psiconv_progress(lev+2,off+len,"Going to read 6 unused longs"); |
103 | psiconv_progress(lev+2,off+len,"Going to read 6 unknown longs"); |
104 | for (i = 0 ; i < 6; i++) { |
104 | for (i = 0 ; i < 6; i++) { |
105 | temp = psiconv_read_u32(buf,lev+2,off+len); |
105 | temp = psiconv_read_u32(buf,lev+2,off+len); |
106 | if (temp != 0x00) { |
106 | if (temp != (i==2?0x02:i==5?0x01:0x00)) { |
107 | psiconv_warn(lev+2,off+len, |
107 | psiconv_warn(lev+2,off+len, |
108 | "Paint data section prologue has unknown values"); |
108 | "Paint data section prologue has unknown values"); |
109 | psiconv_debug(lev+2,off+len, |
109 | psiconv_debug(lev+2,off+len, |
110 | "offset %02x: read %08x, expected %08x",i,temp,0x00); |
110 | "offset %02x: read %08x, expected %08x",i,temp, |
|
|
111 | i==2?0x02:i==5?0x01:0x00); |
111 | res = -1; |
112 | res = -1; |
112 | } |
113 | } |
113 | len += 4; |
114 | len += 4; |
114 | } |
115 | } |
115 | |
116 | |
… | |
… | |
128 | "Pixelnr %08x, Datanr %08x: Read marker %02x", |
129 | "Pixelnr %08x, Datanr %08x: Read marker %02x", |
129 | pixelnr,datanr,marker); |
130 | pixelnr,datanr,marker); |
130 | datanr ++; |
131 | datanr ++; |
131 | if (marker >= 0x80) { |
132 | if (marker >= 0x80) { |
132 | /* 0x100 - marker bytes of data follow */ |
133 | /* 0x100 - marker bytes of data follow */ |
133 | if ((0x100 - marker + datanr > datasize) || |
134 | for (i = 0; i < 0x100-marker; i++,datanr++) { |
134 | ((0x100 - marker) * 4 + pixelnr > picsize)) { |
135 | if ((picsize == pixelnr) || (datasize == datanr)) { |
|
|
136 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
137 | psiconv_debug(lev+3,off+len+datanr, |
|
|
138 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
139 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
140 | datanr,marker); |
|
|
141 | read_err = 1; |
|
|
142 | res = -1; |
|
|
143 | break; |
|
|
144 | } |
|
|
145 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr); |
|
|
146 | (*result)->red[pixelnr] = |
|
|
147 | (*result)->green[pixelnr] = |
|
|
148 | (*result)->blue[pixelnr] = |
|
|
149 | (temp & 0x03) * (1.0/3.0); |
|
|
150 | pixelnr ++; |
|
|
151 | if (pixelnr % linelen == 0) |
|
|
152 | continue; |
|
|
153 | (*result)->red[pixelnr] = |
|
|
154 | (*result)->green[pixelnr] = |
|
|
155 | (*result)->blue[pixelnr] = |
|
|
156 | ((temp >> 2) & 0x03) * (1.0/3.0); |
|
|
157 | pixelnr ++; |
|
|
158 | if (pixelnr % linelen == 0) |
|
|
159 | continue; |
|
|
160 | (*result)->red[pixelnr] = |
|
|
161 | (*result)->green[pixelnr] = |
|
|
162 | (*result)->blue[pixelnr] = |
|
|
163 | ((temp >> 4) & 0x03) * (1.0/3.0); |
|
|
164 | pixelnr ++; |
|
|
165 | if (pixelnr % linelen == 0) |
|
|
166 | continue; |
|
|
167 | (*result)->red[pixelnr] = |
|
|
168 | (*result)->green[pixelnr] = |
|
|
169 | (*result)->blue[pixelnr] = |
|
|
170 | ((temp >> 6) & 0x03) * (1.0/3.0); |
|
|
171 | } |
|
|
172 | } else { /* marker < 0x080 */ |
|
|
173 | if (datasize == datanr) { |
135 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
174 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
136 | psiconv_debug(lev+3,off+len+datanr, |
175 | psiconv_debug(lev+3,off+len+datanr, |
137 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
176 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
138 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
177 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
139 | datanr,marker); |
178 | datanr,marker); |
|
|
179 | read_err = 1; |
140 | res = -1; |
180 | res = -1; |
141 | read_err = 1; |
|
|
142 | } else { |
181 | } else { |
143 | for (i = 0; i < 0x100-marker; i++) { |
|
|
144 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr+i); |
182 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr); |
|
|
183 | for (i = 0; i <= marker; i++) { |
|
|
184 | if (picsize == pixelnr) { |
|
|
185 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
186 | psiconv_debug(lev+3,off+len+datanr, |
|
|
187 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
188 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
189 | datanr,marker); |
|
|
190 | read_err = 1; |
|
|
191 | res = -1; |
|
|
192 | break; |
|
|
193 | } |
145 | (*result)->red[pixelnr + i*4] = |
194 | (*result)->red[pixelnr] = |
146 | (*result)->green[pixelnr + i*4] = |
195 | (*result)->green[pixelnr] = |
147 | (*result)->blue[pixelnr + i*4] = |
196 | (*result)->blue[pixelnr] = |
148 | (temp & 0x03) * (1.0/3.0); |
197 | (temp & 0x03) * (1.0/3.0); |
|
|
198 | pixelnr ++; |
|
|
199 | if (pixelnr % linelen == 0) |
|
|
200 | continue; |
149 | (*result)->red[pixelnr + i*4 + 1] = |
201 | (*result)->red[pixelnr] = |
150 | (*result)->green[pixelnr + i*4 +1] = |
202 | (*result)->green[pixelnr] = |
151 | (*result)->blue[pixelnr + i*4 + 1] = |
203 | (*result)->blue[pixelnr] = |
152 | ((temp >> 2) & 0x03) * (1.0/3.0); |
204 | ((temp >> 2) & 0x03) * (1.0/3.0); |
|
|
205 | pixelnr ++; |
|
|
206 | if (pixelnr % linelen == 0) |
|
|
207 | continue; |
153 | (*result)->red[pixelnr + i*4 + 2] = |
208 | (*result)->red[pixelnr] = |
154 | (*result)->green[pixelnr + i*4 +2] = |
209 | (*result)->green[pixelnr] = |
155 | (*result)->blue[pixelnr + i*4 + 2] = |
210 | (*result)->blue[pixelnr] = |
156 | ((temp >> 4) & 0x03) * (1.0/3.0); |
211 | ((temp >> 4) & 0x03) * (1.0/3.0); |
|
|
212 | pixelnr ++; |
|
|
213 | if (pixelnr % linelen == 0) |
|
|
214 | continue; |
157 | (*result)->red[pixelnr + i*4 + 3] = |
215 | (*result)->red[pixelnr] = |
158 | (*result)->green[pixelnr + i*4 +3] = |
216 | (*result)->green[pixelnr] = |
159 | (*result)->blue[pixelnr + i*4 + 3] = |
217 | (*result)->blue[pixelnr] = |
160 | ((temp >> 6) & 0x03) * (1.0/3.0); |
218 | ((temp >> 6) & 0x03) * (1.0/3.0); |
|
|
219 | pixelnr ++; |
161 | } |
220 | } |
162 | } |
221 | } |
163 | pixelnr += (0x100 - marker) * 4; |
|
|
164 | datanr += (0x100 - marker); |
|
|
165 | } else { /* marker < 0x080 */ |
|
|
166 | if ((datanr + 1 > datasize) || |
|
|
167 | (marker * 4 + pixelnr > picsize)) { |
|
|
168 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
169 | psiconv_debug(lev+3,off+len+datanr, |
|
|
170 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
171 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
172 | datanr,marker); |
|
|
173 | res = -1; |
|
|
174 | read_err = 1; |
|
|
175 | } else { |
|
|
176 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr); |
|
|
177 | for (i = 0; i < marker; i++) { |
|
|
178 | (*result)->red[pixelnr + i*4] = |
|
|
179 | (*result)->green[pixelnr + i*4] = |
|
|
180 | (*result)->blue[pixelnr + i*4] = |
|
|
181 | (temp & 0x03) * (1.0/3.0); |
|
|
182 | (*result)->red[pixelnr + i*4 + 1] = |
|
|
183 | (*result)->green[pixelnr + i*4 +1] = |
|
|
184 | (*result)->blue[pixelnr + i*4 + 1] = |
|
|
185 | ((temp >> 2) & 0x03) * (1.0/3.0); |
|
|
186 | (*result)->red[pixelnr + i*4 + 2] = |
|
|
187 | (*result)->green[pixelnr + i*4 +2] = |
|
|
188 | (*result)->blue[pixelnr + i*4 + 2] = |
|
|
189 | ((temp >> 4) & 0x03) * (1.0/3.0); |
|
|
190 | (*result)->red[pixelnr + i*4 + 3] = |
|
|
191 | (*result)->green[pixelnr + i*4 +3] = |
|
|
192 | (*result)->blue[pixelnr + i*4 + 3] = |
|
|
193 | ((temp >> 6) & 0x03) * (1.0/3.0); |
|
|
194 | } |
|
|
195 | } |
|
|
196 | pixelnr += marker * 4; |
|
|
197 | datanr += 1; |
222 | datanr += 1; |
198 | } |
223 | } |
199 | } |
224 | } |
200 | if (!read_err && ((datanr != datasize) || (pixelnr != picsize))) { |
225 | if (!read_err && ((datanr != datasize) || (pixelnr != picsize))) { |
201 | psiconv_warn(lev+2,off+len,"Corrupted picture data!"); |
226 | psiconv_warn(lev+2,off+len,"Corrupted picture data!"); |
… | |
… | |
215 | |
240 | |
216 | return res; |
241 | return res; |
217 | } |
242 | } |
218 | |
243 | |
219 | |
244 | |
|
|
245 | |
|
|
246 | |
|
|
247 | int psiconv_parse_sketch_section(const psiconv_buffer buf, int lev, |
|
|
248 | psiconv_u32 off, int *length, int is_object, |
|
|
249 | psiconv_sketch_section *result) |
|
|
250 | { |
|
|
251 | int res=0; |
|
|
252 | int len=0; |
|
|
253 | psiconv_u32 temp; |
|
|
254 | int leng; |
|
|
255 | int i; |
|
|
256 | |
|
|
257 | psiconv_progress(lev+1,off,"Going to read the sketch section"); |
|
|
258 | *result = malloc(sizeof(**result)); |
|
|
259 | |
|
|
260 | if (!is_object) { |
|
|
261 | psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); |
|
|
262 | (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len); |
|
|
263 | psiconv_debug(lev+2,off+len,"Form hor. size: %04x", |
|
|
264 | (*result)->form_xsize); |
|
|
265 | len += 0x02; |
|
|
266 | psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); |
|
|
267 | (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len); |
|
|
268 | psiconv_debug(lev+2,off+len,"Form ver. size: %04x", |
|
|
269 | (*result)->form_ysize); |
|
|
270 | len += 0x02; |
|
|
271 | psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); |
|
|
272 | (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len); |
|
|
273 | psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x", |
|
|
274 | (*result)->picture_x_offset); |
|
|
275 | len += 0x02; |
|
|
276 | psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset"); |
|
|
277 | (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len); |
|
|
278 | psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x", |
|
|
279 | (*result)->picture_y_offset); |
|
|
280 | len += 0x02; |
|
|
281 | psiconv_progress(lev+2,off+len,"Going to skip 5 words of zeros"); |
|
|
282 | for (i = 0; i < 5; i++) { |
|
|
283 | temp = psiconv_read_u16(buf,lev+2,off+len); |
|
|
284 | if (temp != 0) { |
|
|
285 | psiconv_warn(lev+2,off+len, |
|
|
286 | "Unexpected value in sketch section preamble"); |
|
|
287 | psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i, |
|
|
288 | temp,0); |
|
|
289 | res = -1; |
|
|
290 | } |
|
|
291 | off += 0x02; |
|
|
292 | } |
|
|
293 | } else { |
|
|
294 | psiconv_progress(lev+2,off+len,"Going to read the displayed hor. size"); |
|
|
295 | (*result)->picture_xsize = psiconv_read_u16(buf,lev+2,off + len); |
|
|
296 | psiconv_debug(lev+2,off+len,"Displayed hor. size: %04x", |
|
|
297 | (*result)->picture_xsize); |
|
|
298 | len += 0x02; |
|
|
299 | psiconv_progress(lev+2,off+len,"Going to read the displayed ver. size"); |
|
|
300 | (*result)->picture_ysize = psiconv_read_u16(buf,lev+2,off + len); |
|
|
301 | psiconv_debug(lev+2,off+len,"Displayed ver. size: %04x", |
|
|
302 | (*result)->picture_ysize); |
|
|
303 | len += 0x02; |
|
|
304 | |
|
|
305 | psiconv_progress(lev+2,off+len,"Going to skip 2 words of zeros"); |
|
|
306 | for (i = 0; i < 2; i++) { |
|
|
307 | temp = psiconv_read_u16(buf,lev+2,off+len); |
|
|
308 | if (temp != 0) { |
|
|
309 | psiconv_warn(lev+2,off+len, |
|
|
310 | "Unexpected value in sketch section preamble"); |
|
|
311 | psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i, |
|
|
312 | temp,0); |
|
|
313 | res = -1; |
|
|
314 | } |
|
|
315 | off += 0x02; |
|
|
316 | } |
|
|
317 | psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); |
|
|
318 | (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len); |
|
|
319 | psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x", |
|
|
320 | (*result)->picture_x_offset); |
|
|
321 | len += 0x02; |
|
|
322 | psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset"); |
|
|
323 | (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len); |
|
|
324 | psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x", |
|
|
325 | (*result)->picture_y_offset); |
|
|
326 | len += 0x02; |
|
|
327 | psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); |
|
|
328 | (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len); |
|
|
329 | psiconv_debug(lev+2,off+len,"Form hor. size: %04x", |
|
|
330 | (*result)->form_xsize); |
|
|
331 | len += 0x02; |
|
|
332 | psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); |
|
|
333 | (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len); |
|
|
334 | psiconv_debug(lev+2,off+len,"Form ver. size: %04x", |
|
|
335 | (*result)->form_ysize); |
|
|
336 | len += 0x02; |
|
|
337 | psiconv_progress(lev+2,off+len,"Going to skip 1 zero word"); |
|
|
338 | temp = psiconv_read_u16(buf,lev+2,off+len); |
|
|
339 | if (temp != 0) { |
|
|
340 | psiconv_warn(lev+2,off+len, |
|
|
341 | "Unexpected value in sketch section preamble"); |
|
|
342 | psiconv_debug(lev+2,off+len,"Read %04x, expected %04x",i, temp,0); |
|
|
343 | res = -1; |
|
|
344 | } |
|
|
345 | off += 0x02; |
|
|
346 | } |
|
|
347 | |
|
|
348 | psiconv_progress(lev+2,off+len,"Going to read the picture data"); |
|
|
349 | res |= psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng, |
|
|
350 | &((*result)->picture)); |
|
|
351 | off += leng; |
|
|
352 | if (!is_object) { |
|
|
353 | (*result)->picture_xsize = (*result)->picture->xsize; |
|
|
354 | (*result)->picture_ysize = (*result)->picture->ysize; |
|
|
355 | } |
|
|
356 | |
|
|
357 | psiconv_progress(lev+2,off+len,"Going to read the hor. magnification"); |
|
|
358 | (*result)->magnification_x = psiconv_read_u16(buf,lev+2,off + len) / 1000.0; |
|
|
359 | psiconv_debug(lev+2,off+len,"Form hor. magnification: %f", |
|
|
360 | (*result)->magnification_x); |
|
|
361 | len += 0x02; |
|
|
362 | psiconv_progress(lev+2,off+len,"Going to read the ver. magnification"); |
|
|
363 | (*result)->magnification_y = psiconv_read_u16(buf,lev+2,off + len) / 1000.0; |
|
|
364 | psiconv_debug(lev+2,off+len,"Form ver. magnification: %f", |
|
|
365 | (*result)->magnification_y); |
|
|
366 | len += 0x02; |
|
|
367 | |
|
|
368 | psiconv_progress(lev+2,off+len,"Going to read the left cut"); |
|
|
369 | temp = psiconv_read_u32(buf,lev+2,off + len); |
|
|
370 | (*result)->cut_left = (temp * 6.0) / (*result)->picture_xsize; |
|
|
371 | psiconv_debug(lev+2,off+len,"Left cut: raw %08x, real: %f", |
|
|
372 | temp,(*result)->cut_left); |
|
|
373 | len += 0x04; |
|
|
374 | psiconv_progress(lev+2,off+len,"Going to read the right cut"); |
|
|
375 | temp = psiconv_read_u32(buf,lev+2,off + len); |
|
|
376 | (*result)->cut_right = (temp * 6.0) / (*result)->picture_xsize; |
|
|
377 | psiconv_debug(lev+2,off+len,"Right cut: raw %08x, real: %f", |
|
|
378 | temp,(*result)->cut_right); |
|
|
379 | len += 0x04; |
|
|
380 | psiconv_progress(lev+2,off+len,"Going to read the top cut"); |
|
|
381 | temp = psiconv_read_u32(buf,lev+2,off + len); |
|
|
382 | (*result)->cut_top = (temp * 6.0) / (*result)->picture_ysize; |
|
|
383 | psiconv_debug(lev+2,off+len,"Top cut: raw %08x, real: %f", |
|
|
384 | temp,(*result)->cut_top); |
|
|
385 | len += 0x04; |
|
|
386 | psiconv_progress(lev+2,off+len,"Going to read the bottom cut"); |
|
|
387 | temp = psiconv_read_u32(buf,lev+2,off + len); |
|
|
388 | (*result)->cut_bottom = (temp * 6.0) / (*result)->picture_ysize; |
|
|
389 | psiconv_debug(lev+2,off+len,"Bottom cut: raw %08x, real: %f", |
|
|
390 | temp,(*result)->cut_bottom); |
|
|
391 | len += 0x04; |
|
|
392 | |
|
|
393 | if (length) |
|
|
394 | *length = len; |
|
|
395 | |
|
|
396 | psiconv_progress(lev,off+len-1, |
|
|
397 | "End of sketch section (total length: %08x)", len); |
|
|
398 | |
|
|
399 | return res; |
|
|
400 | } |
|
|
401 | |