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