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