/[public]/psiconv/trunk/lib/psiconv/parse_image.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/parse_image.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.24  
changed lines
  Added in v.163

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26