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