/[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 178 Revision 230
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.
28 28
29#ifdef DMALLOC 29#ifdef DMALLOC
30#include <dmalloc.h> 30#include <dmalloc.h>
31#endif 31#endif
32 32
33/* Extreme debugging info */
34#undef LOUD
35
33static int psiconv_decode_rle8 (const psiconv_config config, int lev, 36static int psiconv_decode_rle8 (const psiconv_config config, int lev,
37 psiconv_u32 off,
38 const psiconv_pixel_bytes encoded,
39 psiconv_pixel_bytes *decoded);
40
41static int psiconv_decode_rle12 (const psiconv_config config, int lev,
42 psiconv_u32 off,
43 const psiconv_pixel_bytes encoded,
44 psiconv_pixel_bytes *decoded);
45
46static int psiconv_decode_rle16 (const psiconv_config config, int lev,
47 psiconv_u32 off,
48 const psiconv_pixel_bytes encoded,
49 psiconv_pixel_bytes *decoded);
50
51static int psiconv_decode_rle24 (const psiconv_config config, int lev,
34 psiconv_u32 off, 52 psiconv_u32 off,
35 const psiconv_pixel_bytes encoded, 53 const psiconv_pixel_bytes encoded,
36 psiconv_pixel_bytes *decoded); 54 psiconv_pixel_bytes *decoded);
37 55
38static int psiconv_bytes_to_pixel_data(const psiconv_config config, 56static int psiconv_bytes_to_pixel_data(const psiconv_config config,
92 return 0; 110 return 0;
93 111
94ERROR2: 112ERROR2:
95 psiconv_list_free(*result); 113 psiconv_list_free(*result);
96ERROR1: 114ERROR1:
97 psiconv_warn(config,lev+1,off,"Reading of Jumptable Section failed"); 115 psiconv_error(config,lev+1,off,"Reading of Jumptable Section failed");
98 if (length) 116 if (length)
99 *length = 0; 117 *length = 0;
100 if (!res) 118 if (!res)
101 return -PSICONV_E_NOMEM; 119 return -PSICONV_E_NOMEM;
102 else 120 else
103 return res; 121 return res;
104} 122}
105 123
106static int decode_byte(const psiconv_config config, int lev, psiconv_u32 off,
107 psiconv_paint_data_section data, psiconv_u32 *pixelnr,
108 psiconv_u8 byte, int bits_per_pixel, int linelen,
109 int *linepos,int picsize)
110{
111 int mask = (bits_per_pixel << 1) -1;
112 int i;
113 if (*linepos < (data->xsize + (8/bits_per_pixel) - 1) / (8/bits_per_pixel))
114 for (i = 0; i < 8/bits_per_pixel; i ++) {
115 if ((i != 0) && ((*pixelnr % (data->xsize)) == 0)) {
116 psiconv_debug(config,lev+1,off,"Skipping padding: %02x",byte);
117 i = 8;
118 } else if (*pixelnr >= picsize) {
119 psiconv_warn(config,lev+1,off,"Corrupted picture data!");
120 psiconv_debug(config,lev+1,off,"Trying to write a pixel too far");
121 return -1;
122 } else {
123 data->red[*pixelnr] = data->green[*pixelnr] = data->blue[*pixelnr] =
124 ((float) (byte & mask)) / ((1 << bits_per_pixel) -1);
125 psiconv_debug(config,lev+1,off,"Pixel %04x: (%04x,%04x) value %02x, color %f",
126 *pixelnr,*pixelnr % data->xsize,
127 *pixelnr / data->xsize, byte&mask, data->red[*pixelnr]);
128 byte = byte >> bits_per_pixel;
129 (*pixelnr) ++;
130 }
131 }
132 else
133 psiconv_debug(config,lev+1,off,"Skipping padding byte");
134 (*linepos) ++;
135 if (*linepos == linelen)
136 *linepos = 0;
137 return 0;
138}
139
140
141int psiconv_parse_paint_data_section(const psiconv_config config, 124int psiconv_parse_paint_data_section(const psiconv_config config,
142 const psiconv_buffer buf,int lev, 125 const psiconv_buffer buf,int lev,
143 psiconv_u32 off, int *length,int isclipart, 126 psiconv_u32 off, int *length,int isclipart,
144 psiconv_paint_data_section *result) 127 psiconv_paint_data_section *result)
145{ 128{
146 int res = 0; 129 int res = 0;
147 int len = 0; 130 int len = 0;
148 psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; 131 psiconv_u32 size,offset,picsize,temp,datasize,color,
132 redbits,bluebits,greenbits;
149 psiconv_u8 marker; 133 psiconv_u8 byte;
150 int i,leng; 134 int leng,i;
151 psiconv_u32 bits_per_pixel,compression; 135 psiconv_u32 bits_per_pixel,compression;
152 int linepos = 0; 136 psiconv_pixel_bytes bytes,decoded;
137 psiconv_pixel_ints pixels;
138 psiconv_pixel_floats_t floats,palet;
153 139
154 psiconv_progress(config,lev+1,off,"Going to read a paint data section"); 140 psiconv_progress(config,lev+1,off,"Going to read a paint data section");
155 if (!((*result) = malloc(sizeof(**result)))) 141 if (!((*result) = malloc(sizeof(**result))))
156 goto ERROR1; 142 goto ERROR1;
157 143
144 if (!(bytes = psiconv_list_new(sizeof(psiconv_u8))))
145 goto ERROR2;
146
158 psiconv_progress(config,lev+2,off+len,"Going to read section size"); 147 psiconv_progress(config,lev+2,off+len,"Going to read section size");
159 size = psiconv_read_u32(config,buf,lev+2,off+len,&res); 148 size = psiconv_read_u32(config,buf,lev+2,off+len,&res);
160 if (res) 149 if (res)
161 goto ERROR2; 150 goto ERROR3;
162 psiconv_debug(config,lev+2,off+len,"Section size: %08x",size); 151 psiconv_debug(config,lev+2,off+len,"Section size: %08x",size);
163 len += 4; 152 len += 4;
164 153
165 psiconv_progress(config,lev+2,off+len,"Going to read pixel data offset"); 154 psiconv_progress(config,lev+2,off+len,"Going to read pixel data offset");
166 offset = psiconv_read_u32(config,buf,lev+2,off+len,&res); 155 offset = psiconv_read_u32(config,buf,lev+2,off+len,&res);
167 if (res) 156 if (res)
168 goto ERROR2; 157 goto ERROR3;
169 if (offset != 0x28) { 158 if (offset != 0x28) {
170 psiconv_warn(config,lev+2,off+len, 159 psiconv_error(config,lev+2,off+len,
171 "Paint data section data offset has unexpected value"); 160 "Paint data section data offset has unexpected value");
172 psiconv_debug(config,lev+2,off+len, 161 psiconv_debug(config,lev+2,off+len,
173 "Data offset: read %08x, expected %08x",offset,0x28); 162 "Data offset: read %08x, expected %08x",offset,0x28);
174 res = -1; 163 res = -1;
175 } 164 }
176 len += 4; 165 len += 4;
177 166
178 psiconv_progress(config,lev+2,off+len,"Going to read picture X size"); 167 psiconv_progress(config,lev+2,off+len,"Going to read picture X size");
179 (*result)->xsize = psiconv_read_u32(config,buf,lev+2,off+len,&res); 168 (*result)->xsize = psiconv_read_u32(config,buf,lev+2,off+len,&res);
180 if (res) 169 if (res)
181 goto ERROR2; 170 goto ERROR3;
182 psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); 171 psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize);
183 len += 4; 172 len += 4;
184 173
185 psiconv_progress(config,lev+2,off+len,"Going to read picture Y size"); 174 psiconv_progress(config,lev+2,off+len,"Going to read picture Y size");
186 (*result)->ysize = psiconv_read_u32(config,buf,lev+2,off+len,&res); 175 (*result)->ysize = psiconv_read_u32(config,buf,lev+2,off+len,&res);
187 if (res) 176 if (res)
188 goto ERROR2; 177 goto ERROR3;
189 psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); 178 psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize);
190 len += 4; 179 len += 4;
191 180
192 picsize = (*result)->ysize * (*result)->xsize; 181 picsize = (*result)->ysize * (*result)->xsize;
193 182
194 psiconv_progress(config,lev+2,off+len,"Going to read the real picture x size"); 183 psiconv_progress(config,lev+2,off+len,"Going to read the real picture x size");
195 (*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); 184 (*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
196 if (res) 185 if (res)
197 goto ERROR2; 186 goto ERROR3;
198 psiconv_debug(config,lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize); 187 psiconv_debug(config,lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize);
199 len += leng; 188 len += leng;
200 189
201 psiconv_progress(config,lev+2,off+len,"Going to read the real picture y size"); 190 psiconv_progress(config,lev+2,off+len,"Going to read the real picture y size");
202 (*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); 191 (*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res);
203 if (res) 192 if (res)
204 goto ERROR2; 193 goto ERROR3;
205 psiconv_debug(config,lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize); 194 psiconv_debug(config,lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize);
206 len += leng; 195 len += leng;
207 196
208 psiconv_progress(config,lev+2,off+len,"Going to read the number of bits per pixel"); 197 psiconv_progress(config,lev+2,off+len,"Going to read the number of bits per pixel");
209 bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res); 198 bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res);
210 if (res) 199 if (res)
211 goto ERROR2; 200 goto ERROR3;
212 if (bits_per_pixel > 8) {
213 psiconv_warn(config,lev+2,off+len,"Picture has too many colors");
214 psiconv_debug(config,lev+2,off+len,"Read %d colorbits",bits_per_pixel);
215 res = -PSICONV_E_PARSE;
216 goto ERROR2;
217 }
218 psiconv_debug(config,lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); 201 psiconv_debug(config,lev+2,off+len,"Bits per pixel: %d",bits_per_pixel);
219 len += 4; 202 len += 4;
220 203
221 for (i = 0 ; i < 2; i++) { 204 psiconv_progress(config,lev+2,off+len,
205 "Going to read whether this is a colour or greyscale picture");
206 color = psiconv_read_u32(config,buf,lev+2,off+len,&res);
207 if (res)
208 goto ERROR3;
209 if ((color != 0) && (color != 1)) {
210 psiconv_warn(config,lev+2,off+len,
211 "Paint data section unknown color type (ignored)");
212 psiconv_debug(config,lev+2,off+len,
213 "Color: read %08x, expected %08x or %08x",color,0,1);
214 color = 1;
215 } else {
216 psiconv_debug(config,lev+2,off+len,"Color: %08x (%s picture)",
217 color,(color?"color":"greyscale"));
218 }
219 len += 4;
220
222 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); 221 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
223 if (res) 222 if (res)
224 goto ERROR2; 223 goto ERROR3;
225 if (temp != 00) { 224 if (temp != 00) {
226 psiconv_warn(config,lev+2,off+len, 225 psiconv_warn(config,lev+2,off+len,
227 "Paint data section prologue has unknown values (ignored)"); 226 "Paint data section prologue has unknown values (ignored)");
228 psiconv_debug(config,lev+2,off+len, 227 psiconv_debug(config,lev+2,off+len,
229 "offset %02x: read %08x, expected %08x",i,temp, 0x00); 228 "read %08x, expected %08x",temp, 0x00);
230 } 229 }
231 len += 4; 230 len += 4;
232 }
233 231
234 psiconv_progress(config,lev+2,off+len, 232 psiconv_progress(config,lev+2,off+len,
235 "Going to read whether RLE compression is used"); 233 "Going to read whether RLE compression is used");
236 compression=psiconv_read_u32(config,buf,lev+2,off+len,&res); 234 compression=psiconv_read_u32(config,buf,lev+2,off+len,&res);
237 if (res) 235 if (res)
238 goto ERROR2; 236 goto ERROR3;
239 if (compression > 1) { 237 if (compression > 4) {
240 psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " 238 psiconv_warn(config,lev+2,off+len,"Paint data section has unknown "
241 "compression type, assuming RLE"); 239 "compression type, assuming RLE");
242 psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); 240 psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression);
243 compression = 1; 241 compression = 0;
244 } 242 }
245 psiconv_debug(config,lev+2,off+len,"Compression: %s",compression?"RLE8":"none"); 243 psiconv_debug(config,lev+2,off+len,"Compression: %s",
244 compression == 4?"RLE24":compression == 3?"RLE16":
245 compression == 2?"RLE12":compression == 1?"RLE8":"none");
246 len += 4; 246 len += 4;
247 247
248 if (isclipart) { 248 if (isclipart) {
249 psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); 249 psiconv_progress(config,lev+2,off+len,"Going to read an unknown long");
250 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); 250 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
251 if (res) 251 if (res)
252 goto ERROR2; 252 goto ERROR3;
253 if (temp != 0xffffffff) { 253 if (temp != 0xffffffff) {
254 psiconv_warn(config,lev+2,off+len, 254 psiconv_warn(config,lev+2,off+len,
255 "Paint data section prologue has unknown values (ignoring)"); 255 "Paint data section prologue has unknown values (ignoring)");
256 psiconv_debug(config,lev+2,off+len, 256 psiconv_debug(config,lev+2,off+len,
257 "offset %02x: read %08x, expected %08x",i,temp, 0xffffffff); 257 "Read %08x, expected %08x",temp, 0xffffffff);
258 } 258 }
259 len += 4; 259 len += 4;
260 psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long"); 260 psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long");
261 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); 261 temp = psiconv_read_u32(config,buf,lev+2,off+len,&res);
262 if (res) 262 if (res)
263 goto ERROR2; 263 goto ERROR3;
264 if (temp != 0x44) { 264 if (temp != 0x44) {
265 psiconv_warn(config,lev+2,off+len, 265 psiconv_warn(config,lev+2,off+len,
266 "Paint data section prologue has unknown values (ignoring)"); 266 "Paint data section prologue has unknown values (ignoring)");
267 psiconv_debug(config,lev+2,off+len, 267 psiconv_debug(config,lev+2,off+len,
268 "offset %02x: read %08x, expected %08x",i,temp, 0x44); 268 "read %08x, expected %08x",temp, 0x44);
269 } 269 }
270 len += 4; 270 len += 4;
271 } 271 }
272 272
273 if (!((*result)->red = malloc(sizeof(float) * picsize)))
274 goto ERROR2;
275 if (!((*result)->green = malloc(sizeof(float) * picsize)))
276 goto ERROR3;
277 if (!((*result)->blue = malloc(sizeof(float) * picsize)))
278 goto ERROR4;
279 len = offset; 273 len = offset;
280 datasize = size - len; 274 datasize = size - len;
281 if (isclipart) 275 if (isclipart)
282 len += 8; 276 len += 8;
283 277
278 if (color || (bits_per_pixel != 2))
279 psiconv_warn(config,lev+2,off+len,
280 "All image types except 2-bit greyscale are experimental!");
281
284 psiconv_progress(config,lev+2,off+len,"Going to read the pixel data"); 282 psiconv_progress(config,lev+2,off+len,"Going to read the pixel data");
285 pixelnr = 0; 283 for (i = 0; i < datasize; i++) {
286 datanr = 0;
287 if (!compression) {
288 linelen = datasize / (*result)->ysize;
289 psiconv_debug(config,lev+3,off+len,"Line length: %04x bytes",linelen);
290 while((datanr < datasize)) {
291 temp = psiconv_read_u8(config,buf,lev+2,off+len+datanr,&res); 284 byte = psiconv_read_u8(config,buf,lev+2,off+len+i,&res);
285#ifdef LOUD
286 psiconv_debug(config,lev+2,off+len+i,
287 "Pixel byte %04x of %04x has value %02x",
288 i,datasize,byte);
289#endif
292 if (res) 290 if (res)
291 goto ERROR3;
292 psiconv_list_add(bytes,&byte);
293 }
294 len += datasize;
295
296 switch(compression) {
297 case 1:
298 if ((res = psiconv_decode_rle8(config,lev+2,off+len,bytes,&decoded)))
299 goto ERROR3;
300 psiconv_list_free(bytes);
301 bytes = decoded;
302 break;
303 case 2:
304 if ((psiconv_decode_rle12(config,lev+2,off+len,bytes,&decoded)))
305 goto ERROR3;
306 psiconv_list_free(bytes);
307 bytes = decoded;
308 break;
309 case 3:
310 if ((psiconv_decode_rle16(config,lev+2,off+len,bytes,&decoded)))
293 goto ERROR5; 311 goto ERROR3;
294 if (decode_byte(config,lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel, 312 psiconv_list_free(bytes);
295 linelen,&linepos,picsize)) { 313 bytes = decoded;
296 res = -PSICONV_E_PARSE;
297 break; 314 break;
298 } 315 case 4:
299 datanr++; 316 if ((psiconv_decode_rle24(config,lev+2,off+len,bytes,&decoded)))
317 goto ERROR3;
318 psiconv_list_free(bytes);
319 bytes = decoded;
320 break;
321 }
322
323 if ((res = psiconv_bytes_to_pixel_data(config,lev+2,off+len,bytes,
324 &pixels,bits_per_pixel,
325 (*result)->xsize,(*result)->ysize)))
326 goto ERROR3;
327
328 /* Use some heuristics; things may get unexpected around here */
329 bluebits = redbits = greenbits = 0;
330 palet = psiconv_palet_none;
331 if (color) {
332 if (bits_per_pixel == 4)
333 palet = psiconv_palet_color_4;
334 else if (bits_per_pixel == 8)
335 palet = psiconv_palet_color_8;
336 else {
337 redbits = (bits_per_pixel+2) / 3;
338 greenbits = (bits_per_pixel+2) / 3;
339 bluebits = bits_per_pixel - redbits - greenbits;
300 } 340 }
301 } else {
302 psiconv_progress(config,lev+2,off+len,"First pass: determining line length");
303 datanr = 0;
304 i = 0;
305 while (datanr < datasize) {
306 marker = psiconv_read_u8(config,buf,lev+3,off+len+datanr,&res);
307 if (res)
308 goto ERROR5;
309 if (marker >= 0x80) {
310 datanr += 0x100 - marker + 1;
311 i += 0x100 - marker;
312 } else {
313 datanr += 2;
314 i += marker + 1;
315 }
316 } 341 }
317 linelen = i / (*result)->ysize; 342 if ((res = psiconv_pixel_data_to_floats(config,lev+2,off+len,pixels,
318 datanr=0; 343 &floats,bits_per_pixel,color,
319 psiconv_debug(config,lev+2,off+len,"Linelen: %04x bytes",linelen); 344 redbits,greenbits,bluebits,palet)))
320 while((datanr < datasize)) {
321 marker = psiconv_read_u8(config,buf,lev+3,off+len+datanr,&res);
322 if (res)
323 goto ERROR5;
324 psiconv_debug(config,lev+3,off+len+datanr,
325 "Pixelnr %08x, Datanr %08x: Read marker %02x",
326 pixelnr,datanr,marker);
327 datanr ++;
328 if (marker >= 0x80) {
329 /* 0x100 - marker bytes of data follow */
330 for (i = 0; i < 0x100-marker; i++,datanr++) {
331 if (datanr >= datasize) {
332 psiconv_warn(config,lev+3,off+len+datanr,"Corrupted picture data");
333 psiconv_debug(config,lev+3,off+len+datanr,
334 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
335 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr,
336 datanr,marker);
337 res = -PSICONV_E_PARSE;
338 break;
339 }
340 temp = psiconv_read_u8(config,buf,lev+2,off+len+datanr,&res);
341 if (res)
342 goto ERROR5;
343 if (decode_byte(config,lev+2,off+len+datanr,*result,&pixelnr,temp,
344 bits_per_pixel,linelen,&linepos,picsize)) {
345 res = -PSICONV_E_PARSE;
346 break;
347 }
348 }
349 } else {
350 if (datanr >= datasize) {
351 psiconv_warn(config,lev+3,off+len+datanr,"Corrupted picture data");
352 psiconv_debug(config,lev+3,off+len+datanr,
353 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
354 "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr,
355 datanr,marker);
356 res = -PSICONV_E_PARSE;
357 } else {
358 temp = psiconv_read_u8(config,buf,lev+3,off+len+datanr,&res);
359 if (res)
360 goto ERROR5;
361 for (i = 0; i <= marker; i++) {
362 if (decode_byte(config,lev+2,off+len+datanr,*result,&pixelnr,temp,
363 bits_per_pixel,linelen,&linepos,picsize)) {
364 res = -PSICONV_E_PARSE;
365 break;
366 }
367 }
368 datanr ++;
369 }
370 }
371 }
372 }
373
374 if (linepos >= ((*result)->xsize + (8/bits_per_pixel) - 1) /
375 (8/bits_per_pixel))
376 datanr += (linelen - linepos);
377
378 if (res || (datanr != datasize) || (pixelnr != picsize)) {
379 psiconv_warn(config,lev+2,off+len,"Corrupted picture data!");
380 psiconv_debug(config,lev+3,off+len+datanr,
381 "Picsize: %08x, Datasize: %08x, Pixelnr: %08x,"
382 "Datanr: %08x",picsize,datasize,pixelnr,datanr);
383 goto ERROR5; 345 goto ERROR4;
384 } 346
347 (*result)->red = floats.red;
348 (*result)->green = floats.green;
349 (*result)->blue = floats.blue;
385 350
386 len += datanr; 351 psiconv_list_free(bytes);
352 psiconv_list_free(pixels);
353
387 354
388 if (length) 355 if (length)
389 *length = len; 356 *length = len;
390 357
391 psiconv_progress(config,lev+1,off+len-1,"End of paint data section " 358 psiconv_progress(config,lev,off+len-1,
392 "(total length: %08x)", len); 359 "End of Paint Data Section (total length: %08x)", len);
393 360
394 return res; 361 return 0;
395 362
396ERROR5:
397 free((*result)->blue);
398ERROR4: 363ERROR4:
399 free((*result)->green); 364 psiconv_list_free(pixels);
400ERROR3: 365ERROR3:
401 free((*result)->red); 366 psiconv_list_free(bytes);
402ERROR2: 367ERROR2:
403 free (*result); 368 free(*result);
404ERROR1: 369ERROR1:
405 psiconv_warn(config,lev+1,off,"Reading of Paint Data Section failed"); 370 psiconv_error(config,lev+1,off,"Reading of Paint Data Section failed");
406 if (length) 371 if (length)
407 *length = 0; 372 *length = 0;
408 if (!res) 373 if (!res)
409 return -PSICONV_E_NOMEM; 374 return -PSICONV_E_NOMEM;
410 else 375 else
565ERROR3: 530ERROR3:
566 psiconv_free_paint_data_section((*result)->picture); 531 psiconv_free_paint_data_section((*result)->picture);
567ERROR2: 532ERROR2:
568 free (*result); 533 free (*result);
569ERROR1: 534ERROR1:
570 psiconv_warn(config,lev+1,off,"Reading of Sketch Section failed"); 535 psiconv_error(config,lev+1,off,"Reading of Sketch Section failed");
571 if (length) 536 if (length)
572 *length = 0; 537 *length = 0;
573 if (!res) 538 if (!res)
574 return -PSICONV_E_NOMEM; 539 return -PSICONV_E_NOMEM;
575 else 540 else
668 return 0; 633 return 0;
669 634
670ERROR2: 635ERROR2:
671 free (*result); 636 free (*result);
672ERROR1: 637ERROR1:
673 psiconv_warn(config,lev+1,off,"Reading of Font failed"); 638 psiconv_error(config,lev+1,off,"Reading of Font failed");
674 if (length) 639 if (length)
675 *length = 0; 640 *length = 0;
676 if (!res) 641 if (!res)
677 return -PSICONV_E_NOMEM; 642 return -PSICONV_E_NOMEM;
678 else 643 else
690 psiconv_progress(config,lev+1,off,"Going to decode the RLE8 encoding"); 655 psiconv_progress(config,lev+1,off,"Going to decode the RLE8 encoding");
691 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8)))) 656 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
692 goto ERROR1; 657 goto ERROR1;
693 658
694 for (i = 0; i < psiconv_list_length(encoded);) { 659 for (i = 0; i < psiconv_list_length(encoded);) {
660#ifdef LOUD
661 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i);
662#endif
663 if (!(marker = psiconv_list_get(encoded,i)))
664 goto ERROR2;
665#ifdef LOUD
666 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker);
667#endif
668 if (*marker < 0x80) {
669#ifdef LOUD
670 psiconv_debug(config,lev+2,off,"Marker: repeat value byte %02x times",
671 *marker+1); */
672 psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1);
673#endif
674 if (!(value = psiconv_list_get(encoded,i+1)))
675 goto ERROR2;
676#ifdef LOUD
677 psiconv_debug(config,lev+2,off,"Value byte: %02x",*value);
678 psiconv_progress(config,lev+2,off,"Adding %02x pixels %02x",
679 *marker+1,*value);
680#endif
681 for (j = 0; j < *marker + 1; j++)
682 if ((res = psiconv_list_add(*decoded,value)))
683 goto ERROR2;
684 i += 2;
685 } else {
686#ifdef LOUD
687 psiconv_debug(config,lev+2,off,"Marker: %02x value bytes follow",
688 0x100 - *marker);
689#endif
690 for (j = 0; j < (0x100 - *marker); j++) {
691#ifdef LOUD
692 psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",
693 i+j+1);
694#endif
695 if (!(value = psiconv_list_get(encoded,i+j+1)))
696 goto ERROR2;
697#ifdef LOUD
698 psiconv_debug(config,lev+2,off,"Value: %02x",*value);
699#endif
700 if ((res = psiconv_list_add(*decoded,value)))
701 goto ERROR2;
702 }
703 i += (0x100 - *marker) + 1;
704 }
705 }
706 psiconv_progress(config,lev,off,
707 "End of RLE8 decoding process");
708 return 0;
709
710ERROR2:
711 psiconv_list_free(*decoded);
712ERROR1:
713 psiconv_error(config,lev+1,off,"Decoding of RLE8 failed");
714 if (!res)
715 return -PSICONV_E_NOMEM;
716 else
717 return res;
718}
719
720int psiconv_decode_rle12 (const psiconv_config config, int lev, psiconv_u32 off,
721 const psiconv_pixel_bytes encoded,
722 psiconv_pixel_bytes *decoded)
723{
724 int res=0;
725 psiconv_u8 *value0,*value1;
726 psiconv_u32 value,repeat;
727 int i,j;
728
729 psiconv_progress(config,lev+1,off,"Going to decode the RLE12 encoding");
730 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
731 goto ERROR1;
732
733 for (i = 0; i < psiconv_list_length(encoded);) {
734 psiconv_progress(config,lev+2,off,"Going to read data word at %04x",i);
735 if (!(value0 = psiconv_list_get(encoded,i)))
736 goto ERROR2;
737 if (!(value1 = psiconv_list_get(encoded,i+1)))
738 goto ERROR2;
739 psiconv_debug(config,lev+2,off,"Data Word: %04x",*value0 + (*value1 << 8));
740 value = *value0 + ((*value1 & 0x0f) << 8);
741 repeat = (*value1 >> 4) + 1;
742 psiconv_progress(config,lev+2,off,"Adding %02x pixels %03x",
743 repeat,value);
744 for (j = 0; j < repeat; j ++)
745 if ((res = psiconv_list_add(*decoded,&value)))
746 goto ERROR2;
747 i += 2;
748 }
749 psiconv_progress(config,lev,off,
750 "End of RLE12 decoding process");
751 return 0;
752
753ERROR2:
754 psiconv_list_free(*decoded);
755ERROR1:
756 psiconv_error(config,lev+1,off,"Decoding of RLE12 failed");
757 if (!res)
758 return -PSICONV_E_NOMEM;
759 else
760 return res;
761}
762
763int psiconv_decode_rle16 (const psiconv_config config, int lev, psiconv_u32 off,
764 const psiconv_pixel_bytes encoded,
765 psiconv_pixel_bytes *decoded)
766{
767 int res=0;
768 psiconv_u8 *marker,*value0,*value1;
769 psiconv_u32 value;
770 int i,j;
771
772 psiconv_progress(config,lev+1,off,"Going to decode the RLE16 encoding");
773 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
774 goto ERROR1;
775
776 for (i = 0; i < psiconv_list_length(encoded);) {
695 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i); 777 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i);
696 if (!(marker = psiconv_list_get(encoded,i))) 778 if (!(marker = psiconv_list_get(encoded,i)))
697 goto ERROR2; 779 goto ERROR2;
698 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker); 780 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker);
699 if (*marker < 0x80) { 781 if (*marker < 0x80) {
700 psiconv_debug(config,lev+2,off,"Marker: repeat value byte %02x times", 782 psiconv_debug(config,lev+2,off,"Marker: repeat value word %02x times",
701 *marker+1); 783 *marker+1);
702 psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1); 784 psiconv_progress(config,lev+2,off,"Going to read value word at %04x",i+1);
703 if (!(value = psiconv_list_get(encoded,i+1))) 785 if (!(value0 = psiconv_list_get(encoded,i+1)))
704 goto ERROR2; 786 goto ERROR2;
787 if (!(value1 = psiconv_list_get(encoded,i+2)))
788 goto ERROR2;
789 value = *value0 + (*value1 << 8);
705 psiconv_debug(config,lev+2,off,"Value byte: %02x",*value); 790 psiconv_debug(config,lev+2,off,"Value word: %02x",value);
706 psiconv_progress(config,lev+2,off,"Adding %02x bytes %02x", 791 psiconv_progress(config,lev+2,off,"Adding %02x pixels %04x",
707 *marker+1,*value); 792 *marker+1,value);
708 for (j = 0; j < *marker + 1; j++) 793 for (j = 0; j < *marker + 1; j++)
709 if ((res = psiconv_list_add(*decoded,value))) 794 if ((res = psiconv_list_add(*decoded,&value)))
710 goto ERROR2; 795 goto ERROR2;
711 i += 2; 796 i += 3;
712 } else { 797 } else {
713 psiconv_debug(config,lev+2,off,"Marker: %02x value bytes follow", 798 psiconv_debug(config,lev+2,off,"Marker: %02x value words follow",
714 0x100 - *marker); 799 0x100 - *marker);
715 for (j = 0; j < (0x100 - *marker); j++) { 800 for (j = 0; j < (0x100 - *marker); j++) {
716 psiconv_progress(config,lev+2,off,"Going to read value byte at %04x", 801 psiconv_progress(config,lev+2,off,"Going to read value word at %04x",
717 i+j+1); 802 i+j*2+1);
718 if (!(value = psiconv_list_get(encoded,i+j+1))) 803 if (!(value0 = psiconv_list_get(encoded,i+j*2+1)))
719 goto ERROR2; 804 goto ERROR2;
805 if (!(value1 = psiconv_list_get(encoded,i+j*2+2)))
806 goto ERROR2;
807 value = *value0 + (*value1 << 8);
720 psiconv_debug(config,lev+2,off,"Value: %02x",*value); 808 psiconv_debug(config,lev+2,off,"Value: %04x",value);
721 if ((res = psiconv_list_add(*decoded,value))) 809 if ((res = psiconv_list_add(*decoded,&value)))
722 goto ERROR2; 810 goto ERROR2;
723 } 811 }
724 i += (0x100 - *marker) + 1; 812 i += (0x100 - *marker)*2 + 1;
725 } 813 }
726 } 814 }
727 psiconv_progress(config,lev,off, 815 psiconv_progress(config,lev,off,
728 "End of RLE8 decoding process"); 816 "End of RLE16 decoding process");
729 return 0; 817 return 0;
730 818
731ERROR2: 819ERROR2:
732 psiconv_list_free(*decoded); 820 psiconv_list_free(*decoded);
733ERROR1: 821ERROR1:
734 psiconv_warn(config,lev+1,off,"Decoding of RLE8 failed"); 822 psiconv_error(config,lev+1,off,"Decoding of RLE16 failed");
823 if (!res)
824 return -PSICONV_E_NOMEM;
825 else
826 return res;
827}
828
829int psiconv_decode_rle24 (const psiconv_config config, int lev, psiconv_u32 off,
830 const psiconv_pixel_bytes encoded,
831 psiconv_pixel_bytes *decoded)
832{
833 int res=0;
834 psiconv_u8 *marker,*value0,*value1,*value2;
835 psiconv_u32 value;
836 int i,j;
837
838 psiconv_progress(config,lev+1,off,"Going to decode the RLE24 encoding");
839 if (!(*decoded = psiconv_list_new(sizeof(psiconv_u8))))
840 goto ERROR1;
841
842 for (i = 0; i < psiconv_list_length(encoded);) {
843 psiconv_progress(config,lev+2,off,"Going to read marker byte at %04x",i);
844 if (!(marker = psiconv_list_get(encoded,i)))
845 goto ERROR2;
846 psiconv_debug(config,lev+2,off,"Marker byte: %02x",*marker);
847 if (*marker < 0x80) {
848 psiconv_debug(config,lev+2,off,"Marker: repeat value byte triplet %02x times",
849 *marker+1);
850 psiconv_progress(config,lev+2,off,"Going to read value byte triplet at %04x",i+1);
851 if (!(value0 = psiconv_list_get(encoded,i+1)))
852 goto ERROR2;
853 if (!(value1 = psiconv_list_get(encoded,i+2)))
854 goto ERROR2;
855 if (!(value2 = psiconv_list_get(encoded,i+3)))
856 goto ERROR2;
857 value = *value0 + (*value1 << 8) + (*value2 << 16);
858 psiconv_debug(config,lev+2,off,"Value byte triplet: %06x",value);
859 psiconv_progress(config,lev+2,off,"Adding %02x pixels %06x",
860 *marker+1,value);
861 for (j = 0; j < *marker + 1; j++)
862 if ((res = psiconv_list_add(*decoded,&value)))
863 goto ERROR2;
864 i += 4;
865 } else {
866 psiconv_debug(config,lev+2,off,"Marker: %02x value byte triplets follow",
867 0x100 - *marker);
868 for (j = 0; j < (0x100 - *marker); j++) {
869 psiconv_progress(config,lev+2,off,"Going to read value byte triplets at %04x",
870 i+j*3+1);
871 if (!(value0 = psiconv_list_get(encoded,i+j*3+1)))
872 goto ERROR2;
873 if (!(value1 = psiconv_list_get(encoded,i+j*3+2)))
874 goto ERROR2;
875 if (!(value2 = psiconv_list_get(encoded,i+j*3+3)))
876 goto ERROR2;
877 value = *value0 + (*value1 << 8) + (*value2 << 16);
878 psiconv_debug(config,lev+2,off,"Value: %06x",value);
879 if ((res = psiconv_list_add(*decoded,&value)))
880 goto ERROR2;
881 }
882 i += (0x100 - *marker)*3 + 1;
883 }
884 }
885 psiconv_progress(config,lev,off,
886 "End of RLE24 decoding process");
887 return 0;
888
889ERROR2:
890 psiconv_list_free(*decoded);
891ERROR1:
892 psiconv_error(config,lev+1,off,"Decoding of RLE24 failed");
735 if (!res) 893 if (!res)
736 return -PSICONV_E_NOMEM; 894 return -PSICONV_E_NOMEM;
737 else 895 else
738 return res; 896 return res;
739} 897}
760 while (nr % 4) 918 while (nr % 4)
761 nr ++; 919 nr ++;
762 input = 0; 920 input = 0;
763 ibits = 0; 921 ibits = 0;
764 for (x= 0; x < xsize; x++) { 922 for (x= 0; x < xsize; x++) {
923#ifdef LOUD
765 psiconv_progress(config,lev+2,off, 924 psiconv_progress(config,lev+2,off,
766 "Processing pixel at (x,y) = (%04x,%04x)",x,y); 925 "Processing pixel at (x,y) = (%04x,%04x)",x,y);
926#endif
767 output = 0; 927 output = 0;
768 obits = 0; 928 obits = 0;
769 while (obits < colordepth) { 929 while (obits < colordepth) {
770 if (ibits == 0) { 930 if (ibits == 0) {
931#ifdef LOUD
771 psiconv_progress(config,lev+3,off, 932 psiconv_progress(config,lev+3,off,
772 "Going to read byte %08x",nr); 933 "Going to read byte %08x",nr);
934#endif
773 if (!(ientry = psiconv_list_get(bytes,nr))) 935 if (!(ientry = psiconv_list_get(bytes,nr)))
774 goto ERROR2; 936 goto ERROR2;
937#ifdef LOUD
775 psiconv_debug(config,lev+3,off,"Byte value: %02x",*ientry); 938 psiconv_debug(config,lev+3,off,"Byte value: %02x",*ientry);
939#endif
776 input = *ientry; 940 input = *ientry;
777 ibits = 8; 941 ibits = 8;
778 nr ++; 942 nr ++;
779 } 943 }
780 bits = ibits + obits > colordepth?colordepth-obits:ibits; 944 bits = ibits + obits > colordepth?colordepth-obits:ibits;
782 output |= input & ((1 << bits) - 1); 946 output |= input & ((1 << bits) - 1);
783 input = input >> bits; 947 input = input >> bits;
784 ibits -= bits; 948 ibits -= bits;
785 obits += bits; 949 obits += bits;
786 } 950 }
951#ifdef LOUD
787 psiconv_debug(config,lev+2,off,"Pixel value: %08x",output); 952 psiconv_debug(config,lev+2,off,"Pixel value: %08x",output);
953#endif
788 if ((res = psiconv_list_add(*pixels,&output))) 954 if ((res = psiconv_list_add(*pixels,&output)))
789 goto ERROR2; 955 goto ERROR2;
790 } 956 }
791 } 957 }
792 958
796 962
797 963
798ERROR2: 964ERROR2:
799 psiconv_list_free(*pixels); 965 psiconv_list_free(*pixels);
800ERROR1: 966ERROR1:
801 psiconv_warn(config,lev+1,off,"Converting bytes to pixels failed"); 967 psiconv_error(config,lev+1,off,"Converting bytes to pixels failed");
802 if (!res) 968 if (!res)
803 return -PSICONV_E_NOMEM; 969 return -PSICONV_E_NOMEM;
804 else 970 else
805 return res; 971 return res;
806} 972}
830 (*floats).length = psiconv_list_length(pixels); 996 (*floats).length = psiconv_list_length(pixels);
831 997
832 for (i = 0; i < psiconv_list_length(pixels); i++) { 998 for (i = 0; i < psiconv_list_length(pixels); i++) {
833 if (!(pixel = psiconv_list_get(pixels,i))) 999 if (!(pixel = psiconv_list_get(pixels,i)))
834 goto ERROR4; 1000 goto ERROR4;
1001#ifdef LOUD
835 psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel); 1002 psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel);
1003#endif
836 if (!palet.length) { 1004 if (!palet.length) {
837 if (color) { 1005 if (color) {
838 (*floats).blue[i] = (*pixel & ((1 << bluebits) - 1)) / 1006 (*floats).blue[i] = ((float) (*pixel & ((1 << bluebits) - 1))) /
839 (1 << bluebits); 1007 (1 << bluebits);
840 (*floats).green[i] = ((*pixel >> bluebits) & ((1 << greenbits) - 1)) / 1008 (*floats).green[i] = ((float) ((*pixel >> bluebits) &
841 (1 << greenbits); 1009 ((1 << greenbits) - 1))) / (1 << greenbits);
842 (*floats).red[i] = ((*pixel >> (bluebits+greenbits)) & 1010 (*floats).red[i] = ((float) ((*pixel >> (bluebits+greenbits)) &
843 ((1 << redbits) - 1)) / (1 << redbits); 1011 ((1 << redbits) - 1))) / (1 << redbits);
844 } else { 1012 } else {
845 (*floats).red[i] = (*floats).green[i] = 1013 (*floats).red[i] = (*floats).green[i] =
846 (*floats).blue[i] = *pixel / (1 << colordepth); 1014 (*floats).blue[i] = ((float) *pixel) /
1015 (1 << colordepth);
847 } 1016 }
848 } else { 1017 } else {
849 if (*pixel >= palet.length) { 1018 if (*pixel >= palet.length) {
850 psiconv_warn(config,lev+2,off, 1019 psiconv_warn(config,lev+2,off,
851 "Invalid palet color found (using color 0x00)"); 1020 "Invalid palet color found (using color 0x00)");
856 (*floats).red[i] = palet.red[*pixel]; 1025 (*floats).red[i] = palet.red[*pixel];
857 (*floats).green[i] = palet.green[*pixel]; 1026 (*floats).green[i] = palet.green[*pixel];
858 (*floats).blue[i] = palet.blue[*pixel]; 1027 (*floats).blue[i] = palet.blue[*pixel];
859 } 1028 }
860 } 1029 }
1030#ifdef LOUD
861 psiconv_debug(config,lev+2,off, "Pixel: Red (%f), green (%f), blue (%f)", 1031 psiconv_debug(config,lev+2,off, "Pixel: Red (%f), green (%f), blue (%f)",
862 (*floats).red[i],(*floats).green[i],(*floats).blue[i]); 1032 (*floats).red[i],(*floats).green[i],(*floats).blue[i]);
1033#endif
863 } 1034 }
864 psiconv_progress(config,lev+1,off,"Finished converting pixels to floats"); 1035 psiconv_progress(config,lev+1,off,"Finished converting pixels to floats");
865 return 0; 1036 return 0;
866 1037
867ERROR4: 1038ERROR4:
869ERROR3: 1040ERROR3:
870 free((*floats).green); 1041 free((*floats).green);
871ERROR2: 1042ERROR2:
872 free((*floats).red); 1043 free((*floats).red);
873ERROR1: 1044ERROR1:
874 psiconv_warn(config,lev+1,off,"Converting pixels to floats failed"); 1045 psiconv_error(config,lev+1,off,"Converting pixels to floats failed");
875 if (!res) 1046 if (!res)
876 return -PSICONV_E_NOMEM; 1047 return -PSICONV_E_NOMEM;
877 else 1048 else
878 return res; 1049 return res;
879} 1050}

Legend:
Removed from v.178  
changed lines
  Added in v.230

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