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. |
… | |
… | |
29 | #ifdef DMALLOC |
29 | #ifdef DMALLOC |
30 | #include <dmalloc.h> |
30 | #include <dmalloc.h> |
31 | #endif |
31 | #endif |
32 | |
32 | |
33 | static int psiconv_decode_rle8 (const psiconv_config config, int lev, |
33 | static 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 | |
|
|
38 | static 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 | |
|
|
43 | static 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 | |
|
|
48 | static int psiconv_decode_rle24 (const psiconv_config config, int lev, |
34 | psiconv_u32 off, |
49 | psiconv_u32 off, |
35 | const psiconv_pixel_bytes encoded, |
50 | const psiconv_pixel_bytes encoded, |
36 | psiconv_pixel_bytes *decoded); |
51 | psiconv_pixel_bytes *decoded); |
37 | |
52 | |
38 | static int psiconv_bytes_to_pixel_data(const psiconv_config config, |
53 | static int psiconv_bytes_to_pixel_data(const psiconv_config config, |
… | |
… | |
92 | return 0; |
107 | return 0; |
93 | |
108 | |
94 | ERROR2: |
109 | ERROR2: |
95 | psiconv_list_free(*result); |
110 | psiconv_list_free(*result); |
96 | ERROR1: |
111 | ERROR1: |
97 | psiconv_warn(config,lev+1,off,"Reading of Jumptable Section failed"); |
112 | psiconv_error(config,lev+1,off,"Reading of Jumptable Section failed"); |
98 | if (length) |
113 | if (length) |
99 | *length = 0; |
114 | *length = 0; |
100 | if (!res) |
115 | if (!res) |
101 | return -PSICONV_E_NOMEM; |
116 | return -PSICONV_E_NOMEM; |
102 | else |
117 | else |
103 | return res; |
118 | return res; |
104 | } |
119 | } |
105 | |
120 | |
106 | static 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 | |
|
|
141 | int psiconv_parse_paint_data_section(const psiconv_config config, |
121 | int psiconv_parse_paint_data_section(const psiconv_config config, |
142 | const psiconv_buffer buf,int lev, |
122 | const psiconv_buffer buf,int lev, |
143 | psiconv_u32 off, int *length,int isclipart, |
123 | psiconv_u32 off, int *length,int isclipart, |
144 | psiconv_paint_data_section *result) |
124 | psiconv_paint_data_section *result) |
145 | { |
125 | { |
146 | int res = 0; |
126 | int res = 0; |
147 | int len = 0; |
127 | int len = 0; |
148 | psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; |
128 | psiconv_u32 size,offset,picsize,temp,datasize,color, |
|
|
129 | redbits,bluebits,greenbits; |
149 | psiconv_u8 marker; |
130 | psiconv_u8 byte; |
150 | int i,leng; |
131 | int leng,i; |
151 | psiconv_u32 bits_per_pixel,compression; |
132 | psiconv_u32 bits_per_pixel,compression; |
152 | int linepos = 0; |
133 | psiconv_pixel_bytes bytes,decoded; |
|
|
134 | psiconv_pixel_ints pixels; |
|
|
135 | psiconv_pixel_floats_t floats,palet; |
153 | |
136 | |
154 | psiconv_progress(config,lev+1,off,"Going to read a paint data section"); |
137 | psiconv_progress(config,lev+1,off,"Going to read a paint data section"); |
155 | if (!((*result) = malloc(sizeof(**result)))) |
138 | if (!((*result) = malloc(sizeof(**result)))) |
156 | goto ERROR1; |
139 | goto ERROR1; |
157 | |
140 | |
|
|
141 | if (!(bytes = psiconv_list_new(sizeof(psiconv_u8)))) |
|
|
142 | goto ERROR2; |
|
|
143 | |
158 | psiconv_progress(config,lev+2,off+len,"Going to read section size"); |
144 | psiconv_progress(config,lev+2,off+len,"Going to read section size"); |
159 | size = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
145 | size = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
160 | if (res) |
146 | if (res) |
161 | goto ERROR2; |
147 | goto ERROR3; |
162 | psiconv_debug(config,lev+2,off+len,"Section size: %08x",size); |
148 | psiconv_debug(config,lev+2,off+len,"Section size: %08x",size); |
163 | len += 4; |
149 | len += 4; |
164 | |
150 | |
165 | psiconv_progress(config,lev+2,off+len,"Going to read pixel data offset"); |
151 | 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); |
152 | offset = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
167 | if (res) |
153 | if (res) |
168 | goto ERROR2; |
154 | goto ERROR3; |
169 | if (offset != 0x28) { |
155 | if (offset != 0x28) { |
170 | psiconv_warn(config,lev+2,off+len, |
156 | psiconv_error(config,lev+2,off+len, |
171 | "Paint data section data offset has unexpected value"); |
157 | "Paint data section data offset has unexpected value"); |
172 | psiconv_debug(config,lev+2,off+len, |
158 | psiconv_debug(config,lev+2,off+len, |
173 | "Data offset: read %08x, expected %08x",offset,0x28); |
159 | "Data offset: read %08x, expected %08x",offset,0x28); |
174 | res = -1; |
160 | res = -1; |
175 | } |
161 | } |
176 | len += 4; |
162 | len += 4; |
177 | |
163 | |
178 | psiconv_progress(config,lev+2,off+len,"Going to read picture X size"); |
164 | 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); |
165 | (*result)->xsize = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
180 | if (res) |
166 | if (res) |
181 | goto ERROR2; |
167 | goto ERROR3; |
182 | psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
168 | psiconv_debug(config,lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
183 | len += 4; |
169 | len += 4; |
184 | |
170 | |
185 | psiconv_progress(config,lev+2,off+len,"Going to read picture Y size"); |
171 | 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); |
172 | (*result)->ysize = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
187 | if (res) |
173 | if (res) |
188 | goto ERROR2; |
174 | goto ERROR3; |
189 | psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
175 | psiconv_debug(config,lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
190 | len += 4; |
176 | len += 4; |
191 | |
177 | |
192 | picsize = (*result)->ysize * (*result)->xsize; |
178 | picsize = (*result)->ysize * (*result)->xsize; |
193 | |
179 | |
194 | psiconv_progress(config,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"); |
195 | (*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
181 | (*result)->pic_xsize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
196 | if (res) |
182 | if (res) |
197 | goto ERROR2; |
183 | goto ERROR3; |
198 | psiconv_debug(config,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); |
199 | len += leng; |
185 | len += leng; |
200 | |
186 | |
201 | psiconv_progress(config,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"); |
202 | (*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
188 | (*result)->pic_ysize = psiconv_read_length(config,buf,lev+2,off+len,&leng,&res); |
203 | if (res) |
189 | if (res) |
204 | goto ERROR2; |
190 | goto ERROR3; |
205 | psiconv_debug(config,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); |
206 | len += leng; |
192 | len += leng; |
207 | |
193 | |
208 | psiconv_progress(config,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"); |
209 | bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
195 | bits_per_pixel=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
210 | if (res) |
196 | if (res) |
211 | goto ERROR2; |
197 | 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); |
198 | psiconv_debug(config,lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); |
219 | len += 4; |
199 | len += 4; |
220 | |
200 | |
221 | 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 | |
222 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
218 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
223 | if (res) |
219 | if (res) |
224 | goto ERROR2; |
220 | goto ERROR3; |
225 | if (temp != 00) { |
221 | if (temp != 00) { |
226 | psiconv_warn(config,lev+2,off+len, |
222 | psiconv_warn(config,lev+2,off+len, |
227 | "Paint data section prologue has unknown values (ignored)"); |
223 | "Paint data section prologue has unknown values (ignored)"); |
228 | psiconv_debug(config,lev+2,off+len, |
224 | psiconv_debug(config,lev+2,off+len, |
229 | "offset %02x: read %08x, expected %08x",i,temp, 0x00); |
225 | "read %08x, expected %08x",temp, 0x00); |
230 | } |
226 | } |
231 | len += 4; |
227 | len += 4; |
232 | } |
|
|
233 | |
228 | |
234 | psiconv_progress(config,lev+2,off+len, |
229 | psiconv_progress(config,lev+2,off+len, |
235 | "Going to read whether RLE compression is used"); |
230 | "Going to read whether RLE compression is used"); |
236 | compression=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
231 | compression=psiconv_read_u32(config,buf,lev+2,off+len,&res); |
237 | if (res) |
232 | if (res) |
238 | goto ERROR2; |
233 | goto ERROR3; |
239 | if (compression > 1) { |
234 | if (compression > 4) { |
240 | psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " |
235 | psiconv_warn(config,lev+2,off+len,"Paint data section has unknown " |
241 | "compression type, assuming RLE"); |
236 | "compression type, assuming RLE"); |
242 | psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); |
237 | psiconv_debug(config,lev+2,off+len,"Read compression type %d",compression); |
243 | compression = 1; |
238 | compression = 0; |
244 | } |
239 | } |
245 | psiconv_debug(config,lev+2,off+len,"Compression: %s",compression?"RLE8":"none"); |
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"); |
246 | len += 4; |
243 | len += 4; |
247 | |
244 | |
248 | if (isclipart) { |
245 | if (isclipart) { |
249 | psiconv_progress(config,lev+2,off+len,"Going to read an unknown long"); |
246 | 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); |
247 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
251 | if (res) |
248 | if (res) |
252 | goto ERROR2; |
249 | goto ERROR3; |
253 | if (temp != 0xffffffff) { |
250 | if (temp != 0xffffffff) { |
254 | psiconv_warn(config,lev+2,off+len, |
251 | psiconv_warn(config,lev+2,off+len, |
255 | "Paint data section prologue has unknown values (ignoring)"); |
252 | "Paint data section prologue has unknown values (ignoring)"); |
256 | psiconv_debug(config,lev+2,off+len, |
253 | psiconv_debug(config,lev+2,off+len, |
257 | "offset %02x: read %08x, expected %08x",i,temp, 0xffffffff); |
254 | "Read %08x, expected %08x",temp, 0xffffffff); |
258 | } |
255 | } |
259 | len += 4; |
256 | len += 4; |
260 | psiconv_progress(config,lev+2,off+len,"Going to read a second unknown long"); |
257 | 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); |
258 | temp = psiconv_read_u32(config,buf,lev+2,off+len,&res); |
262 | if (res) |
259 | if (res) |
263 | goto ERROR2; |
260 | goto ERROR3; |
264 | if (temp != 0x44) { |
261 | if (temp != 0x44) { |
265 | psiconv_warn(config,lev+2,off+len, |
262 | psiconv_warn(config,lev+2,off+len, |
266 | "Paint data section prologue has unknown values (ignoring)"); |
263 | "Paint data section prologue has unknown values (ignoring)"); |
267 | psiconv_debug(config,lev+2,off+len, |
264 | psiconv_debug(config,lev+2,off+len, |
268 | "offset %02x: read %08x, expected %08x",i,temp, 0x44); |
265 | "read %08x, expected %08x",temp, 0x44); |
269 | } |
266 | } |
270 | len += 4; |
267 | len += 4; |
271 | } |
268 | } |
272 | |
269 | |
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; |
270 | len = offset; |
280 | datasize = size - len; |
271 | datasize = size - len; |
281 | if (isclipart) |
272 | if (isclipart) |
282 | len += 8; |
273 | len += 8; |
283 | |
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 | |
284 | psiconv_progress(config,lev+2,off+len,"Going to read the pixel data"); |
279 | psiconv_progress(config,lev+2,off+len,"Going to read the pixel data"); |
285 | pixelnr = 0; |
280 | 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); |
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); |
292 | 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))) |
293 | goto ERROR5; |
306 | goto ERROR3; |
294 | if (decode_byte(config,lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel, |
307 | psiconv_list_free(bytes); |
295 | linelen,&linepos,picsize)) { |
308 | bytes = decoded; |
296 | res = -PSICONV_E_PARSE; |
|
|
297 | break; |
309 | break; |
298 | } |
310 | case 4: |
299 | 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; |
300 | } |
335 | } |
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 | } |
336 | } |
317 | linelen = i / (*result)->ysize; |
337 | if ((res = psiconv_pixel_data_to_floats(config,lev+2,off+len,pixels, |
318 | datanr=0; |
338 | &floats,bits_per_pixel,color, |
319 | psiconv_debug(config,lev+2,off+len,"Linelen: %04x bytes",linelen); |
339 | 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; |
340 | goto ERROR4; |
384 | } |
341 | |
|
|
342 | (*result)->red = floats.red; |
|
|
343 | (*result)->green = floats.green; |
|
|
344 | (*result)->blue = floats.blue; |
385 | |
345 | |
386 | len += datanr; |
346 | psiconv_list_free(bytes); |
|
|
347 | psiconv_list_free(pixels); |
|
|
348 | |
387 | |
349 | |
388 | if (length) |
350 | if (length) |
389 | *length = len; |
351 | *length = len; |
390 | |
352 | |
391 | psiconv_progress(config,lev+1,off+len-1,"End of paint data section " |
353 | psiconv_progress(config,lev,off+len-1, |
392 | "(total length: %08x)", len); |
354 | "End of Paint Data Section (total length: %08x)", len); |
393 | |
355 | |
394 | return res; |
356 | return 0; |
395 | |
357 | |
396 | ERROR5: |
|
|
397 | free((*result)->blue); |
|
|
398 | ERROR4: |
358 | ERROR4: |
399 | free((*result)->green); |
359 | psiconv_list_free(pixels); |
400 | ERROR3: |
360 | ERROR3: |
401 | free((*result)->red); |
361 | psiconv_list_free(bytes); |
402 | ERROR2: |
362 | ERROR2: |
403 | free (*result); |
363 | free(*result); |
404 | ERROR1: |
364 | ERROR1: |
405 | psiconv_warn(config,lev+1,off,"Reading of Paint Data Section failed"); |
365 | psiconv_error(config,lev+1,off,"Reading of Paint Data Section failed"); |
406 | if (length) |
366 | if (length) |
407 | *length = 0; |
367 | *length = 0; |
408 | if (!res) |
368 | if (!res) |
409 | return -PSICONV_E_NOMEM; |
369 | return -PSICONV_E_NOMEM; |
410 | else |
370 | else |
… | |
… | |
565 | ERROR3: |
525 | ERROR3: |
566 | psiconv_free_paint_data_section((*result)->picture); |
526 | psiconv_free_paint_data_section((*result)->picture); |
567 | ERROR2: |
527 | ERROR2: |
568 | free (*result); |
528 | free (*result); |
569 | ERROR1: |
529 | ERROR1: |
570 | psiconv_warn(config,lev+1,off,"Reading of Sketch Section failed"); |
530 | psiconv_error(config,lev+1,off,"Reading of Sketch Section failed"); |
571 | if (length) |
531 | if (length) |
572 | *length = 0; |
532 | *length = 0; |
573 | if (!res) |
533 | if (!res) |
574 | return -PSICONV_E_NOMEM; |
534 | return -PSICONV_E_NOMEM; |
575 | else |
535 | else |
… | |
… | |
668 | return 0; |
628 | return 0; |
669 | |
629 | |
670 | ERROR2: |
630 | ERROR2: |
671 | free (*result); |
631 | free (*result); |
672 | ERROR1: |
632 | ERROR1: |
673 | psiconv_warn(config,lev+1,off,"Reading of Font failed"); |
633 | psiconv_error(config,lev+1,off,"Reading of Font failed"); |
674 | if (length) |
634 | if (length) |
675 | *length = 0; |
635 | *length = 0; |
676 | if (!res) |
636 | if (!res) |
677 | return -PSICONV_E_NOMEM; |
637 | return -PSICONV_E_NOMEM; |
678 | else |
638 | else |
… | |
… | |
701 | *marker+1); |
661 | *marker+1); |
702 | psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1); |
662 | psiconv_progress(config,lev+2,off,"Going to read value byte at %04x",i+1); |
703 | if (!(value = psiconv_list_get(encoded,i+1))) |
663 | if (!(value = psiconv_list_get(encoded,i+1))) |
704 | goto ERROR2; |
664 | goto ERROR2; |
705 | psiconv_debug(config,lev+2,off,"Value byte: %02x",*value); |
665 | psiconv_debug(config,lev+2,off,"Value byte: %02x",*value); |
706 | psiconv_progress(config,lev+2,off,"Adding %02x bytes %02x", |
666 | psiconv_progress(config,lev+2,off,"Adding %02x pixels %02x", |
707 | *marker+1,*value); |
667 | *marker+1,*value); |
708 | for (j = 0; j < *marker + 1; j++) |
668 | for (j = 0; j < *marker + 1; j++) |
709 | if ((res = psiconv_list_add(*decoded,value))) |
669 | if ((res = psiconv_list_add(*decoded,value))) |
710 | goto ERROR2; |
670 | goto ERROR2; |
711 | i += 2; |
671 | i += 2; |
… | |
… | |
729 | return 0; |
689 | return 0; |
730 | |
690 | |
731 | ERROR2: |
691 | ERROR2: |
732 | psiconv_list_free(*decoded); |
692 | psiconv_list_free(*decoded); |
733 | ERROR1: |
693 | ERROR1: |
734 | psiconv_warn(config,lev+1,off,"Decoding of RLE8 failed"); |
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 | |
|
|
701 | int 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 | |
|
|
734 | ERROR2: |
|
|
735 | psiconv_list_free(*decoded); |
|
|
736 | ERROR1: |
|
|
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 | |
|
|
744 | int 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 | |
|
|
800 | ERROR2: |
|
|
801 | psiconv_list_free(*decoded); |
|
|
802 | ERROR1: |
|
|
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 | |
|
|
810 | int 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 | |
|
|
870 | ERROR2: |
|
|
871 | psiconv_list_free(*decoded); |
|
|
872 | ERROR1: |
|
|
873 | psiconv_error(config,lev+1,off,"Decoding of RLE24 failed"); |
735 | if (!res) |
874 | if (!res) |
736 | return -PSICONV_E_NOMEM; |
875 | return -PSICONV_E_NOMEM; |
737 | else |
876 | else |
738 | return res; |
877 | return res; |
739 | } |
878 | } |
… | |
… | |
796 | |
935 | |
797 | |
936 | |
798 | ERROR2: |
937 | ERROR2: |
799 | psiconv_list_free(*pixels); |
938 | psiconv_list_free(*pixels); |
800 | ERROR1: |
939 | ERROR1: |
801 | psiconv_warn(config,lev+1,off,"Converting bytes to pixels failed"); |
940 | psiconv_error(config,lev+1,off,"Converting bytes to pixels failed"); |
802 | if (!res) |
941 | if (!res) |
803 | return -PSICONV_E_NOMEM; |
942 | return -PSICONV_E_NOMEM; |
804 | else |
943 | else |
805 | return res; |
944 | return res; |
806 | } |
945 | } |
… | |
… | |
833 | if (!(pixel = psiconv_list_get(pixels,i))) |
972 | if (!(pixel = psiconv_list_get(pixels,i))) |
834 | goto ERROR4; |
973 | goto ERROR4; |
835 | psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel); |
974 | psiconv_progress(config,lev+2,off, "Handling pixel %04x (%04x)",i,*pixel); |
836 | if (!palet.length) { |
975 | if (!palet.length) { |
837 | if (color) { |
976 | if (color) { |
838 | (*floats).blue[i] = (*pixel & ((1 << bluebits) - 1)) / |
977 | (*floats).blue[i] = ((float) (*pixel & ((1 << bluebits) - 1))) / |
839 | (1 << bluebits); |
978 | (1 << bluebits); |
840 | (*floats).green[i] = ((*pixel >> bluebits) & ((1 << greenbits) - 1)) / |
979 | (*floats).green[i] = ((float) ((*pixel >> bluebits) & |
841 | (1 << greenbits); |
980 | ((1 << greenbits) - 1))) / (1 << greenbits); |
842 | (*floats).red[i] = ((*pixel >> (bluebits+greenbits)) & |
981 | (*floats).red[i] = ((float) ((*pixel >> (bluebits+greenbits)) & |
843 | ((1 << redbits) - 1)) / (1 << redbits); |
982 | ((1 << redbits) - 1))) / (1 << redbits); |
844 | } else { |
983 | } else { |
845 | (*floats).red[i] = (*floats).green[i] = |
984 | (*floats).red[i] = (*floats).green[i] = |
846 | (*floats).blue[i] = *pixel / (1 << colordepth); |
985 | (*floats).blue[i] = ((float) *pixel) / |
|
|
986 | (1 << colordepth); |
847 | } |
987 | } |
848 | } else { |
988 | } else { |
849 | if (*pixel >= palet.length) { |
989 | if (*pixel >= palet.length) { |
850 | psiconv_warn(config,lev+2,off, |
990 | psiconv_warn(config,lev+2,off, |
851 | "Invalid palet color found (using color 0x00)"); |
991 | "Invalid palet color found (using color 0x00)"); |
… | |
… | |
869 | ERROR3: |
1009 | ERROR3: |
870 | free((*floats).green); |
1010 | free((*floats).green); |
871 | ERROR2: |
1011 | ERROR2: |
872 | free((*floats).red); |
1012 | free((*floats).red); |
873 | ERROR1: |
1013 | ERROR1: |
874 | psiconv_warn(config,lev+1,off,"Converting pixels to floats failed"); |
1014 | psiconv_error(config,lev+1,off,"Converting pixels to floats failed"); |
875 | if (!res) |
1015 | if (!res) |
876 | return -PSICONV_E_NOMEM; |
1016 | return -PSICONV_E_NOMEM; |
877 | else |
1017 | else |
878 | return res; |
1018 | return res; |
879 | } |
1019 | } |