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