/[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 65 Revision 196
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, 2000 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 1999-2004 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"
27#include "image.h"
25 28
29#ifdef DMALLOC
30#include <dmalloc.h>
31#endif
32
33static int psiconv_decode_rle8 (const psiconv_config config, int lev,
34 psiconv_u32 off,
35 const psiconv_pixel_bytes encoded,
36 psiconv_pixel_bytes *decoded);
37
38static int psiconv_decode_rle12 (const psiconv_config config, int lev,
39 psiconv_u32 off,
40 const psiconv_pixel_bytes encoded,
41 psiconv_pixel_bytes *decoded);
42
43static int psiconv_decode_rle16 (const psiconv_config config, int lev,
44 psiconv_u32 off,
45 const psiconv_pixel_bytes encoded,
46 psiconv_pixel_bytes *decoded);
47
48static int psiconv_decode_rle24 (const psiconv_config config, int lev,
49 psiconv_u32 off,
50 const psiconv_pixel_bytes encoded,
51 psiconv_pixel_bytes *decoded);
52
53static int psiconv_bytes_to_pixel_data(const psiconv_config config,
54 int lev, psiconv_u32 off,
55 const psiconv_pixel_bytes bytes,
56 psiconv_pixel_ints *pixels,
57 int colordepth, int xsize, int ysize);
58
59static int psiconv_pixel_data_to_floats (const psiconv_config config, int lev,
60 psiconv_u32 off,
61 const psiconv_pixel_ints pixels,
62 psiconv_pixel_floats_t *floats,
63 int colordepth, int color,
64 int redbits, int bluebits, int greenbits,
65 const psiconv_pixel_floats_t palet);
66
67
68
26int psiconv_parse_jumptable_section(const psiconv_buffer buf,int lev, 69int psiconv_parse_jumptable_section(const psiconv_config config,
70 const psiconv_buffer buf,int lev,
27 psiconv_u32 off, int *length, 71 psiconv_u32 off, int *length,
28 psiconv_jumptable_section *result) 72 psiconv_jumptable_section *result)
29{ 73{
30 int res = 0; 74 int res = 0;
31 int len = 0; 75 int len = 0;
32 psiconv_u32 listlen,temp; 76 psiconv_u32 listlen,temp;
33 int i; 77 int i;
34 78
35 psiconv_progress(lev+1,off+len,"Going to read the jumptable section"); 79 psiconv_progress(config,lev+1,off+len,"Going to read the jumptable section");
36 if (!((*result) = psiconv_list_new(sizeof(psiconv_u32)))) 80 if (!((*result) = psiconv_list_new(sizeof(psiconv_u32))))
37 goto ERROR1; 81 goto ERROR1;
38 82
39 psiconv_progress(lev+2,off+len,"Going to read the list length"); 83 psiconv_progress(config,lev+2,off+len,"Going to read the list length");
40 listlen = psiconv_read_u32(buf,lev+2,off+len,&res); 84 listlen = psiconv_read_u32(config,buf,lev+2,off+len,&res);
41 if (res) 85 if (res)
42 goto ERROR2; 86 goto ERROR2;
43 psiconv_debug(lev+2,off+len,"List length: %08x",listlen); 87 psiconv_debug(config,lev+2,off+len,"List length: %08x",listlen);
44 len += 4; 88 len += 4;
45 89
46 psiconv_progress(lev+2,off+len,"Going to read the list"); 90 psiconv_progress(config,lev+2,off+len,"Going to read the list");
47 for (i = 0; i < listlen; i++) { 91 for (i = 0; i < listlen; i++) {
48 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 92 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
49 if (res) 93 if (res)
50 goto ERROR2; 94 goto ERROR2;
51 if ((res = psiconv_list_add(*result,&temp))) 95 if ((res = psiconv_list_add(*result,&temp)))
52 goto ERROR2; 96 goto ERROR2;
53 psiconv_debug(lev+3,off+len,"Offset: %08x",temp); 97 psiconv_debug(config,lev+3,off+len,"Offset: %08x",temp);
54 len += 4; 98 len += 4;
55 } 99 }
56 100
57 if (length) 101 if (length)
58 *length = len; 102 *length = len;
59 103
60 psiconv_progress(lev+1,off+len-1,"End of jumptable section " 104 psiconv_progress(config,lev+1,off+len-1,"End of jumptable section "
61 "(total length: %08x)", len); 105 "(total length: %08x)", len);
62 106
63 return 0; 107 return 0;
64 108
65ERROR2: 109ERROR2:
66 psiconv_list_free(*result); 110 psiconv_list_free(*result);
67ERROR1: 111ERROR1:
68 psiconv_warn(lev+1,off,"Reading of Jumptable Section failed"); 112 psiconv_error(config,lev+1,off,"Reading of Jumptable Section failed");
69 if (length) 113 if (length)
70 *length = 0; 114 *length = 0;
71 if (!res) 115 if (!res)
72 return -PSICONV_E_NOMEM; 116 return -PSICONV_E_NOMEM;
73 else 117 else
74 return res; 118 return res;
75} 119}
76 120
77static int decode_byte(int lev, psiconv_u32 off,
78 psiconv_paint_data_section data, int *pixelnr,
79 psiconv_u8 byte, int bits_per_pixel, int linelen,
80 int *linepos,int picsize)
81{
82 int mask = (bits_per_pixel << 1) -1;
83 int i;
84 if (*linepos < (data->xsize + (8/bits_per_pixel) - 1) / (8/bits_per_pixel))
85 for (i = 0; i < 8/bits_per_pixel; i ++) {
86 if ((i != 0) && ((*pixelnr % (data->xsize)) == 0)) {
87 psiconv_debug(lev+1,off,"Skipping padding: %02x",byte);
88 i = 8;
89 } else if (*pixelnr >= picsize) {
90 psiconv_warn(lev+1,off,"Corrupted picture data!");
91 psiconv_debug(lev+1,off,"Trying to write a pixel too far");
92 return -1;
93 } else {
94 data->red[*pixelnr] = data->green[*pixelnr] = data->blue[*pixelnr] =
95 ((float) (byte & mask)) / ((1 << bits_per_pixel) -1);
96 psiconv_debug(lev+1,off,"Pixel %04x: (%04x,%04x) value %02x, color %f",
97 *pixelnr,*pixelnr % data->xsize,
98 *pixelnr / data->xsize, byte&mask, data->red[*pixelnr]);
99 byte = byte >> bits_per_pixel;
100 (*pixelnr) ++;
101 }
102 }
103 else
104 psiconv_debug(lev+1,off,"Skipping padding byte");
105 (*linepos) ++;
106 if (*linepos == linelen)
107 *linepos = 0;
108 return 0;
109}
110
111
112int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, 121int psiconv_parse_paint_data_section(const psiconv_config config,
122 const psiconv_buffer buf,int lev,
113 psiconv_u32 off, int *length,int isclipart, 123 psiconv_u32 off, int *length,int isclipart,
114 psiconv_paint_data_section *result) 124 psiconv_paint_data_section *result)
115{ 125{
116 int res = 0; 126 int res = 0;
117 int len = 0; 127 int len = 0;
118 psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; 128 psiconv_u32 size,offset,picsize,temp,datasize,color,
129 redbits,bluebits,greenbits;
119 psiconv_u8 marker; 130 psiconv_u8 byte;
120 int i,leng; 131 int leng,i;
121 psiconv_u32 bits_per_pixel,compression; 132 psiconv_u32 bits_per_pixel,compression;
122 int linepos = 0; 133 psiconv_pixel_bytes bytes,decoded;
134 psiconv_pixel_ints pixels;
135 psiconv_pixel_floats_t floats,palet;
123 136
124 psiconv_progress(lev+1,off,"Going to read a paint data section"); 137 psiconv_progress(config,lev+1,off,"Going to read a paint data section");
125 if (!((*result) = malloc(sizeof(**result)))) 138 if (!((*result) = malloc(sizeof(**result))))
126 goto ERROR1; 139 goto ERROR1;
127 140
141 if (!(bytes = psiconv_list_new(sizeof(psiconv_u8))))
142 goto ERROR2;
143
128 psiconv_progress(lev+2,off+len,"Going to read section size"); 144 psiconv_progress(config,lev+2,off+len,"Going to read section size");
129 size = psiconv_read_u32(buf,lev+2,off+len,&res); 145 size = psiconv_read_u32(config,buf,lev+2,off+len,&res);
130 if (res) 146 if (res)
131 goto ERROR2; 147 goto ERROR3;
132 psiconv_debug(lev+2,off+len,"Section size: %08x",size); 148 psiconv_debug(config,lev+2,off+len,"Section size: %08x",size);
133 len += 4; 149 len += 4;
134 150
135 psiconv_progress(lev+2,off+len,"Going to read pixel data offset"); 151 psiconv_progress(config,lev+2,off+len,"Going to read pixel data offset");
136 offset = psiconv_read_u32(buf,lev+2,off+len,&res); 152 offset = psiconv_read_u32(config,buf,lev+2,off+len,&res);
137 if (res) 153 if (res)
138 goto ERROR2; 154 goto ERROR3;
139 if (offset != 0x28) { 155 if (offset != 0x28) {
140 psiconv_warn(lev+2,off+len, 156 psiconv_error(config,lev+2,off+len,
141 "Paint data section data offset has unexpected value"); 157 "Paint data section data offset has unexpected value");
142 psiconv_debug(lev+2,off+len, 158 psiconv_debug(config,lev+2,off+len,
143 "Data offset: read %08x, expected %08x",offset,0x28); 159 "Data offset: read %08x, expected %08x",offset,0x28);
144 res = -1; 160 res = -1;
145 } 161 }
146 len += 4; 162 len += 4;
147 163
148 psiconv_progress(lev+2,off+len,"Going to read picture X size"); 164 psiconv_progress(config,lev+2,off+len,"Going to read picture X size");
149 (*result)->xsize = psiconv_read_u32(buf,lev+2,off+len,&res); 165 (*result)->xsize = psiconv_read_u32(config,buf,lev+2,off+len,&res);
150 if (res) 166 if (res)
151 goto ERROR2; 167 goto ERROR3;
152 psiconv_debug(lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); 168 psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize);
153 len += 4; 169 len += 4;
154 170
155 psiconv_progress(lev+2,off+len,"Going to read picture Y size"); 171 psiconv_progress(config,lev+2,off+len,"Going to read picture Y size");
156 (*result)->ysize = psiconv_read_u32(buf,lev+2,off+len,&res); 172 (*result)->ysize = psiconv_read_u32(config,buf,lev+2,off+len,&res);
157 if (res) 173 if (res)
158 goto ERROR2; 174 goto ERROR3;
159 psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); 175 psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize);
160 len += 4; 176 len += 4;
161 177
162 picsize = (*result)->ysize * (*result)->xsize; 178 picsize = (*result)->ysize * (*result)->xsize;
163 179
164 psiconv_progress(lev+2,off+len,"Going to read the real picture x size"); 180 psiconv_progress(config,lev+2,off+len,"Going to read the real picture x size");
165 (*result)->pic_xsize = psiconv_read_length(buf,lev+2,off+len,&leng,&res); 181 (*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
166 if (res) 182 if (res)
167 goto ERROR2; 183 goto ERROR3;
168 psiconv_debug(lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize); 184 psiconv_debug(config,lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize);
169 len += leng; 185 len += leng;
170 186
171 psiconv_progress(lev+2,off+len,"Going to read the real picture y size"); 187 psiconv_progress(config,lev+2,off+len,"Going to read the real picture y size");
172 (*result)->pic_ysize = psiconv_read_length(buf,lev+2,off+len,&leng,&res); 188 (*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
173 if (res) 189 if (res)
174 goto ERROR2; 190 goto ERROR3;
175 psiconv_debug(lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize); 191 psiconv_debug(config,lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize);
176 len += leng; 192 len += leng;
177 193
178 psiconv_progress(lev+2,off+len,"Going to read the number of bits per pixel"); 194 psiconv_progress(config,lev+2,off+len,"Going to read the number of bits per pixel");
179 bits_per_pixel=psiconv_read_u32(buf,lev+2,off+len,&res); 195 bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res);
180 if (res) 196 if (res)
181 goto ERROR2; 197 goto ERROR3;
182 if (bits_per_pixel > 8) {
183 psiconv_warn(lev+2,off+len,"Picture has too many colors");
184 psiconv_debug(lev+2,off+len,"Read %d colorbits",bits_per_pixel);
185 res = -PSICONV_E_PARSE;
186 goto ERROR2;
187 }
188 psiconv_debug(lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); 198 psiconv_debug(config,lev+2,off+len,"Bits per pixel: %d",bits_per_pixel);
189 len += 4; 199 len += 4;
190 200
191 for (i = 0 ; i < 2; i++) { 201 psiconv_progress(config,lev+2,off+len,
202 "Going to read whether this is a colour or greyscale picture");
203 color = psiconv_read_u32(config,buf,lev+2,off+len,&res);
204 if (res)
205 goto ERROR3;
206 if ((color != 0) && (color != 1)) {
207 psiconv_warn(config,lev+2,off+len,
208 "Paint data section unknown color type (ignored)");
209 psiconv_debug(config,lev+2,off+len,
210 "Color: read %08x, expected %08x or %08x",color,0,1);
211 color = 1;
212 } else {
213 psiconv_debug(config,lev+2,off+len,"Color: %08x (%s picture)",
214 color,(color?"color":"greyscale"));
215 }
216 len += 4;
217
218 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
219 if (res)
220 goto ERROR3;
221 if (temp != 00) {
222 psiconv_warn(config,lev+2,off+len,
223 "Paint data section prologue has unknown values (ignored)");
224 psiconv_debug(config,lev+2,off+len,
225 "read %08x, expected %08x",temp, 0x00);
226 }
227 len += 4;
228
229 psiconv_progress(config,lev+2,off+len,
230 "Going to read whether RLE compression is used");
231 compression=psiconv_read_u32(config,buf,lev+2,off+len,&res);
232 if (res)
233 goto ERROR3;
234 if (compression > 4) {
235 psiconv_warn(config,lev+2,off+len,"Paint data section has unknown "
236 "compression type, assuming RLE");
237 psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression);
238 compression = 0;
239 }
240 psiconv_debug(config,lev+2,off+len,"Compression: %s",
241 compression == 4?"RLE24":compression == 3?"RLE16":
242 compression == 2?"RLE12":compression == 1?"RLE8":"none");
243 len += 4;
244
245 if (isclipart) {
246 psiconv_progress(config,lev+2,off+len,"Going to read an unknown long");
192 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 247 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
193 if (res) 248 if (res)
194 goto ERROR2; 249 goto ERROR3;
195 if (temp != 00) { 250 if (temp != 0xffffffff) {
196 psiconv_warn(lev+2,off+len, 251 psiconv_warn(config,lev+2,off+len,
197 "Paint data section prologue has unknown values (ignored)"); 252 "Paint data section prologue has unknown values (ignoring)");
198 psiconv_debug(lev+2,off+len, 253 psiconv_debug(config,lev+2,off+len,
199 "offset %02x: read %08x, expected %08x",i,temp, 0x00); 254 "Read %08x, expected %08x",temp, 0xffffffff);
200 } 255 }
201 len += 4; 256 len += 4;
202 }
203
204 psiconv_progress(lev+2,off+len,
205 "Going to read whether RLE compression is used");
206 compression=psiconv_read_u32(buf,lev+2,off+len,&res);
207 if (res)
208 goto ERROR2;
209 if (compression > 1) {
210 psiconv_warn(lev+2,off+len,"Paint data section has unknown "
211 "compression type, assuming RLE");
212 psiconv_debug(lev+2,off+len,"Read compression type %d",compression);
213 compression = 1;
214 }
215 psiconv_debug(lev+2,off+len,"Compression: %s",compression?"RLE":"none");
216 len += 4;
217
218 if (isclipart) {
219 psiconv_progress(lev+2,off+len,"Going to read an unknown long"); 257 psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long");
220 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 258 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
221 if (res) 259 if (res)
222 goto ERROR2; 260 goto ERROR3;
223 if (temp != 0xffffffff) { 261 if (temp != 0x44) {
224 psiconv_warn(lev+2,off+len, 262 psiconv_warn(config,lev+2,off+len,
225 "Paint data section prologue has unknown values (ignoring)"); 263 "Paint data section prologue has unknown values (ignoring)");
226 psiconv_debug(lev+2,off+len, 264 psiconv_debug(config,lev+2,off+len,
227 "offset %02x: read %08x, expected %08x",i,temp, 0xffffffff); 265 "read %08x, expected %08x",temp, 0x44);
228 } 266 }
229 len += 4; 267 len += 4;
230 psiconv_progress(lev+2,off+len,"Going to read a second unknown long");
231 temp = psiconv_read_u32(buf,lev+2,off+len,&res);
232 if (res)
233 goto ERROR2;
234 if (temp != 0x44) {
235 psiconv_warn(lev+2,off+len,
236 "Paint data section prologue has unknown values (ignoring)");
237 psiconv_debug(lev+2,off+len,
238 "offset %02x: read %08x, expected %08x",i,temp, 0x44);
239 } 268 }
240 len += 4;
241 }
242 269
243 if (!((*result)->red = malloc(sizeof(float) * picsize)))
244 goto ERROR2;
245 if (!((*result)->green = malloc(sizeof(float) * picsize)))
246 goto ERROR3;
247 if (!((*result)->blue = malloc(sizeof(float) * picsize)))
248 goto ERROR4;
249 len = offset; 270 len = offset;
250 datasize = size - len; 271 datasize = size - len;
251 if (isclipart) 272 if (isclipart)
252 len += 8; 273 len += 8;
253 274
275 if (color || (bits_per_pixel != 2))
276 psiconv_warn(config,lev+2,off+len,
277 "All image types except 2-bit greyscale are experimental!");
278
254 psiconv_progress(lev+2,off+len,"Going to read the pixel data"); 279 psiconv_progress(config,lev+2,off+len,"Going to read the pixel data");
255 pixelnr = 0; 280 for (i = 0; i < datasize; i++) {
256 datanr = 0;
257 if (!compression) {
258 linelen = datasize / (*result)->ysize;
259 psiconv_debug(lev+3,off+len,"Line length: %04x bytes",linelen);
260 while((datanr < datasize)) {
261 temp = psiconv_read_u8(buf,lev+2,off+len+datanr,&res); 281 byte = psiconv_read_u8(config,buf,lev+2,off+len+i,&res);
282 psiconv_debug(config,lev+2,off+len+i,
283 "Pixel byte %04x of %04x has value %02x",
284 i,datasize,byte);
262 if (res) 285 if (res)
286 goto ERROR3;
287 psiconv_list_add(bytes,&byte);
288 }
289 len += datasize;
290
291 switch(compression) {
292 case 1:
293 if ((res = psiconv_decode_rle8(config,lev+2,off+len,bytes,&decoded)))
294 goto ERROR3;
295 psiconv_list_free(bytes);
296 bytes = decoded;
297 break;
298 case 2:
299 if ((psiconv_decode_rle12(config,lev+2,off+len,bytes,&decoded)))
300 goto ERROR3;
301 psiconv_list_free(bytes);
302 bytes = decoded;
303 break;
304 case 3:
305 if ((psiconv_decode_rle16(config,lev+2,off+len,bytes,&decoded)))
263 goto ERROR5; 306 goto ERROR3;
264 if (decode_byte(lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel, 307 psiconv_list_free(bytes);
265 linelen,&linepos,picsize)) { 308 bytes = decoded;
266 res = -PSICONV_E_PARSE;
267 break; 309 break;
268 } 310 case 4:
269 datanr++; 311 if ((psiconv_decode_rle24(config,lev+2,off+len,bytes,&decoded)))
312 goto ERROR3;
313 psiconv_list_free(bytes);
314 bytes = decoded;
315 break;
316 }
317
318 if ((res = psiconv_bytes_to_pixel_data(config,lev+2,off+len,bytes,
319 &pixels,bits_per_pixel,
320 (*result)->xsize,(*result)->ysize)))
321 goto ERROR3;
322
323 /* Use some heuristics; things may get unexpected around here */
324 bluebits = redbits = greenbits = 0;
325 palet = psiconv_palet_none;
326 if (color) {
327 if (bits_per_pixel == 4)
328 palet = psiconv_palet_color_4;
329 else if (bits_per_pixel == 8)
330 palet = psiconv_palet_color_8;
331 else {
332 redbits = (bits_per_pixel+2) / 3;
333 greenbits = (bits_per_pixel+2) / 3;
334 bluebits = bits_per_pixel - redbits - greenbits;
270 } 335 }
271 } else {
272 psiconv_progress(lev+2,off+len,"First pass: determining line length");
273 datanr = 0;
274 i = 0;
275 while (datanr < datasize) {
276 marker = psiconv_read_u8(buf,lev+3,off+len+datanr,&res);
277 if (res)
278 goto ERROR5;
279 if (marker >= 0x80) {
280 datanr += 0x100 - marker + 1;
281 i += 0x100 - marker;
282 } else {
283 datanr += 2;
284 i += marker + 1;
285 }
286 } 336 }
287 linelen = i / (*result)->ysize; 337 if ((res = psiconv_pixel_data_to_floats(config,lev+2,off+len,pixels,
288 datanr=0; 338 &floats,bits_per_pixel,color,
289 psiconv_debug(lev+2,off+len,"Linelen: %04x bytes",linelen); 339 redbits,greenbits,bluebits,palet)))
290 while((datanr < datasize)) {
291 marker = psiconv_read_u8(buf,lev+3,off+len+datanr,&res);
292 if (res)
293 goto ERROR5;
294 psiconv_debug(lev+3,off+len+datanr,
295 "Pixelnr %08x, Datanr %08x: Read marker %02x",
296 pixelnr,datanr,marker);
297 datanr ++;
298 if (marker >= 0x80) {
299 /* 0x100 - marker bytes of data follow */
300 for (i = 0; i < 0x100-marker; i++,datanr++) {
301 if (datanr >= datasize) {
302 psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data");
303 psiconv_debug(lev+3,off+len+datanr,
304 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
305 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr,
306 datanr,marker);
307 res = -PSICONV_E_PARSE;
308 break;
309 }
310 temp = psiconv_read_u8(buf,lev+2,off+len+datanr,&res);
311 if (res)
312 goto ERROR5;
313 if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp,
314 bits_per_pixel,linelen,&linepos,picsize)) {
315 res = -PSICONV_E_PARSE;
316 break;
317 }
318 }
319 } else {
320 if (datanr >= datasize) {
321 psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data");
322 psiconv_debug(lev+3,off+len+datanr,
323 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
324 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr,
325 datanr,marker);
326 res = -PSICONV_E_PARSE;
327 } else {
328 temp = psiconv_read_u8(buf,lev+3,off+len+datanr,&res);
329 if (res)
330 goto ERROR5;
331 for (i = 0; i <= marker; i++) {
332 if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp,
333 bits_per_pixel,linelen,&linepos,picsize)) {
334 res = -PSICONV_E_PARSE;
335 break;
336 }
337 }
338 datanr ++;
339 }
340 }
341 }
342 }
343
344 if (linepos >= ((*result)->xsize + (8/bits_per_pixel) - 1) /
345 (8/bits_per_pixel))
346 datanr += (linelen - linepos);
347
348 if (res || (datanr != datasize) || (pixelnr != picsize)) {
349 psiconv_warn(lev+2,off+len,"Corrupted picture data!");
350 psiconv_debug(lev+3,off+len+datanr,
351 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
352 "Datanr: %08x",picsize,datasize,pixelnr,datanr);
353 goto ERROR5; 340 goto ERROR4;
354 } 341
342 (*result)->red = floats.red;
343 (*result)->green = floats.green;
344 (*result)->blue = floats.blue;
355 345
356 len += datanr; 346 psiconv_list_free(bytes);
347 psiconv_list_free(pixels);
348
357 349
358 if (length) 350 if (length)
359 *length = len; 351 *length = len;
360 352
361 psiconv_progress(lev+1,off+len-1,"End of paint data section " 353 psiconv_progress(config,lev,off+len-1,
362 "(total length: %08x)", len); 354 "End of Paint Data Section (total length: %08x)", len);
363 355
364 return res; 356 return 0;
365 357
366ERROR5:
367 free((*result)->blue);
368ERROR4: 358ERROR4:
369 free((*result)->green); 359 psiconv_list_free(pixels);
370ERROR3: 360ERROR3:
371 free((*result)->red); 361 psiconv_list_free(bytes);
372ERROR2: 362ERROR2:
373 free (*result); 363 free(*result);
374ERROR1: 364ERROR1:
375 psiconv_warn(lev+1,off,"Reading of Paint Data Section failed"); 365 psiconv_error(config,lev+1,off,"Reading of Paint Data Section failed");
376 if (length) 366 if (length)
377 *length = 0; 367 *length = 0;
378 if (!res) 368 if (!res)
379 return -PSICONV_E_NOMEM; 369 return -PSICONV_E_NOMEM;
380 else 370 else
381 return res; 371 return res;
382} 372}
383 373
384int psiconv_parse_sketch_section(const psiconv_buffer buf, int lev, 374int psiconv_parse_sketch_section(const psiconv_config config,
375 const psiconv_buffer buf, int lev,
385 psiconv_u32 off, int *length, int is_object, 376 psiconv_u32 off, int *length,
386 psiconv_sketch_section *result) 377 psiconv_sketch_section *result)
387{ 378{
388 int res=0; 379 int res=0;
389 int len=0; 380 int len=0;
390 psiconv_u32 temp; 381 psiconv_u32 temp;
391 int leng; 382 int leng;
392 int i;
393 383
394 psiconv_progress(lev+1,off,"Going to read the sketch section"); 384 psiconv_progress(config,lev+1,off,"Going to read the sketch section");
395 if (!(*result = malloc(sizeof(**result)))) 385 if (!(*result = malloc(sizeof(**result))))
396 goto ERROR1; 386 goto ERROR1;
397 387
398 if (!is_object) {
399 psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); 388 psiconv_progress(config,lev+2,off+len,"Going to read the displayed hor. size");
400 (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); 389 (*result)->displayed_xsize = psiconv_read_u16(config,buf,lev+2,off + len,&res);
401 if (res) 390 if (res)
402 goto ERROR2; 391 goto ERROR2;
392 psiconv_debug(config,lev+2,off+len,"Displayed hor. size: %04x",
393 (*result)->displayed_xsize);
394 len += 0x02;
395 psiconv_progress(config,lev+2,off+len,"Going to read displayed ver. size");
396 (*result)->displayed_ysize = psiconv_read_u16(config,buf,lev+2,off + len,&res);
397 if (res)
398 goto ERROR2;
399 psiconv_debug(config,lev+2,off+len,"Displayed ver. size: %04x",
400 (*result)->displayed_ysize);
401 len += 0x02;
402
403 psiconv_progress(config,lev+2,off+len,"Going to read the data hor. offset");
404 (*result)->picture_data_x_offset = psiconv_read_u16(config,buf,lev+2,off + len,
405 &res);
406 if (res)
407 goto ERROR2;
408 psiconv_debug(config,lev+2,off+len,"Data hor. offset: %04x",
409 (*result)->picture_data_x_offset);
410 len += 0x02;
411 psiconv_progress(config,lev+2,off+len,"Going to read the data ver. offset");
412 (*result)->picture_data_y_offset = psiconv_read_u16(config,buf,lev+2,off + len,
413 &res);
414 if (res)
415 goto ERROR2;
416 psiconv_debug(config,lev+2,off+len,"Data ver. offset: %04x",
417 (*result)->picture_data_y_offset);
418 len += 0x02;
419
420 psiconv_progress(config,lev+2,off+len,"Going to read the displayed hor. offset");
421 (*result)->displayed_size_x_offset = psiconv_read_u16(config,buf,lev+2,off + len,
422 &res);
423 if (res)
424 goto ERROR2;
425 psiconv_debug(config,lev+2,off+len,"Displayed hor. offset: %04x",
426 (*result)->displayed_size_x_offset);
427 len += 0x02;
428 psiconv_progress(config,lev+2,off+len,"Going to read the displayed ver. offset");
429 (*result)->displayed_size_y_offset = psiconv_read_u16(config,buf,lev+2,off + len,
430 &res);
431 if (res)
432 goto ERROR2;
433 psiconv_debug(config,lev+2,off+len,"Displayed ver. offset: %04x",
434 (*result)->displayed_size_y_offset);
435 len += 0x02;
436
437 psiconv_progress(config,lev+2,off+len,"Going to read the form hor. size");
438 (*result)->form_xsize = psiconv_read_u16(config,buf,lev+2,off + len,&res);
439 if (res)
440 goto ERROR2;
403 psiconv_debug(lev+2,off+len,"Form hor. size: %04x", 441 psiconv_debug(config,lev+2,off+len,"Form hor. size: %04x",
404 (*result)->form_xsize); 442 (*result)->form_xsize);
405 len += 0x02; 443 len += 0x02;
406 psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); 444 psiconv_progress(config,lev+2,off+len,"Going to read form ver. size");
407 (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); 445 (*result)->form_ysize = psiconv_read_u16(config,buf,lev+2,off + len,&res);
408 if (res) 446 if (res)
409 goto ERROR2; 447 goto ERROR2;
410 psiconv_debug(lev+2,off+len,"Form ver. size: %04x", 448 psiconv_debug(config,lev+2,off+len,"Form ver. size: %04x",
411 (*result)->form_ysize); 449 (*result)->form_ysize);
412 len += 0x02; 450 len += 0x02;
413 psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); 451
414 (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len,&res); 452 psiconv_progress(config,lev+2,off+len,"Going to skip 1 word of zeros");
453 temp = psiconv_read_u16(config,buf,lev+2,off+len,&res);
415 if (res) 454 if (res)
416 goto ERROR2; 455 goto ERROR2;
417 psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x",
418 (*result)->picture_x_offset);
419 len += 0x02;
420 psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset");
421 (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len,&res);
422 if (res)
423 goto ERROR2;
424 psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x",
425 (*result)->picture_y_offset);
426 len += 0x02;
427 psiconv_progress(lev+2,off+len,"Going to skip 5 words of zeros");
428 for (i = 0; i < 5; i++) {
429 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
430 if (res)
431 goto ERROR2;
432 if (temp != 0) {
433 psiconv_warn(lev+2,off+len,
434 "Unexpected value in sketch section preamble (ignored)");
435 psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i,
436 temp,0);
437 }
438 off += 0x02;
439 }
440 } else {
441 psiconv_progress(lev+2,off+len,"Going to read the displayed hor. size");
442 (*result)->picture_xsize = psiconv_read_u16(buf,lev+2,off + len,&res);
443 if (res)
444 goto ERROR2;
445 psiconv_debug(lev+2,off+len,"Displayed hor. size: %04x",
446 (*result)->picture_xsize);
447 len += 0x02;
448 psiconv_progress(lev+2,off+len,"Going to read the displayed ver. size");
449 (*result)->picture_ysize = psiconv_read_u16(buf,lev+2,off + len,&res);
450 if (res)
451 goto ERROR2;
452 psiconv_debug(lev+2,off+len,"Displayed ver. size: %04x",
453 (*result)->picture_ysize);
454 len += 0x02;
455
456 psiconv_progress(lev+2,off+len,"Going to skip 2 words of zeros");
457 for (i = 0; i < 2; i++) {
458 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
459 if (res)
460 goto ERROR2;
461 if (temp != 0) {
462 psiconv_warn(lev+2,off+len,
463 "Unexpected value in sketch section preamble (ignored)");
464 psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i,
465 temp,0);
466 }
467 off += 0x02;
468 }
469 psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset");
470 (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len,&res);
471 if (res)
472 goto ERROR2;
473 psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x",
474 (*result)->picture_x_offset);
475 len += 0x02;
476 psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset");
477 (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len,&res);
478 if (res)
479 goto ERROR2;
480 psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x",
481 (*result)->picture_y_offset);
482 len += 0x02;
483 psiconv_progress(lev+2,off+len,"Going to read the form hor. size");
484 (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len,&res);
485 if (res)
486 goto ERROR2;
487 psiconv_debug(lev+2,off+len,"Form hor. size: %04x",
488 (*result)->form_xsize);
489 len += 0x02;
490 psiconv_progress(lev+2,off+len,"Going to read the form ver. size");
491 (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len,&res);
492 if (res)
493 goto ERROR2;
494 psiconv_debug(lev+2,off+len,"Form ver. size: %04x",
495 (*result)->form_ysize);
496 len += 0x02;
497 psiconv_progress(lev+2,off+len,"Going to skip 1 zero word");
498 temp = psiconv_read_u16(buf,lev+2,off+len,&res);
499 if (res)
500 goto ERROR2;
501 if (temp != 0) { 456 if (temp != 0) {
502 psiconv_warn(lev+2,off+len, 457 psiconv_warn(config,lev+2,off+len,
503 "Unexpected value in sketch section preamble (ignored)"); 458 "Unexpected value in sketch section preamble (ignored)");
504 psiconv_debug(lev+2,off+len,"Read %04x, expected %04x",i, temp,0); 459 psiconv_debug(config,lev+2,off+len,"Read %04x, expected %04x",
460 temp,0);
505 } 461 }
506 off += 0x02; 462 off += 0x02;
507 }
508 463
509 psiconv_progress(lev+2,off+len,"Going to read the picture data"); 464 psiconv_progress(config,lev+2,off+len,"Going to read the picture data");
510 if ((res = psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,0, 465 if ((res = psiconv_parse_paint_data_section(config,buf,lev+2,off+len,&leng,0,
511 &((*result)->picture)))) 466 &((*result)->picture))))
512 goto ERROR2; 467 goto ERROR2;
513 off += leng; 468 off += leng;
514 if (!is_object) {
515 (*result)->picture_xsize = (*result)->picture->xsize;
516 (*result)->picture_ysize = (*result)->picture->ysize;
517 }
518 469
519 psiconv_progress(lev+2,off+len,"Going to read the hor. magnification"); 470 psiconv_progress(config,lev+2,off+len,"Going to read the hor. magnification");
520 (*result)->magnification_x = psiconv_read_u16(buf,lev+2,off+len,&res)/1000.0; 471 (*result)->magnification_x = psiconv_read_u16(config,buf,lev+2,off+len,&res)/1000.0;
521 if (res) 472 if (res)
522 goto ERROR3; 473 goto ERROR3;
523 psiconv_debug(lev+2,off+len,"Form hor. magnification: %f", 474 psiconv_debug(config,lev+2,off+len,"Form hor. magnification: %f",
524 (*result)->magnification_x); 475 (*result)->magnification_x);
525 len += 0x02; 476 len += 0x02;
526 psiconv_progress(lev+2,off+len,"Going to read the ver. magnification"); 477 psiconv_progress(config,lev+2,off+len,"Going to read the ver. magnification");
527 (*result)->magnification_y = psiconv_read_u16(buf,lev+2,off+len,&res)/1000.0; 478 (*result)->magnification_y = psiconv_read_u16(config,buf,lev+2,off+len,&res)/1000.0;
528 if (res) 479 if (res)
529 goto ERROR3; 480 goto ERROR3;
530 psiconv_debug(lev+2,off+len,"Form ver. magnification: %f", 481 psiconv_debug(config,lev+2,off+len,"Form ver. magnification: %f",
531 (*result)->magnification_y); 482 (*result)->magnification_y);
532 len += 0x02; 483 len += 0x02;
533 484
534 psiconv_progress(lev+2,off+len,"Going to read the left cut"); 485 psiconv_progress(config,lev+2,off+len,"Going to read the left cut");
535 temp = psiconv_read_u32(buf,lev+2,off + len,&res); 486 temp = psiconv_read_u32(config,buf,lev+2,off + len,&res);
536 if (res) 487 if (res)
537 goto ERROR3; 488 goto ERROR3;
538 (*result)->cut_left = (temp * 6.0) / (*result)->picture_xsize; 489 (*result)->cut_left = (temp * 6.0) / (*result)->displayed_xsize;
539 psiconv_debug(lev+2,off+len,"Left cut: raw %08x, real: %f", 490 psiconv_debug(config,lev+2,off+len,"Left cut: raw %08x, real: %f",
540 temp,(*result)->cut_left); 491 temp,(*result)->cut_left);
541 len += 0x04; 492 len += 0x04;
542 psiconv_progress(lev+2,off+len,"Going to read the right cut"); 493 psiconv_progress(config,lev+2,off+len,"Going to read the right cut");
543 temp = psiconv_read_u32(buf,lev+2,off + len,&res); 494 temp = psiconv_read_u32(config,buf,lev+2,off + len,&res);
544 if (res) 495 if (res)
545 goto ERROR3; 496 goto ERROR3;
546 (*result)->cut_right = (temp * 6.0) / (*result)->picture_xsize; 497 (*result)->cut_right = (temp * 6.0) / (*result)->displayed_xsize;
547 psiconv_debug(lev+2,off+len,"Right cut: raw %08x, real: %f", 498 psiconv_debug(config,lev+2,off+len,"Right cut: raw %08x, real: %f",
548 temp,(*result)->cut_right); 499 temp,(*result)->cut_right);
549 len += 0x04; 500 len += 0x04;
550 psiconv_progress(lev+2,off+len,"Going to read the top cut"); 501 psiconv_progress(config,lev+2,off+len,"Going to read the top cut");
551 temp = psiconv_read_u32(buf,lev+2,off + len,&res); 502 temp = psiconv_read_u32(config,buf,lev+2,off + len,&res);
552 if (res) 503 if (res)
553 goto ERROR3; 504 goto ERROR3;
554 (*result)->cut_top = (temp * 6.0) / (*result)->picture_ysize; 505 (*result)->cut_top = (temp * 6.0) / (*result)->displayed_ysize;
555 psiconv_debug(lev+2,off+len,"Top cut: raw %08x, real: %f", 506 psiconv_debug(config,lev+2,off+len,"Top cut: raw %08x, real: %f",
556 temp,(*result)->cut_top); 507 temp,(*result)->cut_top);
557 len += 0x04; 508 len += 0x04;
558 psiconv_progress(lev+2,off+len,"Going to read the bottom cut"); 509 psiconv_progress(config,lev+2,off+len,"Going to read the bottom cut");
559 temp = psiconv_read_u32(buf,lev+2,off + len,&res); 510 temp = psiconv_read_u32(config,buf,lev+2,off + len,&res);
560 if (res) 511 if (res)
561 goto ERROR3; 512 goto ERROR3;
562 (*result)->cut_bottom = (temp * 6.0) / (*result)->picture_ysize; 513 (*result)->cut_bottom = (temp * 6.0) / (*result)->displayed_ysize;
563 psiconv_debug(lev+2,off+len,"Bottom cut: raw %08x, real: %f", 514 psiconv_debug(config,lev+2,off+len,"Bottom cut: raw %08x, real: %f",
564 temp,(*result)->cut_bottom); 515 temp,(*result)->cut_bottom);
565 len += 0x04; 516 len += 0x04;
566 517
567 if (length) 518 if (length)
568 *length = len; 519 *length = len;
569 520
570 psiconv_progress(lev,off+len-1, 521 psiconv_progress(config,lev,off+len-1,
571 "End of sketch section (total length: %08x)", len); 522 "End of sketch section (total length: %08x)", len);
572 523
573 return res; 524 return res;
574ERROR3: 525ERROR3:
575 psiconv_free_paint_data_section((*result)->picture); 526 psiconv_free_paint_data_section((*result)->picture);
576ERROR2: 527ERROR2:
577 free (*result); 528 free (*result);
578ERROR1: 529ERROR1:
579 psiconv_warn(lev+1,off,"Reading of Sketch Section failed"); 530 psiconv_error(config,lev+1,off,"Reading of Sketch Section failed");
580 if (length) 531 if (length)
581 *length = 0; 532 *length = 0;
582 if (!res) 533 if (!res)
583 return -PSICONV_E_NOMEM; 534 return -PSICONV_E_NOMEM;
584 else 535 else
585 return res; 536 return res;
586} 537}
587 538
588 539
589int psiconv_parse_clipart_section(const psiconv_buffer buf,int lev, 540int psiconv_parse_clipart_section(const psiconv_config config,
541 const psiconv_buffer buf,int lev,
590 psiconv_u32 off, int *length, 542 psiconv_u32 off, int *length,
591 psiconv_clipart_section *result) 543 psiconv_clipart_section *result)
592{ 544{
593 int res=0; 545 int res=0;
594 int len=0; 546 int len=0;
595 int leng; 547 int leng;
596 psiconv_u32 temp; 548 psiconv_u32 temp;
597 549
598 psiconv_progress(lev+1,off+len,"Going to read the clipart section"); 550 psiconv_progress(config,lev+1,off+len,"Going to read the clipart section");
599 if (!(*result = malloc(sizeof(**result)))) 551 if (!(*result = malloc(sizeof(**result))))
600 goto ERROR1; 552 goto ERROR1;
601 553
602 psiconv_progress(lev+2,off+len,"Going to read the section ID"); 554 psiconv_progress(config,lev+2,off+len,"Going to read the section ID");
603 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 555 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
604 if (res) 556 if (res)
605 goto ERROR2; 557 goto ERROR2;
606 if (temp != PSICONV_ID_CLIPART_ITEM) { 558 if (temp != PSICONV_ID_CLIPART_ITEM) {
607 psiconv_warn(lev+2,off+len, 559 psiconv_warn(config,lev+2,off+len,
608 "Unexpected value in clipart section preamble (ignored)"); 560 "Unexpected value in clipart section preamble (ignored)");
609 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 561 psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp,
610 PSICONV_ID_CLIPART_ITEM); 562 PSICONV_ID_CLIPART_ITEM);
611 } else 563 } else
612 psiconv_debug(lev+2,off+len,"Clipart ID: %08x", temp); 564 psiconv_debug(config,lev+2,off+len,"Clipart ID: %08x", temp);
613 off += 4; 565 off += 4;
614 566
615 psiconv_progress(lev+2,off+len,"Going to read an unknown long"); 567 psiconv_progress(config,lev+2,off+len,"Going to read an unknown long");
616 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 568 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
617 if (res) 569 if (res)
618 goto ERROR2; 570 goto ERROR2;
619 if (temp != 0x02) { 571 if (temp != 0x02) {
620 psiconv_warn(lev+2,off+len, 572 psiconv_warn(config,lev+2,off+len,
621 "Unexpected value in clipart section preamble (ignored)"); 573 "Unexpected value in clipart section preamble (ignored)");
622 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 574 psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp,
623 0x02); 575 0x02);
624 } else 576 } else
625 psiconv_debug(lev+2,off+len,"First unknown long: %08x", temp); 577 psiconv_debug(config,lev+2,off+len,"First unknown long: %08x", temp);
626 off += 4; 578 off += 4;
627 579
628 psiconv_progress(lev+2,off+len,"Going to read a second unknown long"); 580 psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long");
629 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 581 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
630 if (res) 582 if (res)
631 goto ERROR2; 583 goto ERROR2;
632 if (temp != 0) { 584 if (temp != 0) {
633 psiconv_warn(lev+2,off+len, 585 psiconv_warn(config,lev+2,off+len,
634 "Unexpected value in clipart section preamble (ignored)"); 586 "Unexpected value in clipart section preamble (ignored)");
635 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0); 587 psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, 0);
636 } else 588 } else
637 psiconv_debug(lev+2,off+len,"Second unknown long: %08x", temp); 589 psiconv_debug(config,lev+2,off+len,"Second unknown long: %08x", temp);
638 off += 4; 590 off += 4;
639 591
640 psiconv_progress(lev+2,off+len,"Going to read a third unknown long"); 592 psiconv_progress(config,lev+2,off+len,"Going to read a third unknown long");
641 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 593 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
642 if (res) 594 if (res)
643 goto ERROR2; 595 goto ERROR2;
644 if (temp != 0) { 596 if (temp != 0) {
645 psiconv_warn(lev+2,off+len, 597 psiconv_warn(config,lev+2,off+len,
646 "Unexpected value in clipart section preamble (ignored)"); 598 "Unexpected value in clipart section preamble (ignored)");
647 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0); 599 psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x",temp, 0);
648 } else 600 } else
649 psiconv_debug(lev+2,off+len,"Third unknown long: %08x", temp); 601 psiconv_debug(config,lev+2,off+len,"Third unknown long: %08x", temp);
650 off += 4; 602 off += 4;
651 603
652 psiconv_progress(lev+2,off+len,"Going to read a fourth unknown long"); 604 psiconv_progress(config,lev+2,off+len,"Going to read a fourth unknown long");
653 temp = psiconv_read_u32(buf,lev+2,off+len,&res); 605 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
654 if (res) 606 if (res)
655 goto ERROR2; 607 goto ERROR2;
656 if ((temp != 0x0c) && (temp != 0x08)) { 608 if ((temp != 0x0c) && (temp != 0x08)) {
657 psiconv_warn(lev+2,off+len, 609 psiconv_warn(config,lev+2,off+len,
658 "Unexpected value in clipart section preamble (ignored)"); 610 "Unexpected value in clipart section preamble (ignored)");
659 psiconv_debug(lev+2,off+len,"Read %08x, expected %08x or %08x",temp, 611 psiconv_debug(config,lev+2,off+len,"Read %08x, expected %08x or %08x",temp,
660 0x0c, 0x08); 612 0x0c, 0x08);
661 } else 613 } else
662 psiconv_debug(lev+2,off+len,"Fourth unknown long: %08x", temp); 614 psiconv_debug(config,lev+2,off+len,"Fourth unknown long: %08x", temp);
663 off += 4; 615 off += 4;
664 616
665 psiconv_progress(lev+2,off+len,"Going to read the Paint Data Section"); 617 psiconv_progress(config,lev+2,off+len,"Going to read the Paint Data Section");
666 if ((res = psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,1, 618 if ((res = psiconv_parse_paint_data_section(config,buf,lev+2,off+len,&leng,1,
667 &((*result)->picture)))) 619 &((*result)->picture))))
668 goto ERROR2; 620 goto ERROR2;
669 len += leng; 621 len += leng;
670 622
671 if (length) 623 if (length)
672 *length = len; 624 *length = len;
673 625
674 psiconv_progress(lev,off+len-1, 626 psiconv_progress(config,lev,off+len-1,
675 "End of clipart section (total length: %08x)", len); 627 "End of clipart section (total length: %08x)", len);
676 return 0; 628 return 0;
677 629
678ERROR2: 630ERROR2:
679 free (*result); 631 free (*result);
680ERROR1: 632ERROR1:
681 psiconv_warn(lev+1,off,"Reading of Font failed"); 633 psiconv_error(config,lev+1,off,"Reading of Font failed");
682 if (length) 634 if (length)
683 *length = 0; 635 *length = 0;
684 if (!res) 636 if (!res)
685 return -PSICONV_E_NOMEM; 637 return -PSICONV_E_NOMEM;
686 else 638 else
687 return res; 639 return res;
688} 640}
641
642int psiconv_decode_rle8 (const psiconv_config config, int lev, psiconv_u32 off,
643 const psiconv_pixel_bytes encoded,
644 psiconv_pixel_bytes *decoded)
645{
646 int res=0;
647 psiconv_u8 *marker,*value;
648 int i,j;
649
650 psiconv_progress(config,lev+1,off,"Going to decode the RLE8 encoding");
651 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
652 goto ERROR1;
653
654 for (i = 0; i < psiconv_list_length(encoded);) {
655 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i);
656 if (!(marker = psiconv_list_get(encoded,i)))
657 goto ERROR2;
658 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker);
659 if (*marker < 0x80) {
660 psiconv_debug(config,lev+2,off,"Marker: repeat value byte %02x times",
661 *marker+1);
662 psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1);
663 if (!(value = psiconv_list_get(encoded,i+1)))
664 goto ERROR2;
665 psiconv_debug(config,lev+2,off,"Value byte: %02x",*value);
666 psiconv_progress(config,lev+2,off,"Adding %02x pixels %02x",
667 *marker+1,*value);
668 for (j = 0; j < *marker + 1; j++)
669 if ((res = psiconv_list_add(*decoded,value)))
670 goto ERROR2;
671 i += 2;
672 } else {
673 psiconv_debug(config,lev+2,off,"Marker: %02x value bytes follow",
674 0x100 - *marker);
675 for (j = 0; j < (0x100 - *marker); j++) {
676 psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",
677 i+j+1);
678 if (!(value = psiconv_list_get(encoded,i+j+1)))
679 goto ERROR2;
680 psiconv_debug(config,lev+2,off,"Value: %02x",*value);
681 if ((res = psiconv_list_add(*decoded,value)))
682 goto ERROR2;
683 }
684 i += (0x100 - *marker) + 1;
685 }
686 }
687 psiconv_progress(config,lev,off,
688 "End of RLE8 decoding process");
689 return 0;
690
691ERROR2:
692 psiconv_list_free(*decoded);
693ERROR1:
694 psiconv_error(config,lev+1,off,"Decoding of RLE8 failed");
695 if (!res)
696 return -PSICONV_E_NOMEM;
697 else
698 return res;
699}
700
701int psiconv_decode_rle12 (const psiconv_config config, int lev, psiconv_u32 off,
702 const psiconv_pixel_bytes encoded,
703 psiconv_pixel_bytes *decoded)
704{
705 int res=0;
706 psiconv_u8 *value0,*value1;
707 psiconv_u32 value,repeat;
708 int i,j;
709
710 psiconv_progress(config,lev+1,off,"Going to decode the RLE12 encoding");
711 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
712 goto ERROR1;
713
714 for (i = 0; i < psiconv_list_length(encoded);) {
715 psiconv_progress(config,lev+2,off,"Going to read data word at %04x",i);
716 if (!(value0 = psiconv_list_get(encoded,i)))
717 goto ERROR2;
718 if (!(value1 = psiconv_list_get(encoded,i+1)))
719 goto ERROR2;
720 psiconv_debug(config,lev+2,off,"Data Word: %04x",*value0 + (*value1 << 8));
721 value = *value0 + ((*value1 & 0x0f) << 8);
722 repeat = (*value1 >> 4) + 1;
723 psiconv_progress(config,lev+2,off,"Adding %02x pixels %03x",
724 repeat,value);
725 for (j = 0; j < repeat; j ++)
726 if ((res = psiconv_list_add(*decoded,&value)))
727 goto ERROR2;
728 i += 2;
729 }
730 psiconv_progress(config,lev,off,
731 "End of RLE12 decoding process");
732 return 0;
733
734ERROR2:
735 psiconv_list_free(*decoded);
736ERROR1:
737 psiconv_error(config,lev+1,off,"Decoding of RLE12 failed");
738 if (!res)
739 return -PSICONV_E_NOMEM;
740 else
741 return res;
742}
743
744int psiconv_decode_rle16 (const psiconv_config config, int lev, psiconv_u32 off,
745 const psiconv_pixel_bytes encoded,
746 psiconv_pixel_bytes *decoded)
747{
748 int res=0;
749 psiconv_u8 *marker,*value0,*value1;
750 psiconv_u32 value;
751 int i,j;
752
753 psiconv_progress(config,lev+1,off,"Going to decode the RLE16 encoding");
754 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
755 goto ERROR1;
756
757 for (i = 0; i < psiconv_list_length(encoded);) {
758 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i);
759 if (!(marker = psiconv_list_get(encoded,i)))
760 goto ERROR2;
761 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker);
762 if (*marker < 0x80) {
763 psiconv_debug(config,lev+2,off,"Marker: repeat value word %02x times",
764 *marker+1);
765 psiconv_progress(config,lev+2,off,"Going to read value word at %04x",i+1);
766 if (!(value0 = psiconv_list_get(encoded,i+1)))
767 goto ERROR2;
768 if (!(value1 = psiconv_list_get(encoded,i+2)))
769 goto ERROR2;
770 value = *value0 + (*value1 << 8);
771 psiconv_debug(config,lev+2,off,"Value word: %02x",value);
772 psiconv_progress(config,lev+2,off,"Adding %02x pixels %04x",
773 *marker+1,value);
774 for (j = 0; j < *marker + 1; j++)
775 if ((res = psiconv_list_add(*decoded,&value)))
776 goto ERROR2;
777 i += 3;
778 } else {
779 psiconv_debug(config,lev+2,off,"Marker: %02x value words follow",
780 0x100 - *marker);
781 for (j = 0; j < (0x100 - *marker); j++) {
782 psiconv_progress(config,lev+2,off,"Going to read value word at %04x",
783 i+j*2+1);
784 if (!(value0 = psiconv_list_get(encoded,i+j*2+1)))
785 goto ERROR2;
786 if (!(value1 = psiconv_list_get(encoded,i+j*2+2)))
787 goto ERROR2;
788 value = *value0 + (*value1 << 8);
789 psiconv_debug(config,lev+2,off,"Value: %04x",value);
790 if ((res = psiconv_list_add(*decoded,&value)))
791 goto ERROR2;
792 }
793 i += (0x100 - *marker)*2 + 1;
794 }
795 }
796 psiconv_progress(config,lev,off,
797 "End of RLE16 decoding process");
798 return 0;
799
800ERROR2:
801 psiconv_list_free(*decoded);
802ERROR1:
803 psiconv_error(config,lev+1,off,"Decoding of RLE16 failed");
804 if (!res)
805 return -PSICONV_E_NOMEM;
806 else
807 return res;
808}
809
810int psiconv_decode_rle24 (const psiconv_config config, int lev, psiconv_u32 off,
811 const psiconv_pixel_bytes encoded,
812 psiconv_pixel_bytes *decoded)
813{
814 int res=0;
815 psiconv_u8 *marker,*value0,*value1,*value2;
816 psiconv_u32 value;
817 int i,j;
818
819 psiconv_progress(config,lev+1,off,"Going to decode the RLE24 encoding");
820 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
821 goto ERROR1;
822
823 for (i = 0; i < psiconv_list_length(encoded);) {
824 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i);
825 if (!(marker = psiconv_list_get(encoded,i)))
826 goto ERROR2;
827 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker);
828 if (*marker < 0x80) {
829 psiconv_debug(config,lev+2,off,"Marker: repeat value byte triplet %02x times",
830 *marker+1);
831 psiconv_progress(config,lev+2,off,"Going to read value byte triplet at %04x",i+1);
832 if (!(value0 = psiconv_list_get(encoded,i+1)))
833 goto ERROR2;
834 if (!(value1 = psiconv_list_get(encoded,i+2)))
835 goto ERROR2;
836 if (!(value2 = psiconv_list_get(encoded,i+3)))
837 goto ERROR2;
838 value = *value0 + (*value1 << 8) + (*value2 << 16);
839 psiconv_debug(config,lev+2,off,"Value byte triplet: %06x",value);
840 psiconv_progress(config,lev+2,off,"Adding %02x pixels %06x",
841 *marker+1,value);
842 for (j = 0; j < *marker + 1; j++)
843 if ((res = psiconv_list_add(*decoded,&value)))
844 goto ERROR2;
845 i += 4;
846 } else {
847 psiconv_debug(config,lev+2,off,"Marker: %02x value byte triplets follow",
848 0x100 - *marker);
849 for (j = 0; j < (0x100 - *marker); j++) {
850 psiconv_progress(config,lev+2,off,"Going to read value byte triplets at %04x",
851 i+j*3+1);
852 if (!(value0 = psiconv_list_get(encoded,i+j*3+1)))
853 goto ERROR2;
854 if (!(value1 = psiconv_list_get(encoded,i+j*3+2)))
855 goto ERROR2;
856 if (!(value2 = psiconv_list_get(encoded,i+j*3+3)))
857 goto ERROR2;
858 value = *value0 + (*value1 << 8) + (*value2 << 16);
859 psiconv_debug(config,lev+2,off,"Value: %06x",value);
860 if ((res = psiconv_list_add(*decoded,&value)))
861 goto ERROR2;
862 }
863 i += (0x100 - *marker)*3 + 1;
864 }
865 }
866 psiconv_progress(config,lev,off,
867 "End of RLE24 decoding process");
868 return 0;
869
870ERROR2:
871 psiconv_list_free(*decoded);
872ERROR1:
873 psiconv_error(config,lev+1,off,"Decoding of RLE24 failed");
874 if (!res)
875 return -PSICONV_E_NOMEM;
876 else
877 return res;
878}
879
880int psiconv_bytes_to_pixel_data(const psiconv_config config,
881 int lev, psiconv_u32 off,
882 const psiconv_pixel_bytes bytes,
883 psiconv_pixel_ints *pixels,
884 int colordepth, int xsize, int ysize)
885{
886 int res=0;
887 int ibits,obits,x,y,bits;
888 psiconv_u8 input;
889 psiconv_u32 nr,output;
890 psiconv_u8 *ientry;
891
892 psiconv_progress(config,lev+1,off,"Going to convert the bytes to pixels");
893 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32))))
894 goto ERROR1;
895
896 nr = 0;
897 for (y = 0; y < ysize; y++) {
898 /* New lines will start at longs */
899 while (nr % 4)
900 nr ++;
901 input = 0;
902 ibits = 0;
903 for (x= 0; x < xsize; x++) {
904 psiconv_progress(config,lev+2,off,
905 "Processing pixel at (x,y) = (%04x,%04x)",x,y);
906 output = 0;
907 obits = 0;
908 while (obits < colordepth) {
909 if (ibits == 0) {
910 psiconv_progress(config,lev+3,off,
911 "Going to read byte %08x",nr);
912 if (!(ientry = psiconv_list_get(bytes,nr)))
913 goto ERROR2;
914 psiconv_debug(config,lev+3,off,"Byte value: %02x",*ientry);
915 input = *ientry;
916 ibits = 8;
917 nr ++;
918 }
919 bits = ibits + obits > colordepth?colordepth-obits:ibits;
920 output = output << bits;
921 output |= input & ((1 << bits) - 1);
922 input = input >> bits;
923 ibits -= bits;
924 obits += bits;
925 }
926 psiconv_debug(config,lev+2,off,"Pixel value: %08x",output);
927 if ((res = psiconv_list_add(*pixels,&output)))
928 goto ERROR2;
929 }
930 }
931
932 psiconv_progress(config,lev,off,
933 "Converting bytes to pixels completed");
934 return 0;
935
936
937ERROR2:
938 psiconv_list_free(*pixels);
939ERROR1:
940 psiconv_error(config,lev+1,off,"Converting bytes to pixels failed");
941 if (!res)
942 return -PSICONV_E_NOMEM;
943 else
944 return res;
945}
946
947int psiconv_pixel_data_to_floats (const psiconv_config config, int lev,
948 psiconv_u32 off,
949 const psiconv_pixel_ints pixels,
950 psiconv_pixel_floats_t *floats,
951 int colordepth, int color,
952 int redbits, int bluebits, int greenbits,
953 const psiconv_pixel_floats_t palet)
954{
955 int res = 0;
956 psiconv_u32 i;
957 psiconv_u32 *pixel;
958
959 psiconv_progress(config,lev+1,off,"Going to convert pixels to floats");
960 if (!((*floats).red = malloc(psiconv_list_length(pixels) *
961 sizeof(*(*floats).red))))
962 goto ERROR1;
963 if (!((*floats).green = malloc(psiconv_list_length(pixels) *
964 sizeof(*(*floats).green))))
965 goto ERROR2;
966 if (!((*floats).blue = malloc(psiconv_list_length(pixels) *
967 sizeof(*(*floats).blue))))
968 goto ERROR3;
969 (*floats).length = psiconv_list_length(pixels);
970
971 for (i = 0; i < psiconv_list_length(pixels); i++) {
972 if (!(pixel = psiconv_list_get(pixels,i)))
973 goto ERROR4;
974 psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel);
975 if (!palet.length) {
976 if (color) {
977 (*floats).blue[i] = ((float) (*pixel & ((1 << bluebits) - 1))) /
978 (1 << bluebits);
979 (*floats).green[i] = ((float) ((*pixel >> bluebits) &
980 ((1 << greenbits) - 1))) / (1 << greenbits);
981 (*floats).red[i] = ((float) ((*pixel >> (bluebits+greenbits)) &
982 ((1 << redbits) - 1))) / (1 << redbits);
983 } else {
984 (*floats).red[i] = (*floats).green[i] =
985 (*floats).blue[i] = ((float) *pixel) /
986 (1 << colordepth);
987 }
988 } else {
989 if (*pixel >= palet.length) {
990 psiconv_warn(config,lev+2,off,
991 "Invalid palet color found (using color 0x00)");
992 (*floats).red[i] = palet.red[0];
993 (*floats).green[i] = palet.green[0];
994 (*floats).blue[i] = palet.blue[0];
995 } else {
996 (*floats).red[i] = palet.red[*pixel];
997 (*floats).green[i] = palet.green[*pixel];
998 (*floats).blue[i] = palet.blue[*pixel];
999 }
1000 }
1001 psiconv_debug(config,lev+2,off, "Pixel: Red (%f), green (%f), blue (%f)",
1002 (*floats).red[i],(*floats).green[i],(*floats).blue[i]);
1003 }
1004 psiconv_progress(config,lev+1,off,"Finished converting pixels to floats");
1005 return 0;
1006
1007ERROR4:
1008 free((*floats).blue);
1009ERROR3:
1010 free((*floats).green);
1011ERROR2:
1012 free((*floats).red);
1013ERROR1:
1014 psiconv_error(config,lev+1,off,"Converting pixels to floats failed");
1015 if (!res)
1016 return -PSICONV_E_NOMEM;
1017 else
1018 return res;
1019}
1020
1021
1022

Legend:
Removed from v.65  
changed lines
  Added in v.196

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