--- psiconv/trunk/program/psiconv/gen_image.c 1999/12/03 00:15:17 40 +++ psiconv/trunk/program/psiconv/gen_image.c 2002/01/29 18:38:38 142 @@ -18,37 +18,80 @@ */ #include "config.h" -#include "data.h" +#include "psiconv/data.h" #include "gen.h" #include #include "psiconv.h" -#if IMAGEMAGICK +#ifdef IMAGEMAGICK +#include "magick-aux.h" #include + +#ifdef DMALLOC +#include "dmalloc.h" #endif +static void set_filename(char *dest,const char *type, const char *name) +{ + int len; + strcpy(dest,type); + len = strlen(dest); + dest[len] = ':'; + strcpy(dest + len + 1,name); +} + /* This is ridiculously simple using ImageMagick. Without it, it would - be quite somewhat harder - it will be left for later on */ + be quite somewhat harder - it will be left for later on. + Note that we ignore any errors. Dangerous... */ static Image *get_paint_data_section(psiconv_paint_data_section sec) { Image *image; - image = CreateImage(sec->xsize,sec->ysize,sec->red,sec->green, - sec->blue,NULL); + float *pixel, *p, *red, *green, *blue; + int x,y; + ExceptionInfo exc; + + GetExceptionInfo(&exc); + red = sec->red; + green = sec->green; + blue = sec->blue; + p = pixel = malloc(sec->xsize * sec->ysize * 3 * sizeof(float)); + for (y = 0; y < sec->ysize; y++) { + for (x = 0; x < sec->xsize; x++) { + *p++ = *red++; + *p++ = *green++; + *p++ = *blue++; + } + } + + image = ConstituteImage(sec->xsize,sec->ysize,"RGB",FloatPixel,pixel,&exc); + if (! image) { + MagickError(exc.severity,exc.reason,exc.description); + } + free(pixel); + return image; } +#endif -static int psiconv_gen_image_mbm(FILE *of, psiconv_mbm_f f, const char *dest) +static int psiconv_gen_image_clipart(const char *filename, psiconv_clipart_f f, + const char *dest) { #if IMAGEMAGICK - psiconv_paint_data_section section; - MagickInfo *mi = GetMagickInfo(dest); - ImageInfo image_info; + psiconv_clipart_section section; + const MagickInfo *mi; + ImageInfo *image_info; Image *image = NULL; Image *last_image = NULL; - Image *this_image; + Image *this_image, *images; + ExceptionInfo exc; int i; + unsigned int status; + + GetExceptionInfo(&exc); + if (!(mi = GetMagickInfo(dest,&exc))) + return -1; if ((psiconv_list_length(f->sections) < 1) || ((psiconv_list_length(f->sections)) > 1 && ! (mi->adjoin))) @@ -57,63 +100,132 @@ #if IMAGEMAGICK for (i = 0; i < psiconv_list_length(f->sections); i++) { section = psiconv_list_get(f->sections,i); + this_image = get_paint_data_section(section->picture); + if (! image) { + image = this_image; + } else { + last_image->next=this_image; + this_image->previous=last_image; + } + last_image = this_image; + } + + image_info = CloneImageInfo(NULL); + set_filename(image->filename,dest,filename); + if (image->next) + images = CoalesceImages(image,&exc); + else + images = image; + if (! images) + CatchImageException(image); + + status = WriteImage(image_info,images); + if (!status) + CatchImageException(images); + DestroyImageInfo(image_info); + if (image != images) + DestroyImages(image); + DestroyImages(images); + return 0; +#endif +} + +static int psiconv_gen_image_mbm(const char *filename, psiconv_mbm_f f, + const char *dest) +{ + +#if IMAGEMAGICK + psiconv_paint_data_section section; + const MagickInfo *mi; + ImageInfo *image_info; + Image *image = NULL; + Image *last_image = NULL; + Image *this_image, *images; + ExceptionInfo exc; + int i; + unsigned int status; + + GetExceptionInfo(&exc); + if (!(mi = GetMagickInfo(dest,&exc))) + return -1; + + if ((psiconv_list_length(f->sections) < 1) || + ((psiconv_list_length(f->sections)) > 1 && ! (mi->adjoin))) +#endif + return -1; +#if IMAGEMAGICK + + for (i = 0; i < psiconv_list_length(f->sections); i++) { + section = psiconv_list_get(f->sections,i); this_image = get_paint_data_section(section); if (! image) { image = this_image; - last_image = image; } else { last_image->next=this_image; this_image->previous=last_image; - this_image->scene=i; - last_image=this_image; } + last_image = this_image; } - GetImageInfo(&image_info); - image_info.file = of; - strcpy(image->magick, dest); - image_info.adjoin=1; - WriteImage(&image_info,image); + image_info = CloneImageInfo(NULL); + set_filename(image->filename,dest,filename); + if (image->next) + images = CoalesceImages(image,&exc); + else + images = image; + if (! images) + CatchImageException(image); + + status = WriteImage(image_info,images); + if (!status) + CatchImageException(images); + DestroyImageInfo(image_info); + if (image != images) + DestroyImages(image); + DestroyImages(images); return 0; #endif } -static void psiconv_gen_image_sketch(FILE *of, psiconv_sketch_f f, +static void psiconv_gen_image_sketch(const char *filename, psiconv_sketch_f f, const char *dest) { #if IMAGEMAGICK - ImageInfo image_info; + ImageInfo *image_info; Image *image; image = get_paint_data_section(f->sketch_sec->picture); - GetImageInfo(&image_info); - image_info.file = of; - strcpy(image->magick, dest); - WriteImage(&image_info,image); + image_info = CloneImageInfo(NULL); + set_filename(image->filename,dest,filename); + WriteImage(image_info,image); + DestroyImageInfo(image_info); + DestroyImage(image); #endif } -static int psiconv_gen_image(FILE * of, const psiconv_file file, +static int psiconv_gen_image(const char * filename, const psiconv_file file, const char *dest) { if (file->type == psiconv_mbm_file) - return psiconv_gen_image_mbm(of,(psiconv_mbm_f) file->file,dest); + psiconv_gen_image_mbm(filename,(psiconv_mbm_f) file->file,dest); + else if (file->type == psiconv_clipart_file) + psiconv_gen_image_clipart(filename,(psiconv_clipart_f) file->file,dest); else if (file->type == psiconv_sketch_file) { - psiconv_gen_image_sketch(of,(psiconv_sketch_f) file->file,dest); - return 0; + psiconv_gen_image_sketch(filename,(psiconv_sketch_f) file->file,dest); } else return -1; + return 0; } void init_image(void) { - struct psiconv_fileformat ff; + struct psiconv_fileformat_s ff; #if IMAGEMAGICK - MagickInfo *mi; + const MagickInfo *mi; ff.output = psiconv_gen_image; - for (mi = GetMagickInfo(NULL); mi ; mi = mi->next) { + for (mi = GetMagickFileList(); mi ; mi = mi->next) { if (mi->encoder) { - ff.name = strdup(mi->tag); + ff.name = strdup(mi->name); ff.description = strdup(mi->description); psiconv_list_add(fileformat_list,&ff); }