/[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 45
21#include <stdlib.h> 21#include <stdlib.h>
22 22
23#include "data.h" 23#include "data.h"
24#include "parse_routines.h" 24#include "parse_routines.h"
25 25
26int psiconv_parse_mbm_jumptable_section(const psiconv_buffer buf,int lev, 26int psiconv_parse_jumptable_section(const psiconv_buffer buf,int lev,
27 psiconv_u32 off, int *length, 27 psiconv_u32 off, int *length,
28 psiconv_mbm_jumptable_section *result) 28 psiconv_jumptable_section *result)
29{ 29{
30 int res = 0; 30 int res = 0;
31 int len = 0; 31 int len = 0;
32 psiconv_u32 listlen,temp; 32 psiconv_u32 listlen,temp;
33 int i; 33 int i;
34 34
35 psiconv_progress(lev+1,off+len,"Going to read the mbm jumptable section"); 35 psiconv_progress(lev+1,off+len,"Going to read the jumptable section");
36 (*result) = psiconv_list_new(sizeof(psiconv_u32)); 36 (*result) = psiconv_list_new(sizeof(psiconv_u32));
37 37
38 psiconv_progress(lev+2,off+len,"Going to read the list length"); 38 psiconv_progress(lev+2,off+len,"Going to read the list length");
39 listlen = psiconv_read_u32(buf,lev+2,off+len); 39 listlen = psiconv_read_u32(buf,lev+2,off+len);
40 psiconv_debug(lev+2,off+len,"List length: %08x",listlen); 40 psiconv_debug(lev+2,off+len,"List length: %08x",listlen);
49 } 49 }
50 50
51 if (length) 51 if (length)
52 *length = len; 52 *length = len;
53 53
54 psiconv_progress(lev+1,off+len-1,"End of mbm jumptable section " 54 psiconv_progress(lev+1,off+len-1,"End of jumptable section "
55 "(total length: %08x)", len); 55 "(total length: %08x)", len);
56 56
57 return res; 57 return res;
58} 58}
59 59
60static int decode_byte(int lev, psiconv_u32 off,
61 psiconv_paint_data_section data, int *pixelnr,
62 psiconv_u8 byte, int bits_per_pixel, int linelen,
63 int *linepos,int picsize)
64{
65 int mask = (bits_per_pixel << 1) -1;
66 int i;
67 if (*linepos < (data->xsize + (8/bits_per_pixel) - 1) / (8/bits_per_pixel))
68 for (i = 0; i < 8/bits_per_pixel; i ++) {
69 if ((i != 0) && ((*pixelnr % (data->xsize)) == 0)) {
70 psiconv_debug(lev+1,off,"Skipping padding: %02x",byte);
71 i = 8;
72 } else if (*pixelnr >= picsize) {
73 psiconv_warn(lev+1,off,"Corrupted picture data!");
74 psiconv_debug(lev+1,off,"Trying to write a pixel too far");
75 return -1;
76 } else {
77 data->red[*pixelnr] = data->green[*pixelnr] = data->blue[*pixelnr] =
78 ((float) (byte & mask)) / ((1 << bits_per_pixel) -1);
79 psiconv_debug(lev+1,off,"Pixel %04x: (%04x,%04x) value %02x, color %f",
80 *pixelnr,*pixelnr % data->xsize,*pixelnr / data->xsize, byte&mask, data->red[*pixelnr]);
81 byte = byte >> bits_per_pixel;
82 (*pixelnr) ++;
83 }
84 }
85 else
86 psiconv_debug(lev+1,off,"Skipping padding byte");
87 (*linepos) ++;
88 if (*linepos == linelen)
89 *linepos = 0;
90 return 0;
91}
92
93
60int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, 94int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev,
61 psiconv_u32 off, int *length, 95 psiconv_u32 off, int *length,int isclipart,
62 psiconv_paint_data_section *result) 96 psiconv_paint_data_section *result)
63{ 97{
64 int res = 0; 98 int res = 0;
65 int len = 0; 99 int len = 0;
66 int read_err = 0; 100 int read_err = 0;
67 psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; 101 psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen;
68 psiconv_u8 marker; 102 psiconv_u8 marker;
69 int i; 103 int i,leng;
104 psiconv_u32 bits_per_pixel,compression;
105 int linepos = 0;
70 106
71 psiconv_progress(lev+1,off,"Going to read a paint data section"); 107 psiconv_progress(lev+1,off,"Going to read a paint data section");
72 (*result) = malloc(sizeof(**result)); 108 (*result) = malloc(sizeof(**result));
73 109
74 psiconv_progress(lev+2,off+len,"Going to read section size"); 110 psiconv_progress(lev+2,off+len,"Going to read section size");
95 psiconv_progress(lev+2,off+len,"Going to read picture Y size"); 131 psiconv_progress(lev+2,off+len,"Going to read picture Y size");
96 (*result)->ysize = psiconv_read_u32(buf,lev+2,off+len); 132 (*result)->ysize = psiconv_read_u32(buf,lev+2,off+len);
97 psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); 133 psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize);
98 len += 4; 134 len += 4;
99 135
100 picsize = (*result)->ysize * (*result)->xsize * 2; 136 picsize = (*result)->ysize * (*result)->xsize;
101 linelen = (*result)->xsize;
102 137
103 psiconv_progress(lev+2,off+len,"Going to read 6 unknown longs"); 138 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);
140 psiconv_debug(lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize);
141 len += leng;
142
143 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);
145 psiconv_debug(lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize);
146 len += leng;
147
148 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);
150 if (bits_per_pixel > 8) {
151 psiconv_warn(lev+2,off+len,"Picture has too many colors");
152 psiconv_debug(lev+2,off+len,"Read %d colorbits",bits_per_pixel);
153 res = -1;
154 bits_per_pixel = 2;
155 }
156 psiconv_debug(lev+2,off+len,"Bits per pixel: %d",bits_per_pixel);
157 len += 4;
158
104 for (i = 0 ; i < 6; i++) { 159 for (i = 0 ; i < 2; i++) {
105 temp = psiconv_read_u32(buf,lev+2,off+len); 160 temp = psiconv_read_u32(buf,lev+2,off+len);
106 if (temp != (i==2?0x02:i==5?0x01:0x00)) { 161 if (temp != 00) {
107 psiconv_warn(lev+2,off+len, 162 psiconv_warn(lev+2,off+len,
108 "Paint data section prologue has unknown values"); 163 "Paint data section prologue has unknown values");
109 psiconv_debug(lev+2,off+len, 164 psiconv_debug(lev+2,off+len,
110 "offset %02x: read %08x, expected %08x",i,temp, 165 "offset %02x: read %08x, expected %08x",i,temp, 0x00);
111 i==2?0x02:i==5?0x01:0x00); 166 res = -1;
167 }
168 len += 4;
169 }
170
171 psiconv_progress(lev+2,off+len,
172 "Going to read whether RLE compression is used");
173 compression=psiconv_read_u32(buf,lev+2,off+len);
174 if (compression > 1) {
175 psiconv_warn(lev+2,off+len,"Paint data section has unknown "
176 "compression type, assuming RLE");
177 psiconv_debug(lev+2,off+len,"Read compression type %d",compression);
178 compression = 1;
179 }
180 psiconv_debug(lev+2,off+len,"Compression: %s",compression?"RLE":"none");
181 len += 4;
182
183 if (isclipart) {
184 psiconv_progress(lev+2,off+len,"Going to read an unknown long");
185 temp = psiconv_read_u32(buf,lev+2,off+len);
186 if (temp != 0xffffffff) {
187 psiconv_warn(lev+2,off+len,
188 "Paint data section prologue has unknown values");
189 psiconv_debug(lev+2,off+len,
190 "offset %02x: read %08x, expected %08x",i,temp, 0xffffffff);
191 res = -1;
192 }
193 len += 4;
194 psiconv_progress(lev+2,off+len,"Going to read a second unknown long");
195 temp = psiconv_read_u32(buf,lev+2,off+len);
196 if (temp != 0x44) {
197 psiconv_warn(lev+2,off+len,
198 "Paint data section prologue has unknown values");
199 psiconv_debug(lev+2,off+len,
200 "offset %02x: read %08x, expected %08x",i,temp, 0x44);
112 res = -1; 201 res = -1;
113 } 202 }
114 len += 4; 203 len += 4;
115 } 204 }
116 205
117 (*result)->red = malloc(sizeof(float) * picsize); 206 (*result)->red = malloc(sizeof(float) * picsize);
118 (*result)->green = malloc(sizeof(float) * picsize); 207 (*result)->green = malloc(sizeof(float) * picsize);
119 (*result)->blue = malloc(sizeof(float) * picsize); 208 (*result)->blue = malloc(sizeof(float) * picsize);
120 len = offset; 209 len = offset;
121 datasize = size - len; 210 datasize = size - len;
211 if (isclipart)
212 len += 8;
122 213
123 psiconv_progress(lev+2,off+len,"Going to read the pixel data"); 214 psiconv_progress(lev+2,off+len,"Going to read the pixel data");
124 pixelnr = 0; 215 pixelnr = 0;
125 datanr = 0; 216 datanr = 0;
126 while ((datanr < datasize) && (pixelnr < picsize)) { 217 if (!compression) {
218 linelen = datasize / (*result)->ysize;
219 psiconv_debug(lev+3,off+len,"Line length: %04x bytes",linelen);
220 while((datanr < datasize)) {
221 temp = psiconv_read_u8(buf,lev+2,off+len+datanr);
222 if (decode_byte(lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel,
223 linelen,&linepos,picsize)) {
224 read_err = 1;
225 res = -1;
226 break;
227 }
228 datanr++;
229 }
230 } else {
231 psiconv_progress(lev+2,off+len,"First pass: determining line length");
232 datanr = 0;
233 i = 0;
234 while (datanr < datasize) {
127 marker = psiconv_read_u8(buf,lev+3,off+len+datanr); 235 marker = psiconv_read_u8(buf,lev+3,off+len+datanr);
236 if (marker >= 0x80) {
237 datanr += 0x100 - marker + 1;
238 i += 0x100 - marker;
239 } else {
240 datanr += 2;
241 i += marker + 1;
242 }
243 }
244 linelen = i / (*result)->ysize;
245 datanr=0;
246 psiconv_debug(lev+2,off+len,"Linelen: %04x bytes",linelen);
247 while((datanr < datasize)) {
248 marker = psiconv_read_u8(buf,lev+3,off+len+datanr);
128 psiconv_debug(lev+3,off+len+datanr, 249 psiconv_debug(lev+3,off+len+datanr,
129 "Pixelnr %08x, Datanr %08x: Read marker %02x", 250 "Pixelnr %08x, Datanr %08x: Read marker %02x",
130 pixelnr,datanr,marker); 251 pixelnr,datanr,marker);
131 datanr ++; 252 datanr ++;
132 if (marker >= 0x80) { 253 if (marker >= 0x80) {
133 /* 0x100 - marker bytes of data follow */ 254 /* 0x100 - marker bytes of data follow */
134 for (i = 0; i < 0x100-marker; i++,datanr++) { 255 for (i = 0; i < 0x100-marker; i++,datanr++) {
135 if ((picsize == pixelnr) || (datasize == datanr)) { 256 if (datanr >= datasize) {
257 psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data");
258 psiconv_debug(lev+3,off+len+datanr,
259 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
260 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr,
261 datanr,marker);
262 read_err = 1;
263 res = -1;
264 break;
265 }
266 temp = psiconv_read_u8(buf,lev+2,off+len+datanr);
267 if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp,
268 bits_per_pixel,linelen,&linepos,picsize)) {
269 res = -1;
270 read_err = 1;
271 break;
272 }
273 }
274 } else {
275 if (datanr >= datasize) {
136 psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); 276 psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data");
137 psiconv_debug(lev+3,off+len+datanr, 277 psiconv_debug(lev+3,off+len+datanr,
138 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," 278 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
139 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, 279 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr,
140 datanr,marker); 280 datanr,marker);
141 read_err = 1; 281 read_err = 1;
142 res = -1; 282 res = -1;
143 break; 283 } else {
144 }
145 temp = psiconv_read_u8(buf,lev+3,off+len+datanr); 284 temp = psiconv_read_u8(buf,lev+3,off+len+datanr);
146 (*result)->red[pixelnr] = 285 for (i = 0; i <= marker; i++) {
147 (*result)->green[pixelnr] = 286 if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp,
148 (*result)->blue[pixelnr] = 287 bits_per_pixel,linelen,&linepos,picsize)) {
149 (temp & 0x03) * (1.0/3.0); 288 read_err = 1;
150 pixelnr ++; 289 res = -1;
151 if (pixelnr % linelen == 0) 290 break;
152 continue; 291 }
153 (*result)->red[pixelnr] = 292 }
154 (*result)->green[pixelnr] = 293 datanr ++;
155 (*result)->blue[pixelnr] = 294 }
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 } 295 }
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 } 296 }
222 datanr += 1; 297 }
298
299 if (linepos >= ((*result)->xsize + (8/bits_per_pixel) - 1) /
300 (8/bits_per_pixel))
301 datanr += (linelen - linepos);
223 } 302
224 }
225 if (!read_err && ((datanr != datasize) || (pixelnr != picsize))) { 303 if (!read_err && ((datanr != datasize) || (pixelnr != picsize))) {
226 psiconv_warn(lev+2,off+len,"Corrupted picture data!"); 304 psiconv_warn(lev+2,off+len,"Corrupted picture data!");
227 psiconv_debug(lev+3,off+len+datanr, 305 psiconv_debug(lev+3,off+len+datanr,
228 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," 306 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
229 "Datanr: %08x",picsize,datasize,pixelnr,datanr); 307 "Datanr: %08x",picsize,datasize,pixelnr,datanr);
238 psiconv_progress(lev+1,off+len-1,"End of paint data section " 316 psiconv_progress(lev+1,off+len-1,"End of paint data section "
239 "(total length: %08x)", len); 317 "(total length: %08x)", len);
240 318
241 return res; 319 return res;
242} 320}
243
244
245
246 321
247int psiconv_parse_sketch_section(const psiconv_buffer buf, int lev, 322int psiconv_parse_sketch_section(const psiconv_buffer buf, int lev,
248 psiconv_u32 off, int *length, int is_object, 323 psiconv_u32 off, int *length, int is_object,
249 psiconv_sketch_section *result) 324 psiconv_sketch_section *result)
250{ 325{
344 } 419 }
345 off += 0x02; 420 off += 0x02;
346 } 421 }
347 422
348 psiconv_progress(lev+2,off+len,"Going to read the picture data"); 423 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, 424 res |= psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,0,
350 &((*result)->picture)); 425 &((*result)->picture));
351 off += leng; 426 off += leng;
352 if (!is_object) { 427 if (!is_object) {
353 (*result)->picture_xsize = (*result)->picture->xsize; 428 (*result)->picture_xsize = (*result)->picture->xsize;
354 (*result)->picture_ysize = (*result)->picture->ysize; 429 (*result)->picture_ysize = (*result)->picture->ysize;
397 "End of sketch section (total length: %08x)", len); 472 "End of sketch section (total length: %08x)", len);
398 473
399 return res; 474 return res;
400} 475}
401 476
477
478int psiconv_parse_clipart_section(const psiconv_buffer buf,int lev,
479 psiconv_u32 off, int *length,
480 psiconv_clipart_section *result)
481{
482 int res=0;
483 int len=0;
484 int leng;
485 psiconv_u32 temp;
486
487 psiconv_progress(lev+1,off+len,"Going to read the clipart section");
488 *result = malloc(sizeof(**result));
489
490 psiconv_progress(lev+2,off+len,"Going to read the section ID");
491 temp = psiconv_read_u32(buf,lev+2,off+len);
492 if (temp != PSICONV_ID_CLIPART_ITEM) {
493 psiconv_warn(lev+2,off+len,
494 "Unexpected value in clipart section preamble");
495 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp,
496 PSICONV_ID_CLIPART_ITEM);
497 res = -1;
498 } else
499 psiconv_debug(lev+2,off+len,"Clipart ID: %08x", temp);
500 off += 4;
501
502 psiconv_progress(lev+2,off+len,"Going to read an unknown long");
503 temp = psiconv_read_u32(buf,lev+2,off+len);
504 if (temp != 0x02) {
505 psiconv_warn(lev+2,off+len,
506 "Unexpected value in clipart section preamble");
507 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp,
508 0x02);
509 res = -1;
510 } else
511 psiconv_debug(lev+2,off+len,"First unknown long: %08x", temp);
512 off += 4;
513
514 psiconv_progress(lev+2,off+len,"Going to read a second unknown long");
515 temp = psiconv_read_u32(buf,lev+2,off+len);
516 if (temp != 0) {
517 psiconv_warn(lev+2,off+len,
518 "Unexpected value in clipart section preamble");
519 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0);
520 res = -1;
521 } else
522 psiconv_debug(lev+2,off+len,"Second unknown long: %08x", temp);
523 off += 4;
524
525 psiconv_progress(lev+2,off+len,"Going to read a third unknown long");
526 temp = psiconv_read_u32(buf,lev+2,off+len);
527 if (temp != 0) {
528 psiconv_warn(lev+2,off+len,
529 "Unexpected value in clipart section preamble");
530 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0);
531 res = -1;
532 } else
533 psiconv_debug(lev+2,off+len,"Third unknown long: %08x", temp);
534 off += 4;
535
536 psiconv_progress(lev+2,off+len,"Going to read a fourth unknown long");
537 temp = psiconv_read_u32(buf,lev+2,off+len);
538 if ((temp != 0x0c) && (temp != 0x08)) {
539 psiconv_warn(lev+2,off+len,
540 "Unexpected value in clipart section preamble");
541 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x or %08x",temp,
542 0x0c, 0x08);
543 res = -1;
544 } else
545 psiconv_debug(lev+2,off+len,"Fourth unknown long: %08x", temp);
546 off += 4;
547
548 psiconv_progress(lev+2,off+len,"Going to read the Paint Data Section");
549 res |= psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,1,
550 &((*result)->picture));
551 len += leng;
552
553 if (length)
554 *length = len;
555
556 psiconv_progress(lev,off+len-1,
557 "End of clipart section (total length: %08x)", len);
558
559 return res;
560}

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

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