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

Legend:
Removed from v.25  
changed lines
  Added in v.97

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