/[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 170 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
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 33
42static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, 34static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,
43 int xsize,int ysize, 35 int xsize,int ysize,
44 const psiconv_pixel_floats_t data, 36 const psiconv_pixel_floats_t data,
45 int colordepth, 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);
47static int psiconv_pixel_data_to_bytes(const psiconv_config config, 40static int psiconv_pixel_data_to_bytes(const psiconv_config config,
48 psiconv_pixel_bytes *bytes, int xsize, 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);
51 44static int psiconv_encode_rle8(const psiconv_config config,
52#define PALET_GREY_2_LEN 4 45 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}; 46 psiconv_pixel_bytes *encoded_bytes);
54#define PALET_GREY_4_LEN 16 47static int psiconv_encode_rle12(const psiconv_config config,
55float palet_grey_4_rgb[PALET_GREY_4_LEN] = 48 const psiconv_pixel_bytes plain_bytes,
56 { 0.0/15, 1.0/15, 2.0/15, 3.0/15, 49 psiconv_pixel_bytes *encoded_bytes);
57 4.0/15, 5.0/15, 6.0/15, 7.0/15, 50static int psiconv_encode_rle16(const psiconv_config config,
58 8.0/15, 9.0/15, 10.0/15, 11.0/15, 51 const psiconv_pixel_bytes plain_bytes,
59 12.0/15, 13.0/15, 14.0/15, 15.0/15}; 52 psiconv_pixel_bytes *encoded_bytes);
60#define PALET_NONE_LEN 0 53static int psiconv_encode_rle24(const psiconv_config config,
61 54 const psiconv_pixel_bytes plain_bytes,
62psiconv_pixel_floats_t palet_grey_2 = 55 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 56
87int psiconv_write_paint_data_section(const psiconv_config config, 57int psiconv_write_paint_data_section(const psiconv_config config,
88 psiconv_buffer buf, 58 psiconv_buffer buf, int lev,
89 const psiconv_paint_data_section value) 59 const psiconv_paint_data_section value,
60 int is_clipart)
90{ 61{
91 int res,colordepth,i; 62 int res,colordepth,i;
92 psiconv_pixel_ints ints; 63 psiconv_pixel_ints ints;
93 psiconv_pixel_floats_t floats,palet; 64 psiconv_pixel_floats_t floats,palet;
94 psiconv_list bytes; 65 psiconv_list bytes,bytes_rle;
95 psiconv_u8 *byteptr; 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
96 108
97 if (!value) { 109 if (!value) {
98 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");
99 res = -PSICONV_E_GENERATE; 111 res = -PSICONV_E_GENERATE;
100 goto ERROR1; 112 goto ERROR1;
101 } 113 }
102 114
103 floats.red = value->red; 115 floats.red = value->red;
104 floats.green = value->green; 116 floats.green = value->green;
105 floats.blue = value->blue; 117 floats.blue = value->blue;
106 floats.length = value->xsize * value->ysize; 118 floats.length = value->xsize * value->ysize;
107 119
120 palet = psiconv_palet_none;
121 if ((config->color) && (config->redbits == 0) && (config->greenbits == 0) &&
122 (config->bluebits == 0))
108 switch (config->colordepth) { 123 switch (config->colordepth) {
109 default: 124 case 4: palet = psiconv_palet_color_4; break;
110 case 2: palet = (config->color?palet_none:palet_grey_2); 125 case 8: palet = psiconv_palet_color_8; break;
111 break; 126 default: palet = psiconv_palet_none; break;
112 case 4: palet = (config->color?palet_none:palet_grey_4);
113 break;
114 } 127 }
115 128
116 if ((res = psiconv_collect_pixel_data(&ints,value->xsize, 129 if ((res = psiconv_collect_pixel_data(&ints,value->xsize,
117 value->ysize,floats, 130 value->ysize,floats,
118 config->colordepth,palet))) 131 config->colordepth,config->color,
132 config->redbits,config->greenbits,
133 config->bluebits,palet))) {
134 psiconv_error(config,lev,0,"Error collecting pixel data");
119 goto ERROR1; 135 goto ERROR1;
136 }
120 137
121 if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, 138 if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize,
122 ints,config->colordepth))) 139 ints,config->colordepth))) {
140 psiconv_error(config,lev,0,"Error translating pixel data to bytes");
123 goto ERROR2; 141 goto ERROR2;
142 }
124 143
125 if ((res = psiconv_write_u32(config,buf,0x28+psiconv_list_length(bytes)))) 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;
186 }
187 }
188
189 if ((res = psiconv_write_u32(config,buf,lev+1,
190 0x28+psiconv_list_length(bytes))))
126 goto ERROR3; 191 goto ERROR3;
127 if ((res = psiconv_write_u32(config,buf,0x28))) 192 if ((res = psiconv_write_u32(config,buf,lev+1,0x28)))
128 goto ERROR3; 193 goto ERROR3;
129 if ((res = psiconv_write_u32(config,buf,value->xsize))) 194 if ((res = psiconv_write_u32(config,buf,lev+1,value->xsize)))
130 goto ERROR3; 195 goto ERROR3;
131 if ((res = psiconv_write_u32(config,buf,value->ysize))) 196 if ((res = psiconv_write_u32(config,buf,lev+1,value->ysize)))
132 goto ERROR3; 197 goto ERROR3;
133 if ((res = psiconv_write_length(config,buf,value->pic_xsize))) 198 if ((res = psiconv_write_length(config,buf,lev+1,value->pic_xsize)))
134 goto ERROR3; 199 goto ERROR3;
135 if ((res = psiconv_write_length(config,buf,value->pic_ysize))) 200 if ((res = psiconv_write_length(config,buf,lev+1,value->pic_ysize)))
136 goto ERROR3; 201 goto ERROR3;
137 colordepth = config->colordepth; 202 colordepth = config->colordepth;
138 if ((colordepth != 2) && colordepth != 4)
139 colordepth = 2;
140 if ((res = psiconv_write_u32(config,buf,colordepth))) 203 if ((res = psiconv_write_u32(config,buf,lev+1,colordepth)))
141 goto ERROR3; 204 goto ERROR3;
142 if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) 205 if ((res = psiconv_write_u32(config,buf,lev+1,(config->color?1:0))))
143 goto ERROR3; 206 goto ERROR3;
144 if ((res = psiconv_write_u32(config,buf,0))) 207 if ((res = psiconv_write_u32(config,buf,lev+1,0)))
145 goto ERROR3; 208 goto ERROR3;
146 /* Encoding: no RLE for now */
147 if ((res = psiconv_write_u32(config,buf,0))) 209 if ((res = psiconv_write_u32(config,buf,lev+1,encoding)))
148 goto ERROR3; 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 }
149 for (i = 0; i < psiconv_list_length(bytes); i++) { 217 for (i = 0; i < psiconv_list_length(bytes); i++) {
150 if (!(byteptr = psiconv_list_get(bytes,i))) 218 if (!(byteptr = psiconv_list_get(bytes,i)))
151 goto ERROR3; 219 goto ERROR3;
152 if ((res = psiconv_write_u8(config,buf,*byteptr))) 220 if ((res = psiconv_write_u8(config,buf,lev+1,*byteptr)))
153 goto ERROR3; 221 goto ERROR3;
154 } 222 }
155 223
156ERROR3: 224ERROR3:
157 psiconv_list_free(bytes); 225 psiconv_list_free(bytes);
165 The palet is optional; without it, we just use the 233 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 234 colordepth. With a large palet this is not very fast, but it will do for
167 now. For greyscale pictures, just use the palet. */ 235 now. For greyscale pictures, just use the palet. */
168int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize, 236int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize,
169 const psiconv_pixel_floats_t data, 237 const psiconv_pixel_floats_t data,
170 int colordepth, 238 int colordepth,int color,
239 int redbits,int bluebits,int greenbits,
171 const psiconv_pixel_floats_t palet) 240 const psiconv_pixel_floats_t palet)
172{ 241{
173 int res,x,y,i; 242 int res,x,y,i;
174 psiconv_u32 index,pixel; 243 psiconv_u32 index,pixel;
175 float p_red,p_green,p_blue,mult,dist,new_dist; 244 float p_red,p_green,p_blue,dist,new_dist;
176 245
177 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { 246 if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) {
178 res = -PSICONV_E_NOMEM; 247 res = -PSICONV_E_NOMEM;
179 goto ERROR1; 248 goto ERROR1;
180 } 249 }
181 250
182 mult = 1 << colordepth;
183 for (y = 0; y < ysize; y++) { 251 for (y = 0; y < ysize; y++) {
184 for (x = 0; x < xsize; x++) { 252 for (x = 0; x < xsize; x++) {
185 index = y*xsize+x; 253 index = y*xsize+x;
186 p_red = data.red[index]; 254 p_red = data.red[index];
187 p_green = data.green[index]; 255 p_green = data.green[index];
188 p_blue = data.blue[index]; 256 p_blue = data.blue[index];
189 if (!palet.length) { 257 if (!palet.length) {
190 pixel = (((psiconv_u32) (p_red*mult+0.5)) << (2*colordepth)) + 258 if (color)
191 (((psiconv_u32) (p_green*mult+0.5)) << colordepth) + 259 pixel = (((psiconv_u32) (p_red * (1 << redbits) + 0.5))
260 << (greenbits+bluebits)) +
261 (((psiconv_u32) (p_green * (1 << greenbits) + 0.5))
262 << bluebits) +
192 ((psiconv_u32) (p_blue*mult+0.5)); 263 ((psiconv_u32) (p_blue * (1 << bluebits) + 0.5));
264 else
265 pixel = (p_red + p_green + p_blue)/3.0 * (1 << colordepth);
193 } else { 266 } else {
194 dist = 4; /* Max distance is 3, so this is safe */ 267 dist = 4; /* Max distance is 3, so this is safe */
195 pixel = -1; 268 pixel = -1;
196 for (i = 0; i < palet.length; i++) { 269 for (i = 0; i < palet.length; i++) {
197 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]) +
228 psiconv_u32 *pixelptr; 301 psiconv_u32 *pixelptr;
229 int inputbitsleft,outputbitnr,bitsfit,outputbytenr; 302 int inputbitsleft,outputbitnr,bitsfit,outputbytenr;
230 303
231 304
232 if (!bytes) { 305 if (!bytes) {
233 psiconv_warn(config,0,0,"NULL pixel data"); 306 psiconv_error(config,0,0,"NULL pixel data");
234 res = -PSICONV_E_GENERATE; 307 res = -PSICONV_E_GENERATE;
235 goto ERROR1; 308 goto ERROR1;
236 } 309 }
237 if (!pixels) { 310 if (!pixels) {
238 psiconv_warn(config,0,0,"NULL pixel data"); 311 psiconv_error(config,0,0,"NULL pixel data");
239 res = -PSICONV_E_GENERATE; 312 res = -PSICONV_E_GENERATE;
240 goto ERROR1; 313 goto ERROR1;
241 } 314 }
242 if (psiconv_list_length(pixels) != xsize * ysize) { 315 if (psiconv_list_length(pixels) != xsize * ysize) {
243 psiconv_warn(config,0,0,"Pixel number is not correct"); 316 psiconv_error(config,0,0,"Pixel number is not correct");
244 res = -PSICONV_E_GENERATE; 317 res = -PSICONV_E_GENERATE;
245 goto ERROR1; 318 goto ERROR1;
246 } 319 }
247 320
248 if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) { 321 if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) {
255 outputbyte = 0; 328 outputbyte = 0;
256 for (y = 0; y < ysize; y++) { 329 for (y = 0; y < ysize; y++) {
257 outputbytenr = 0; 330 outputbytenr = 0;
258 for (x = 0; x < xsize; x++) { 331 for (x = 0; x < xsize; x++) {
259 if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) { 332 if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) {
260 psiconv_warn(config,0,0,"Massive internal corruption"); 333 psiconv_error(config,0,0,"Massive internal corruption");
261 res = -PSICONV_E_NOMEM; 334 res = -PSICONV_E_NOMEM;
262 goto ERROR2; 335 goto ERROR2;
263 } 336 }
264 inputbitsleft = colordepth; 337 inputbitsleft = colordepth;
265 inputdata = *pixelptr; 338 inputdata = *pixelptr;
300 psiconv_list_free(*bytes); 373 psiconv_list_free(*bytes);
301ERROR1: 374ERROR1:
302 return res; 375 return res;
303} 376}
304 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 */
305int psiconv_write_sketch_section(const psiconv_config config, 382int psiconv_encode_rle8(const psiconv_config config,
306 psiconv_buffer buf, 383 const psiconv_pixel_bytes plain_bytes,
307 psiconv_sketch_section value) 384 psiconv_pixel_bytes *encoded_bytes)
308{ 385{
309 int res; 386 int res,i,j,len;
387 psiconv_u8 *entry,*next;
388 psiconv_u8 temp;
310 389
311 if (!value) { 390 if (!(*encoded_bytes = psiconv_list_new(sizeof(*entry)))) {
312 psiconv_warn(config,0,0,"NULL sketch section");
313 res = -PSICONV_E_GENERATE; 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;
314 goto ERROR1; 449 goto ERROR2;
315 } 450 }
316 451 if ((res = psiconv_list_add(*encoded_bytes,next)))
317 if ((res = psiconv_write_u16(config,buf,value->displayed_xsize)))
318 goto ERROR1; 452 goto ERROR2;
319 if ((res = psiconv_write_u16(config,buf,value->displayed_ysize))) 453 }
320 goto ERROR1; 454 i += len;
321 if ((res = psiconv_write_u16(config,buf,value->picture_data_x_offset))) 455 }
322 goto ERROR1; 456 }
323 if ((res = psiconv_write_u16(config,buf,value->picture_data_y_offset))) 457 return 0;
324 goto ERROR1;
325 if ((res = psiconv_write_u16(config,buf,value->displayed_size_x_offset)))
326 goto ERROR1;
327 if ((res = psiconv_write_u16(config,buf,value->displayed_size_y_offset)))
328 goto ERROR1;
329 if ((res = psiconv_write_u16(config,buf,value->form_xsize)))
330 goto ERROR1;
331 if ((res = psiconv_write_u16(config,buf,value->form_ysize)))
332 goto ERROR1;
333 if ((res = psiconv_write_u16(config,buf,0x0000)))
334 goto ERROR1;
335 if ((res = psiconv_write_paint_data_section(config,buf,value->picture)))
336 goto ERROR1;
337 if ((res = psiconv_write_u16(config,buf,value->magnification_x * 0x03e8)))
338 goto ERROR1;
339 if ((res = psiconv_write_u16(config,buf,value->magnification_y * 0x03e8)))
340 goto ERROR1;
341 if ((res = psiconv_write_u32(config,buf,value->cut_left * 0x0c *
342 value->displayed_xsize)))
343 goto ERROR1;
344 if ((res = psiconv_write_u32(config,buf,value->cut_right * 0x0c *
345 value->displayed_xsize)))
346 goto ERROR1;
347 if ((res = psiconv_write_u32(config,buf,value->cut_top * 0x0c *
348 value->displayed_ysize)))
349 goto ERROR1;
350 if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c *
351 value->displayed_ysize)))
352 goto ERROR1;
353 458
459ERROR2:
460 psiconv_list_free(*encoded_bytes);
354ERROR1: 461ERROR1:
355 return res; 462 return res;
356} 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.170  
changed lines
  Added in v.184

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