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