/[public]/psiconv/tags/rel-0-9-9/lib/psiconv/generate_image.c
ViewVC logotype

Diff of /psiconv/tags/rel-0-9-9/lib/psiconv/generate_image.c

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

Revision 171 Revision 196
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
30typedef psiconv_list psiconv_pixel_bytes; /* psiconv_u8 */
31
32typedef psiconv_list psiconv_pixel_ints; /* of psiconv_u32 */
33
34typedef struct psiconv_pixel_float_s
35{
36 psiconv_u32 length;
37 float *red;
38 float *green;
39 float *blue;
40} psiconv_pixel_floats_t;
41 32
42static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, 33static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,
43 int xsize,int ysize, 34 int xsize,int ysize,
44 const psiconv_pixel_floats_t data, 35 const psiconv_pixel_floats_t data,
45 int colordepth, 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);
47static int psiconv_pixel_data_to_bytes(const psiconv_config config, 39static int psiconv_pixel_data_to_bytes(const psiconv_config config,
48 psiconv_pixel_bytes *bytes, int xsize, 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);
51 43static int psiconv_encode_rle8(const psiconv_config config,
52#define PALET_GREY_2_LEN 4 44 const psiconv_pixel_bytes plain_bytes,
53float palet_grey_2_rgb[PALET_GREY_2_LEN] = {0.0/3, 1.0/3, 2.0/3, 3.0/3}; 45 psiconv_pixel_bytes *encoded_bytes);
54#define PALET_GREY_4_LEN 16 46static int psiconv_encode_rle12(const psiconv_config config,
55float palet_grey_4_rgb[PALET_GREY_4_LEN] = 47 const psiconv_pixel_bytes plain_bytes,
56 { 0.0/15, 1.0/15, 2.0/15, 3.0/15, 48 psiconv_pixel_bytes *encoded_bytes);
57 4.0/15, 5.0/15, 6.0/15, 7.0/15, 49static int psiconv_encode_rle16(const psiconv_config config,
58 8.0/15, 9.0/15, 10.0/15, 11.0/15, 50 const psiconv_pixel_bytes plain_bytes,
59 12.0/15, 13.0/15, 14.0/15, 15.0/15}; 51 psiconv_pixel_bytes *encoded_bytes);
60#define PALET_NONE_LEN 0 52static int psiconv_encode_rle24(const psiconv_config config,
61 53 const psiconv_pixel_bytes plain_bytes,
62psiconv_pixel_floats_t palet_grey_2 = 54 psiconv_pixel_bytes *encoded_bytes);
63 {
64 PALET_GREY_2_LEN,
65 (float *) palet_grey_2_rgb,
66 (float *) palet_grey_2_rgb,
67 (float *) palet_grey_2_rgb
68 };
69
70psiconv_pixel_floats_t palet_grey_4 =
71 {
72 PALET_GREY_4_LEN,
73 (float *) palet_grey_4_rgb,
74 (float *) palet_grey_4_rgb,
75 (float *) palet_grey_4_rgb
76 };
77
78psiconv_pixel_floats_t palet_none =
79 {
80 PALET_NONE_LEN,
81 NULL,
82 NULL,
83 NULL
84 };
85
86 55
87int psiconv_write_paint_data_section(const psiconv_config config, 56int psiconv_write_paint_data_section(const psiconv_config config,
88 psiconv_buffer buf, 57 psiconv_buffer buf, int lev,
89 const psiconv_paint_data_section value) 58 const psiconv_paint_data_section value,
59 int is_clipart)
90{ 60{
91 int res,colordepth,i; 61 int res,colordepth,i;
92 psiconv_pixel_ints ints; 62 psiconv_pixel_ints ints;
93 psiconv_pixel_floats_t floats,palet; 63 psiconv_pixel_floats_t floats,palet;
94 psiconv_list bytes; 64 psiconv_list bytes,bytes_rle;
95 psiconv_u8 *byteptr; 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
96 107
97 if (!value) { 108 if (!value) {
98 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");
99 res = -PSICONV_E_GENERATE; 110 res = -PSICONV_E_GENERATE;
100 goto ERROR1; 111 goto ERROR1;
101 } 112 }
102 113
103 floats.red = value->red; 114 floats.red = value->red;
104 floats.green = value->green; 115 floats.green = value->green;
105 floats.blue = value->blue; 116 floats.blue = value->blue;
106 floats.length = value->xsize * value->ysize; 117 floats.length = value->xsize * value->ysize;
107 118
119 palet = psiconv_palet_none;
120 if ((config->color) && (config->redbits == 0) && (config->greenbits == 0) &&
121 (config->bluebits == 0))
108 switch (config->colordepth) { 122 switch (config->colordepth) {
109 default: 123 case 4: palet = psiconv_palet_color_4; break;
110 case 2: palet = (config->color?palet_none:palet_grey_2); 124 case 8: palet = psiconv_palet_color_8; break;
111 break; 125 default: palet = psiconv_palet_none; break;
112 case 4: palet = (config->color?palet_none:palet_grey_4);
113 break;
114 } 126 }
115 127
116 if ((res = psiconv_collect_pixel_data(&ints,value->xsize, 128 if ((res = psiconv_collect_pixel_data(&ints,value->xsize,
117 value->ysize,floats, 129 value->ysize,floats,
118 config->colordepth,palet))) 130 config->colordepth,config->color,
131 config->redbits,config->greenbits,
132 config->bluebits,palet))) {
133 psiconv_error(config,lev,0,"Error collecting pixel data");
119 goto ERROR1; 134 goto ERROR1;
135 }
120 136
121 if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, 137 if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize,
122 ints,config->colordepth))) 138 ints,config->colordepth))) {
139 psiconv_error(config,lev,0,"Error translating pixel data to bytes");
123 goto ERROR2; 140 goto ERROR2;
141 }
124 142
125 if ((res = psiconv_write_u32(config,buf,0x28+psiconv_list_length(bytes)))) 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;
185 }
186 }
187
188 if ((res = psiconv_write_u32(config,buf,lev+1,
189 0x28+psiconv_list_length(bytes))))
126 goto ERROR3; 190 goto ERROR3;
127 if ((res = psiconv_write_u32(config,buf,0x28))) 191 if ((res = psiconv_write_u32(config,buf,lev+1,0x28)))
128 goto ERROR3; 192 goto ERROR3;
129 if ((res = psiconv_write_u32(config,buf,value->xsize))) 193 if ((res = psiconv_write_u32(config,buf,lev+1,value->xsize)))
130 goto ERROR3; 194 goto ERROR3;
131 if ((res = psiconv_write_u32(config,buf,value->ysize))) 195 if ((res = psiconv_write_u32(config,buf,lev+1,value->ysize)))
132 goto ERROR3; 196 goto ERROR3;
133 if ((res = psiconv_write_length(config,buf,value->pic_xsize))) 197 if ((res = psiconv_write_length(config,buf,lev+1,value->pic_xsize)))
134 goto ERROR3; 198 goto ERROR3;
135 if ((res = psiconv_write_length(config,buf,value->pic_ysize))) 199 if ((res = psiconv_write_length(config,buf,lev+1,value->pic_ysize)))
136 goto ERROR3; 200 goto ERROR3;
137 colordepth = config->colordepth; 201 colordepth = config->colordepth;
138 if ((colordepth != 2) && colordepth != 4)
139 colordepth = 2;
140 if ((res = psiconv_write_u32(config,buf,colordepth))) 202 if ((res = psiconv_write_u32(config,buf,lev+1,colordepth)))
141 goto ERROR3; 203 goto ERROR3;
142 if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) 204 if ((res = psiconv_write_u32(config,buf,lev+1,(config->color?1:0))))
143 goto ERROR3; 205 goto ERROR3;
144 if ((res = psiconv_write_u32(config,buf,0))) 206 if ((res = psiconv_write_u32(config,buf,lev+1,0)))
145 goto ERROR3; 207 goto ERROR3;
146 /* Encoding: no RLE for now */
147 if ((res = psiconv_write_u32(config,buf,0))) 208 if ((res = psiconv_write_u32(config,buf,lev+1,encoding)))
148 goto ERROR3; 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 }
149 for (i = 0; i < psiconv_list_length(bytes); i++) { 216 for (i = 0; i < psiconv_list_length(bytes); i++) {
150 if (!(byteptr = psiconv_list_get(bytes,i))) 217 if (!(byteptr = psiconv_list_get(bytes,i)))
151 goto ERROR3; 218 goto ERROR3;
152 if ((res = psiconv_write_u8(config,buf,*byteptr))) 219 if ((res = psiconv_write_u8(config,buf,lev+1,*byteptr)))
153 goto ERROR3; 220 goto ERROR3;
154 } 221 }
155 222
156ERROR3: 223ERROR3:
157 psiconv_list_free(bytes); 224 psiconv_list_free(bytes);
165 The palet is optional; without it, we just use the 232 The palet is optional; without it, we just use the
166 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
167 now. For greyscale pictures, just use the palet. */ 234 now. For greyscale pictures, just use the palet. */
168int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize, 235int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize,
169 const psiconv_pixel_floats_t data, 236 const psiconv_pixel_floats_t data,
170 int colordepth, 237 int colordepth,int color,
238 int redbits,int bluebits,int greenbits,
171 const psiconv_pixel_floats_t palet) 239 const psiconv_pixel_floats_t palet)
172{ 240{
173 int res,x,y,i; 241 int res,x,y,i;
174 psiconv_u32 index,pixel; 242 psiconv_u32 index,pixel;
175 float p_red,p_green,p_blue,mult,dist,new_dist; 243 float p_red,p_green,p_blue,dist,new_dist;
176 244
177 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { 245 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) {
178 res = -PSICONV_E_NOMEM; 246 res = -PSICONV_E_NOMEM;
179 goto ERROR1; 247 goto ERROR1;
180 } 248 }
181 249
182 mult = 1 << colordepth;
183 for (y = 0; y < ysize; y++) { 250 for (y = 0; y < ysize; y++) {
184 for (x = 0; x < xsize; x++) { 251 for (x = 0; x < xsize; x++) {
185 index = y*xsize+x; 252 index = y*xsize+x;
186 p_red = data.red[index]; 253 p_red = data.red[index];
187 p_green = data.green[index]; 254 p_green = data.green[index];
188 p_blue = data.blue[index]; 255 p_blue = data.blue[index];
189 if (!palet.length) { 256 if (!palet.length) {
190 pixel = (((psiconv_u32) (p_red*mult+0.5)) << (2*colordepth)) + 257 if (color)
191 (((psiconv_u32) (p_green*mult+0.5)) << colordepth) + 258 pixel = (((psiconv_u32) (p_red * (1 << redbits) + 0.5))
259 << (greenbits+bluebits)) +
260 (((psiconv_u32) (p_green * (1 << greenbits) + 0.5))
261 << bluebits) +
192 ((psiconv_u32) (p_blue*mult+0.5)); 262 ((psiconv_u32) (p_blue * (1 << bluebits) + 0.5));
263 else
264 pixel = (p_red + p_green + p_blue)/3.0 * (1 << colordepth);
193 } else { 265 } else {
194 dist = 4; /* Max distance is 3, so this is safe */ 266 dist = 4; /* Max distance is 3, so this is safe */
195 pixel = -1; 267 pixel = -1;
196 for (i = 0; i < palet.length; i++) { 268 for (i = 0; i < palet.length; i++) {
197 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]) +
228 psiconv_u32 *pixelptr; 300 psiconv_u32 *pixelptr;
229 int inputbitsleft,outputbitnr,bitsfit,outputbytenr; 301 int inputbitsleft,outputbitnr,bitsfit,outputbytenr;
230 302
231 303
232 if (!bytes) { 304 if (!bytes) {
233 psiconv_warn(config,0,0,"NULL pixel data"); 305 psiconv_error(config,0,0,"NULL pixel data");
234 res = -PSICONV_E_GENERATE; 306 res = -PSICONV_E_GENERATE;
235 goto ERROR1; 307 goto ERROR1;
236 } 308 }
237 if (!pixels) { 309 if (!pixels) {
238 psiconv_warn(config,0,0,"NULL pixel data"); 310 psiconv_error(config,0,0,"NULL pixel data");
239 res = -PSICONV_E_GENERATE; 311 res = -PSICONV_E_GENERATE;
240 goto ERROR1; 312 goto ERROR1;
241 } 313 }
242 if (psiconv_list_length(pixels) != xsize * ysize) { 314 if (psiconv_list_length(pixels) != xsize * ysize) {
243 psiconv_warn(config,0,0,"Pixel number is not correct"); 315 psiconv_error(config,0,0,"Pixel number is not correct");
244 res = -PSICONV_E_GENERATE; 316 res = -PSICONV_E_GENERATE;
245 goto ERROR1; 317 goto ERROR1;
246 } 318 }
247 319
248 if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) { 320 if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) {
255 outputbyte = 0; 327 outputbyte = 0;
256 for (y = 0; y < ysize; y++) { 328 for (y = 0; y < ysize; y++) {
257 outputbytenr = 0; 329 outputbytenr = 0;
258 for (x = 0; x < xsize; x++) { 330 for (x = 0; x < xsize; x++) {
259 if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) { 331 if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) {
260 psiconv_warn(config,0,0,"Massive internal corruption"); 332 psiconv_error(config,0,0,"Massive internal corruption");
261 res = -PSICONV_E_NOMEM; 333 res = -PSICONV_E_NOMEM;
262 goto ERROR2; 334 goto ERROR2;
263 } 335 }
264 inputbitsleft = colordepth; 336 inputbitsleft = colordepth;
265 inputdata = *pixelptr; 337 inputdata = *pixelptr;
300 psiconv_list_free(*bytes); 372 psiconv_list_free(*bytes);
301ERROR1: 373ERROR1:
302 return res; 374 return res;
303} 375}
304 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 */
381int 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
458ERROR2:
459 psiconv_list_free(*encoded_bytes);
460ERROR1:
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 */
467int 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
561ERROR3:
562 psiconv_list_free(*encoded_bytes);
563ERROR2:
564 psiconv_list_free(data);
565ERROR1:
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 */
573int 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
679ERROR2:
680 psiconv_list_free(*encoded_bytes);
681ERROR1:
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 */
689int 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
827ERROR2:
828 psiconv_list_free(*encoded_bytes);
829ERROR1:
830 return res;
831}
832
833
305int psiconv_write_sketch_section(const psiconv_config config, 834int psiconv_write_sketch_section(const psiconv_config config,
306 psiconv_buffer buf, 835 psiconv_buffer buf, int lev,
307 const psiconv_sketch_section value) 836 const psiconv_sketch_section value)
308{ 837{
309 int res; 838 int res;
310 839
840 psiconv_progress(config,lev,0,"Writing sketch section");
311 if (!value) { 841 if (!value) {
312 psiconv_warn(config,0,0,"NULL sketch section"); 842 psiconv_error(config,0,0,"NULL sketch section");
313 res = -PSICONV_E_GENERATE; 843 res = -PSICONV_E_GENERATE;
314 goto ERROR1; 844 goto ERROR1;
315 } 845 }
316 846
317 if ((res = psiconv_write_u16(config,buf,value->displayed_xsize))) 847 if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_xsize)))
318 goto ERROR1; 848 goto ERROR1;
319 if ((res = psiconv_write_u16(config,buf,value->displayed_ysize))) 849 if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_ysize)))
320 goto ERROR1; 850 goto ERROR1;
321 if ((res = psiconv_write_u16(config,buf,value->picture_data_x_offset))) 851 if ((res = psiconv_write_u16(config,buf,lev+1,value->picture_data_x_offset)))
322 goto ERROR1; 852 goto ERROR1;
323 if ((res = psiconv_write_u16(config,buf,value->picture_data_y_offset))) 853 if ((res = psiconv_write_u16(config,buf,lev+1,value->picture_data_y_offset)))
324 goto ERROR1; 854 goto ERROR1;
325 if ((res = psiconv_write_u16(config,buf,value->displayed_size_x_offset))) 855 if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_size_x_offset)))
326 goto ERROR1; 856 goto ERROR1;
327 if ((res = psiconv_write_u16(config,buf,value->displayed_size_y_offset))) 857 if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_size_y_offset)))
328 goto ERROR1; 858 goto ERROR1;
329 if ((res = psiconv_write_u16(config,buf,value->form_xsize))) 859 if ((res = psiconv_write_u16(config,buf,lev+1,value->form_xsize)))
330 goto ERROR1; 860 goto ERROR1;
331 if ((res = psiconv_write_u16(config,buf,value->form_ysize))) 861 if ((res = psiconv_write_u16(config,buf,lev+1,value->form_ysize)))
332 goto ERROR1; 862 goto ERROR1;
333 if ((res = psiconv_write_u16(config,buf,0x0000))) 863 if ((res = psiconv_write_u16(config,buf,lev+1,0x0000)))
334 goto ERROR1; 864 goto ERROR1;
335 if ((res = psiconv_write_paint_data_section(config,buf,value->picture))) 865 if ((res = psiconv_write_paint_data_section(config,buf,lev+1,value->picture,0)))
336 goto ERROR1; 866 goto ERROR1;
337 if ((res = psiconv_write_u16(config,buf,value->magnification_x * 0x03e8))) 867 if ((res = psiconv_write_u16(config,buf,lev+1,value->magnification_x * 0x03e8)))
338 goto ERROR1; 868 goto ERROR1;
339 if ((res = psiconv_write_u16(config,buf,value->magnification_y * 0x03e8))) 869 if ((res = psiconv_write_u16(config,buf,lev+1,value->magnification_y * 0x03e8)))
340 goto ERROR1; 870 goto ERROR1;
341 if ((res = psiconv_write_u32(config,buf,value->cut_left * 0x0c * 871 if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_left * 0x0c *
342 value->displayed_xsize))) 872 value->displayed_xsize)))
343 goto ERROR1; 873 goto ERROR1;
344 if ((res = psiconv_write_u32(config,buf,value->cut_right * 0x0c * 874 if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_right * 0x0c *
345 value->displayed_xsize))) 875 value->displayed_xsize)))
346 goto ERROR1; 876 goto ERROR1;
347 if ((res = psiconv_write_u32(config,buf,value->cut_top * 0x0c * 877 if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_top * 0x0c *
348 value->displayed_ysize))) 878 value->displayed_ysize)))
349 goto ERROR1; 879 goto ERROR1;
350 if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c * 880 if ((res = psiconv_write_u32(config,buf,lev+1,value->cut_bottom * 0x0c *
351 value->displayed_ysize))) 881 value->displayed_ysize)))
352 goto ERROR1; 882 goto ERROR1;
353 883
354ERROR1: 884ERROR1:
355 return res; 885 return res;
356} 886}
357 887
888int 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
914ERROR:
915 return res;
916}
917
358int psiconv_write_jumptable_section(const psiconv_config config, 918int psiconv_write_jumptable_section(const psiconv_config config,
359 psiconv_buffer buf, 919 psiconv_buffer buf, int lev,
360 const psiconv_jumptable_section value) 920 const psiconv_jumptable_section value)
361{ 921{
362 int res,i; 922 int res,i;
363 psiconv_u32 *offset_ptr; 923 psiconv_u32 *offset_ptr;
364 924
925 psiconv_progress(config,lev,0,"Writing jumptable section");
365 926
366 if (!value) { 927 if (!value) {
367 psiconv_warn(config,0,psiconv_buffer_length(buf), 928 psiconv_error(config,0,psiconv_buffer_length(buf),
368 "NULL Jumptable Section"); 929 "NULL Jumptable Section");
369 res = -PSICONV_E_GENERATE; 930 res = -PSICONV_E_GENERATE;
370 goto ERROR; 931 goto ERROR;
371 } 932 }
372 if ((res = psiconv_write_u32(config,buf,psiconv_list_length(value)))) 933 if ((res = psiconv_write_u32(config,buf,lev+1,psiconv_list_length(value))))
373 goto ERROR; 934 goto ERROR;
374 for (i = 0; i < psiconv_list_length(value); i++) { 935 for (i = 0; i < psiconv_list_length(value); i++) {
375 if (!(offset_ptr = psiconv_list_get(value,i))) { 936 if (!(offset_ptr = psiconv_list_get(value,i))) {
376 psiconv_warn(config,0,psiconv_buffer_length(buf), 937 psiconv_error(config,0,psiconv_buffer_length(buf),
377 "Massive memory corruption"); 938 "Massive memory corruption");
378 res = -PSICONV_E_NOMEM; 939 res = -PSICONV_E_NOMEM;
379 goto ERROR; 940 goto ERROR;
380 } 941 }
381 if ((res = psiconv_write_offset(config,buf,*offset_ptr))) 942 if ((res = psiconv_write_offset(config,buf,lev+1,*offset_ptr)))
382 goto ERROR; 943 goto ERROR;
383 } 944 }
384 945
385ERROR: 946ERROR:
386 return res; 947 return res;
387} 948}
949

Legend:
Removed from v.171  
changed lines
  Added in v.196

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