/[public]/psiconv/trunk/lib/psiconv/generate_image.c
ViewVC logotype

Diff of /psiconv/trunk/lib/psiconv/generate_image.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

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

Legend:
Removed from v.168  
changed lines
  Added in v.184

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26