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