/[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 175 Revision 196
1/* 1/*
2
3 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
4 Copyright (c) 1999, 2000 Frodo Looijaard <frodol@dds.nl> 3 Copyright (c) 1999-2004 Frodo Looijaard <frodol@dds.nl>
5 4
6 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
7 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
8 the Free Software Foundation; either version 2 of the License, or 7 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version. 8 (at your option) any later version.
22#include "compat.h" 21#include "compat.h"
23 22
24#include "generate_routines.h" 23#include "generate_routines.h"
25#include "error.h" 24#include "error.h"
26#include "list.h" 25#include "list.h"
26#include "image.h"
27 27
28#ifdef DMALLOC 28#ifdef DMALLOC
29#include <dmalloc.h> 29#include <dmalloc.h>
30#endif 30#endif
31 31
32typedef psiconv_list psiconv_pixel_bytes; /* psiconv_u8 */
33
34typedef psiconv_list psiconv_pixel_ints; /* of psiconv_u32 */
35
36typedef struct psiconv_pixel_float_s
37{
38 psiconv_u32 length;
39 float *red;
40 float *green;
41 float *blue;
42} psiconv_pixel_floats_t;
43 32
44static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, 33static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,
45 int xsize,int ysize, 34 int xsize,int ysize,
46 const psiconv_pixel_floats_t data, 35 const psiconv_pixel_floats_t data,
47 int colordepth, 36 int colordepth,int color,
37 int redbits,int greenbits,int bluebits,
48 const psiconv_pixel_floats_t palet); 38 const psiconv_pixel_floats_t palet);
49static int psiconv_pixel_data_to_bytes(const psiconv_config config, 39static int psiconv_pixel_data_to_bytes(const psiconv_config config,
50 psiconv_pixel_bytes *bytes, int xsize, 40 psiconv_pixel_bytes *bytes, int xsize,
51 int ysize, const psiconv_pixel_ints pixels, 41 int ysize, const psiconv_pixel_ints pixels,
52 int colordepth); 42 int colordepth);
53static int psiconv_encode_rle8(const psiconv_config config, 43static int psiconv_encode_rle8(const psiconv_config config,
54 const psiconv_pixel_bytes plain_bytes, 44 const psiconv_pixel_bytes plain_bytes,
55 psiconv_pixel_bytes *encoded_bytes); 45 psiconv_pixel_bytes *encoded_bytes);
46static int psiconv_encode_rle12(const psiconv_config config,
47 const psiconv_pixel_bytes plain_bytes,
48 psiconv_pixel_bytes *encoded_bytes);
56static int psiconv_encode_rle16(const psiconv_config config, 49static int psiconv_encode_rle16(const psiconv_config config,
57 const psiconv_pixel_bytes plain_bytes, 50 const psiconv_pixel_bytes plain_bytes,
58 psiconv_pixel_bytes *encoded_bytes); 51 psiconv_pixel_bytes *encoded_bytes);
59static int psiconv_encode_rle24(const psiconv_config config, 52static int psiconv_encode_rle24(const psiconv_config config,
60 const psiconv_pixel_bytes plain_bytes, 53 const psiconv_pixel_bytes plain_bytes,
61 psiconv_pixel_bytes *encoded_bytes); 54 psiconv_pixel_bytes *encoded_bytes);
62 55
63#define PALET_GREY_2_LEN 4
64float palet_grey_2_rgb[PALET_GREY_2_LEN] = {0.0/3, 1.0/3, 2.0/3, 3.0/3};
65#define PALET_GREY_4_LEN 16
66float palet_grey_4_rgb[PALET_GREY_4_LEN] =
67 { 0.0/15, 1.0/15, 2.0/15, 3.0/15,
68 4.0/15, 5.0/15, 6.0/15, 7.0/15,
69 8.0/15, 9.0/15, 10.0/15, 11.0/15,
70 12.0/15, 13.0/15, 14.0/15, 15.0/15};
71#define PALET_NONE_LEN 0
72
73psiconv_pixel_floats_t palet_grey_2 =
74 {
75 PALET_GREY_2_LEN,
76 (float *) palet_grey_2_rgb,
77 (float *) palet_grey_2_rgb,
78 (float *) palet_grey_2_rgb
79 };
80
81psiconv_pixel_floats_t palet_grey_4 =
82 {
83 PALET_GREY_4_LEN,
84 (float *) palet_grey_4_rgb,
85 (float *) palet_grey_4_rgb,
86 (float *) palet_grey_4_rgb
87 };
88
89psiconv_pixel_floats_t palet_none =
90 {
91 PALET_NONE_LEN,
92 NULL,
93 NULL,
94 NULL
95 };
96
97
98int psiconv_write_paint_data_section(const psiconv_config config, 56int psiconv_write_paint_data_section(const psiconv_config config,
99 psiconv_buffer buf, 57 psiconv_buffer buf, int lev,
100 const psiconv_paint_data_section value, 58 const psiconv_paint_data_section value,
101 int is_clipart) 59 int is_clipart)
102{ 60{
103 int res,colordepth,i; 61 int res,colordepth,i;
104 psiconv_pixel_ints ints; 62 psiconv_pixel_ints ints;
105 psiconv_pixel_floats_t floats,palet; 63 psiconv_pixel_floats_t floats,palet;
106 psiconv_list bytes,bytes_rle; 64 psiconv_list bytes,bytes_rle;
107 psiconv_u8 *byteptr,encoding; 65 psiconv_u8 *byteptr,encoding;
108 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
107
109 if (!value) { 108 if (!value) {
110 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");
111 res = -PSICONV_E_GENERATE; 110 res = -PSICONV_E_GENERATE;
112 goto ERROR1; 111 goto ERROR1;
113 } 112 }
114 113
115 floats.red = value->red; 114 floats.red = value->red;
116 floats.green = value->green; 115 floats.green = value->green;
117 floats.blue = value->blue; 116 floats.blue = value->blue;
118 floats.length = value->xsize * value->ysize; 117 floats.length = value->xsize * value->ysize;
119 118
119 palet = psiconv_palet_none;
120 if ((config->color) && (config->redbits == 0) && (config->greenbits == 0) &&
121 (config->bluebits == 0))
120 switch (config->colordepth) { 122 switch (config->colordepth) {
121 default: 123 case 4: palet = psiconv_palet_color_4; break;
122 case 2: palet = (config->color?palet_none:palet_grey_2); 124 case 8: palet = psiconv_palet_color_8; break;
123 break; 125 default: palet = psiconv_palet_none; break;
124 case 4: palet = (config->color?palet_none:palet_grey_4);
125 break;
126 } 126 }
127 127
128 if ((res = psiconv_collect_pixel_data(&ints,value->xsize, 128 if ((res = psiconv_collect_pixel_data(&ints,value->xsize,
129 value->ysize,floats, 129 value->ysize,floats,
130 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");
131 goto ERROR1; 134 goto ERROR1;
135 }
132 136
133 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,
134 ints,config->colordepth))) 138 ints,config->colordepth))) {
139 psiconv_error(config,lev,0,"Error translating pixel data to bytes");
135 goto ERROR2; 140 goto ERROR2;
141 }
136 142
137 143
144 switch (config->colordepth) {
145 case 2:
146 case 4:
147 case 8:
138 encoding = 0x00; 148 encoding = 0x01;
139 if ((res = psiconv_encode_rle8(config,bytes,&bytes_rle))) 149 if ((res = psiconv_encode_rle8(config,bytes,&bytes_rle))) {
150 psiconv_error(config,lev,0,"Error encoding RLE8");
140 goto ERROR3; 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) {
141 if (psiconv_list_length(bytes_rle) < psiconv_list_length(bytes)) { 179 if (psiconv_list_length(bytes_rle) < psiconv_list_length(bytes)) {
142 encoding = 0x01;
143 psiconv_list_free(bytes); 180 psiconv_list_free(bytes);
144 bytes = bytes_rle; 181 bytes = bytes_rle;
145 } else { 182 } else {
146 bytes_rle = NULL; 183 psiconv_list_free(bytes_rle);
184 encoding = 0x00;
147 } 185 }
186 }
148 187
149 if ((res = psiconv_write_u32(config,buf, 188 if ((res = psiconv_write_u32(config,buf,lev+1,
150 0x28+psiconv_list_length(bytes)))) 189 0x28+psiconv_list_length(bytes))))
151 goto ERROR3; 190 goto ERROR3;
152 if ((res = psiconv_write_u32(config,buf,0x28))) 191 if ((res = psiconv_write_u32(config,buf,lev+1,0x28)))
153 goto ERROR3; 192 goto ERROR3;
154 if ((res = psiconv_write_u32(config,buf,value->xsize))) 193 if ((res = psiconv_write_u32(config,buf,lev+1,value->xsize)))
155 goto ERROR3; 194 goto ERROR3;
156 if ((res = psiconv_write_u32(config,buf,value->ysize))) 195 if ((res = psiconv_write_u32(config,buf,lev+1,value->ysize)))
157 goto ERROR3; 196 goto ERROR3;
158 if ((res = psiconv_write_length(config,buf,value->pic_xsize))) 197 if ((res = psiconv_write_length(config,buf,lev+1,value->pic_xsize)))
159 goto ERROR3; 198 goto ERROR3;
160 if ((res = psiconv_write_length(config,buf,value->pic_ysize))) 199 if ((res = psiconv_write_length(config,buf,lev+1,value->pic_ysize)))
161 goto ERROR3; 200 goto ERROR3;
162 colordepth = config->colordepth; 201 colordepth = config->colordepth;
163 if ((colordepth != 2) && colordepth != 4)
164 colordepth = 2;
165 if ((res = psiconv_write_u32(config,buf,colordepth))) 202 if ((res = psiconv_write_u32(config,buf,lev+1,colordepth)))
166 goto ERROR3; 203 goto ERROR3;
167 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))))
168 goto ERROR3; 205 goto ERROR3;
169 if ((res = psiconv_write_u32(config,buf,0))) 206 if ((res = psiconv_write_u32(config,buf,lev+1,0)))
170 goto ERROR3; 207 goto ERROR3;
171 if ((res = psiconv_write_u32(config,buf,encoding))) 208 if ((res = psiconv_write_u32(config,buf,lev+1,encoding)))
172 goto ERROR3; 209 goto ERROR3;
173 if (is_clipart) { 210 if (is_clipart) {
174 if ((res = psiconv_write_u32(config,buf,0xffffffff))) 211 if ((res = psiconv_write_u32(config,buf,lev+1,0xffffffff)))
175 goto ERROR3; 212 goto ERROR3;
176 if ((res = psiconv_write_u32(config,buf,0x00000044))) 213 if ((res = psiconv_write_u32(config,buf,lev+1,0x00000044)))
177 goto ERROR3; 214 goto ERROR3;
178 } 215 }
179 for (i = 0; i < psiconv_list_length(bytes); i++) { 216 for (i = 0; i < psiconv_list_length(bytes); i++) {
180 if (!(byteptr = psiconv_list_get(bytes,i))) 217 if (!(byteptr = psiconv_list_get(bytes,i)))
181 goto ERROR3; 218 goto ERROR3;
182 if ((res = psiconv_write_u8(config,buf,*byteptr))) 219 if ((res = psiconv_write_u8(config,buf,lev+1,*byteptr)))
183 goto ERROR3; 220 goto ERROR3;
184 } 221 }
185 222
186ERROR3: 223ERROR3:
187 psiconv_list_free(bytes); 224 psiconv_list_free(bytes);
195 The palet is optional; without it, we just use the 232 The palet is optional; without it, we just use the
196 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
197 now. For greyscale pictures, just use the palet. */ 234 now. For greyscale pictures, just use the palet. */
198int 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,
199 const psiconv_pixel_floats_t data, 236 const psiconv_pixel_floats_t data,
200 int colordepth, 237 int colordepth,int color,
238 int redbits,int bluebits,int greenbits,
201 const psiconv_pixel_floats_t palet) 239 const psiconv_pixel_floats_t palet)
202{ 240{
203 int res,x,y,i; 241 int res,x,y,i;
204 psiconv_u32 index,pixel; 242 psiconv_u32 index,pixel;
205 float p_red,p_green,p_blue,mult,dist,new_dist; 243 float p_red,p_green,p_blue,dist,new_dist;
206 244
207 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { 245 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) {
208 res = -PSICONV_E_NOMEM; 246 res = -PSICONV_E_NOMEM;
209 goto ERROR1; 247 goto ERROR1;
210 } 248 }
211 249
212 mult = 1 << colordepth;
213 for (y = 0; y < ysize; y++) { 250 for (y = 0; y < ysize; y++) {
214 for (x = 0; x < xsize; x++) { 251 for (x = 0; x < xsize; x++) {
215 index = y*xsize+x; 252 index = y*xsize+x;
216 p_red = data.red[index]; 253 p_red = data.red[index];
217 p_green = data.green[index]; 254 p_green = data.green[index];
218 p_blue = data.blue[index]; 255 p_blue = data.blue[index];
219 if (!palet.length) { 256 if (!palet.length) {
220 pixel = (((psiconv_u32) (p_red*mult+0.5)) << (2*colordepth)) + 257 if (color)
221 (((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) +
222 ((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);
223 } else { 265 } else {
224 dist = 4; /* Max distance is 3, so this is safe */ 266 dist = 4; /* Max distance is 3, so this is safe */
225 pixel = -1; 267 pixel = -1;
226 for (i = 0; i < palet.length; i++) { 268 for (i = 0; i < palet.length; i++) {
227 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]) +
258 psiconv_u32 *pixelptr; 300 psiconv_u32 *pixelptr;
259 int inputbitsleft,outputbitnr,bitsfit,outputbytenr; 301 int inputbitsleft,outputbitnr,bitsfit,outputbytenr;
260 302
261 303
262 if (!bytes) { 304 if (!bytes) {
263 psiconv_warn(config,0,0,"NULL pixel data"); 305 psiconv_error(config,0,0,"NULL pixel data");
264 res = -PSICONV_E_GENERATE; 306 res = -PSICONV_E_GENERATE;
265 goto ERROR1; 307 goto ERROR1;
266 } 308 }
267 if (!pixels) { 309 if (!pixels) {
268 psiconv_warn(config,0,0,"NULL pixel data"); 310 psiconv_error(config,0,0,"NULL pixel data");
269 res = -PSICONV_E_GENERATE; 311 res = -PSICONV_E_GENERATE;
270 goto ERROR1; 312 goto ERROR1;
271 } 313 }
272 if (psiconv_list_length(pixels) != xsize * ysize) { 314 if (psiconv_list_length(pixels) != xsize * ysize) {
273 psiconv_warn(config,0,0,"Pixel number is not correct"); 315 psiconv_error(config,0,0,"Pixel number is not correct");
274 res = -PSICONV_E_GENERATE; 316 res = -PSICONV_E_GENERATE;
275 goto ERROR1; 317 goto ERROR1;
276 } 318 }
277 319
278 if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) { 320 if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) {
285 outputbyte = 0; 327 outputbyte = 0;
286 for (y = 0; y < ysize; y++) { 328 for (y = 0; y < ysize; y++) {
287 outputbytenr = 0; 329 outputbytenr = 0;
288 for (x = 0; x < xsize; x++) { 330 for (x = 0; x < xsize; x++) {
289 if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) { 331 if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) {
290 psiconv_warn(config,0,0,"Massive internal corruption"); 332 psiconv_error(config,0,0,"Massive internal corruption");
291 res = -PSICONV_E_NOMEM; 333 res = -PSICONV_E_NOMEM;
292 goto ERROR2; 334 goto ERROR2;
293 } 335 }
294 inputbitsleft = colordepth; 336 inputbitsleft = colordepth;
295 inputdata = *pixelptr; 337 inputdata = *pixelptr;
330 psiconv_list_free(*bytes); 372 psiconv_list_free(*bytes);
331ERROR1: 373ERROR1:
332 return res; 374 return res;
333} 375}
334 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 */
335int psiconv_encode_rle8(const psiconv_config config, 381int psiconv_encode_rle8(const psiconv_config config,
336 const psiconv_pixel_bytes plain_bytes, 382 const psiconv_pixel_bytes plain_bytes,
337 psiconv_pixel_bytes *encoded_bytes) 383 psiconv_pixel_bytes *encoded_bytes)
338{ 384{
339 int res,i,j,len; 385 int res,i,j,len;
362 goto ERROR2; 408 goto ERROR2;
363 if ((res = psiconv_list_add(*encoded_bytes,next))) 409 if ((res = psiconv_list_add(*encoded_bytes,next)))
364 goto ERROR2; 410 goto ERROR2;
365 i +=2; 411 i +=2;
366 } else if (*next == *entry) { 412 } else if (*next == *entry) {
367 len = 0; 413 len = 1;
368 while ((*next == *entry) && 414 while ((*next == *entry) &&
369 (i+len + 1 < psiconv_list_length(plain_bytes)) && 415 (i+len + 2 < psiconv_list_length(plain_bytes)) &&
370 len < 0x80) { 416 len < 0x80) {
371 len ++; 417 len ++;
372 if (!(next = psiconv_list_get(plain_bytes,i+len))) { 418 if (!(next = psiconv_list_get(plain_bytes,i+len))) {
373 res = -PSICONV_E_NOMEM; 419 res = -PSICONV_E_NOMEM;
374 goto ERROR2; 420 goto ERROR2;
413 psiconv_list_free(*encoded_bytes); 459 psiconv_list_free(*encoded_bytes);
414ERROR1: 460ERROR1:
415 return res; 461 return res;
416} 462}
417 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 */
418int psiconv_encode_rle16(const psiconv_config config, 573int psiconv_encode_rle16(const psiconv_config config,
419 const psiconv_pixel_bytes plain_bytes, 574 const psiconv_pixel_bytes plain_bytes,
420 psiconv_pixel_bytes *encoded_bytes) 575 psiconv_pixel_bytes *encoded_bytes)
421{ 576{
422 int res,i,j,len; 577 int res,i,j,len;
525 psiconv_list_free(*encoded_bytes); 680 psiconv_list_free(*encoded_bytes);
526ERROR1: 681ERROR1:
527 return res; 682 return res;
528} 683}
529 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 */
530int psiconv_encode_rle24(const psiconv_config config, 689int psiconv_encode_rle24(const psiconv_config config,
531 const psiconv_pixel_bytes plain_bytes, 690 const psiconv_pixel_bytes plain_bytes,
532 psiconv_pixel_bytes *encoded_bytes) 691 psiconv_pixel_bytes *encoded_bytes)
533{ 692{
534 int res,i,j,len; 693 int res,i,j,len;
671 return res; 830 return res;
672} 831}
673 832
674 833
675int psiconv_write_sketch_section(const psiconv_config config, 834int psiconv_write_sketch_section(const psiconv_config config,
676 psiconv_buffer buf, 835 psiconv_buffer buf, int lev,
677 const psiconv_sketch_section value) 836 const psiconv_sketch_section value)
678{ 837{
679 int res; 838 int res;
680 839
840 psiconv_progress(config,lev,0,"Writing sketch section");
681 if (!value) { 841 if (!value) {
682 psiconv_warn(config,0,0,"NULL sketch section"); 842 psiconv_error(config,0,0,"NULL sketch section");
683 res = -PSICONV_E_GENERATE; 843 res = -PSICONV_E_GENERATE;
684 goto ERROR1; 844 goto ERROR1;
685 } 845 }
686 846
687 if ((res = psiconv_write_u16(config,buf,value->displayed_xsize))) 847 if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_xsize)))
688 goto ERROR1; 848 goto ERROR1;
689 if ((res = psiconv_write_u16(config,buf,value->displayed_ysize))) 849 if ((res = psiconv_write_u16(config,buf,lev+1,value->displayed_ysize)))
690 goto ERROR1; 850 goto ERROR1;
691 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)))
692 goto ERROR1; 852 goto ERROR1;
693 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)))
694 goto ERROR1; 854 goto ERROR1;
695 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)))
696 goto ERROR1; 856 goto ERROR1;
697 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)))
698 goto ERROR1; 858 goto ERROR1;
699 if ((res = psiconv_write_u16(config,buf,value->form_xsize))) 859 if ((res = psiconv_write_u16(config,buf,lev+1,value->form_xsize)))
700 goto ERROR1; 860 goto ERROR1;
701 if ((res = psiconv_write_u16(config,buf,value->form_ysize))) 861 if ((res = psiconv_write_u16(config,buf,lev+1,value->form_ysize)))
702 goto ERROR1; 862 goto ERROR1;
703 if ((res = psiconv_write_u16(config,buf,0x0000))) 863 if ((res = psiconv_write_u16(config,buf,lev+1,0x0000)))
704 goto ERROR1; 864 goto ERROR1;
705 if ((res = psiconv_write_paint_data_section(config,buf,value->picture,0))) 865 if ((res = psiconv_write_paint_data_section(config,buf,lev+1,value->picture,0)))
706 goto ERROR1; 866 goto ERROR1;
707 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)))
708 goto ERROR1; 868 goto ERROR1;
709 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)))
710 goto ERROR1; 870 goto ERROR1;
711 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 *
712 value->displayed_xsize))) 872 value->displayed_xsize)))
713 goto ERROR1; 873 goto ERROR1;
714 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 *
715 value->displayed_xsize))) 875 value->displayed_xsize)))
716 goto ERROR1; 876 goto ERROR1;
717 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 *
718 value->displayed_ysize))) 878 value->displayed_ysize)))
719 goto ERROR1; 879 goto ERROR1;
720 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 *
721 value->displayed_ysize))) 881 value->displayed_ysize)))
722 goto ERROR1; 882 goto ERROR1;
723 883
724ERROR1: 884ERROR1:
725 return res; 885 return res;
726} 886}
727 887
728int psiconv_write_clipart_section(const psiconv_config config, 888int psiconv_write_clipart_section(const psiconv_config config,
729 psiconv_buffer buf, 889 psiconv_buffer buf, int lev,
730 const psiconv_clipart_section value) 890 const psiconv_clipart_section value)
731{ 891{
732 int res; 892 int res;
733 893
734 894 psiconv_progress(config,lev,0,"Writing clipart section");
735 if (!value) { 895 if (!value) {
736 psiconv_warn(config,0,psiconv_buffer_length(buf), 896 psiconv_error(config,0,psiconv_buffer_length(buf),
737 "NULL Clipart Section"); 897 "NULL Clipart Section");
738 res = -PSICONV_E_GENERATE; 898 res = -PSICONV_E_GENERATE;
739 goto ERROR; 899 goto ERROR;
740 } 900 }
741 if ((res = psiconv_write_u32(config,buf,PSICONV_ID_CLIPART_ITEM))) 901 if ((res = psiconv_write_u32(config,buf,lev+1,PSICONV_ID_CLIPART_ITEM)))
742 goto ERROR; 902 goto ERROR;
743 if ((res = psiconv_write_u32(config,buf,0x00000002))) 903 if ((res = psiconv_write_u32(config,buf,lev+1,0x00000002)))
744 goto ERROR; 904 goto ERROR;
745 if ((res = psiconv_write_u32(config,buf,0x00000000))) 905 if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000)))
746 goto ERROR; 906 goto ERROR;
747 if ((res = psiconv_write_u32(config,buf,0x00000000))) 907 if ((res = psiconv_write_u32(config,buf,lev+1,0x00000000)))
748 goto ERROR; 908 goto ERROR;
749 if ((res = psiconv_write_u32(config,buf,0x0000000C))) 909 if ((res = psiconv_write_u32(config,buf,lev+1,0x0000000C)))
750 goto ERROR; 910 goto ERROR;
751 if ((res = psiconv_write_paint_data_section(config,buf,value->picture,1))) 911 if ((res = psiconv_write_paint_data_section(config,buf,lev+1,value->picture,1)))
752 goto ERROR; 912 goto ERROR;
753 913
754ERROR: 914ERROR:
755 return res; 915 return res;
756} 916}
757 917
758int psiconv_write_jumptable_section(const psiconv_config config, 918int psiconv_write_jumptable_section(const psiconv_config config,
759 psiconv_buffer buf, 919 psiconv_buffer buf, int lev,
760 const psiconv_jumptable_section value) 920 const psiconv_jumptable_section value)
761{ 921{
762 int res,i; 922 int res,i;
763 psiconv_u32 *offset_ptr; 923 psiconv_u32 *offset_ptr;
764 924
925 psiconv_progress(config,lev,0,"Writing jumptable section");
765 926
766 if (!value) { 927 if (!value) {
767 psiconv_warn(config,0,psiconv_buffer_length(buf), 928 psiconv_error(config,0,psiconv_buffer_length(buf),
768 "NULL Jumptable Section"); 929 "NULL Jumptable Section");
769 res = -PSICONV_E_GENERATE; 930 res = -PSICONV_E_GENERATE;
770 goto ERROR; 931 goto ERROR;
771 } 932 }
772 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))))
773 goto ERROR; 934 goto ERROR;
774 for (i = 0; i < psiconv_list_length(value); i++) { 935 for (i = 0; i < psiconv_list_length(value); i++) {
775 if (!(offset_ptr = psiconv_list_get(value,i))) { 936 if (!(offset_ptr = psiconv_list_get(value,i))) {
776 psiconv_warn(config,0,psiconv_buffer_length(buf), 937 psiconv_error(config,0,psiconv_buffer_length(buf),
777 "Massive memory corruption"); 938 "Massive memory corruption");
778 res = -PSICONV_E_NOMEM; 939 res = -PSICONV_E_NOMEM;
779 goto ERROR; 940 goto ERROR;
780 } 941 }
781 if ((res = psiconv_write_offset(config,buf,*offset_ptr))) 942 if ((res = psiconv_write_offset(config,buf,lev+1,*offset_ptr)))
782 goto ERROR; 943 goto ERROR;
783 } 944 }
784 945
785ERROR: 946ERROR:
786 return res; 947 return res;

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

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