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