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