/[public]/psiconv/trunk/program/psiconv/gen_image.c
ViewVC logotype

Diff of /psiconv/trunk/program/psiconv/gen_image.c

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

Revision 142 Revision 249
1/* 1/*
2 * gen_image.c - Part of psiconv, a PSION 5 file formats converter 2 * gen_image.c - Part of psiconv, a PSION 5 file formats converter
3 * Copyright (c) 1999 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.
18 */ 18 */
19 19
20#include "config.h" 20#include "config.h"
21#include "psiconv/data.h" 21#include "psiconv/data.h"
22#include "gen.h" 22#include "gen.h"
23#include "psiconv.h"
23#include <string.h> 24#include <string.h>
24#include "psiconv.h" 25#include <stdlib.h>
25 26
26#ifdef IMAGEMAGICK 27#ifdef IMAGEMAGICK
27#include "magick-aux.h" 28#include "magick-aux.h"
28#include <magick/magick.h> 29#include <magick/magick.h>
30#endif
29 31
30#ifdef DMALLOC 32#ifdef DMALLOC
31#include "dmalloc.h" 33#include "dmalloc.h"
32#endif 34#endif
33 35
34static void set_filename(char *dest,const char *type, const char *name) 36#ifdef IMAGEMAGICK
35{ 37static Image *get_paint_data_section(psiconv_paint_data_section sec);
36 int len; 38static void image_to_list(psiconv_list list,Image *image,const char *dest);
37 strcpy(dest,type); 39static void gen_image_list(const psiconv_config config,psiconv_list list,
38 len = strlen(dest); 40 const psiconv_list sections, const char *dest);
39 dest[len] = ':'; 41static void gen_clipart(const psiconv_config config,psiconv_list list,
40 strcpy(dest + len + 1,name); 42 const psiconv_clipart_f f, const char *dest);
41} 43static void gen_mbm(const psiconv_config config,psiconv_list list,
44 const psiconv_mbm_f f, const char *dest);
45static void gen_sketch(const psiconv_config config,psiconv_list list,
46 const psiconv_sketch_f f, const char *dest);
47static int gen_image(psiconv_config config, psiconv_list list,
48 const psiconv_file file, const char *dest,
49 const encoding encoding_type);
42 50
43/* This is ridiculously simple using ImageMagick. Without it, it would 51/* This is ridiculously simple using ImageMagick. Without it, it would
44 be quite somewhat harder - it will be left for later on. 52 be quite somewhat harder - it will be left for later on.
45 Note that we ignore any errors. Dangerous... */ 53 Note that we ignore any errors. Dangerous... */
46 54
47static Image *get_paint_data_section(psiconv_paint_data_section sec) 55Image *get_paint_data_section(psiconv_paint_data_section sec)
48{ 56{
49 Image *image; 57 Image *image;
50 float *pixel, *p, *red, *green, *blue; 58 float *pixel, *p, *red, *green, *blue;
51 int x,y; 59 int x,y;
52 ExceptionInfo exc; 60 ExceptionInfo exc;
63 *p++ = *blue++; 71 *p++ = *blue++;
64 } 72 }
65 } 73 }
66 74
67 image = ConstituteImage(sec->xsize,sec->ysize,"RGB",FloatPixel,pixel,&exc); 75 image = ConstituteImage(sec->xsize,sec->ysize,"RGB",FloatPixel,pixel,&exc);
68 if (! image) { 76 if (! image || (exc.severity != UndefinedException)) {
69 MagickError(exc.severity,exc.reason,exc.description); 77 MagickError(exc.severity,exc.reason,exc.description);
78 exit(1);
70 } 79 }
71 free(pixel); 80 free(pixel);
72 81
82 DestroyExceptionInfo(&exc);
83
73 return image; 84 return image;
74} 85}
75 86
76#endif
77 87
78static int psiconv_gen_image_clipart(const char *filename, psiconv_clipart_f f, 88void image_to_list(psiconv_list list,Image *image,const char *dest)
79 const char *dest)
80{ 89{
81#if IMAGEMAGICK
82 psiconv_clipart_section section;
83 const MagickInfo *mi;
84 ImageInfo *image_info; 90 ImageInfo *image_info;
85 Image *image = NULL;
86 Image *last_image = NULL;
87 Image *this_image, *images;
88 ExceptionInfo exc; 91 ExceptionInfo exc;
92 size_t length;
93 char *data;
89 int i; 94 int i;
90 unsigned int status; 95
91 96 strcpy(image->magick,dest);
97 image_info = CloneImageInfo(NULL);
92 GetExceptionInfo(&exc); 98 GetExceptionInfo(&exc);
93 if (!(mi = GetMagickInfo(dest,&exc))) 99 data = ImageToBlob(image_info,image,&length,&exc);
94 return -1; 100 if (!data || (exc.severity != UndefinedException)) {
101 MagickError(exc.severity,exc.reason,exc.description);
102 exit(1);
95 103 }
96 if ((psiconv_list_length(f->sections) < 1) || 104 for (i = 0; i < length; i++) {
97 ((psiconv_list_length(f->sections)) > 1 && ! (mi->adjoin))) 105 if (psiconv_list_add(list,data+i)) {
98#endif 106 fprintf(stderr,"Out of memory error");
99 return -1; 107 exit(1);
100#if IMAGEMAGICK
101 for (i = 0; i < psiconv_list_length(f->sections); i++) {
102 section = psiconv_list_get(f->sections,i);
103 this_image = get_paint_data_section(section->picture);
104 if (! image) {
105 image = this_image;
106 } else {
107 last_image->next=this_image;
108 this_image->previous=last_image;
109 }
110 last_image = this_image;
111 } 108 }
112 109 }
113 image_info = CloneImageInfo(NULL); 110 DestroyExceptionInfo(&exc);
114 set_filename(image->filename,dest,filename);
115 if (image->next)
116 images = CoalesceImages(image,&exc);
117 else
118 images = image;
119 if (! images)
120 CatchImageException(image);
121
122 status = WriteImage(image_info,images);
123 if (!status)
124 CatchImageException(images);
125 DestroyImageInfo(image_info); 111 DestroyImageInfo(image_info);
126 if (image != images)
127 DestroyImages(image);
128 DestroyImages(images);
129 return 0;
130#endif
131} 112}
132 113
133static int psiconv_gen_image_mbm(const char *filename, psiconv_mbm_f f, 114void gen_image_list(const psiconv_config config,psiconv_list list,
134 const char *dest) 115 const psiconv_list sections, const char *dest)
135{ 116{
136
137#if IMAGEMAGICK
138 psiconv_paint_data_section section; 117 psiconv_paint_data_section section;
139 const MagickInfo *mi; 118 const MagickInfo *mi;
140 ImageInfo *image_info; 119 ImageInfo *image_info;
141 Image *image = NULL; 120 Image *image = NULL;
142 Image *last_image = NULL; 121 Image *last_image = NULL;
143 Image *this_image, *images; 122 Image *this_image, *images;
144 ExceptionInfo exc; 123 ExceptionInfo exc;
145 int i; 124 int i;
146 unsigned int status;
147 125
148 GetExceptionInfo(&exc); 126 GetExceptionInfo(&exc);
149 if (!(mi = GetMagickInfo(dest,&exc))) 127 mi = GetMagickInfo(dest,&exc);
150 return -1; 128 if (!mi || (exc.severity != UndefinedException)) {
129 MagickError(exc.severity,exc.reason,exc.description);
130 exit(1);
131 }
151 132
152 if ((psiconv_list_length(f->sections) < 1) || 133 if ((psiconv_list_length(sections) < 1) ||
153 ((psiconv_list_length(f->sections)) > 1 && ! (mi->adjoin))) 134 ((psiconv_list_length(sections)) > 1 && ! (mi->adjoin))) {
154#endif 135 fprintf(stderr,"This image type supports only one image\n");
155 return -1; 136 exit(1);
156#if IMAGEMAGICK
157 137 }
138
158 for (i = 0; i < psiconv_list_length(f->sections); i++) { 139 for (i = 0; i < psiconv_list_length(sections); i++) {
159 section = psiconv_list_get(f->sections,i); 140 if (!(section = psiconv_list_get(sections,i))) {
141 fprintf(stderr,"Internal data structures corrupted\n");
142 exit(1);
143 }
160 this_image = get_paint_data_section(section); 144 this_image = get_paint_data_section(section);
161 if (! image) { 145 if (! image) {
162 image = this_image; 146 image = this_image;
163 } else { 147 } else {
164 last_image->next=this_image; 148 last_image->next=this_image;
166 } 150 }
167 last_image = this_image; 151 last_image = this_image;
168 } 152 }
169 153
170 image_info = CloneImageInfo(NULL); 154 image_info = CloneImageInfo(NULL);
171 set_filename(image->filename,dest,filename);
172 if (image->next) 155 if (image->next) {
173 images = CoalesceImages(image,&exc); 156 images = CoalesceImages(image,&exc);
157 if (!images || (exc.severity != UndefinedException)) {
158 MagickError(exc.severity,exc.reason,exc.description);
159 exit(1);
160 }
174 else 161 } else
175 images = image; 162 images = image;
176 if (! images)
177 CatchImageException(image);
178 163
179 status = WriteImage(image_info,images); 164 image_to_list(list,image,dest);
180 if (!status) 165
181 CatchImageException(images); 166 DestroyExceptionInfo(&exc);
182 DestroyImageInfo(image_info); 167 DestroyImageInfo(image_info);
183 if (image != images) 168 if (image != images)
184 DestroyImages(image); 169 DestroyImages(image);
185 DestroyImages(images); 170 DestroyImages(images);
186 return 0;
187#endif
188} 171}
189 172
190static void psiconv_gen_image_sketch(const char *filename, psiconv_sketch_f f, 173void gen_clipart(const psiconv_config config,psiconv_list list,
191 const char *dest) 174 const psiconv_clipart_f f, const char *dest)
192{ 175{
193#if IMAGEMAGICK 176 int i;
194 ImageInfo *image_info; 177 psiconv_list sections;
178 psiconv_clipart_section section;
179
180 if (!(sections = psiconv_list_new(sizeof(*section->picture)))) {
181 fprintf(stderr,"Out of memory error\n");
182 exit(1);
183 }
184 for (i = 0; i < psiconv_list_length(f->sections); i ++) {
185 if (!(section = psiconv_list_get(f->sections,i))) {
186 fprintf(stderr,"Internal data structures corrupted\n");
187 exit(1);
188 }
189 if ((psiconv_list_add(sections,section->picture))) {
190 fprintf(stderr,"Out of memory error\n");
191 exit(1);
192 }
193 }
194 gen_image_list(config,list,sections,dest);
195 psiconv_list_free(sections);
196}
197
198void gen_mbm(const psiconv_config config,psiconv_list list,
199 const psiconv_mbm_f f, const char *dest)
200{
201 gen_image_list(config,list,f->sections,dest);
202}
203
204
205void gen_sketch(const psiconv_config config,psiconv_list list,
206 const psiconv_sketch_f f, const char *dest)
207{
195 Image *image; 208 Image *image;
196 209
197 image = get_paint_data_section(f->sketch_sec->picture); 210 image = get_paint_data_section(f->sketch_sec->picture);
198 image_info = CloneImageInfo(NULL); 211 image_to_list(list,image,dest);
199 set_filename(image->filename,dest,filename);
200 WriteImage(image_info,image);
201 DestroyImageInfo(image_info);
202 DestroyImage(image); 212 DestroyImage(image);
203#endif
204} 213}
205 214
206static int psiconv_gen_image(const char * filename, const psiconv_file file, 215
207 const char *dest) 216int gen_image(psiconv_config config, psiconv_list list,
217 const psiconv_file file, const char *dest,
218 const encoding encoding_type)
208{ 219{
209 if (file->type == psiconv_mbm_file) 220 if (file->type == psiconv_mbm_file)
210 psiconv_gen_image_mbm(filename,(psiconv_mbm_f) file->file,dest); 221 gen_mbm(config,list,(psiconv_mbm_f) file->file,dest);
211 else if (file->type == psiconv_clipart_file) 222 else if (file->type == psiconv_clipart_file)
212 psiconv_gen_image_clipart(filename,(psiconv_clipart_f) file->file,dest); 223 gen_clipart(config,list,
224 (psiconv_clipart_f) file->file,dest);
225 else
213 else if (file->type == psiconv_sketch_file) { 226 if (file->type == psiconv_sketch_file) {
214 psiconv_gen_image_sketch(filename,(psiconv_sketch_f) file->file,dest); 227 gen_sketch(config, list,(psiconv_sketch_f) file->file,dest);
215 } else 228 } else
216 return -1; 229 return -1;
217 return 0; 230 return 0;
218} 231}
219 232
233#endif
234
220void init_image(void) 235void init_image(void)
221{ 236{
222 struct psiconv_fileformat_s ff; 237 struct fileformat_s ff;
223#if IMAGEMAGICK 238#if IMAGEMAGICK
224 const MagickInfo *mi; 239 const MagickInfo *mi;
225 ff.output = psiconv_gen_image; 240 ff.output = gen_image;
226 for (mi = GetMagickFileList(); mi ; mi = mi->next) { 241 for (mi = GetMagickFileList(); mi ; mi = mi->next) {
227 if (mi->encoder) { 242 if (mi->encoder) {
228 ff.name = strdup(mi->name); 243 ff.name = strdup(mi->name);
229 ff.description = strdup(mi->description); 244 ff.description = strdup(mi->description);
245 ff.supported_format = FORMAT_CLIPART_SINGLE | FORMAT_MBM_SINGLE |
246 FORMAT_SKETCH;
247 if (mi->adjoin)
248 ff.supported_format |= FORMAT_MBM_MULTIPLE | FORMAT_CLIPART_MULTIPLE;
230 psiconv_list_add(fileformat_list,&ff); 249 psiconv_list_add(fileformat_list,&ff);
231 } 250 }
232 } 251 }
233#endif 252#endif
234} 253}

Legend:
Removed from v.142  
changed lines
  Added in v.249

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