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