1 | /* |
1 | /* |
2 | generate_image.c - Part of psiconv, a PSION 5 file formats converter |
2 | generate_image.c - Part of psiconv, a PSION 5 file formats converter |
3 | Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> |
3 | Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl> |
4 | |
4 | |
5 | This program is free software; you can redistribute it and/or modify |
5 | This program is free software; you can redistribute it and/or modify |
6 | it under the terms of the GNU General Public License as published by |
6 | it under the terms of the GNU General Public License as published by |
7 | the Free Software Foundation; either version 2 of the License, or |
7 | the Free Software Foundation; either version 2 of the License, or |
8 | (at your option) any later version. |
8 | (at your option) any later version. |
… | |
… | |
20 | #include "config.h" |
20 | #include "config.h" |
21 | #include "compat.h" |
21 | #include "compat.h" |
22 | |
22 | |
23 | #include "generate_routines.h" |
23 | #include "generate_routines.h" |
24 | #include "error.h" |
24 | #include "error.h" |
|
|
25 | #include "list.h" |
|
|
26 | #include "image.h" |
25 | |
27 | |
26 | #ifdef DMALLOC |
28 | #ifdef DMALLOC |
27 | #include <dmalloc.h> |
29 | #include <dmalloc.h> |
28 | #endif |
30 | #endif |
29 | |
31 | |
30 | typedef psiconv_list psiconv_pixel_bytes; /* psiconv_u8 */ |
|
|
31 | |
32 | |
32 | typedef psiconv_list psiconv_pixel_ints; /* of psiconv_u32 */ |
33 | static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, |
33 | |
|
|
34 | typedef struct psiconv_pixel_float_s |
|
|
35 | { |
|
|
36 | float *red; |
|
|
37 | float *green; |
|
|
38 | float *blue; |
|
|
39 | } psiconv_pixel_floats_t; |
|
|
40 | |
|
|
41 | static int psiconv_collect_pixel_data(const psiconv_config config, |
|
|
42 | psiconv_pixel_ints *pixels,int xsize, |
|
|
43 | int ysize, |
34 | int xsize,int ysize, |
44 | const psiconv_pixel_floats_t data, |
35 | const psiconv_pixel_floats_t data, |
45 | int colordepth, int palet_len, |
36 | int colordepth,int color, |
|
|
37 | int redbits,int greenbits,int bluebits, |
46 | const psiconv_pixel_floats_t palet); |
38 | const psiconv_pixel_floats_t palet); |
47 | |
|
|
48 | static int psiconv_pixel_data_to_bytes(const psiconv_config config, psiconv_pixel_bytes *bytes, int xsize, |
39 | static int psiconv_pixel_data_to_bytes(const psiconv_config config, |
|
|
40 | psiconv_pixel_bytes *bytes, int xsize, |
49 | int ysize, const psiconv_pixel_ints pixels, |
41 | int ysize, const psiconv_pixel_ints pixels, |
50 | int colordepth); |
42 | int colordepth); |
|
|
43 | static int psiconv_encode_rle8(const psiconv_config config, |
|
|
44 | const psiconv_pixel_bytes plain_bytes, |
|
|
45 | psiconv_pixel_bytes *encoded_bytes); |
|
|
46 | static int psiconv_encode_rle12(const psiconv_config config, |
|
|
47 | const psiconv_pixel_bytes plain_bytes, |
|
|
48 | psiconv_pixel_bytes *encoded_bytes); |
|
|
49 | static int psiconv_encode_rle16(const psiconv_config config, |
|
|
50 | const psiconv_pixel_bytes plain_bytes, |
|
|
51 | psiconv_pixel_bytes *encoded_bytes); |
|
|
52 | static int psiconv_encode_rle24(const psiconv_config config, |
|
|
53 | const psiconv_pixel_bytes plain_bytes, |
|
|
54 | psiconv_pixel_bytes *encoded_bytes); |
51 | |
55 | |
52 | float palet_grey_2_rgb[4] = {0.0/3, 1.0/3, 2.0/3, 3.0/3}; |
|
|
53 | float palet_grey_4_rgb[16] = { 0.0/15, 1.0/15, 2.0/15, 3.0/15, |
|
|
54 | 4.0/15, 5.0/15, 6.0/15, 7.0/15, |
|
|
55 | 8.0/15, 9.0/15, 10.0/15, 11.0/15, |
|
|
56 | 12.0/15, 13.0/15, 14.0/15, 15.0/15}; |
|
|
57 | |
|
|
58 | psiconv_pixel_floats_t palet_grey_2 = |
|
|
59 | { |
|
|
60 | (float *) palet_grey_2_rgb, |
|
|
61 | (float *) palet_grey_2_rgb, |
|
|
62 | (float *) palet_grey_2_rgb |
|
|
63 | }; |
|
|
64 | |
|
|
65 | psiconv_pixel_floats_t palet_grey_4 = |
|
|
66 | { |
|
|
67 | (float *) palet_grey_4_rgb, |
|
|
68 | (float *) palet_grey_4_rgb, |
|
|
69 | (float *) palet_grey_4_rgb |
|
|
70 | }; |
|
|
71 | |
|
|
72 | |
|
|
73 | /* For now, we only write 2-bit non-RLE-encoded stuff */ |
|
|
74 | int psiconv_write_paint_data_section(const psiconv_config config, |
56 | int psiconv_write_paint_data_section(const psiconv_config config, |
75 | psiconv_buffer buf, |
57 | psiconv_buffer buf, int lev, |
76 | const psiconv_paint_data_section value) |
58 | const psiconv_paint_data_section value, |
|
|
59 | int is_clipart) |
77 | { |
60 | { |
78 | int res; |
61 | int res,colordepth,i; |
79 | psiconv_buffer pix_buf; |
62 | psiconv_pixel_ints ints; |
|
|
63 | psiconv_pixel_floats_t floats,palet; |
|
|
64 | psiconv_list bytes,bytes_rle; |
|
|
65 | psiconv_u8 *byteptr,encoding; |
|
|
66 | |
|
|
67 | psiconv_progress(config,lev,0,"Writing paint data section"); |
|
|
68 | |
|
|
69 | /* First, we check whether we can cope with the current configuration. |
|
|
70 | If not, we stop at once */ |
|
|
71 | if ((config->colordepth != 2) && (config->colordepth != 4) && |
|
|
72 | (config->colordepth != 8) && (config->colordepth != 12) && |
|
|
73 | (config->colordepth != 16) && (config->colordepth != 24)) { |
|
|
74 | psiconv_error(config,0,psiconv_buffer_length(buf), |
|
|
75 | "Unsupported color depth (%d); try 2, 4, 8, 16 or 24", |
|
|
76 | config->colordepth); |
|
|
77 | res = -PSICONV_E_GENERATE; |
|
|
78 | goto ERROR1; |
|
|
79 | } |
|
|
80 | |
|
|
81 | if ((config->color) && |
|
|
82 | (config->bluebits || config->redbits || config->greenbits) && |
|
|
83 | (config->bluebits+config->redbits+config->greenbits!=config->colordepth)) { |
|
|
84 | psiconv_error(config,0,psiconv_buffer_length(buf), |
|
|
85 | "Sum of red (%d), green (%d) and blue (%d) bits should be " |
|
|
86 | "equal to the color depth (%d)", |
|
|
87 | config->redbits,config->greenbits,config->bluebits, |
|
|
88 | config->colordepth); |
|
|
89 | res = -PSICONV_E_GENERATE; |
|
|
90 | goto ERROR1; |
|
|
91 | } |
|
|
92 | |
|
|
93 | if (config->color && |
|
|
94 | !(config->redbits || config->greenbits || config->bluebits) && |
|
|
95 | (config->colordepth != 4) && (config->colordepth != 8)) { |
|
|
96 | psiconv_error(config,0,psiconv_buffer_length(buf), |
|
|
97 | "Current color depth (%d) has no palet associated with it", |
|
|
98 | config->colordepth); |
|
|
99 | res = -PSICONV_E_GENERATE; |
|
|
100 | goto ERROR1; |
|
|
101 | } |
|
|
102 | |
|
|
103 | if (config->color || (config->colordepth != 2)) |
|
|
104 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
105 | "All image types except 2-bit greyscale are experimental!"); |
|
|
106 | |
80 | |
107 | |
81 | if (!value) { |
108 | if (!value) { |
82 | psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paint data section"); |
109 | psiconv_error(config,0,psiconv_buffer_length(buf),"Null paint data section"); |
83 | res = -PSICONV_E_GENERATE; |
110 | res = -PSICONV_E_GENERATE; |
84 | goto ERROR1; |
111 | goto ERROR1; |
85 | } |
112 | } |
86 | |
113 | |
87 | if (!(pix_buf = psiconv_buffer_new())) { |
114 | floats.red = value->red; |
88 | res = -PSICONV_E_NOMEM; |
115 | floats.green = value->green; |
|
|
116 | floats.blue = value->blue; |
|
|
117 | floats.length = value->xsize * value->ysize; |
|
|
118 | |
|
|
119 | palet = psiconv_palet_none; |
|
|
120 | if ((config->color) && (config->redbits == 0) && (config->greenbits == 0) && |
|
|
121 | (config->bluebits == 0)) |
|
|
122 | switch (config->colordepth) { |
|
|
123 | case 4: palet = psiconv_palet_color_4; break; |
|
|
124 | case 8: palet = psiconv_palet_color_8; break; |
|
|
125 | default: palet = psiconv_palet_none; break; |
|
|
126 | } |
|
|
127 | |
|
|
128 | if ((res = psiconv_collect_pixel_data(&ints,value->xsize, |
|
|
129 | value->ysize,floats, |
|
|
130 | config->colordepth,config->color, |
|
|
131 | config->redbits,config->greenbits, |
|
|
132 | config->bluebits,palet))) { |
|
|
133 | psiconv_error(config,lev,0,"Error collecting pixel data"); |
|
|
134 | goto ERROR1; |
|
|
135 | } |
|
|
136 | |
|
|
137 | if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, |
|
|
138 | ints,config->colordepth))) { |
|
|
139 | psiconv_error(config,lev,0,"Error translating pixel data to bytes"); |
89 | goto ERROR1; |
140 | goto ERROR2; |
|
|
141 | } |
|
|
142 | |
|
|
143 | |
|
|
144 | switch (config->colordepth) { |
|
|
145 | case 2: |
|
|
146 | case 4: |
|
|
147 | case 8: |
|
|
148 | encoding = 0x01; |
|
|
149 | if ((res = psiconv_encode_rle8(config,bytes,&bytes_rle))) { |
|
|
150 | psiconv_error(config,lev,0,"Error encoding RLE8"); |
|
|
151 | goto ERROR3; |
|
|
152 | } |
|
|
153 | break; |
|
|
154 | case 12: |
|
|
155 | encoding = 0x02; |
|
|
156 | if ((res = psiconv_encode_rle12(config,bytes,&bytes_rle))) { |
|
|
157 | psiconv_error(config,lev,0,"Error encoding RLE12"); |
|
|
158 | goto ERROR3; |
|
|
159 | } |
|
|
160 | break; |
|
|
161 | case 16: |
|
|
162 | encoding = 0x03; |
|
|
163 | if ((res = psiconv_encode_rle16(config,bytes,&bytes_rle))) { |
|
|
164 | psiconv_error(config,lev,0,"Error encoding RLE16"); |
|
|
165 | goto ERROR3; |
|
|
166 | } |
|
|
167 | break; |
|
|
168 | case 24: |
|
|
169 | encoding = 0x04; |
|
|
170 | if ((res = psiconv_encode_rle24(config,bytes,&bytes_rle))) { |
|
|
171 | psiconv_error(config,lev,0,"Error encoding RLE24"); |
|
|
172 | goto ERROR3; |
|
|
173 | } |
|
|
174 | break; |
|
|
175 | default: |
|
|
176 | encoding = 0x00; |
|
|
177 | } |
|
|
178 | if (encoding) { |
|
|
179 | if (psiconv_list_length(bytes_rle) < psiconv_list_length(bytes)) { |
|
|
180 | psiconv_list_free(bytes); |
|
|
181 | bytes = bytes_rle; |
|
|
182 | } else { |
|
|
183 | psiconv_list_free(bytes_rle); |
|
|
184 | encoding = 0x00; |
90 | } |
185 | } |
|
|
186 | } |
91 | |
187 | |
92 | return 0; |
188 | if ((res = psiconv_write_u32(config,buf,lev+1, |
|
|
189 | 0x28+psiconv_list_length(bytes)))) |
|
|
190 | goto ERROR3; |
|
|
191 | if ((res = psiconv_write_u32(config,buf,lev+1,0x28))) |
|
|
192 | goto ERROR3; |
|
|
193 | if ((res = psiconv_write_u32(config,buf,lev+1,value->xsize))) |
|
|
194 | goto ERROR3; |
|
|
195 | if ((res = psiconv_write_u32(config,buf,lev+1,value->ysize))) |
|
|
196 | goto ERROR3; |
|
|
197 | if ((res = psiconv_write_length(config,buf,lev+1,value->pic_xsize))) |
|
|
198 | goto ERROR3; |
|
|
199 | if ((res = psiconv_write_length(config,buf,lev+1,value->pic_ysize))) |
|
|
200 | goto ERROR3; |
|
|
201 | colordepth = config->colordepth; |
|
|
202 | if ((res = psiconv_write_u32(config,buf,lev+1,colordepth))) |
|
|
203 | goto ERROR3; |
|
|
204 | if ((res = psiconv_write_u32(config,buf,lev+1,(config->color?1:0)))) |
|
|
205 | goto ERROR3; |
|
|
206 | if ((res = psiconv_write_u32(config,buf,lev+1,0))) |
|
|
207 | goto ERROR3; |
|
|
208 | if ((res = psiconv_write_u32(config,buf,lev+1,encoding))) |
|
|
209 | goto ERROR3; |
|
|
210 | if (is_clipart) { |
|
|
211 | if ((res = psiconv_write_u32(config,buf,lev+1,0xffffffff))) |
|
|
212 | goto ERROR3; |
|
|
213 | if ((res = psiconv_write_u32(config,buf,lev+1,0x00000044))) |
|
|
214 | goto ERROR3; |
|
|
215 | } |
|
|
216 | for (i = 0; i < psiconv_list_length(bytes); i++) { |
|
|
217 | if (!(byteptr = psiconv_list_get(bytes,i))) |
|
|
218 | goto ERROR3; |
|
|
219 | if ((res = psiconv_write_u8(config,buf,lev+1,*byteptr))) |
|
|
220 | goto ERROR3; |
|
|
221 | } |
93 | |
222 | |
|
|
223 | ERROR3: |
|
|
224 | psiconv_list_free(bytes); |
94 | ERROR2: |
225 | ERROR2: |
95 | psiconv_buffer_free(pix_buf); |
226 | psiconv_list_free(ints); |
96 | ERROR1: |
227 | ERROR1: |
97 | return res; |
228 | return res; |
98 | } |
229 | } |
99 | |
230 | |
100 | /* Translate the floating point RGB information into pixel values. |
231 | /* Translate the floating point RGB information into pixel values. |
101 | The palet is optional; without it, we just use the |
232 | The palet is optional; without it, we just use the |
102 | colordepth. With a large palet this is not very fast, but it will do for |
233 | colordepth. With a large palet this is not very fast, but it will do for |
103 | now. For greyscale pictures, just use the palet. */ |
234 | now. For greyscale pictures, just use the palet. */ |
104 | int psiconv_collect_pixel_data(const psiconv_config config, |
235 | int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize, |
105 | psiconv_pixel_ints *pixels,int xsize,int ysize, |
|
|
106 | const psiconv_pixel_floats_t data, |
236 | const psiconv_pixel_floats_t data, |
107 | int colordepth, int palet_len, |
237 | int colordepth,int color, |
|
|
238 | int redbits,int bluebits,int greenbits, |
108 | const psiconv_pixel_floats_t palet) |
239 | const psiconv_pixel_floats_t palet) |
109 | { |
240 | { |
110 | int res,x,y,i; |
241 | int res,x,y,i; |
111 | psiconv_u32 index,mask,pixel; |
242 | psiconv_u32 index,pixel; |
112 | float p_red,p_green,p_blue,mult,dist,new_dist; |
243 | float p_red,p_green,p_blue,dist,new_dist; |
113 | |
244 | |
114 | if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { |
245 | if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { |
115 | res = -PSICONV_E_NOMEM; |
246 | res = -PSICONV_E_NOMEM; |
116 | goto ERROR1; |
247 | goto ERROR1; |
117 | } |
248 | } |
118 | |
249 | |
119 | mult = 1 << colordepth; |
|
|
120 | mask = mult -1; |
|
|
121 | for (y = 0; y < ysize; y++) { |
250 | for (y = 0; y < ysize; y++) { |
122 | for (x = 0; x < xsize; x++) { |
251 | for (x = 0; x < xsize; x++) { |
123 | index = y*ysize+x; |
252 | index = y*xsize+x; |
124 | p_red = data.red[index]; |
253 | p_red = data.red[index]; |
125 | p_green = data.green[index]; |
254 | p_green = data.green[index]; |
126 | p_blue = data.blue[index]; |
255 | p_blue = data.blue[index]; |
127 | if (! palet_len) { |
256 | if (!palet.length) { |
128 | pixel = (((psiconv_u32) (p_red*mult) & mask) << (2*colordepth)) + |
257 | if (color) |
129 | (((psiconv_u32) (p_green*mult) & mask) << colordepth) + |
258 | pixel = (((psiconv_u32) (p_red * (1 << redbits) + 0.5)) |
130 | ((psiconv_u32) (p_blue*mult) & mask); |
259 | << (greenbits+bluebits)) + |
|
|
260 | (((psiconv_u32) (p_green * (1 << greenbits) + 0.5)) |
|
|
261 | << bluebits) + |
|
|
262 | ((psiconv_u32) (p_blue * (1 << bluebits) + 0.5)); |
|
|
263 | else |
|
|
264 | pixel = (p_red + p_green + p_blue)/3.0 * (1 << colordepth); |
131 | } else { |
265 | } else { |
132 | dist = 4; /* Max distance is 3, so this is safe */ |
266 | dist = 4; /* Max distance is 3, so this is safe */ |
133 | pixel = -1; |
267 | pixel = -1; |
134 | for (i = 0; i < palet_len; i++) { |
268 | for (i = 0; i < palet.length; i++) { |
135 | new_dist = (p_red - palet.red[i]) * (p_red - palet.red[i]) + |
269 | new_dist = (p_red - palet.red[i]) * (p_red - palet.red[i]) + |
136 | (p_green - palet.green[i]) * (p_green - palet.green[i]) + |
270 | (p_green - palet.green[i]) * (p_green - palet.green[i]) + |
137 | (p_blue - palet.blue[i]) * (p_blue - palet.blue[i]); |
271 | (p_blue - palet.blue[i]) * (p_blue - palet.blue[i]); |
138 | if (new_dist < dist) { |
272 | if (new_dist < dist) { |
139 | pixel = i; |
273 | pixel = i; |
… | |
… | |
151 | psiconv_list_free(*pixels); |
285 | psiconv_list_free(*pixels); |
152 | ERROR1: |
286 | ERROR1: |
153 | return res; |
287 | return res; |
154 | } |
288 | } |
155 | |
289 | |
156 | int psiconv_pixel_data_to_bytes(const psiconv_config config, psiconv_pixel_bytes *bytes, int xsize, |
290 | int psiconv_pixel_data_to_bytes(const psiconv_config config, |
|
|
291 | psiconv_pixel_bytes *bytes, int xsize, |
157 | int ysize, const psiconv_pixel_ints pixels, |
292 | int ysize, const psiconv_pixel_ints pixels, |
158 | int colordepth) |
293 | int colordepth) |
159 | { |
294 | { |
160 | int res; |
295 | int res; |
161 | int x,y; |
296 | int x,y; |
162 | |
297 | |
163 | psiconv_u32 inputdata; |
298 | psiconv_u32 inputdata; |
164 | psiconv_u8 outputbyte; |
299 | psiconv_u8 outputbyte; |
165 | psiconv_u32 *pixelptr; |
300 | psiconv_u32 *pixelptr; |
166 | int inputbitsleft,outputbitnr,bitsfit; |
301 | int inputbitsleft,outputbitnr,bitsfit,outputbytenr; |
167 | |
302 | |
168 | |
303 | |
169 | if (!bytes) { |
304 | if (!bytes) { |
170 | psiconv_warn(config,0,0,"NULL pixel data"); |
305 | psiconv_error(config,0,0,"NULL pixel data"); |
171 | res = -PSICONV_E_GENERATE; |
306 | res = -PSICONV_E_GENERATE; |
172 | goto ERROR1; |
307 | goto ERROR1; |
173 | } |
308 | } |
174 | if (!pixels) { |
309 | if (!pixels) { |
175 | psiconv_warn(config,0,0,"NULL pixel data"); |
310 | psiconv_error(config,0,0,"NULL pixel data"); |
176 | res = -PSICONV_E_GENERATE; |
311 | res = -PSICONV_E_GENERATE; |
177 | goto ERROR1; |
312 | goto ERROR1; |
178 | } |
313 | } |
179 | if (psiconv_list_length(pixels) != xsize * ysize) { |
314 | if (psiconv_list_length(pixels) != xsize * ysize) { |
180 | psiconv_warn(config,0,0,"Pixel number is not correct"); |
315 | psiconv_error(config,0,0,"Pixel number is not correct"); |
181 | res = -PSICONV_E_GENERATE; |
316 | res = -PSICONV_E_GENERATE; |
182 | goto ERROR1; |
317 | goto ERROR1; |
183 | } |
318 | } |
184 | |
319 | |
185 | if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) { |
320 | if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) { |
… | |
… | |
189 | |
324 | |
190 | |
325 | |
191 | outputbitnr = 0; |
326 | outputbitnr = 0; |
192 | outputbyte = 0; |
327 | outputbyte = 0; |
193 | for (y = 0; y < ysize; y++) { |
328 | for (y = 0; y < ysize; y++) { |
|
|
329 | outputbytenr = 0; |
194 | for (x = 0; x < xsize; x++) { |
330 | for (x = 0; x < xsize; x++) { |
195 | if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) { |
331 | if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) { |
196 | psiconv_warn(config,0,0,"Massive internal corruption"); |
332 | psiconv_error(config,0,0,"Massive internal corruption"); |
197 | res = -PSICONV_E_NOMEM; |
333 | res = -PSICONV_E_NOMEM; |
198 | goto ERROR2; |
334 | goto ERROR2; |
199 | } |
335 | } |
200 | inputbitsleft = colordepth; |
336 | inputbitsleft = colordepth; |
201 | inputdata = *pixelptr; |
337 | inputdata = *pixelptr; |
… | |
… | |
208 | if (outputbitnr == 8) { |
344 | if (outputbitnr == 8) { |
209 | if ((res = psiconv_list_add(*bytes,&outputbyte))) |
345 | if ((res = psiconv_list_add(*bytes,&outputbyte))) |
210 | goto ERROR2; |
346 | goto ERROR2; |
211 | outputbitnr = 0; |
347 | outputbitnr = 0; |
212 | outputbyte = 0; |
348 | outputbyte = 0; |
|
|
349 | outputbytenr ++; |
213 | } |
350 | } |
214 | } |
351 | } |
215 | } |
352 | } |
216 | /* Always end lines on a byte border */ |
353 | /* Always end lines on a long border */ |
217 | if (outputbitnr != 0) { |
354 | if (outputbitnr != 0) { |
218 | if ((res = psiconv_list_add(*bytes,&outputbyte))) |
355 | if ((res = psiconv_list_add(*bytes,&outputbyte))) |
219 | goto ERROR2; |
356 | goto ERROR2; |
220 | outputbitnr = 0; |
357 | outputbitnr = 0; |
221 | outputbyte = 0; |
358 | outputbyte = 0; |
|
|
359 | outputbytenr ++; |
|
|
360 | } |
|
|
361 | |
|
|
362 | while (outputbytenr % 0x04) { |
|
|
363 | if ((res = psiconv_list_add(*bytes,&outputbyte))) |
|
|
364 | goto ERROR2; |
|
|
365 | outputbytenr ++; |
222 | } |
366 | } |
223 | } |
367 | } |
224 | |
368 | |
225 | return 0; |
369 | return 0; |
226 | |
370 | |
227 | ERROR2: |
371 | ERROR2: |
228 | psiconv_list_free(*bytes); |
372 | psiconv_list_free(*bytes); |
229 | ERROR1: |
373 | ERROR1: |
230 | return res; |
374 | return res; |
231 | } |
375 | } |
|
|
376 | |
|
|
377 | /* RLE8 encoding: |
|
|
378 | Marker bytes followed by one or more data bytes. |
|
|
379 | Marker value 0x00-0x7f: repeat the next data byte (marker+1) times |
|
|
380 | Marker value 0xff-0x80: (0x100-marker) data bytes follow */ |
|
|
381 | int psiconv_encode_rle8(const psiconv_config config, |
|
|
382 | const psiconv_pixel_bytes plain_bytes, |
|
|
383 | psiconv_pixel_bytes *encoded_bytes) |
|
|
384 | { |
|
|
385 | int res,i,j,len; |
|
|
386 | psiconv_u8 *entry,*next; |
|
|
387 | psiconv_u8 temp; |
|
|
388 | |
|
|
389 | if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry)))) { |
|
|
390 | res = -PSICONV_E_NOMEM; |
|
|
391 | goto ERROR1; |
|
|
392 | } |
|
|
393 | |
|
|
394 | for (i = 0; i < psiconv_list_length(plain_bytes);) { |
|
|
395 | if (!(entry = psiconv_list_get(plain_bytes,i))) { |
|
|
396 | res = -PSICONV_E_NOMEM; |
|
|
397 | goto ERROR2; |
|
|
398 | } |
|
|
399 | if (!(next = psiconv_list_get(plain_bytes,i+1))) { |
|
|
400 | res = -PSICONV_E_NOMEM; |
|
|
401 | goto ERROR2; |
|
|
402 | } |
|
|
403 | if (i == psiconv_list_length(plain_bytes) - 2) { |
|
|
404 | temp = 0xfe; |
|
|
405 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
406 | goto ERROR2; |
|
|
407 | if ((res = psiconv_list_add(*encoded_bytes,entry))) |
|
|
408 | goto ERROR2; |
|
|
409 | if ((res = psiconv_list_add(*encoded_bytes,next))) |
|
|
410 | goto ERROR2; |
|
|
411 | i +=2; |
|
|
412 | } else if (*next == *entry) { |
|
|
413 | len = 1; |
|
|
414 | while ((*next == *entry) && |
|
|
415 | (i+len + 2 < psiconv_list_length(plain_bytes)) && |
|
|
416 | len < 0x80) { |
|
|
417 | len ++; |
|
|
418 | if (!(next = psiconv_list_get(plain_bytes,i+len))) { |
|
|
419 | res = -PSICONV_E_NOMEM; |
|
|
420 | goto ERROR2; |
|
|
421 | } |
|
|
422 | } |
|
|
423 | temp = len - 1; |
|
|
424 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
425 | goto ERROR2; |
|
|
426 | if ((res = psiconv_list_add(*encoded_bytes,entry))) |
|
|
427 | goto ERROR2; |
|
|
428 | i += len; |
|
|
429 | } else { |
|
|
430 | len = 1; |
|
|
431 | while ((*next != *entry) && |
|
|
432 | (i+len+1 < psiconv_list_length(plain_bytes)) && |
|
|
433 | len < 0x80) { |
|
|
434 | len ++; |
|
|
435 | entry = next; |
|
|
436 | if (!(next = psiconv_list_get(plain_bytes,i+len))) { |
|
|
437 | res = -PSICONV_E_NOMEM; |
|
|
438 | goto ERROR2; |
|
|
439 | } |
|
|
440 | } |
|
|
441 | len --; |
|
|
442 | temp = 0x100 - len; |
|
|
443 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
444 | goto ERROR2; |
|
|
445 | for (j = 0; j < len; j++) { |
|
|
446 | if (!(next = psiconv_list_get(plain_bytes,i+j))) { |
|
|
447 | res = -PSICONV_E_NOMEM; |
|
|
448 | goto ERROR2; |
|
|
449 | } |
|
|
450 | if ((res = psiconv_list_add(*encoded_bytes,next))) |
|
|
451 | goto ERROR2; |
|
|
452 | } |
|
|
453 | i += len; |
|
|
454 | } |
|
|
455 | } |
|
|
456 | return 0; |
|
|
457 | |
|
|
458 | ERROR2: |
|
|
459 | psiconv_list_free(*encoded_bytes); |
|
|
460 | ERROR1: |
|
|
461 | return res; |
|
|
462 | } |
|
|
463 | |
|
|
464 | /* RLE12 encoding: |
|
|
465 | Word based. The 12 least significant bits contain the pixel colors. |
|
|
466 | the 4 most signigicant bits are the number of repetitions minus 1 */ |
|
|
467 | int psiconv_encode_rle12(const psiconv_config config, |
|
|
468 | const psiconv_pixel_bytes plain_bytes, |
|
|
469 | psiconv_pixel_bytes *encoded_bytes) |
|
|
470 | { |
|
|
471 | typedef psiconv_list psiconv_word_data; /* of psiconv_u16 */ |
|
|
472 | psiconv_word_data data; |
|
|
473 | int res,i,len,location; |
|
|
474 | psiconv_u16 *word_entry,*word_next; |
|
|
475 | psiconv_u16 word_data; |
|
|
476 | psiconv_u8 byte_temp; |
|
|
477 | psiconv_u8 *byte_entry; |
|
|
478 | |
|
|
479 | |
|
|
480 | /* First extract the 12-bit values to encode */ |
|
|
481 | if (!(data = psiconv_list_new(sizeof(psiconv_u16)))) { |
|
|
482 | res = -PSICONV_E_NOMEM; |
|
|
483 | goto ERROR1; |
|
|
484 | } |
|
|
485 | |
|
|
486 | for (i = 0; i < psiconv_list_length(plain_bytes); i++) { |
|
|
487 | if (!(byte_entry = psiconv_list_get(plain_bytes,i))) { |
|
|
488 | res = -PSICONV_E_NOMEM; |
|
|
489 | goto ERROR2; |
|
|
490 | } |
|
|
491 | location = 0; |
|
|
492 | if (location == 0) { |
|
|
493 | word_data = *byte_entry; |
|
|
494 | location ++; |
|
|
495 | } else if (location == 1) { |
|
|
496 | word_data = (word_data << 4) + (*byte_entry & 0x0f); |
|
|
497 | if ((res = psiconv_list_add(data,&word_data))) |
|
|
498 | goto ERROR2; |
|
|
499 | word_data = *byte_entry >> 4; |
|
|
500 | location ++; |
|
|
501 | } else { |
|
|
502 | word_data = (word_data << 8) + *byte_entry; |
|
|
503 | if ((res = psiconv_list_add(data,&word_data))) |
|
|
504 | goto ERROR2; |
|
|
505 | location = 0; |
|
|
506 | } |
|
|
507 | } |
|
|
508 | |
|
|
509 | if (!(*encoded_bytes = psiconv_list_new(sizeof(psiconv_u8)))) { |
|
|
510 | res = -PSICONV_E_NOMEM; |
|
|
511 | goto ERROR2; |
|
|
512 | } |
|
|
513 | |
|
|
514 | for (i = 0; i < psiconv_list_length(data);) { |
|
|
515 | if (!(word_entry = psiconv_list_get(data,i))) { |
|
|
516 | res = -PSICONV_E_NOMEM; |
|
|
517 | goto ERROR3; |
|
|
518 | } |
|
|
519 | |
|
|
520 | if (!(word_next = psiconv_list_get(data,i+1))) { |
|
|
521 | res = -PSICONV_E_NOMEM; |
|
|
522 | goto ERROR3; |
|
|
523 | } |
|
|
524 | |
|
|
525 | if (i == psiconv_list_length(data) - 2) { |
|
|
526 | byte_temp = *word_entry && 0xff; |
|
|
527 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
528 | goto ERROR3; |
|
|
529 | byte_temp = *word_entry >> 8; |
|
|
530 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
531 | goto ERROR3; |
|
|
532 | byte_temp = *word_next && 0xff; |
|
|
533 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
534 | goto ERROR3; |
|
|
535 | byte_temp = *word_next >> 8; |
|
|
536 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
537 | goto ERROR3; |
|
|
538 | i += 2; |
|
|
539 | } |
|
|
540 | |
|
|
541 | len = 0; |
|
|
542 | while ((*word_entry == *word_next) && (len < 16) && |
|
|
543 | (i+len+1 < psiconv_list_length(data))) { |
|
|
544 | len ++; |
|
|
545 | if (!(word_next = psiconv_list_get(data,i+len))) { |
|
|
546 | res = -PSICONV_E_NOMEM; |
|
|
547 | goto ERROR3; |
|
|
548 | } |
|
|
549 | } |
|
|
550 | |
|
|
551 | byte_temp = *word_entry && 0xff; |
|
|
552 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
553 | goto ERROR3; |
|
|
554 | byte_temp = (*word_entry >> 8) + ((len - 1) << 4); |
|
|
555 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
556 | goto ERROR3; |
|
|
557 | i += len; |
|
|
558 | } |
|
|
559 | return 0; |
|
|
560 | |
|
|
561 | ERROR3: |
|
|
562 | psiconv_list_free(*encoded_bytes); |
|
|
563 | ERROR2: |
|
|
564 | psiconv_list_free(data); |
|
|
565 | ERROR1: |
|
|
566 | return res; |
|
|
567 | } |
|
|
568 | |
|
|
569 | /* RLE16 encoding: |
|
|
570 | Marker bytes followed by one or more data words. |
|
|
571 | Marker value 0x00-0x7f: repeat the next data word (marker+1) times |
|
|
572 | Marker value 0xff-0x80: (0x100-marker) data words follow */ |
|
|
573 | int psiconv_encode_rle16(const psiconv_config config, |
|
|
574 | const psiconv_pixel_bytes plain_bytes, |
|
|
575 | psiconv_pixel_bytes *encoded_bytes) |
|
|
576 | { |
|
|
577 | int res,i,j,len; |
|
|
578 | psiconv_u8 *entry1,*entry2,*next1,*next2; |
|
|
579 | psiconv_u8 temp; |
|
|
580 | |
|
|
581 | if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry1)))) { |
|
|
582 | res = -PSICONV_E_NOMEM; |
|
|
583 | goto ERROR1; |
|
|
584 | } |
|
|
585 | |
|
|
586 | for (i = 0; i < psiconv_list_length(plain_bytes);) { |
|
|
587 | if (!(entry1 = psiconv_list_get(plain_bytes,i))) { |
|
|
588 | res = -PSICONV_E_NOMEM; |
|
|
589 | goto ERROR2; |
|
|
590 | } |
|
|
591 | if (!(entry2 = psiconv_list_get(plain_bytes,i+1))) { |
|
|
592 | res = -PSICONV_E_NOMEM; |
|
|
593 | goto ERROR2; |
|
|
594 | } |
|
|
595 | if (!(next1 = psiconv_list_get(plain_bytes,i+2))) { |
|
|
596 | res = -PSICONV_E_NOMEM; |
|
|
597 | goto ERROR2; |
|
|
598 | } |
|
|
599 | if (!(next2 = psiconv_list_get(plain_bytes,i+3))) { |
|
|
600 | res = -PSICONV_E_NOMEM; |
|
|
601 | goto ERROR2; |
|
|
602 | } |
|
|
603 | if (i == psiconv_list_length(plain_bytes) - 4) { |
|
|
604 | temp = 0xfe; |
|
|
605 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
606 | goto ERROR2; |
|
|
607 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
608 | goto ERROR2; |
|
|
609 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
610 | goto ERROR2; |
|
|
611 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
612 | goto ERROR2; |
|
|
613 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
614 | goto ERROR2; |
|
|
615 | i +=4; |
|
|
616 | } else if ((*next1 == *entry1) && (*next2 == *entry2)) { |
|
|
617 | len = 0; |
|
|
618 | while (((*next1 == *entry1) && (*next2 == *entry2)) && |
|
|
619 | (i+2*len + 4 < psiconv_list_length(plain_bytes)) && |
|
|
620 | len < 0x80) { |
|
|
621 | len ++; |
|
|
622 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*2))) { |
|
|
623 | res = -PSICONV_E_NOMEM; |
|
|
624 | goto ERROR2; |
|
|
625 | } |
|
|
626 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*2+1))) { |
|
|
627 | res = -PSICONV_E_NOMEM; |
|
|
628 | goto ERROR2; |
|
|
629 | } |
|
|
630 | } |
|
|
631 | temp = len - 1; |
|
|
632 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
633 | goto ERROR2; |
|
|
634 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
635 | goto ERROR2; |
|
|
636 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
637 | goto ERROR2; |
|
|
638 | i += len*2; |
|
|
639 | } else { |
|
|
640 | len = 1; |
|
|
641 | while (((*next1 != *entry1) || (*next2 != *entry2))&& |
|
|
642 | (i+len*2+4 < psiconv_list_length(plain_bytes)) && |
|
|
643 | len < 0x80) { |
|
|
644 | len ++; |
|
|
645 | entry1 = next1; |
|
|
646 | entry2 = next2; |
|
|
647 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*2))) { |
|
|
648 | res = -PSICONV_E_NOMEM; |
|
|
649 | goto ERROR2; |
|
|
650 | } |
|
|
651 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*2+1))) { |
|
|
652 | res = -PSICONV_E_NOMEM; |
|
|
653 | goto ERROR2; |
|
|
654 | } |
|
|
655 | } |
|
|
656 | len --; |
|
|
657 | temp = 0x100 - len; |
|
|
658 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
659 | goto ERROR2; |
|
|
660 | for (j = 0; j < len; j++) { |
|
|
661 | if (!(next1 = psiconv_list_get(plain_bytes,i+j*2))) { |
|
|
662 | res = -PSICONV_E_NOMEM; |
|
|
663 | goto ERROR2; |
|
|
664 | } |
|
|
665 | if (!(next2 = psiconv_list_get(plain_bytes,i+j*2+1))) { |
|
|
666 | res = -PSICONV_E_NOMEM; |
|
|
667 | goto ERROR2; |
|
|
668 | } |
|
|
669 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
670 | goto ERROR2; |
|
|
671 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
672 | goto ERROR2; |
|
|
673 | } |
|
|
674 | i += len*2; |
|
|
675 | } |
|
|
676 | } |
|
|
677 | return 0; |
|
|
678 | |
|
|
679 | ERROR2: |
|
|
680 | psiconv_list_free(*encoded_bytes); |
|
|
681 | ERROR1: |
|
|
682 | return res; |
|
|
683 | } |
|
|
684 | |
|
|
685 | /* RLE24 encoding: |
|
|
686 | Marker bytes followed by one or more data byte-triplets. |
|
|
687 | Marker value 0x00-0x7f: repeat the next data byte-triplets (marker+1) times |
|
|
688 | Marker value 0xff-0x80: (0x100-marker) data byte-triplets follow */ |
|
|
689 | int psiconv_encode_rle24(const psiconv_config config, |
|
|
690 | const psiconv_pixel_bytes plain_bytes, |
|
|
691 | psiconv_pixel_bytes *encoded_bytes) |
|
|
692 | { |
|
|
693 | int res,i,j,len; |
|
|
694 | psiconv_u8 *entry1,*entry2,*entry3,*next1,*next2,*next3; |
|
|
695 | psiconv_u8 temp; |
|
|
696 | |
|
|
697 | if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry1)))) { |
|
|
698 | res = -PSICONV_E_NOMEM; |
|
|
699 | goto ERROR1; |
|
|
700 | } |
|
|
701 | |
|
|
702 | for (i = 0; i < psiconv_list_length(plain_bytes);) { |
|
|
703 | if (!(entry1 = psiconv_list_get(plain_bytes,i))) { |
|
|
704 | res = -PSICONV_E_NOMEM; |
|
|
705 | goto ERROR2; |
|
|
706 | } |
|
|
707 | if (!(entry2 = psiconv_list_get(plain_bytes,i+1))) { |
|
|
708 | res = -PSICONV_E_NOMEM; |
|
|
709 | goto ERROR2; |
|
|
710 | } |
|
|
711 | if (!(entry3 = psiconv_list_get(plain_bytes,i+2))) { |
|
|
712 | res = -PSICONV_E_NOMEM; |
|
|
713 | goto ERROR2; |
|
|
714 | } |
|
|
715 | if (!(next1 = psiconv_list_get(plain_bytes,i+3))) { |
|
|
716 | res = -PSICONV_E_NOMEM; |
|
|
717 | goto ERROR2; |
|
|
718 | } |
|
|
719 | if (!(next2 = psiconv_list_get(plain_bytes,i+4))) { |
|
|
720 | res = -PSICONV_E_NOMEM; |
|
|
721 | goto ERROR2; |
|
|
722 | } |
|
|
723 | if (!(next3 = psiconv_list_get(plain_bytes,i+5))) { |
|
|
724 | res = -PSICONV_E_NOMEM; |
|
|
725 | goto ERROR2; |
|
|
726 | } |
|
|
727 | if (i == psiconv_list_length(plain_bytes) - 6) { |
|
|
728 | temp = 0xfe; |
|
|
729 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
730 | goto ERROR2; |
|
|
731 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
732 | goto ERROR2; |
|
|
733 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
734 | goto ERROR2; |
|
|
735 | if ((res = psiconv_list_add(*encoded_bytes,entry3))) |
|
|
736 | goto ERROR2; |
|
|
737 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
738 | goto ERROR2; |
|
|
739 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
740 | goto ERROR2; |
|
|
741 | if ((res = psiconv_list_add(*encoded_bytes,next3))) |
|
|
742 | goto ERROR2; |
|
|
743 | i +=4; |
|
|
744 | } else if ((*next1 == *entry1) && (*next2 == *entry2) && |
|
|
745 | (*next3 == *entry3)) { |
|
|
746 | len = 0; |
|
|
747 | while (((*next1 == *entry1) && (*next2 == *entry2) && |
|
|
748 | (*next3 == *entry3)) && |
|
|
749 | (i+3*len + 6 < psiconv_list_length(plain_bytes)) && |
|
|
750 | len < 0x80) { |
|
|
751 | len ++; |
|
|
752 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*3))) { |
|
|
753 | res = -PSICONV_E_NOMEM; |
|
|
754 | goto ERROR2; |
|
|
755 | } |
|
|
756 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*3+1))) { |
|
|
757 | res = -PSICONV_E_NOMEM; |
|
|
758 | goto ERROR2; |
|
|
759 | } |
|
|
760 | if (!(next3 = psiconv_list_get(plain_bytes,i+len*3+2))) { |
|
|
761 | res = -PSICONV_E_NOMEM; |
|
|
762 | goto ERROR2; |
|
|
763 | } |
|
|
764 | } |
|
|
765 | temp = len - 1; |
|
|
766 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
767 | goto ERROR2; |
|
|
768 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
769 | goto ERROR2; |
|
|
770 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
771 | goto ERROR2; |
|
|
772 | if ((res = psiconv_list_add(*encoded_bytes,entry3))) |
|
|
773 | goto ERROR2; |
|
|
774 | i += len*3; |
|
|
775 | } else { |
|
|
776 | len = 1; |
|
|
777 | while (((*next1 != *entry1) || (*next2 != *entry2) || |
|
|
778 | (*next3 != *entry3)) && |
|
|
779 | (i+len*3+6 < psiconv_list_length(plain_bytes)) && |
|
|
780 | len < 0x80) { |
|
|
781 | len ++; |
|
|
782 | entry1 = next1; |
|
|
783 | entry2 = next2; |
|
|
784 | entry3 = next3; |
|
|
785 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*3))) { |
|
|
786 | res = -PSICONV_E_NOMEM; |
|
|
787 | goto ERROR2; |
|
|
788 | } |
|
|
789 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*3+1))) { |
|
|
790 | res = -PSICONV_E_NOMEM; |
|
|
791 | goto ERROR2; |
|
|
792 | } |
|
|
793 | if (!(next3 = psiconv_list_get(plain_bytes,i+len*3+2))) { |
|
|
794 | res = -PSICONV_E_NOMEM; |
|
|
795 | goto ERROR2; |
|
|
796 | } |
|
|
797 | } |
|
|
798 | len --; |
|
|
799 | temp = 0x100 - len; |
|
|
800 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
801 | goto ERROR2; |
|
|
802 | for (j = 0; j < len; j++) { |
|
|
803 | if (!(next1 = psiconv_list_get(plain_bytes,i+j*3))) { |
|
|
804 | res = -PSICONV_E_NOMEM; |
|
|
805 | goto ERROR2; |
|
|
806 | } |
|
|
807 | if (!(next2 = psiconv_list_get(plain_bytes,i+j*3+1))) { |
|
|
808 | res = -PSICONV_E_NOMEM; |
|
|
809 | goto ERROR2; |
|
|
810 | } |
|
|
811 | if (!(next2 = psiconv_list_get(plain_bytes,i+j*3+2))) { |
|
|
812 | res = -PSICONV_E_NOMEM; |
|
|
813 | goto ERROR2; |
|
|
814 | } |
|
|
815 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
816 | goto ERROR2; |
|
|
817 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
818 | goto ERROR2; |
|
|
819 | if ((res = psiconv_list_add(*encoded_bytes,next3))) |
|
|
820 | goto ERROR2; |
|
|
821 | } |
|
|
822 | i += len*3; |
|
|
823 | } |
|
|
824 | } |
|
|
825 | return 0; |
|
|
826 | |
|
|
827 | ERROR2: |
|
|
828 | psiconv_list_free(*encoded_bytes); |
|
|
829 | ERROR1: |
|
|
830 | return res; |
|
|
831 | } |
|
|
832 | |
|
|
833 | |
|
|
834 | int psiconv_write_sketch_section(const psiconv_config config, |
|
|
835 | psiconv_buffer buf, int lev, |
|
|
836 | const psiconv_sketch_section value) |
|
|
837 | { |
|
|
838 | int res; |
|
|
839 | |
|
|
840 | psiconv_progress(config,lev,0,"Writing sketch section"); |
|
|
841 | if (!value) { |
|
|
842 | psiconv_error(config,0,0,"NULL sketch section"); |
|
|
843 | res = -PSICONV_E_GENERATE; |
|
|
844 | goto ERROR1; |
|
|
845 | } |
|
|
846 | |
|
|
847 | if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_xsize))) |
|
|
848 | goto ERROR1; |
|
|
849 | if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_ysize))) |
|
|
850 | goto ERROR1; |
|
|
851 | if ((res = psiconv_write_u16(config,buf,lev+1,value->picture_data_x_offset))) |
|
|
852 | goto ERROR1; |
|
|
853 | if ((res = psiconv_write_u16(config,buf,lev+1,value->picture_data_y_offset))) |
|
|
854 | goto ERROR1; |
|
|
855 | if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_size_x_offset))) |
|
|
856 | goto ERROR1; |
|
|
857 | if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_size_y_offset))) |
|
|
858 | goto ERROR1; |
|
|
859 | if ((res = psiconv_write_u16(config,buf,lev+1,value->form_xsize))) |
|
|
860 | goto ERROR1; |
|
|
861 | if ((res = psiconv_write_u16(config,buf,lev+1,value->form_ysize))) |
|
|
862 | goto ERROR1; |
|
|
863 | if ((res = psiconv_write_u16(config,buf,lev+1,0x0000))) |
|
|
864 | goto ERROR1; |
|
|
865 | if ((res = psiconv_write_paint_data_section(config,buf,lev+1,value->picture,0))) |
|
|
866 | goto ERROR1; |
|
|
867 | if ((res = psiconv_write_u16(config,buf,lev+1,value->magnification_x * 0x03e8))) |
|
|
868 | goto ERROR1; |
|
|
869 | if ((res = psiconv_write_u16(config,buf,lev+1,value->magnification_y * 0x03e8))) |
|
|
870 | goto ERROR1; |
|
|
871 | if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_left * 0x0c * |
|
|
872 | value->displayed_xsize))) |
|
|
873 | goto ERROR1; |
|
|
874 | if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_right * 0x0c * |
|
|
875 | value->displayed_xsize))) |
|
|
876 | goto ERROR1; |
|
|
877 | if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_top * 0x0c * |
|
|
878 | value->displayed_ysize))) |
|
|
879 | goto ERROR1; |
|
|
880 | if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_bottom * 0x0c * |
|
|
881 | value->displayed_ysize))) |
|
|
882 | goto ERROR1; |
|
|
883 | |
|
|
884 | ERROR1: |
|
|
885 | return res; |
|
|
886 | } |
|
|
887 | |
|
|
888 | int psiconv_write_clipart_section(const psiconv_config config, |
|
|
889 | psiconv_buffer buf, int lev, |
|
|
890 | const psiconv_clipart_section value) |
|
|
891 | { |
|
|
892 | int res; |
|
|
893 | |
|
|
894 | psiconv_progress(config,lev,0,"Writing clipart section"); |
|
|
895 | if (!value) { |
|
|
896 | psiconv_error(config,0,psiconv_buffer_length(buf), |
|
|
897 | "NULL Clipart Section"); |
|
|
898 | res = -PSICONV_E_GENERATE; |
|
|
899 | goto ERROR; |
|
|
900 | } |
|
|
901 | if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_CLIPART_ITEM))) |
|
|
902 | goto ERROR; |
|
|
903 | if ((res = psiconv_write_u32(config,buf,lev+1,0x00000002))) |
|
|
904 | goto ERROR; |
|
|
905 | if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000))) |
|
|
906 | goto ERROR; |
|
|
907 | if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000))) |
|
|
908 | goto ERROR; |
|
|
909 | if ((res = psiconv_write_u32(config,buf,lev+1,0x0000000C))) |
|
|
910 | goto ERROR; |
|
|
911 | if ((res = psiconv_write_paint_data_section(config,buf,lev+1,value->picture,1))) |
|
|
912 | goto ERROR; |
|
|
913 | |
|
|
914 | ERROR: |
|
|
915 | return res; |
|
|
916 | } |
|
|
917 | |
|
|
918 | int psiconv_write_jumptable_section(const psiconv_config config, |
|
|
919 | psiconv_buffer buf, int lev, |
|
|
920 | const psiconv_jumptable_section value) |
|
|
921 | { |
|
|
922 | int res,i; |
|
|
923 | psiconv_u32 *offset_ptr; |
|
|
924 | |
|
|
925 | psiconv_progress(config,lev,0,"Writing jumptable section"); |
|
|
926 | |
|
|
927 | if (!value) { |
|
|
928 | psiconv_error(config,0,psiconv_buffer_length(buf), |
|
|
929 | "NULL Jumptable Section"); |
|
|
930 | res = -PSICONV_E_GENERATE; |
|
|
931 | goto ERROR; |
|
|
932 | } |
|
|
933 | if ((res = psiconv_write_u32(config,buf,lev+1,psiconv_list_length(value)))) |
|
|
934 | goto ERROR; |
|
|
935 | for (i = 0; i < psiconv_list_length(value); i++) { |
|
|
936 | if (!(offset_ptr = psiconv_list_get(value,i))) { |
|
|
937 | psiconv_error(config,0,psiconv_buffer_length(buf), |
|
|
938 | "Massive memory corruption"); |
|
|
939 | res = -PSICONV_E_NOMEM; |
|
|
940 | goto ERROR; |
|
|
941 | } |
|
|
942 | if ((res = psiconv_write_offset(config,buf,lev+1,*offset_ptr))) |
|
|
943 | goto ERROR; |
|
|
944 | } |
|
|
945 | |
|
|
946 | ERROR: |
|
|
947 | return res; |
|
|
948 | } |
|
|
949 | |