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 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
16 | along with this program; if not, write to the Free Software |
16 | along with this program; if not, write to the Free Software |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
18 | */ |
18 | */ |
19 | |
19 | |
20 | #include "config.h" |
20 | #include "config.h" |
|
|
21 | #include "compat.h" |
|
|
22 | |
21 | #include <stdlib.h> |
23 | #include <stdlib.h> |
22 | |
24 | |
23 | #include "data.h" |
|
|
24 | #include "parse_routines.h" |
25 | #include "parse_routines.h" |
|
|
26 | #include "error.h" |
25 | |
27 | |
|
|
28 | #ifdef DMALLOC |
|
|
29 | #include <dmalloc.h> |
|
|
30 | #endif |
|
|
31 | |
|
|
32 | |
26 | int psiconv_parse_mbm_jumptable_section(const psiconv_buffer buf,int lev, |
33 | int psiconv_parse_jumptable_section(const psiconv_buffer buf,int lev, |
27 | psiconv_u32 off, int *length, |
34 | psiconv_u32 off, int *length, |
28 | psiconv_mbm_jumptable_section *result) |
35 | psiconv_jumptable_section *result) |
29 | { |
36 | { |
30 | int res = 0; |
37 | int res = 0; |
31 | int len = 0; |
38 | int len = 0; |
32 | psiconv_u32 listlen,temp; |
39 | psiconv_u32 listlen,temp; |
33 | int i; |
40 | int i; |
34 | |
41 | |
35 | psiconv_progress(lev+1,off+len,"Going to read the mbm jumptable section"); |
42 | psiconv_progress(lev+1,off+len,"Going to read the jumptable section"); |
36 | (*result) = psiconv_list_new(sizeof(psiconv_u32)); |
43 | if (!((*result) = psiconv_list_new(sizeof(psiconv_u32)))) |
|
|
44 | goto ERROR1; |
37 | |
45 | |
38 | psiconv_progress(lev+2,off+len,"Going to read the list length"); |
46 | psiconv_progress(lev+2,off+len,"Going to read the list length"); |
39 | listlen = psiconv_read_u32(buf,lev+2,off+len); |
47 | listlen = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
48 | if (res) |
|
|
49 | goto ERROR2; |
40 | psiconv_debug(lev+2,off+len,"List length: %08x",listlen); |
50 | psiconv_debug(lev+2,off+len,"List length: %08x",listlen); |
41 | len += 4; |
51 | len += 4; |
42 | |
52 | |
43 | psiconv_progress(lev+2,off+len,"Going to read the list"); |
53 | psiconv_progress(lev+2,off+len,"Going to read the list"); |
44 | for (i = 0; i < listlen; i++) { |
54 | for (i = 0; i < listlen; i++) { |
45 | temp = psiconv_read_u32(buf,lev+2,off+len); |
55 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
56 | if (res) |
|
|
57 | goto ERROR2; |
46 | psiconv_list_add(*result,&temp); |
58 | if ((res = psiconv_list_add(*result,&temp))) |
|
|
59 | goto ERROR2; |
47 | psiconv_debug(lev+3,off+len,"Offset: %08x",temp); |
60 | psiconv_debug(lev+3,off+len,"Offset: %08x",temp); |
48 | len += 4; |
61 | len += 4; |
49 | } |
62 | } |
50 | |
63 | |
51 | if (length) |
64 | if (length) |
52 | *length = len; |
65 | *length = len; |
53 | |
66 | |
54 | psiconv_progress(lev+1,off+len-1,"End of mbm jumptable section " |
67 | psiconv_progress(lev+1,off+len-1,"End of jumptable section " |
55 | "(total length: %08x)", len); |
68 | "(total length: %08x)", len); |
56 | |
69 | |
|
|
70 | return 0; |
|
|
71 | |
|
|
72 | ERROR2: |
|
|
73 | psiconv_list_free(*result); |
|
|
74 | ERROR1: |
|
|
75 | psiconv_warn(lev+1,off,"Reading of Jumptable Section failed"); |
|
|
76 | if (length) |
|
|
77 | *length = 0; |
|
|
78 | if (!res) |
|
|
79 | return -PSICONV_E_NOMEM; |
|
|
80 | else |
57 | return res; |
81 | return res; |
58 | } |
82 | } |
59 | |
83 | |
|
|
84 | static int decode_byte(int lev, psiconv_u32 off, |
|
|
85 | psiconv_paint_data_section data, psiconv_u32 *pixelnr, |
|
|
86 | psiconv_u8 byte, int bits_per_pixel, int linelen, |
|
|
87 | int *linepos,int picsize) |
|
|
88 | { |
|
|
89 | int mask = (bits_per_pixel << 1) -1; |
|
|
90 | int i; |
|
|
91 | if (*linepos < (data->xsize + (8/bits_per_pixel) - 1) / (8/bits_per_pixel)) |
|
|
92 | for (i = 0; i < 8/bits_per_pixel; i ++) { |
|
|
93 | if ((i != 0) && ((*pixelnr % (data->xsize)) == 0)) { |
|
|
94 | psiconv_debug(lev+1,off,"Skipping padding: %02x",byte); |
|
|
95 | i = 8; |
|
|
96 | } else if (*pixelnr >= picsize) { |
|
|
97 | psiconv_warn(lev+1,off,"Corrupted picture data!"); |
|
|
98 | psiconv_debug(lev+1,off,"Trying to write a pixel too far"); |
|
|
99 | return -1; |
|
|
100 | } else { |
|
|
101 | data->red[*pixelnr] = data->green[*pixelnr] = data->blue[*pixelnr] = |
|
|
102 | ((float) (byte & mask)) / ((1 << bits_per_pixel) -1); |
|
|
103 | psiconv_debug(lev+1,off,"Pixel %04x: (%04x,%04x) value %02x, color %f", |
|
|
104 | *pixelnr,*pixelnr % data->xsize, |
|
|
105 | *pixelnr / data->xsize, byte&mask, data->red[*pixelnr]); |
|
|
106 | byte = byte >> bits_per_pixel; |
|
|
107 | (*pixelnr) ++; |
|
|
108 | } |
|
|
109 | } |
|
|
110 | else |
|
|
111 | psiconv_debug(lev+1,off,"Skipping padding byte"); |
|
|
112 | (*linepos) ++; |
|
|
113 | if (*linepos == linelen) |
|
|
114 | *linepos = 0; |
|
|
115 | return 0; |
|
|
116 | } |
|
|
117 | |
|
|
118 | |
60 | int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, |
119 | int psiconv_parse_paint_data_section(const psiconv_buffer buf,int lev, |
61 | psiconv_u32 off, int *length, |
120 | psiconv_u32 off, int *length,int isclipart, |
62 | psiconv_paint_data_section *result) |
121 | psiconv_paint_data_section *result) |
63 | { |
122 | { |
64 | int res = 0; |
123 | int res = 0; |
65 | int len = 0; |
124 | int len = 0; |
66 | int read_err = 0; |
|
|
67 | psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; |
125 | psiconv_u32 size,offset,picsize,temp,datasize,pixelnr,datanr,linelen; |
68 | psiconv_u8 marker; |
126 | psiconv_u8 marker; |
69 | int i; |
127 | int i,leng; |
|
|
128 | psiconv_u32 bits_per_pixel,compression; |
|
|
129 | int linepos = 0; |
70 | |
130 | |
71 | psiconv_progress(lev+1,off,"Going to read a paint data section"); |
131 | psiconv_progress(lev+1,off,"Going to read a paint data section"); |
72 | (*result) = malloc(sizeof(**result)); |
132 | if (!((*result) = malloc(sizeof(**result)))) |
|
|
133 | goto ERROR1; |
73 | |
134 | |
74 | psiconv_progress(lev+2,off+len,"Going to read section size"); |
135 | psiconv_progress(lev+2,off+len,"Going to read section size"); |
75 | size = psiconv_read_u32(buf,lev+2,off+len); |
136 | size = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
137 | if (res) |
|
|
138 | goto ERROR2; |
76 | psiconv_debug(lev+2,off+len,"Section size: %08x",size); |
139 | psiconv_debug(lev+2,off+len,"Section size: %08x",size); |
77 | len += 4; |
140 | len += 4; |
78 | |
141 | |
79 | psiconv_progress(lev+2,off+len,"Going to read pixel data offset"); |
142 | psiconv_progress(lev+2,off+len,"Going to read pixel data offset"); |
80 | offset = psiconv_read_u32(buf,lev+2,off+len); |
143 | offset = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
144 | if (res) |
|
|
145 | goto ERROR2; |
81 | if (offset != 0x28) { |
146 | if (offset != 0x28) { |
82 | psiconv_warn(lev+2,off+len, |
147 | psiconv_warn(lev+2,off+len, |
83 | "Paint data section data offset has unexpected value"); |
148 | "Paint data section data offset has unexpected value"); |
84 | psiconv_debug(lev+2,off+len, |
149 | psiconv_debug(lev+2,off+len, |
85 | "Data offset: read %08x, expected %08x",offset,0x28); |
150 | "Data offset: read %08x, expected %08x",offset,0x28); |
86 | res = -1; |
151 | res = -1; |
87 | } |
152 | } |
88 | len += 4; |
153 | len += 4; |
89 | |
154 | |
90 | psiconv_progress(lev+2,off+len,"Going to read picture X size"); |
155 | psiconv_progress(lev+2,off+len,"Going to read picture X size"); |
91 | (*result)->xsize = psiconv_read_u32(buf,lev+2,off+len); |
156 | (*result)->xsize = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
157 | if (res) |
|
|
158 | goto ERROR2; |
92 | psiconv_debug(lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
159 | psiconv_debug(lev+2,off+len,"Picture X size: %08x:",(*result)->xsize); |
93 | len += 4; |
160 | len += 4; |
94 | |
161 | |
95 | psiconv_progress(lev+2,off+len,"Going to read picture Y size"); |
162 | psiconv_progress(lev+2,off+len,"Going to read picture Y size"); |
96 | (*result)->ysize = psiconv_read_u32(buf,lev+2,off+len); |
163 | (*result)->ysize = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
164 | if (res) |
|
|
165 | goto ERROR2; |
97 | psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
166 | psiconv_debug(lev+2,off+len,"Picture Y size: %08x:",(*result)->ysize); |
98 | len += 4; |
167 | len += 4; |
99 | |
168 | |
100 | picsize = (*result)->ysize * (*result)->xsize * 2; |
169 | picsize = (*result)->ysize * (*result)->xsize; |
101 | linelen = (*result)->xsize; |
|
|
102 | |
170 | |
|
|
171 | psiconv_progress(lev+2,off+len,"Going to read the real picture x size"); |
|
|
172 | (*result)->pic_xsize = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
|
|
173 | if (res) |
|
|
174 | goto ERROR2; |
|
|
175 | psiconv_debug(lev+2,off+len,"Picture x size: %f",(*result)->pic_xsize); |
|
|
176 | len += leng; |
|
|
177 | |
|
|
178 | psiconv_progress(lev+2,off+len,"Going to read the real picture y size"); |
|
|
179 | (*result)->pic_ysize = psiconv_read_length(buf,lev+2,off+len,&leng,&res); |
|
|
180 | if (res) |
|
|
181 | goto ERROR2; |
|
|
182 | psiconv_debug(lev+2,off+len,"Picture y size: %f",(*result)->pic_ysize); |
|
|
183 | len += leng; |
|
|
184 | |
103 | psiconv_progress(lev+2,off+len,"Going to read 6 unused longs"); |
185 | psiconv_progress(lev+2,off+len,"Going to read the number of bits per pixel"); |
|
|
186 | bits_per_pixel=psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
187 | if (res) |
|
|
188 | goto ERROR2; |
|
|
189 | if (bits_per_pixel > 8) { |
|
|
190 | psiconv_warn(lev+2,off+len,"Picture has too many colors"); |
|
|
191 | psiconv_debug(lev+2,off+len,"Read %d colorbits",bits_per_pixel); |
|
|
192 | res = -PSICONV_E_PARSE; |
|
|
193 | goto ERROR2; |
|
|
194 | } |
|
|
195 | psiconv_debug(lev+2,off+len,"Bits per pixel: %d",bits_per_pixel); |
|
|
196 | len += 4; |
|
|
197 | |
104 | for (i = 0 ; i < 6; i++) { |
198 | for (i = 0 ; i < 2; i++) { |
105 | temp = psiconv_read_u32(buf,lev+2,off+len); |
199 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
200 | if (res) |
|
|
201 | goto ERROR2; |
106 | if (temp != 0x00) { |
202 | if (temp != 00) { |
107 | psiconv_warn(lev+2,off+len, |
203 | psiconv_warn(lev+2,off+len, |
108 | "Paint data section prologue has unknown values"); |
204 | "Paint data section prologue has unknown values (ignored)"); |
109 | psiconv_debug(lev+2,off+len, |
205 | psiconv_debug(lev+2,off+len, |
110 | "offset %02x: read %08x, expected %08x",i,temp,0x00); |
206 | "offset %02x: read %08x, expected %08x",i,temp, 0x00); |
111 | res = -1; |
|
|
112 | } |
207 | } |
113 | len += 4; |
208 | len += 4; |
114 | } |
209 | } |
|
|
210 | |
|
|
211 | psiconv_progress(lev+2,off+len, |
|
|
212 | "Going to read whether RLE compression is used"); |
|
|
213 | compression=psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
214 | if (res) |
|
|
215 | goto ERROR2; |
|
|
216 | if (compression > 1) { |
|
|
217 | psiconv_warn(lev+2,off+len,"Paint data section has unknown " |
|
|
218 | "compression type, assuming RLE"); |
|
|
219 | psiconv_debug(lev+2,off+len,"Read compression type %d",compression); |
|
|
220 | compression = 1; |
|
|
221 | } |
|
|
222 | psiconv_debug(lev+2,off+len,"Compression: %s",compression?"RLE":"none"); |
|
|
223 | len += 4; |
115 | |
224 | |
|
|
225 | if (isclipart) { |
|
|
226 | psiconv_progress(lev+2,off+len,"Going to read an unknown long"); |
|
|
227 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
228 | if (res) |
|
|
229 | goto ERROR2; |
|
|
230 | if (temp != 0xffffffff) { |
|
|
231 | psiconv_warn(lev+2,off+len, |
|
|
232 | "Paint data section prologue has unknown values (ignoring)"); |
|
|
233 | psiconv_debug(lev+2,off+len, |
|
|
234 | "offset %02x: read %08x, expected %08x",i,temp, 0xffffffff); |
|
|
235 | } |
|
|
236 | len += 4; |
|
|
237 | psiconv_progress(lev+2,off+len,"Going to read a second unknown long"); |
|
|
238 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
239 | if (res) |
|
|
240 | goto ERROR2; |
|
|
241 | if (temp != 0x44) { |
|
|
242 | psiconv_warn(lev+2,off+len, |
|
|
243 | "Paint data section prologue has unknown values (ignoring)"); |
|
|
244 | psiconv_debug(lev+2,off+len, |
|
|
245 | "offset %02x: read %08x, expected %08x",i,temp, 0x44); |
|
|
246 | } |
|
|
247 | len += 4; |
|
|
248 | } |
|
|
249 | |
116 | (*result)->red = malloc(sizeof(float) * picsize); |
250 | if (!((*result)->red = malloc(sizeof(float) * picsize))) |
|
|
251 | goto ERROR2; |
117 | (*result)->green = malloc(sizeof(float) * picsize); |
252 | if (!((*result)->green = malloc(sizeof(float) * picsize))) |
|
|
253 | goto ERROR3; |
118 | (*result)->blue = malloc(sizeof(float) * picsize); |
254 | if (!((*result)->blue = malloc(sizeof(float) * picsize))) |
|
|
255 | goto ERROR4; |
119 | len = offset; |
256 | len = offset; |
120 | datasize = size - len; |
257 | datasize = size - len; |
|
|
258 | if (isclipart) |
|
|
259 | len += 8; |
121 | |
260 | |
122 | psiconv_progress(lev+2,off+len,"Going to read the pixel data"); |
261 | psiconv_progress(lev+2,off+len,"Going to read the pixel data"); |
123 | pixelnr = 0; |
262 | pixelnr = 0; |
124 | datanr = 0; |
263 | datanr = 0; |
125 | while ((datanr < datasize) && (pixelnr < picsize)) { |
264 | if (!compression) { |
|
|
265 | linelen = datasize / (*result)->ysize; |
|
|
266 | psiconv_debug(lev+3,off+len,"Line length: %04x bytes",linelen); |
|
|
267 | while((datanr < datasize)) { |
|
|
268 | temp = psiconv_read_u8(buf,lev+2,off+len+datanr,&res); |
|
|
269 | if (res) |
|
|
270 | goto ERROR5; |
|
|
271 | if (decode_byte(lev+3,off+len+datanr,*result,&pixelnr,temp,bits_per_pixel, |
|
|
272 | linelen,&linepos,picsize)) { |
|
|
273 | res = -PSICONV_E_PARSE; |
|
|
274 | break; |
|
|
275 | } |
|
|
276 | datanr++; |
|
|
277 | } |
|
|
278 | } else { |
|
|
279 | psiconv_progress(lev+2,off+len,"First pass: determining line length"); |
|
|
280 | datanr = 0; |
|
|
281 | i = 0; |
|
|
282 | while (datanr < datasize) { |
126 | marker = psiconv_read_u8(buf,lev+3,off+len+datanr); |
283 | marker = psiconv_read_u8(buf,lev+3,off+len+datanr,&res); |
127 | psiconv_debug(lev+3,off+len+datanr, |
284 | if (res) |
128 | "Pixelnr %08x, Datanr %08x: Read marker %02x", |
285 | goto ERROR5; |
129 | pixelnr,datanr,marker); |
|
|
130 | datanr ++; |
|
|
131 | if (marker >= 0x80) { |
286 | if (marker >= 0x80) { |
132 | /* 0x100 - marker bytes of data follow */ |
287 | datanr += 0x100 - marker + 1; |
133 | if ((0x100 - marker + datanr > datasize) || |
288 | i += 0x100 - marker; |
134 | ((0x100 - marker) * 4 + pixelnr > picsize)) { |
|
|
135 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
136 | psiconv_debug(lev+3,off+len+datanr, |
|
|
137 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
138 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
139 | datanr,marker); |
|
|
140 | res = -1; |
|
|
141 | read_err = 1; |
|
|
142 | } else { |
289 | } else { |
143 | for (i = 0; i < 0x100-marker; i++) { |
290 | datanr += 2; |
144 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr+i); |
291 | i += marker + 1; |
145 | (*result)->red[pixelnr + i*4] = |
|
|
146 | (*result)->green[pixelnr + i*4] = |
|
|
147 | (*result)->blue[pixelnr + i*4] = |
|
|
148 | (temp & 0x03) * (1.0/3.0); |
|
|
149 | (*result)->red[pixelnr + i*4 + 1] = |
|
|
150 | (*result)->green[pixelnr + i*4 +1] = |
|
|
151 | (*result)->blue[pixelnr + i*4 + 1] = |
|
|
152 | ((temp >> 2) & 0x03) * (1.0/3.0); |
|
|
153 | (*result)->red[pixelnr + i*4 + 2] = |
|
|
154 | (*result)->green[pixelnr + i*4 +2] = |
|
|
155 | (*result)->blue[pixelnr + i*4 + 2] = |
|
|
156 | ((temp >> 4) & 0x03) * (1.0/3.0); |
|
|
157 | (*result)->red[pixelnr + i*4 + 3] = |
|
|
158 | (*result)->green[pixelnr + i*4 +3] = |
|
|
159 | (*result)->blue[pixelnr + i*4 + 3] = |
|
|
160 | ((temp >> 6) & 0x03) * (1.0/3.0); |
|
|
161 | } |
|
|
162 | } |
292 | } |
163 | pixelnr += (0x100 - marker) * 4; |
293 | } |
164 | datanr += (0x100 - marker); |
294 | linelen = i / (*result)->ysize; |
165 | } else { /* marker < 0x080 */ |
295 | datanr=0; |
166 | if ((datanr + 1 > datasize) || |
296 | psiconv_debug(lev+2,off+len,"Linelen: %04x bytes",linelen); |
167 | (marker * 4 + pixelnr > picsize)) { |
297 | while((datanr < datasize)) { |
168 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
298 | marker = psiconv_read_u8(buf,lev+3,off+len+datanr,&res); |
|
|
299 | if (res) |
|
|
300 | goto ERROR5; |
169 | psiconv_debug(lev+3,off+len+datanr, |
301 | psiconv_debug(lev+3,off+len+datanr, |
|
|
302 | "Pixelnr %08x, Datanr %08x: Read marker %02x", |
|
|
303 | pixelnr,datanr,marker); |
|
|
304 | datanr ++; |
|
|
305 | if (marker >= 0x80) { |
|
|
306 | /* 0x100 - marker bytes of data follow */ |
|
|
307 | for (i = 0; i < 0x100-marker; i++,datanr++) { |
|
|
308 | if (datanr >= datasize) { |
|
|
309 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
310 | psiconv_debug(lev+3,off+len+datanr, |
170 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
311 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
171 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
312 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
172 | datanr,marker); |
313 | datanr,marker); |
173 | res = -1; |
314 | res = -PSICONV_E_PARSE; |
174 | read_err = 1; |
315 | break; |
|
|
316 | } |
|
|
317 | temp = psiconv_read_u8(buf,lev+2,off+len+datanr,&res); |
|
|
318 | if (res) |
|
|
319 | goto ERROR5; |
|
|
320 | if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp, |
|
|
321 | bits_per_pixel,linelen,&linepos,picsize)) { |
|
|
322 | res = -PSICONV_E_PARSE; |
|
|
323 | break; |
|
|
324 | } |
|
|
325 | } |
175 | } else { |
326 | } else { |
|
|
327 | if (datanr >= datasize) { |
|
|
328 | psiconv_warn(lev+3,off+len+datanr,"Corrupted picture data"); |
|
|
329 | psiconv_debug(lev+3,off+len+datanr, |
|
|
330 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
|
|
331 | "Datanr: %08x, marker: %02x",picsize,datasize,pixelnr, |
|
|
332 | datanr,marker); |
|
|
333 | res = -PSICONV_E_PARSE; |
|
|
334 | } else { |
176 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr); |
335 | temp = psiconv_read_u8(buf,lev+3,off+len+datanr,&res); |
|
|
336 | if (res) |
|
|
337 | goto ERROR5; |
177 | for (i = 0; i < marker; i++) { |
338 | for (i = 0; i <= marker; i++) { |
178 | (*result)->red[pixelnr + i*4] = |
339 | if (decode_byte(lev+2,off+len+datanr,*result,&pixelnr,temp, |
179 | (*result)->green[pixelnr + i*4] = |
340 | bits_per_pixel,linelen,&linepos,picsize)) { |
180 | (*result)->blue[pixelnr + i*4] = |
341 | res = -PSICONV_E_PARSE; |
181 | (temp & 0x03) * (1.0/3.0); |
342 | break; |
182 | (*result)->red[pixelnr + i*4 + 1] = |
|
|
183 | (*result)->green[pixelnr + i*4 +1] = |
|
|
184 | (*result)->blue[pixelnr + i*4 + 1] = |
|
|
185 | ((temp >> 2) & 0x03) * (1.0/3.0); |
|
|
186 | (*result)->red[pixelnr + i*4 + 2] = |
|
|
187 | (*result)->green[pixelnr + i*4 +2] = |
|
|
188 | (*result)->blue[pixelnr + i*4 + 2] = |
|
|
189 | ((temp >> 4) & 0x03) * (1.0/3.0); |
|
|
190 | (*result)->red[pixelnr + i*4 + 3] = |
|
|
191 | (*result)->green[pixelnr + i*4 +3] = |
|
|
192 | (*result)->blue[pixelnr + i*4 + 3] = |
|
|
193 | ((temp >> 6) & 0x03) * (1.0/3.0); |
|
|
194 | } |
343 | } |
|
|
344 | } |
|
|
345 | datanr ++; |
|
|
346 | } |
195 | } |
347 | } |
196 | pixelnr += marker * 4; |
348 | } |
197 | datanr += 1; |
349 | } |
|
|
350 | |
|
|
351 | if (linepos >= ((*result)->xsize + (8/bits_per_pixel) - 1) / |
|
|
352 | (8/bits_per_pixel)) |
|
|
353 | datanr += (linelen - linepos); |
198 | } |
354 | |
199 | } |
|
|
200 | if (!read_err && ((datanr != datasize) || (pixelnr != picsize))) { |
355 | if (res || (datanr != datasize) || (pixelnr != picsize)) { |
201 | psiconv_warn(lev+2,off+len,"Corrupted picture data!"); |
356 | psiconv_warn(lev+2,off+len,"Corrupted picture data!"); |
202 | psiconv_debug(lev+3,off+len+datanr, |
357 | psiconv_debug(lev+3,off+len+datanr, |
203 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
358 | "Picsize: %08x, Datasize: %08x, Pixelnr: %08x," |
204 | "Datanr: %08x",picsize,datasize,pixelnr,datanr); |
359 | "Datanr: %08x",picsize,datasize,pixelnr,datanr); |
205 | res = -1; |
360 | goto ERROR5; |
206 | } |
361 | } |
207 | |
362 | |
208 | len += datanr; |
363 | len += datanr; |
209 | |
364 | |
210 | if (length) |
365 | if (length) |
… | |
… | |
212 | |
367 | |
213 | psiconv_progress(lev+1,off+len-1,"End of paint data section " |
368 | psiconv_progress(lev+1,off+len-1,"End of paint data section " |
214 | "(total length: %08x)", len); |
369 | "(total length: %08x)", len); |
215 | |
370 | |
216 | return res; |
371 | return res; |
|
|
372 | |
|
|
373 | ERROR5: |
|
|
374 | free((*result)->blue); |
|
|
375 | ERROR4: |
|
|
376 | free((*result)->green); |
|
|
377 | ERROR3: |
|
|
378 | free((*result)->red); |
|
|
379 | ERROR2: |
|
|
380 | free (*result); |
|
|
381 | ERROR1: |
|
|
382 | psiconv_warn(lev+1,off,"Reading of Paint Data Section failed"); |
|
|
383 | if (length) |
|
|
384 | *length = 0; |
|
|
385 | if (!res) |
|
|
386 | return -PSICONV_E_NOMEM; |
|
|
387 | else |
|
|
388 | return res; |
217 | } |
389 | } |
218 | |
390 | |
|
|
391 | int psiconv_parse_sketch_section(const psiconv_buffer buf, int lev, |
|
|
392 | psiconv_u32 off, int *length, int is_object, |
|
|
393 | psiconv_sketch_section *result) |
|
|
394 | { |
|
|
395 | int res=0; |
|
|
396 | int len=0; |
|
|
397 | psiconv_u32 temp; |
|
|
398 | int leng; |
|
|
399 | int i; |
219 | |
400 | |
|
|
401 | psiconv_progress(lev+1,off,"Going to read the sketch section"); |
|
|
402 | if (!(*result = malloc(sizeof(**result)))) |
|
|
403 | goto ERROR1; |
|
|
404 | |
|
|
405 | if (!is_object) { |
|
|
406 | psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); |
|
|
407 | (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
408 | if (res) |
|
|
409 | goto ERROR2; |
|
|
410 | psiconv_debug(lev+2,off+len,"Form hor. size: %04x", |
|
|
411 | (*result)->form_xsize); |
|
|
412 | len += 0x02; |
|
|
413 | psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); |
|
|
414 | (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
415 | if (res) |
|
|
416 | goto ERROR2; |
|
|
417 | psiconv_debug(lev+2,off+len,"Form ver. size: %04x", |
|
|
418 | (*result)->form_ysize); |
|
|
419 | len += 0x02; |
|
|
420 | psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); |
|
|
421 | (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
422 | if (res) |
|
|
423 | goto ERROR2; |
|
|
424 | psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x", |
|
|
425 | (*result)->picture_x_offset); |
|
|
426 | len += 0x02; |
|
|
427 | psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset"); |
|
|
428 | (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
429 | if (res) |
|
|
430 | goto ERROR2; |
|
|
431 | psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x", |
|
|
432 | (*result)->picture_y_offset); |
|
|
433 | len += 0x02; |
|
|
434 | psiconv_progress(lev+2,off+len,"Going to skip 5 words of zeros"); |
|
|
435 | for (i = 0; i < 5; i++) { |
|
|
436 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
437 | if (res) |
|
|
438 | goto ERROR2; |
|
|
439 | if (temp != 0) { |
|
|
440 | psiconv_warn(lev+2,off+len, |
|
|
441 | "Unexpected value in sketch section preamble (ignored)"); |
|
|
442 | psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i, |
|
|
443 | temp,0); |
|
|
444 | } |
|
|
445 | off += 0x02; |
|
|
446 | } |
|
|
447 | } else { |
|
|
448 | psiconv_progress(lev+2,off+len,"Going to read the displayed hor. size"); |
|
|
449 | (*result)->picture_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
450 | if (res) |
|
|
451 | goto ERROR2; |
|
|
452 | psiconv_debug(lev+2,off+len,"Displayed hor. size: %04x", |
|
|
453 | (*result)->picture_xsize); |
|
|
454 | len += 0x02; |
|
|
455 | psiconv_progress(lev+2,off+len,"Going to read the displayed ver. size"); |
|
|
456 | (*result)->picture_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
457 | if (res) |
|
|
458 | goto ERROR2; |
|
|
459 | psiconv_debug(lev+2,off+len,"Displayed ver. size: %04x", |
|
|
460 | (*result)->picture_ysize); |
|
|
461 | len += 0x02; |
|
|
462 | |
|
|
463 | psiconv_progress(lev+2,off+len,"Going to skip 2 words of zeros"); |
|
|
464 | for (i = 0; i < 2; i++) { |
|
|
465 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
466 | if (res) |
|
|
467 | goto ERROR2; |
|
|
468 | if (temp != 0) { |
|
|
469 | psiconv_warn(lev+2,off+len, |
|
|
470 | "Unexpected value in sketch section preamble (ignored)"); |
|
|
471 | psiconv_debug(lev+2,off+len,"Word %d: Read %04x, expected %04x",i, |
|
|
472 | temp,0); |
|
|
473 | } |
|
|
474 | off += 0x02; |
|
|
475 | } |
|
|
476 | psiconv_progress(lev+2,off+len,"Going to read the picture hor. offset"); |
|
|
477 | (*result)->picture_x_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
478 | if (res) |
|
|
479 | goto ERROR2; |
|
|
480 | psiconv_debug(lev+2,off+len,"Picture hor. offset: %04x", |
|
|
481 | (*result)->picture_x_offset); |
|
|
482 | len += 0x02; |
|
|
483 | psiconv_progress(lev+2,off+len,"Going to read the picture ver. offset"); |
|
|
484 | (*result)->picture_y_offset = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
485 | if (res) |
|
|
486 | goto ERROR2; |
|
|
487 | psiconv_debug(lev+2,off+len,"Picture ver. offset: %04x", |
|
|
488 | (*result)->picture_y_offset); |
|
|
489 | len += 0x02; |
|
|
490 | psiconv_progress(lev+2,off+len,"Going to read the form hor. size"); |
|
|
491 | (*result)->form_xsize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
492 | if (res) |
|
|
493 | goto ERROR2; |
|
|
494 | psiconv_debug(lev+2,off+len,"Form hor. size: %04x", |
|
|
495 | (*result)->form_xsize); |
|
|
496 | len += 0x02; |
|
|
497 | psiconv_progress(lev+2,off+len,"Going to read the form ver. size"); |
|
|
498 | (*result)->form_ysize = psiconv_read_u16(buf,lev+2,off + len,&res); |
|
|
499 | if (res) |
|
|
500 | goto ERROR2; |
|
|
501 | psiconv_debug(lev+2,off+len,"Form ver. size: %04x", |
|
|
502 | (*result)->form_ysize); |
|
|
503 | len += 0x02; |
|
|
504 | psiconv_progress(lev+2,off+len,"Going to skip 1 zero word"); |
|
|
505 | temp = psiconv_read_u16(buf,lev+2,off+len,&res); |
|
|
506 | if (res) |
|
|
507 | goto ERROR2; |
|
|
508 | if (temp != 0) { |
|
|
509 | psiconv_warn(lev+2,off+len, |
|
|
510 | "Unexpected value in sketch section preamble (ignored)"); |
|
|
511 | psiconv_debug(lev+2,off+len,"Read %04x, expected %04x",i, temp,0); |
|
|
512 | } |
|
|
513 | off += 0x02; |
|
|
514 | } |
|
|
515 | |
|
|
516 | psiconv_progress(lev+2,off+len,"Going to read the picture data"); |
|
|
517 | if ((res = psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,0, |
|
|
518 | &((*result)->picture)))) |
|
|
519 | goto ERROR2; |
|
|
520 | off += leng; |
|
|
521 | if (!is_object) { |
|
|
522 | (*result)->picture_xsize = (*result)->picture->xsize; |
|
|
523 | (*result)->picture_ysize = (*result)->picture->ysize; |
|
|
524 | } |
|
|
525 | |
|
|
526 | psiconv_progress(lev+2,off+len,"Going to read the hor. magnification"); |
|
|
527 | (*result)->magnification_x = psiconv_read_u16(buf,lev+2,off+len,&res)/1000.0; |
|
|
528 | if (res) |
|
|
529 | goto ERROR3; |
|
|
530 | psiconv_debug(lev+2,off+len,"Form hor. magnification: %f", |
|
|
531 | (*result)->magnification_x); |
|
|
532 | len += 0x02; |
|
|
533 | psiconv_progress(lev+2,off+len,"Going to read the ver. magnification"); |
|
|
534 | (*result)->magnification_y = psiconv_read_u16(buf,lev+2,off+len,&res)/1000.0; |
|
|
535 | if (res) |
|
|
536 | goto ERROR3; |
|
|
537 | psiconv_debug(lev+2,off+len,"Form ver. magnification: %f", |
|
|
538 | (*result)->magnification_y); |
|
|
539 | len += 0x02; |
|
|
540 | |
|
|
541 | psiconv_progress(lev+2,off+len,"Going to read the left cut"); |
|
|
542 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
543 | if (res) |
|
|
544 | goto ERROR3; |
|
|
545 | (*result)->cut_left = (temp * 6.0) / (*result)->picture_xsize; |
|
|
546 | psiconv_debug(lev+2,off+len,"Left cut: raw %08x, real: %f", |
|
|
547 | temp,(*result)->cut_left); |
|
|
548 | len += 0x04; |
|
|
549 | psiconv_progress(lev+2,off+len,"Going to read the right cut"); |
|
|
550 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
551 | if (res) |
|
|
552 | goto ERROR3; |
|
|
553 | (*result)->cut_right = (temp * 6.0) / (*result)->picture_xsize; |
|
|
554 | psiconv_debug(lev+2,off+len,"Right cut: raw %08x, real: %f", |
|
|
555 | temp,(*result)->cut_right); |
|
|
556 | len += 0x04; |
|
|
557 | psiconv_progress(lev+2,off+len,"Going to read the top cut"); |
|
|
558 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
559 | if (res) |
|
|
560 | goto ERROR3; |
|
|
561 | (*result)->cut_top = (temp * 6.0) / (*result)->picture_ysize; |
|
|
562 | psiconv_debug(lev+2,off+len,"Top cut: raw %08x, real: %f", |
|
|
563 | temp,(*result)->cut_top); |
|
|
564 | len += 0x04; |
|
|
565 | psiconv_progress(lev+2,off+len,"Going to read the bottom cut"); |
|
|
566 | temp = psiconv_read_u32(buf,lev+2,off + len,&res); |
|
|
567 | if (res) |
|
|
568 | goto ERROR3; |
|
|
569 | (*result)->cut_bottom = (temp * 6.0) / (*result)->picture_ysize; |
|
|
570 | psiconv_debug(lev+2,off+len,"Bottom cut: raw %08x, real: %f", |
|
|
571 | temp,(*result)->cut_bottom); |
|
|
572 | len += 0x04; |
|
|
573 | |
|
|
574 | if (length) |
|
|
575 | *length = len; |
|
|
576 | |
|
|
577 | psiconv_progress(lev,off+len-1, |
|
|
578 | "End of sketch section (total length: %08x)", len); |
|
|
579 | |
|
|
580 | return res; |
|
|
581 | ERROR3: |
|
|
582 | psiconv_free_paint_data_section((*result)->picture); |
|
|
583 | ERROR2: |
|
|
584 | free (*result); |
|
|
585 | ERROR1: |
|
|
586 | psiconv_warn(lev+1,off,"Reading of Sketch Section failed"); |
|
|
587 | if (length) |
|
|
588 | *length = 0; |
|
|
589 | if (!res) |
|
|
590 | return -PSICONV_E_NOMEM; |
|
|
591 | else |
|
|
592 | return res; |
|
|
593 | } |
|
|
594 | |
|
|
595 | |
|
|
596 | int psiconv_parse_clipart_section(const psiconv_buffer buf,int lev, |
|
|
597 | psiconv_u32 off, int *length, |
|
|
598 | psiconv_clipart_section *result) |
|
|
599 | { |
|
|
600 | int res=0; |
|
|
601 | int len=0; |
|
|
602 | int leng; |
|
|
603 | psiconv_u32 temp; |
|
|
604 | |
|
|
605 | psiconv_progress(lev+1,off+len,"Going to read the clipart section"); |
|
|
606 | if (!(*result = malloc(sizeof(**result)))) |
|
|
607 | goto ERROR1; |
|
|
608 | |
|
|
609 | psiconv_progress(lev+2,off+len,"Going to read the section ID"); |
|
|
610 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
611 | if (res) |
|
|
612 | goto ERROR2; |
|
|
613 | if (temp != PSICONV_ID_CLIPART_ITEM) { |
|
|
614 | psiconv_warn(lev+2,off+len, |
|
|
615 | "Unexpected value in clipart section preamble (ignored)"); |
|
|
616 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, |
|
|
617 | PSICONV_ID_CLIPART_ITEM); |
|
|
618 | } else |
|
|
619 | psiconv_debug(lev+2,off+len,"Clipart ID: %08x", temp); |
|
|
620 | off += 4; |
|
|
621 | |
|
|
622 | psiconv_progress(lev+2,off+len,"Going to read an unknown long"); |
|
|
623 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
624 | if (res) |
|
|
625 | goto ERROR2; |
|
|
626 | if (temp != 0x02) { |
|
|
627 | psiconv_warn(lev+2,off+len, |
|
|
628 | "Unexpected value in clipart section preamble (ignored)"); |
|
|
629 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, |
|
|
630 | 0x02); |
|
|
631 | } else |
|
|
632 | psiconv_debug(lev+2,off+len,"First unknown long: %08x", temp); |
|
|
633 | off += 4; |
|
|
634 | |
|
|
635 | psiconv_progress(lev+2,off+len,"Going to read a second unknown long"); |
|
|
636 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
637 | if (res) |
|
|
638 | goto ERROR2; |
|
|
639 | if (temp != 0) { |
|
|
640 | psiconv_warn(lev+2,off+len, |
|
|
641 | "Unexpected value in clipart section preamble (ignored)"); |
|
|
642 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
|
|
643 | } else |
|
|
644 | psiconv_debug(lev+2,off+len,"Second unknown long: %08x", temp); |
|
|
645 | off += 4; |
|
|
646 | |
|
|
647 | psiconv_progress(lev+2,off+len,"Going to read a third unknown long"); |
|
|
648 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
649 | if (res) |
|
|
650 | goto ERROR2; |
|
|
651 | if (temp != 0) { |
|
|
652 | psiconv_warn(lev+2,off+len, |
|
|
653 | "Unexpected value in clipart section preamble (ignored)"); |
|
|
654 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x",temp, 0); |
|
|
655 | } else |
|
|
656 | psiconv_debug(lev+2,off+len,"Third unknown long: %08x", temp); |
|
|
657 | off += 4; |
|
|
658 | |
|
|
659 | psiconv_progress(lev+2,off+len,"Going to read a fourth unknown long"); |
|
|
660 | temp = psiconv_read_u32(buf,lev+2,off+len,&res); |
|
|
661 | if (res) |
|
|
662 | goto ERROR2; |
|
|
663 | if ((temp != 0x0c) && (temp != 0x08)) { |
|
|
664 | psiconv_warn(lev+2,off+len, |
|
|
665 | "Unexpected value in clipart section preamble (ignored)"); |
|
|
666 | psiconv_debug(lev+2,off+len,"Read %08x, expected %08x or %08x",temp, |
|
|
667 | 0x0c, 0x08); |
|
|
668 | } else |
|
|
669 | psiconv_debug(lev+2,off+len,"Fourth unknown long: %08x", temp); |
|
|
670 | off += 4; |
|
|
671 | |
|
|
672 | psiconv_progress(lev+2,off+len,"Going to read the Paint Data Section"); |
|
|
673 | if ((res = psiconv_parse_paint_data_section(buf,lev+2,off+len,&leng,1, |
|
|
674 | &((*result)->picture)))) |
|
|
675 | goto ERROR2; |
|
|
676 | len += leng; |
|
|
677 | |
|
|
678 | if (length) |
|
|
679 | *length = len; |
|
|
680 | |
|
|
681 | psiconv_progress(lev,off+len-1, |
|
|
682 | "End of clipart section (total length: %08x)", len); |
|
|
683 | return 0; |
|
|
684 | |
|
|
685 | ERROR2: |
|
|
686 | free (*result); |
|
|
687 | ERROR1: |
|
|
688 | psiconv_warn(lev+1,off,"Reading of Font failed"); |
|
|
689 | if (length) |
|
|
690 | *length = 0; |
|
|
691 | if (!res) |
|
|
692 | return -PSICONV_E_NOMEM; |
|
|
693 | else |
|
|
694 | return res; |
|
|
695 | } |