/[public]/psiconv/tags/rel-0-9-9/lib/psiconv/parse_image.c
ViewVC logotype

Diff of /psiconv/tags/rel-0-9-9/lib/psiconv/parse_image.c

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

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

Legend:
Removed from v.63  
changed lines
  Added in v.351

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