--- psiconv/trunk/lib/psiconv/generate_image.c 2002/01/29 18:38:38 142 +++ psiconv/trunk/lib/psiconv/generate_image.c 2003/11/27 12:08:13 174 @@ -1,4 +1,5 @@ /* + generate_image.c - Part of psiconv, a PSION 5 file formats converter Copyright (c) 1999, 2000 Frodo Looijaard @@ -27,4 +28,398 @@ #include #endif +typedef psiconv_list psiconv_pixel_bytes; /* psiconv_u8 */ + +typedef psiconv_list psiconv_pixel_ints; /* of psiconv_u32 */ + +typedef struct psiconv_pixel_float_s +{ + psiconv_u32 length; + float *red; + float *green; + float *blue; +} psiconv_pixel_floats_t; + +static int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels, + int xsize,int ysize, + const psiconv_pixel_floats_t data, + int colordepth, + const psiconv_pixel_floats_t palet); +static int psiconv_pixel_data_to_bytes(const psiconv_config config, + psiconv_pixel_bytes *bytes, int xsize, + int ysize, const psiconv_pixel_ints pixels, + int colordepth); + +#define PALET_GREY_2_LEN 4 +float palet_grey_2_rgb[PALET_GREY_2_LEN] = {0.0/3, 1.0/3, 2.0/3, 3.0/3}; +#define PALET_GREY_4_LEN 16 +float palet_grey_4_rgb[PALET_GREY_4_LEN] = + { 0.0/15, 1.0/15, 2.0/15, 3.0/15, + 4.0/15, 5.0/15, 6.0/15, 7.0/15, + 8.0/15, 9.0/15, 10.0/15, 11.0/15, + 12.0/15, 13.0/15, 14.0/15, 15.0/15}; +#define PALET_NONE_LEN 0 + +psiconv_pixel_floats_t palet_grey_2 = + { + PALET_GREY_2_LEN, + (float *) palet_grey_2_rgb, + (float *) palet_grey_2_rgb, + (float *) palet_grey_2_rgb + }; + +psiconv_pixel_floats_t palet_grey_4 = + { + PALET_GREY_4_LEN, + (float *) palet_grey_4_rgb, + (float *) palet_grey_4_rgb, + (float *) palet_grey_4_rgb + }; + +psiconv_pixel_floats_t palet_none = + { + PALET_NONE_LEN, + NULL, + NULL, + NULL + }; + + +int psiconv_write_paint_data_section(const psiconv_config config, + psiconv_buffer buf, + const psiconv_paint_data_section value, + int is_clipart) +{ + int res,colordepth,i; + psiconv_pixel_ints ints; + psiconv_pixel_floats_t floats,palet; + psiconv_list bytes; + psiconv_u8 *byteptr; + + if (!value) { + psiconv_warn(config,0,psiconv_buffer_length(buf),"Null paint data section"); + res = -PSICONV_E_GENERATE; + goto ERROR1; + } + + floats.red = value->red; + floats.green = value->green; + floats.blue = value->blue; + floats.length = value->xsize * value->ysize; + + switch (config->colordepth) { + default: + case 2: palet = (config->color?palet_none:palet_grey_2); + break; + case 4: palet = (config->color?palet_none:palet_grey_4); + break; + } + + if ((res = psiconv_collect_pixel_data(&ints,value->xsize, + value->ysize,floats, + config->colordepth,palet))) + goto ERROR1; + + if ((res = psiconv_pixel_data_to_bytes(config,&bytes,value->xsize,value->ysize, + ints,config->colordepth))) + goto ERROR2; + + if ((res = psiconv_write_u32(config,buf,0x28+psiconv_list_length(bytes)))) + goto ERROR3; + if ((res = psiconv_write_u32(config,buf,0x28))) + goto ERROR3; + if ((res = psiconv_write_u32(config,buf,value->xsize))) + goto ERROR3; + if ((res = psiconv_write_u32(config,buf,value->ysize))) + goto ERROR3; + if ((res = psiconv_write_length(config,buf,value->pic_xsize))) + goto ERROR3; + if ((res = psiconv_write_length(config,buf,value->pic_ysize))) + goto ERROR3; + colordepth = config->colordepth; + if ((colordepth != 2) && colordepth != 4) + colordepth = 2; + if ((res = psiconv_write_u32(config,buf,colordepth))) + goto ERROR3; + if ((res = psiconv_write_u32(config,buf,(config->color?1:0)))) + goto ERROR3; + if ((res = psiconv_write_u32(config,buf,0))) + goto ERROR3; + /* Encoding: no RLE for now */ + if ((res = psiconv_write_u32(config,buf,0))) + goto ERROR3; + if (is_clipart) { + if ((res = psiconv_write_u32(config,buf,0xffffffff))) + goto ERROR3; + if ((res = psiconv_write_u32(config,buf,0x00000044))) + goto ERROR3; + } + for (i = 0; i < psiconv_list_length(bytes); i++) { + if (!(byteptr = psiconv_list_get(bytes,i))) + goto ERROR3; + if ((res = psiconv_write_u8(config,buf,*byteptr))) + goto ERROR3; + } + +ERROR3: + psiconv_list_free(bytes); +ERROR2: + psiconv_list_free(ints); +ERROR1: + return res; +} + +/* Translate the floating point RGB information into pixel values. + The palet is optional; without it, we just use the + colordepth. With a large palet this is not very fast, but it will do for + now. For greyscale pictures, just use the palet. */ +int psiconv_collect_pixel_data(psiconv_pixel_ints *pixels,int xsize,int ysize, + const psiconv_pixel_floats_t data, + int colordepth, + const psiconv_pixel_floats_t palet) +{ + int res,x,y,i; + psiconv_u32 index,pixel; + float p_red,p_green,p_blue,mult,dist,new_dist; + + if (!(*pixels = psiconv_list_new(sizeof(psiconv_u32)))) { + res = -PSICONV_E_NOMEM; + goto ERROR1; + } + + mult = 1 << colordepth; + for (y = 0; y < ysize; y++) { + for (x = 0; x < xsize; x++) { + index = y*xsize+x; + p_red = data.red[index]; + p_green = data.green[index]; + p_blue = data.blue[index]; + if (!palet.length) { + pixel = (((psiconv_u32) (p_red*mult+0.5)) << (2*colordepth)) + + (((psiconv_u32) (p_green*mult+0.5)) << colordepth) + + ((psiconv_u32) (p_blue*mult+0.5)); + } else { + dist = 4; /* Max distance is 3, so this is safe */ + pixel = -1; + for (i = 0; i < palet.length; i++) { + new_dist = (p_red - palet.red[i]) * (p_red - palet.red[i]) + + (p_green - palet.green[i]) * (p_green - palet.green[i]) + + (p_blue - palet.blue[i]) * (p_blue - palet.blue[i]); + if (new_dist < dist) { + pixel = i; + dist = new_dist; + } + } + } + if ((res = psiconv_list_add(*pixels,&pixel))) + goto ERROR2; + } + } + return 0; + +ERROR2: + psiconv_list_free(*pixels); +ERROR1: + return res; +} + +int psiconv_pixel_data_to_bytes(const psiconv_config config, + psiconv_pixel_bytes *bytes, int xsize, + int ysize, const psiconv_pixel_ints pixels, + int colordepth) +{ + int res; + int x,y; + + psiconv_u32 inputdata; + psiconv_u8 outputbyte; + psiconv_u32 *pixelptr; + int inputbitsleft,outputbitnr,bitsfit,outputbytenr; + + + if (!bytes) { + psiconv_warn(config,0,0,"NULL pixel data"); + res = -PSICONV_E_GENERATE; + goto ERROR1; + } + if (!pixels) { + psiconv_warn(config,0,0,"NULL pixel data"); + res = -PSICONV_E_GENERATE; + goto ERROR1; + } + if (psiconv_list_length(pixels) != xsize * ysize) { + psiconv_warn(config,0,0,"Pixel number is not correct"); + res = -PSICONV_E_GENERATE; + goto ERROR1; + } + + if (!(*bytes = psiconv_list_new(sizeof(psiconv_u8)))) { + res = -PSICONV_E_NOMEM; + goto ERROR1; + } + + + outputbitnr = 0; + outputbyte = 0; + for (y = 0; y < ysize; y++) { + outputbytenr = 0; + for (x = 0; x < xsize; x++) { + if (!(pixelptr = psiconv_list_get(pixels,y*xsize+x))) { + psiconv_warn(config,0,0,"Massive internal corruption"); + res = -PSICONV_E_NOMEM; + goto ERROR2; + } + inputbitsleft = colordepth; + inputdata = *pixelptr; + while (inputbitsleft) { + bitsfit = (inputbitsleft+outputbitnr<=8?inputbitsleft:8-outputbitnr); + outputbyte |= (inputdata & ((1 << bitsfit) - 1)) << outputbitnr; + inputdata = inputdata >> bitsfit; + inputbitsleft -= bitsfit; + outputbitnr += bitsfit; + if (outputbitnr == 8) { + if ((res = psiconv_list_add(*bytes,&outputbyte))) + goto ERROR2; + outputbitnr = 0; + outputbyte = 0; + outputbytenr ++; + } + } + } + /* Always end lines on a long border */ + if (outputbitnr != 0) { + if ((res = psiconv_list_add(*bytes,&outputbyte))) + goto ERROR2; + outputbitnr = 0; + outputbyte = 0; + outputbytenr ++; + } + + while (outputbytenr % 0x04) { + if ((res = psiconv_list_add(*bytes,&outputbyte))) + goto ERROR2; + outputbytenr ++; + } + } + + return 0; + +ERROR2: + psiconv_list_free(*bytes); +ERROR1: + return res; +} + +int psiconv_write_sketch_section(const psiconv_config config, + psiconv_buffer buf, + const psiconv_sketch_section value) +{ + int res; + + if (!value) { + psiconv_warn(config,0,0,"NULL sketch section"); + res = -PSICONV_E_GENERATE; + goto ERROR1; + } + + if ((res = psiconv_write_u16(config,buf,value->displayed_xsize))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->displayed_ysize))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->picture_data_x_offset))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->picture_data_y_offset))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->displayed_size_x_offset))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->displayed_size_y_offset))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->form_xsize))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->form_ysize))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,0x0000))) + goto ERROR1; + if ((res = psiconv_write_paint_data_section(config,buf,value->picture,0))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->magnification_x * 0x03e8))) + goto ERROR1; + if ((res = psiconv_write_u16(config,buf,value->magnification_y * 0x03e8))) + goto ERROR1; + if ((res = psiconv_write_u32(config,buf,value->cut_left * 0x0c * + value->displayed_xsize))) + goto ERROR1; + if ((res = psiconv_write_u32(config,buf,value->cut_right * 0x0c * + value->displayed_xsize))) + goto ERROR1; + if ((res = psiconv_write_u32(config,buf,value->cut_top * 0x0c * + value->displayed_ysize))) + goto ERROR1; + if ((res = psiconv_write_u32(config,buf,value->cut_bottom * 0x0c * + value->displayed_ysize))) + goto ERROR1; + +ERROR1: + return res; +} + +int psiconv_write_jumptable_section(const psiconv_config config, + psiconv_buffer buf, + const psiconv_jumptable_section value) +{ + int res,i; + psiconv_u32 *offset_ptr; + + + if (!value) { + psiconv_warn(config,0,psiconv_buffer_length(buf), + "NULL Jumptable Section"); + res = -PSICONV_E_GENERATE; + goto ERROR; + } + if ((res = psiconv_write_u32(config,buf,psiconv_list_length(value)))) + goto ERROR; + for (i = 0; i < psiconv_list_length(value); i++) { + if (!(offset_ptr = psiconv_list_get(value,i))) { + psiconv_warn(config,0,psiconv_buffer_length(buf), + "Massive memory corruption"); + res = -PSICONV_E_NOMEM; + goto ERROR; + } + if ((res = psiconv_write_offset(config,buf,*offset_ptr))) + goto ERROR; + } + +ERROR: + return res; +} + +int psiconv_write_clipart_section(const psiconv_config config, + psiconv_buffer buf, + const psiconv_clipart_section value) +{ + int res; + + + if (!value) { + psiconv_warn(config,0,psiconv_buffer_length(buf), + "NULL Clipart Section"); + res = -PSICONV_E_GENERATE; + goto ERROR; + } + if ((res = psiconv_write_u32(config,buf,PSICONV_ID_CLIPART_ITEM))) + goto ERROR; + if ((res = psiconv_write_u32(config,buf,0x00000002))) + goto ERROR; + if ((res = psiconv_write_u32(config,buf,0x00000000))) + goto ERROR; + if ((res = psiconv_write_u32(config,buf,0x00000000))) + goto ERROR; + if ((res = psiconv_write_u32(config,buf,0x0000000C))) + goto ERROR; + if ((res = psiconv_write_paint_data_section(config,buf,value->picture,1))) + goto ERROR; +ERROR: + return res; +}