… | |
… | |
21 | #include "config.h" |
21 | #include "config.h" |
22 | #include "compat.h" |
22 | #include "compat.h" |
23 | |
23 | |
24 | #include "generate_routines.h" |
24 | #include "generate_routines.h" |
25 | #include "error.h" |
25 | #include "error.h" |
|
|
26 | #include "list.h" |
|
|
27 | #include "image.h" |
26 | |
28 | |
27 | #ifdef DMALLOC |
29 | #ifdef DMALLOC |
28 | #include <dmalloc.h> |
30 | #include <dmalloc.h> |
29 | #endif |
31 | #endif |
30 | |
32 | |
31 | typedef psiconv_list psiconv_pixel_bytes; /* psiconv_u8 */ |
|
|
32 | |
|
|
33 | typedef psiconv_list psiconv_pixel_ints; /* of psiconv_u32 */ |
|
|
34 | |
|
|
35 | typedef struct psiconv_pixel_float_s |
|
|
36 | { |
|
|
37 | psiconv_u32 length; |
|
|
38 | float *red; |
|
|
39 | float *green; |
|
|
40 | float *blue; |
|
|
41 | } psiconv_pixel_floats_t; |
|
|
42 | |
33 | |
43 | static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, |
34 | static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, |
44 | int xsize,int ysize, |
35 | int xsize,int ysize, |
45 | const psiconv_pixel_floats_t data, |
36 | const psiconv_pixel_floats_t data, |
46 | int colordepth, |
37 | int colordepth,int color, |
|
|
38 | int redbits,int greenbits,int bluebits, |
47 | const psiconv_pixel_floats_t palet); |
39 | const psiconv_pixel_floats_t palet); |
48 | static int psiconv_pixel_data_to_bytes(const psiconv_config config, |
40 | static int psiconv_pixel_data_to_bytes(const psiconv_config config, |
49 | psiconv_pixel_bytes *bytes, int xsize, |
41 | psiconv_pixel_bytes *bytes, int xsize, |
50 | int ysize, const psiconv_pixel_ints pixels, |
42 | int ysize, const psiconv_pixel_ints pixels, |
51 | int colordepth); |
43 | int colordepth); |
52 | |
44 | static int psiconv_encode_rle8(const psiconv_config config, |
53 | #define PALET_GREY_2_LEN 4 |
45 | const psiconv_pixel_bytes plain_bytes, |
54 | float palet_grey_2_rgb[PALET_GREY_2_LEN] = {0.0/3, 1.0/3, 2.0/3, 3.0/3}; |
46 | psiconv_pixel_bytes *encoded_bytes); |
55 | #define PALET_GREY_4_LEN 16 |
47 | static int psiconv_encode_rle12(const psiconv_config config, |
56 | float palet_grey_4_rgb[PALET_GREY_4_LEN] = |
48 | const psiconv_pixel_bytes plain_bytes, |
57 | { 0.0/15, 1.0/15, 2.0/15, 3.0/15, |
49 | psiconv_pixel_bytes *encoded_bytes); |
58 | 4.0/15, 5.0/15, 6.0/15, 7.0/15, |
50 | static int psiconv_encode_rle16(const psiconv_config config, |
59 | 8.0/15, 9.0/15, 10.0/15, 11.0/15, |
51 | const psiconv_pixel_bytes plain_bytes, |
60 | 12.0/15, 13.0/15, 14.0/15, 15.0/15}; |
52 | psiconv_pixel_bytes *encoded_bytes); |
61 | #define PALET_NONE_LEN 0 |
53 | static int psiconv_encode_rle24(const psiconv_config config, |
62 | |
54 | const psiconv_pixel_bytes plain_bytes, |
63 | psiconv_pixel_floats_t palet_grey_2 = |
55 | psiconv_pixel_bytes *encoded_bytes); |
64 | { |
|
|
65 | PALET_GREY_2_LEN, |
|
|
66 | (float *) palet_grey_2_rgb, |
|
|
67 | (float *) palet_grey_2_rgb, |
|
|
68 | (float *) palet_grey_2_rgb |
|
|
69 | }; |
|
|
70 | |
|
|
71 | psiconv_pixel_floats_t palet_grey_4 = |
|
|
72 | { |
|
|
73 | PALET_GREY_4_LEN, |
|
|
74 | (float *) palet_grey_4_rgb, |
|
|
75 | (float *) palet_grey_4_rgb, |
|
|
76 | (float *) palet_grey_4_rgb |
|
|
77 | }; |
|
|
78 | |
|
|
79 | psiconv_pixel_floats_t palet_none = |
|
|
80 | { |
|
|
81 | PALET_NONE_LEN, |
|
|
82 | NULL, |
|
|
83 | NULL, |
|
|
84 | NULL |
|
|
85 | }; |
|
|
86 | |
|
|
87 | |
56 | |
88 | int psiconv_write_paint_data_section(const psiconv_config config, |
57 | int psiconv_write_paint_data_section(const psiconv_config config, |
89 | psiconv_buffer buf, |
58 | psiconv_buffer buf, |
90 | const psiconv_paint_data_section value, |
59 | const psiconv_paint_data_section value, |
91 | int is_clipart) |
60 | int is_clipart) |
92 | { |
61 | { |
93 | int res,colordepth,i; |
62 | int res,colordepth,i; |
94 | psiconv_pixel_ints ints; |
63 | psiconv_pixel_ints ints; |
95 | psiconv_pixel_floats_t floats,palet; |
64 | psiconv_pixel_floats_t floats,palet; |
96 | psiconv_list bytes; |
65 | psiconv_list bytes,bytes_rle; |
97 | psiconv_u8 *byteptr; |
66 | psiconv_u8 *byteptr,encoding; |
|
|
67 | |
|
|
68 | /* First, we check whether we can cope with the current configuration. |
|
|
69 | If not, we stop at once */ |
|
|
70 | if ((config->colordepth != 2) && (config->colordepth != 4) && |
|
|
71 | (config->colordepth != 8) && (config->colordepth != 12) && |
|
|
72 | (config->colordepth != 16) && (config->colordepth != 24)) { |
|
|
73 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
74 | "Unsupported color depth (%d); try 2, 4, 8, 16 or 24", |
|
|
75 | config->colordepth); |
|
|
76 | res = -PSICONV_E_GENERATE; |
|
|
77 | goto ERROR1; |
|
|
78 | } |
|
|
79 | |
|
|
80 | if ((config->color) && |
|
|
81 | (config->bluebits || config->redbits || config->greenbits) && |
|
|
82 | (config->bluebits+config->redbits+config->greenbits!=config->colordepth)) { |
|
|
83 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
84 | "Sum of red (%d), green (%d) and blue (%d) bits should be " |
|
|
85 | "equal to the color depth (%d)", |
|
|
86 | config->redbits,config->greenbits,config->bluebits, |
|
|
87 | config->colordepth); |
|
|
88 | res = -PSICONV_E_GENERATE; |
|
|
89 | goto ERROR1; |
|
|
90 | } |
|
|
91 | |
|
|
92 | if (config->color && |
|
|
93 | !(config->redbits || config->greenbits || config->bluebits) && |
|
|
94 | (config->colordepth != 4) && (config->colordepth != 8)) { |
|
|
95 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
96 | "Current color depth (%d) has no palet associated with it", |
|
|
97 | config->colordepth); |
|
|
98 | res = -PSICONV_E_GENERATE; |
|
|
99 | goto ERROR1; |
|
|
100 | } |
|
|
101 | |
|
|
102 | if (config->color || (config->colordepth != 2)) |
|
|
103 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
104 | "All image types except 2-bit greyscale are experimental!"); |
|
|
105 | |
98 | |
106 | |
99 | if (!value) { |
107 | if (!value) { |
100 | psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paint data section"); |
108 | psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paint data section"); |
101 | res = -PSICONV_E_GENERATE; |
109 | res = -PSICONV_E_GENERATE; |
102 | goto ERROR1; |
110 | goto ERROR1; |
… | |
… | |
105 | floats.red = value->red; |
113 | floats.red = value->red; |
106 | floats.green = value->green; |
114 | floats.green = value->green; |
107 | floats.blue = value->blue; |
115 | floats.blue = value->blue; |
108 | floats.length = value->xsize * value->ysize; |
116 | floats.length = value->xsize * value->ysize; |
109 | |
117 | |
|
|
118 | palet = psiconv_palet_none; |
|
|
119 | if ((config->color) && (config->redbits == 0) && (config->greenbits == 0) &&\ |
|
|
120 | (config->bluebits == 0)) |
110 | switch (config->colordepth) { |
121 | switch (config->colordepth) { |
111 | default: |
122 | case 4: palet = psiconv_palet_color_4; break; |
112 | case 2: palet = (config->color?palet_none:palet_grey_2); |
123 | case 8: palet = psiconv_palet_color_8; break; |
113 | break; |
124 | default: palet = psiconv_palet_none; break; |
114 | case 4: palet = (config->color?palet_none:palet_grey_4); |
|
|
115 | break; |
|
|
116 | } |
125 | } |
117 | |
126 | |
118 | if ((res = psiconv_collect_pixel_data(&ints,value->xsize, |
127 | if ((res = psiconv_collect_pixel_data(&ints,value->xsize, |
119 | value->ysize,floats, |
128 | value->ysize,floats, |
120 | config->colordepth,palet))) |
129 | config->colordepth,config->color, |
|
|
130 | config->redbits,config->greenbits, |
|
|
131 | config->bluebits,palet))) |
121 | goto ERROR1; |
132 | goto ERROR1; |
122 | |
133 | |
123 | if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, |
134 | if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, |
124 | ints,config->colordepth))) |
135 | ints,config->colordepth))) |
125 | goto ERROR2; |
136 | goto ERROR2; |
126 | |
137 | |
127 | if ((res = psiconv_write_u32(config,buf,0x28+psiconv_list_length(bytes)))) |
138 | |
|
|
139 | switch (config->colordepth) { |
|
|
140 | case 2: |
|
|
141 | case 4: |
|
|
142 | case 8: |
|
|
143 | encoding = 0x01; |
|
|
144 | if ((res = psiconv_encode_rle8(config,bytes,&bytes_rle))) |
|
|
145 | goto ERROR3; |
|
|
146 | break; |
|
|
147 | case 12: |
|
|
148 | encoding = 0x02; |
|
|
149 | if ((res = psiconv_encode_rle12(config,bytes,&bytes_rle))) |
|
|
150 | goto ERROR3; |
|
|
151 | break; |
|
|
152 | case 16: |
|
|
153 | encoding = 0x03; |
|
|
154 | if ((res = psiconv_encode_rle16(config,bytes,&bytes_rle))) |
|
|
155 | goto ERROR3; |
|
|
156 | break; |
|
|
157 | case 24: |
|
|
158 | encoding = 0x04; |
|
|
159 | if ((res = psiconv_encode_rle24(config,bytes,&bytes_rle))) |
|
|
160 | goto ERROR3; |
|
|
161 | break; |
|
|
162 | default: |
|
|
163 | encoding = 0x00; |
|
|
164 | } |
|
|
165 | if (encoding) { |
|
|
166 | if (psiconv_list_length(bytes_rle) < psiconv_list_length(bytes)) { |
|
|
167 | psiconv_list_free(bytes); |
|
|
168 | bytes = bytes_rle; |
|
|
169 | } else { |
|
|
170 | psiconv_list_free(bytes_rle); |
|
|
171 | encoding = 0x00; |
|
|
172 | } |
|
|
173 | } |
|
|
174 | |
|
|
175 | if ((res = psiconv_write_u32(config,buf, |
|
|
176 | 0x28+psiconv_list_length(bytes)))) |
128 | goto ERROR3; |
177 | goto ERROR3; |
129 | if ((res = psiconv_write_u32(config,buf,0x28))) |
178 | if ((res = psiconv_write_u32(config,buf,0x28))) |
130 | goto ERROR3; |
179 | goto ERROR3; |
131 | if ((res = psiconv_write_u32(config,buf,value->xsize))) |
180 | if ((res = psiconv_write_u32(config,buf,value->xsize))) |
132 | goto ERROR3; |
181 | goto ERROR3; |
… | |
… | |
135 | if ((res = psiconv_write_length(config,buf,value->pic_xsize))) |
184 | if ((res = psiconv_write_length(config,buf,value->pic_xsize))) |
136 | goto ERROR3; |
185 | goto ERROR3; |
137 | if ((res = psiconv_write_length(config,buf,value->pic_ysize))) |
186 | if ((res = psiconv_write_length(config,buf,value->pic_ysize))) |
138 | goto ERROR3; |
187 | goto ERROR3; |
139 | colordepth = config->colordepth; |
188 | colordepth = config->colordepth; |
140 | if ((colordepth != 2) && colordepth != 4) |
|
|
141 | colordepth = 2; |
|
|
142 | if ((res = psiconv_write_u32(config,buf,colordepth))) |
189 | if ((res = psiconv_write_u32(config,buf,colordepth))) |
143 | goto ERROR3; |
190 | goto ERROR3; |
144 | if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) |
191 | if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) |
145 | goto ERROR3; |
192 | goto ERROR3; |
146 | if ((res = psiconv_write_u32(config,buf,0))) |
193 | if ((res = psiconv_write_u32(config,buf,0))) |
147 | goto ERROR3; |
194 | goto ERROR3; |
148 | /* Encoding: no RLE for now */ |
|
|
149 | if ((res = psiconv_write_u32(config,buf,0))) |
195 | if ((res = psiconv_write_u32(config,buf,encoding))) |
150 | goto ERROR3; |
196 | goto ERROR3; |
151 | if (is_clipart) { |
197 | if (is_clipart) { |
152 | if ((res = psiconv_write_u32(config,buf,0xffffffff))) |
198 | if ((res = psiconv_write_u32(config,buf,0xffffffff))) |
153 | goto ERROR3; |
199 | goto ERROR3; |
154 | if ((res = psiconv_write_u32(config,buf,0x00000044))) |
200 | if ((res = psiconv_write_u32(config,buf,0x00000044))) |
… | |
… | |
173 | The palet is optional; without it, we just use the |
219 | The palet is optional; without it, we just use the |
174 | colordepth. With a large palet this is not very fast, but it will do for |
220 | colordepth. With a large palet this is not very fast, but it will do for |
175 | now. For greyscale pictures, just use the palet. */ |
221 | now. For greyscale pictures, just use the palet. */ |
176 | int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize, |
222 | int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize, |
177 | const psiconv_pixel_floats_t data, |
223 | const psiconv_pixel_floats_t data, |
178 | int colordepth, |
224 | int colordepth,int color, |
|
|
225 | int redbits,int bluebits,int greenbits, |
179 | const psiconv_pixel_floats_t palet) |
226 | const psiconv_pixel_floats_t palet) |
180 | { |
227 | { |
181 | int res,x,y,i; |
228 | int res,x,y,i; |
182 | psiconv_u32 index,pixel; |
229 | psiconv_u32 index,pixel; |
183 | float p_red,p_green,p_blue,mult,dist,new_dist; |
230 | float p_red,p_green,p_blue,dist,new_dist; |
184 | |
231 | |
185 | if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { |
232 | if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { |
186 | res = -PSICONV_E_NOMEM; |
233 | res = -PSICONV_E_NOMEM; |
187 | goto ERROR1; |
234 | goto ERROR1; |
188 | } |
235 | } |
189 | |
236 | |
190 | mult = 1 << colordepth; |
|
|
191 | for (y = 0; y < ysize; y++) { |
237 | for (y = 0; y < ysize; y++) { |
192 | for (x = 0; x < xsize; x++) { |
238 | for (x = 0; x < xsize; x++) { |
193 | index = y*xsize+x; |
239 | index = y*xsize+x; |
194 | p_red = data.red[index]; |
240 | p_red = data.red[index]; |
195 | p_green = data.green[index]; |
241 | p_green = data.green[index]; |
196 | p_blue = data.blue[index]; |
242 | p_blue = data.blue[index]; |
197 | if (!palet.length) { |
243 | if (!palet.length) { |
198 | pixel = (((psiconv_u32) (p_red*mult+0.5)) << (2*colordepth)) + |
244 | if (color) |
199 | (((psiconv_u32) (p_green*mult+0.5)) << colordepth) + |
245 | pixel = (((psiconv_u32) (p_red * (1 << redbits) + 0.5)) |
|
|
246 | << (greenbits+bluebits)) + |
|
|
247 | (((psiconv_u32) (p_green * (1 << greenbits) + 0.5)) |
|
|
248 | << bluebits) + |
200 | ((psiconv_u32) (p_blue*mult+0.5)); |
249 | ((psiconv_u32) (p_blue * (1 << bluebits) + 0.5)); |
|
|
250 | else |
|
|
251 | pixel = (p_red + p_green + p_blue)/3.0 * (1 << colordepth); |
201 | } else { |
252 | } else { |
202 | dist = 4; /* Max distance is 3, so this is safe */ |
253 | dist = 4; /* Max distance is 3, so this is safe */ |
203 | pixel = -1; |
254 | pixel = -1; |
204 | for (i = 0; i < palet.length; i++) { |
255 | for (i = 0; i < palet.length; i++) { |
205 | new_dist = (p_red - palet.red[i]) * (p_red - palet.red[i]) + |
256 | new_dist = (p_red - palet.red[i]) * (p_red - palet.red[i]) + |
… | |
… | |
308 | psiconv_list_free(*bytes); |
359 | psiconv_list_free(*bytes); |
309 | ERROR1: |
360 | ERROR1: |
310 | return res; |
361 | return res; |
311 | } |
362 | } |
312 | |
363 | |
|
|
364 | /* RLE8 encoding: |
|
|
365 | Marker bytes followed by one or more data bytes. |
|
|
366 | Marker value 0x00-0x7f: repeat the next data byte (marker+1) times |
|
|
367 | Marker value 0xff-0x80: (0x100-marker) data bytes follow */ |
|
|
368 | int psiconv_encode_rle8(const psiconv_config config, |
|
|
369 | const psiconv_pixel_bytes plain_bytes, |
|
|
370 | psiconv_pixel_bytes *encoded_bytes) |
|
|
371 | { |
|
|
372 | int res,i,j,len; |
|
|
373 | psiconv_u8 *entry,*next; |
|
|
374 | psiconv_u8 temp; |
|
|
375 | |
|
|
376 | if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry)))) { |
|
|
377 | res = -PSICONV_E_NOMEM; |
|
|
378 | goto ERROR1; |
|
|
379 | } |
|
|
380 | |
|
|
381 | for (i = 0; i < psiconv_list_length(plain_bytes);) { |
|
|
382 | if (!(entry = psiconv_list_get(plain_bytes,i))) { |
|
|
383 | res = -PSICONV_E_NOMEM; |
|
|
384 | goto ERROR2; |
|
|
385 | } |
|
|
386 | if (!(next = psiconv_list_get(plain_bytes,i+1))) { |
|
|
387 | res = -PSICONV_E_NOMEM; |
|
|
388 | goto ERROR2; |
|
|
389 | } |
|
|
390 | if (i == psiconv_list_length(plain_bytes) - 2) { |
|
|
391 | temp = 0xfe; |
|
|
392 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
393 | goto ERROR2; |
|
|
394 | if ((res = psiconv_list_add(*encoded_bytes,entry))) |
|
|
395 | goto ERROR2; |
|
|
396 | if ((res = psiconv_list_add(*encoded_bytes,next))) |
|
|
397 | goto ERROR2; |
|
|
398 | i +=2; |
|
|
399 | } else if (*next == *entry) { |
|
|
400 | len = 0; |
|
|
401 | while ((*next == *entry) && |
|
|
402 | (i+len + 1 < psiconv_list_length(plain_bytes)) && |
|
|
403 | len < 0x80) { |
|
|
404 | len ++; |
|
|
405 | if (!(next = psiconv_list_get(plain_bytes,i+len))) { |
|
|
406 | res = -PSICONV_E_NOMEM; |
|
|
407 | goto ERROR2; |
|
|
408 | } |
|
|
409 | } |
|
|
410 | temp = len - 1; |
|
|
411 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
412 | goto ERROR2; |
|
|
413 | if ((res = psiconv_list_add(*encoded_bytes,entry))) |
|
|
414 | goto ERROR2; |
|
|
415 | i += len; |
|
|
416 | } else { |
|
|
417 | len = 1; |
|
|
418 | while ((*next != *entry) && |
|
|
419 | (i+len+1 < psiconv_list_length(plain_bytes)) && |
|
|
420 | len < 0x80) { |
|
|
421 | len ++; |
|
|
422 | entry = next; |
|
|
423 | if (!(next = psiconv_list_get(plain_bytes,i+len))) { |
|
|
424 | res = -PSICONV_E_NOMEM; |
|
|
425 | goto ERROR2; |
|
|
426 | } |
|
|
427 | } |
|
|
428 | len --; |
|
|
429 | temp = 0x100 - len; |
|
|
430 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
431 | goto ERROR2; |
|
|
432 | for (j = 0; j < len; j++) { |
|
|
433 | if (!(next = psiconv_list_get(plain_bytes,i+j))) { |
|
|
434 | res = -PSICONV_E_NOMEM; |
|
|
435 | goto ERROR2; |
|
|
436 | } |
|
|
437 | if ((res = psiconv_list_add(*encoded_bytes,next))) |
|
|
438 | goto ERROR2; |
|
|
439 | } |
|
|
440 | i += len; |
|
|
441 | } |
|
|
442 | } |
|
|
443 | return 0; |
|
|
444 | |
|
|
445 | ERROR2: |
|
|
446 | psiconv_list_free(*encoded_bytes); |
|
|
447 | ERROR1: |
|
|
448 | return res; |
|
|
449 | } |
|
|
450 | |
|
|
451 | /* RLE12 encoding: |
|
|
452 | Word based. The 12 least significant bits contain the pixel colors. |
|
|
453 | the 4 most signigicant bits are the number of repetitions minus 1 */ |
|
|
454 | int psiconv_encode_rle12(const psiconv_config config, |
|
|
455 | const psiconv_pixel_bytes plain_bytes, |
|
|
456 | psiconv_pixel_bytes *encoded_bytes) |
|
|
457 | { |
|
|
458 | typedef psiconv_list psiconv_word_data; /* of psiconv_u16 */ |
|
|
459 | psiconv_word_data data; |
|
|
460 | int res,i,len,location; |
|
|
461 | psiconv_u16 *word_entry,*word_next; |
|
|
462 | psiconv_u16 word_data; |
|
|
463 | psiconv_u8 byte_temp; |
|
|
464 | psiconv_u8 *byte_entry; |
|
|
465 | |
|
|
466 | |
|
|
467 | /* First extract the 12-bit values to encode */ |
|
|
468 | if (!(data = psiconv_list_new(sizeof(psiconv_u16)))) { |
|
|
469 | res = -PSICONV_E_NOMEM; |
|
|
470 | goto ERROR1; |
|
|
471 | } |
|
|
472 | |
|
|
473 | for (i = 0; i < psiconv_list_length(plain_bytes); i++) { |
|
|
474 | if (!(byte_entry = psiconv_list_get(plain_bytes,i))) { |
|
|
475 | res = -PSICONV_E_NOMEM; |
|
|
476 | goto ERROR2; |
|
|
477 | } |
|
|
478 | location = 0; |
|
|
479 | if (location == 0) { |
|
|
480 | word_data = *byte_entry; |
|
|
481 | location ++; |
|
|
482 | } else if (location == 1) { |
|
|
483 | word_data = (word_data << 4) + (*byte_entry & 0x0f); |
|
|
484 | if ((res = psiconv_list_add(data,&word_data))) |
|
|
485 | goto ERROR2; |
|
|
486 | word_data = *byte_entry >> 4; |
|
|
487 | location ++; |
|
|
488 | } else { |
|
|
489 | word_data = (word_data << 8) + *byte_entry; |
|
|
490 | if ((res = psiconv_list_add(data,&word_data))) |
|
|
491 | goto ERROR2; |
|
|
492 | location = 0; |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | if (!(*encoded_bytes = psiconv_list_new(sizeof(psiconv_u8)))) { |
|
|
497 | res = -PSICONV_E_NOMEM; |
|
|
498 | goto ERROR2; |
|
|
499 | } |
|
|
500 | |
|
|
501 | for (i = 0; i < psiconv_list_length(data);) { |
|
|
502 | if (!(word_entry = psiconv_list_get(data,i))) { |
|
|
503 | res = -PSICONV_E_NOMEM; |
|
|
504 | goto ERROR3; |
|
|
505 | } |
|
|
506 | |
|
|
507 | if (!(word_next = psiconv_list_get(data,i+1))) { |
|
|
508 | res = -PSICONV_E_NOMEM; |
|
|
509 | goto ERROR3; |
|
|
510 | } |
|
|
511 | |
|
|
512 | if (i == psiconv_list_length(data) - 2) { |
|
|
513 | byte_temp = *word_entry && 0xff; |
|
|
514 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
515 | goto ERROR3; |
|
|
516 | byte_temp = *word_entry >> 8; |
|
|
517 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
518 | goto ERROR3; |
|
|
519 | byte_temp = *word_next && 0xff; |
|
|
520 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
521 | goto ERROR3; |
|
|
522 | byte_temp = *word_next >> 8; |
|
|
523 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
524 | goto ERROR3; |
|
|
525 | i += 2; |
|
|
526 | } |
|
|
527 | |
|
|
528 | len = 0; |
|
|
529 | while ((*word_entry == *word_next) && (len < 16) && |
|
|
530 | (i+len+1 < psiconv_list_length(data))) { |
|
|
531 | len ++; |
|
|
532 | if (!(word_next = psiconv_list_get(data,i+len))) { |
|
|
533 | res = -PSICONV_E_NOMEM; |
|
|
534 | goto ERROR3; |
|
|
535 | } |
|
|
536 | } |
|
|
537 | |
|
|
538 | byte_temp = *word_entry && 0xff; |
|
|
539 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
540 | goto ERROR3; |
|
|
541 | byte_temp = (*word_entry >> 8) + ((len - 1) << 4); |
|
|
542 | if ((res = psiconv_list_add(*encoded_bytes,&byte_temp))) |
|
|
543 | goto ERROR3; |
|
|
544 | i += len; |
|
|
545 | } |
|
|
546 | return 0; |
|
|
547 | |
|
|
548 | ERROR3: |
|
|
549 | psiconv_list_free(*encoded_bytes); |
|
|
550 | ERROR2: |
|
|
551 | psiconv_list_free(data); |
|
|
552 | ERROR1: |
|
|
553 | return res; |
|
|
554 | } |
|
|
555 | |
|
|
556 | /* RLE16 encoding: |
|
|
557 | Marker bytes followed by one or more data words. |
|
|
558 | Marker value 0x00-0x7f: repeat the next data word (marker+1) times |
|
|
559 | Marker value 0xff-0x80: (0x100-marker) data words follow */ |
|
|
560 | int psiconv_encode_rle16(const psiconv_config config, |
|
|
561 | const psiconv_pixel_bytes plain_bytes, |
|
|
562 | psiconv_pixel_bytes *encoded_bytes) |
|
|
563 | { |
|
|
564 | int res,i,j,len; |
|
|
565 | psiconv_u8 *entry1,*entry2,*next1,*next2; |
|
|
566 | psiconv_u8 temp; |
|
|
567 | |
|
|
568 | if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry1)))) { |
|
|
569 | res = -PSICONV_E_NOMEM; |
|
|
570 | goto ERROR1; |
|
|
571 | } |
|
|
572 | |
|
|
573 | for (i = 0; i < psiconv_list_length(plain_bytes);) { |
|
|
574 | if (!(entry1 = psiconv_list_get(plain_bytes,i))) { |
|
|
575 | res = -PSICONV_E_NOMEM; |
|
|
576 | goto ERROR2; |
|
|
577 | } |
|
|
578 | if (!(entry2 = psiconv_list_get(plain_bytes,i+1))) { |
|
|
579 | res = -PSICONV_E_NOMEM; |
|
|
580 | goto ERROR2; |
|
|
581 | } |
|
|
582 | if (!(next1 = psiconv_list_get(plain_bytes,i+2))) { |
|
|
583 | res = -PSICONV_E_NOMEM; |
|
|
584 | goto ERROR2; |
|
|
585 | } |
|
|
586 | if (!(next2 = psiconv_list_get(plain_bytes,i+3))) { |
|
|
587 | res = -PSICONV_E_NOMEM; |
|
|
588 | goto ERROR2; |
|
|
589 | } |
|
|
590 | if (i == psiconv_list_length(plain_bytes) - 4) { |
|
|
591 | temp = 0xfe; |
|
|
592 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
593 | goto ERROR2; |
|
|
594 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
595 | goto ERROR2; |
|
|
596 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
597 | goto ERROR2; |
|
|
598 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
599 | goto ERROR2; |
|
|
600 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
601 | goto ERROR2; |
|
|
602 | i +=4; |
|
|
603 | } else if ((*next1 == *entry1) && (*next2 == *entry2)) { |
|
|
604 | len = 0; |
|
|
605 | while (((*next1 == *entry1) && (*next2 == *entry2)) && |
|
|
606 | (i+2*len + 4 < psiconv_list_length(plain_bytes)) && |
|
|
607 | len < 0x80) { |
|
|
608 | len ++; |
|
|
609 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*2))) { |
|
|
610 | res = -PSICONV_E_NOMEM; |
|
|
611 | goto ERROR2; |
|
|
612 | } |
|
|
613 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*2+1))) { |
|
|
614 | res = -PSICONV_E_NOMEM; |
|
|
615 | goto ERROR2; |
|
|
616 | } |
|
|
617 | } |
|
|
618 | temp = len - 1; |
|
|
619 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
620 | goto ERROR2; |
|
|
621 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
622 | goto ERROR2; |
|
|
623 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
624 | goto ERROR2; |
|
|
625 | i += len*2; |
|
|
626 | } else { |
|
|
627 | len = 1; |
|
|
628 | while (((*next1 != *entry1) || (*next2 != *entry2))&& |
|
|
629 | (i+len*2+4 < psiconv_list_length(plain_bytes)) && |
|
|
630 | len < 0x80) { |
|
|
631 | len ++; |
|
|
632 | entry1 = next1; |
|
|
633 | entry2 = next2; |
|
|
634 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*2))) { |
|
|
635 | res = -PSICONV_E_NOMEM; |
|
|
636 | goto ERROR2; |
|
|
637 | } |
|
|
638 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*2+1))) { |
|
|
639 | res = -PSICONV_E_NOMEM; |
|
|
640 | goto ERROR2; |
|
|
641 | } |
|
|
642 | } |
|
|
643 | len --; |
|
|
644 | temp = 0x100 - len; |
|
|
645 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
646 | goto ERROR2; |
|
|
647 | for (j = 0; j < len; j++) { |
|
|
648 | if (!(next1 = psiconv_list_get(plain_bytes,i+j*2))) { |
|
|
649 | res = -PSICONV_E_NOMEM; |
|
|
650 | goto ERROR2; |
|
|
651 | } |
|
|
652 | if (!(next2 = psiconv_list_get(plain_bytes,i+j*2+1))) { |
|
|
653 | res = -PSICONV_E_NOMEM; |
|
|
654 | goto ERROR2; |
|
|
655 | } |
|
|
656 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
657 | goto ERROR2; |
|
|
658 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
659 | goto ERROR2; |
|
|
660 | } |
|
|
661 | i += len*2; |
|
|
662 | } |
|
|
663 | } |
|
|
664 | return 0; |
|
|
665 | |
|
|
666 | ERROR2: |
|
|
667 | psiconv_list_free(*encoded_bytes); |
|
|
668 | ERROR1: |
|
|
669 | return res; |
|
|
670 | } |
|
|
671 | |
|
|
672 | /* RLE24 encoding: |
|
|
673 | Marker bytes followed by one or more data byte-triplets. |
|
|
674 | Marker value 0x00-0x7f: repeat the next data byte-triplets (marker+1) times |
|
|
675 | Marker value 0xff-0x80: (0x100-marker) data byte-triplets follow */ |
|
|
676 | int psiconv_encode_rle24(const psiconv_config config, |
|
|
677 | const psiconv_pixel_bytes plain_bytes, |
|
|
678 | psiconv_pixel_bytes *encoded_bytes) |
|
|
679 | { |
|
|
680 | int res,i,j,len; |
|
|
681 | psiconv_u8 *entry1,*entry2,*entry3,*next1,*next2,*next3; |
|
|
682 | psiconv_u8 temp; |
|
|
683 | |
|
|
684 | if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry1)))) { |
|
|
685 | res = -PSICONV_E_NOMEM; |
|
|
686 | goto ERROR1; |
|
|
687 | } |
|
|
688 | |
|
|
689 | for (i = 0; i < psiconv_list_length(plain_bytes);) { |
|
|
690 | if (!(entry1 = psiconv_list_get(plain_bytes,i))) { |
|
|
691 | res = -PSICONV_E_NOMEM; |
|
|
692 | goto ERROR2; |
|
|
693 | } |
|
|
694 | if (!(entry2 = psiconv_list_get(plain_bytes,i+1))) { |
|
|
695 | res = -PSICONV_E_NOMEM; |
|
|
696 | goto ERROR2; |
|
|
697 | } |
|
|
698 | if (!(entry3 = psiconv_list_get(plain_bytes,i+2))) { |
|
|
699 | res = -PSICONV_E_NOMEM; |
|
|
700 | goto ERROR2; |
|
|
701 | } |
|
|
702 | if (!(next1 = psiconv_list_get(plain_bytes,i+3))) { |
|
|
703 | res = -PSICONV_E_NOMEM; |
|
|
704 | goto ERROR2; |
|
|
705 | } |
|
|
706 | if (!(next2 = psiconv_list_get(plain_bytes,i+4))) { |
|
|
707 | res = -PSICONV_E_NOMEM; |
|
|
708 | goto ERROR2; |
|
|
709 | } |
|
|
710 | if (!(next3 = psiconv_list_get(plain_bytes,i+5))) { |
|
|
711 | res = -PSICONV_E_NOMEM; |
|
|
712 | goto ERROR2; |
|
|
713 | } |
|
|
714 | if (i == psiconv_list_length(plain_bytes) - 6) { |
|
|
715 | temp = 0xfe; |
|
|
716 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
717 | goto ERROR2; |
|
|
718 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
719 | goto ERROR2; |
|
|
720 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
721 | goto ERROR2; |
|
|
722 | if ((res = psiconv_list_add(*encoded_bytes,entry3))) |
|
|
723 | goto ERROR2; |
|
|
724 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
725 | goto ERROR2; |
|
|
726 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
727 | goto ERROR2; |
|
|
728 | if ((res = psiconv_list_add(*encoded_bytes,next3))) |
|
|
729 | goto ERROR2; |
|
|
730 | i +=4; |
|
|
731 | } else if ((*next1 == *entry1) && (*next2 == *entry2) && |
|
|
732 | (*next3 == *entry3)) { |
|
|
733 | len = 0; |
|
|
734 | while (((*next1 == *entry1) && (*next2 == *entry2) && |
|
|
735 | (*next3 == *entry3)) && |
|
|
736 | (i+3*len + 6 < psiconv_list_length(plain_bytes)) && |
|
|
737 | len < 0x80) { |
|
|
738 | len ++; |
|
|
739 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*3))) { |
|
|
740 | res = -PSICONV_E_NOMEM; |
|
|
741 | goto ERROR2; |
|
|
742 | } |
|
|
743 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*3+1))) { |
|
|
744 | res = -PSICONV_E_NOMEM; |
|
|
745 | goto ERROR2; |
|
|
746 | } |
|
|
747 | if (!(next3 = psiconv_list_get(plain_bytes,i+len*3+2))) { |
|
|
748 | res = -PSICONV_E_NOMEM; |
|
|
749 | goto ERROR2; |
|
|
750 | } |
|
|
751 | } |
|
|
752 | temp = len - 1; |
|
|
753 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
754 | goto ERROR2; |
|
|
755 | if ((res = psiconv_list_add(*encoded_bytes,entry1))) |
|
|
756 | goto ERROR2; |
|
|
757 | if ((res = psiconv_list_add(*encoded_bytes,entry2))) |
|
|
758 | goto ERROR2; |
|
|
759 | if ((res = psiconv_list_add(*encoded_bytes,entry3))) |
|
|
760 | goto ERROR2; |
|
|
761 | i += len*3; |
|
|
762 | } else { |
|
|
763 | len = 1; |
|
|
764 | while (((*next1 != *entry1) || (*next2 != *entry2) || |
|
|
765 | (*next3 != *entry3)) && |
|
|
766 | (i+len*3+6 < psiconv_list_length(plain_bytes)) && |
|
|
767 | len < 0x80) { |
|
|
768 | len ++; |
|
|
769 | entry1 = next1; |
|
|
770 | entry2 = next2; |
|
|
771 | entry3 = next3; |
|
|
772 | if (!(next1 = psiconv_list_get(plain_bytes,i+len*3))) { |
|
|
773 | res = -PSICONV_E_NOMEM; |
|
|
774 | goto ERROR2; |
|
|
775 | } |
|
|
776 | if (!(next2 = psiconv_list_get(plain_bytes,i+len*3+1))) { |
|
|
777 | res = -PSICONV_E_NOMEM; |
|
|
778 | goto ERROR2; |
|
|
779 | } |
|
|
780 | if (!(next3 = psiconv_list_get(plain_bytes,i+len*3+2))) { |
|
|
781 | res = -PSICONV_E_NOMEM; |
|
|
782 | goto ERROR2; |
|
|
783 | } |
|
|
784 | } |
|
|
785 | len --; |
|
|
786 | temp = 0x100 - len; |
|
|
787 | if ((res = psiconv_list_add(*encoded_bytes,&temp))) |
|
|
788 | goto ERROR2; |
|
|
789 | for (j = 0; j < len; j++) { |
|
|
790 | if (!(next1 = psiconv_list_get(plain_bytes,i+j*3))) { |
|
|
791 | res = -PSICONV_E_NOMEM; |
|
|
792 | goto ERROR2; |
|
|
793 | } |
|
|
794 | if (!(next2 = psiconv_list_get(plain_bytes,i+j*3+1))) { |
|
|
795 | res = -PSICONV_E_NOMEM; |
|
|
796 | goto ERROR2; |
|
|
797 | } |
|
|
798 | if (!(next2 = psiconv_list_get(plain_bytes,i+j*3+2))) { |
|
|
799 | res = -PSICONV_E_NOMEM; |
|
|
800 | goto ERROR2; |
|
|
801 | } |
|
|
802 | if ((res = psiconv_list_add(*encoded_bytes,next1))) |
|
|
803 | goto ERROR2; |
|
|
804 | if ((res = psiconv_list_add(*encoded_bytes,next2))) |
|
|
805 | goto ERROR2; |
|
|
806 | if ((res = psiconv_list_add(*encoded_bytes,next3))) |
|
|
807 | goto ERROR2; |
|
|
808 | } |
|
|
809 | i += len*3; |
|
|
810 | } |
|
|
811 | } |
|
|
812 | return 0; |
|
|
813 | |
|
|
814 | ERROR2: |
|
|
815 | psiconv_list_free(*encoded_bytes); |
|
|
816 | ERROR1: |
|
|
817 | return res; |
|
|
818 | } |
|
|
819 | |
|
|
820 | |
313 | int psiconv_write_sketch_section(const psiconv_config config, |
821 | int psiconv_write_sketch_section(const psiconv_config config, |
314 | psiconv_buffer buf, |
822 | psiconv_buffer buf, |
315 | const psiconv_sketch_section value) |
823 | const psiconv_sketch_section value) |
316 | { |
824 | { |
317 | int res; |
825 | int res; |
… | |
… | |
358 | if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c * |
866 | if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c * |
359 | value->displayed_ysize))) |
867 | value->displayed_ysize))) |
360 | goto ERROR1; |
868 | goto ERROR1; |
361 | |
869 | |
362 | ERROR1: |
870 | ERROR1: |
363 | return res; |
|
|
364 | } |
|
|
365 | |
|
|
366 | int psiconv_write_jumptable_section(const psiconv_config config, |
|
|
367 | psiconv_buffer buf, |
|
|
368 | const psiconv_jumptable_section value) |
|
|
369 | { |
|
|
370 | int res,i; |
|
|
371 | psiconv_u32 *offset_ptr; |
|
|
372 | |
|
|
373 | |
|
|
374 | if (!value) { |
|
|
375 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
376 | "NULL Jumptable Section"); |
|
|
377 | res = -PSICONV_E_GENERATE; |
|
|
378 | goto ERROR; |
|
|
379 | } |
|
|
380 | if ((res = psiconv_write_u32(config,buf,psiconv_list_length(value)))) |
|
|
381 | goto ERROR; |
|
|
382 | for (i = 0; i < psiconv_list_length(value); i++) { |
|
|
383 | if (!(offset_ptr = psiconv_list_get(value,i))) { |
|
|
384 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
385 | "Massive memory corruption"); |
|
|
386 | res = -PSICONV_E_NOMEM; |
|
|
387 | goto ERROR; |
|
|
388 | } |
|
|
389 | if ((res = psiconv_write_offset(config,buf,*offset_ptr))) |
|
|
390 | goto ERROR; |
|
|
391 | } |
|
|
392 | |
|
|
393 | ERROR: |
|
|
394 | return res; |
871 | return res; |
395 | } |
872 | } |
396 | |
873 | |
397 | int psiconv_write_clipart_section(const psiconv_config config, |
874 | int psiconv_write_clipart_section(const psiconv_config config, |
398 | psiconv_buffer buf, |
875 | psiconv_buffer buf, |
… | |
… | |
421 | goto ERROR; |
898 | goto ERROR; |
422 | |
899 | |
423 | ERROR: |
900 | ERROR: |
424 | return res; |
901 | return res; |
425 | } |
902 | } |
|
|
903 | |
|
|
904 | int psiconv_write_jumptable_section(const psiconv_config config, |
|
|
905 | psiconv_buffer buf, |
|
|
906 | const psiconv_jumptable_section value) |
|
|
907 | { |
|
|
908 | int res,i; |
|
|
909 | psiconv_u32 *offset_ptr; |
|
|
910 | |
|
|
911 | |
|
|
912 | if (!value) { |
|
|
913 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
914 | "NULL Jumptable Section"); |
|
|
915 | res = -PSICONV_E_GENERATE; |
|
|
916 | goto ERROR; |
|
|
917 | } |
|
|
918 | if ((res = psiconv_write_u32(config,buf,psiconv_list_length(value)))) |
|
|
919 | goto ERROR; |
|
|
920 | for (i = 0; i < psiconv_list_length(value); i++) { |
|
|
921 | if (!(offset_ptr = psiconv_list_get(value,i))) { |
|
|
922 | psiconv_warn(config,0,psiconv_buffer_length(buf), |
|
|
923 | "Massive memory corruption"); |
|
|
924 | res = -PSICONV_E_NOMEM; |
|
|
925 | goto ERROR; |
|
|
926 | } |
|
|
927 | if ((res = psiconv_write_offset(config,buf,*offset_ptr))) |
|
|
928 | goto ERROR; |
|
|
929 | } |
|
|
930 | |
|
|
931 | ERROR: |
|
|
932 | return res; |
|
|
933 | } |
|
|
934 | |