/[public]/psiconv/trunk/program/psiconv/gen_image.c
ViewVC logotype

Annotation of /psiconv/trunk/program/psiconv/gen_image.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 187 - (hide annotations)
Sun Jan 18 19:58:12 2004 UTC (20 years, 2 months ago) by frodo
File MIME type: text/plain
File size: 6392 byte(s)
(Frodo) Image generation active again in psiconv program

1 frodo 32 /*
2     * gen_image.c - Part of psiconv, a PSION 5 file formats converter
3     * Copyright (c) 1999 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 frodo 58 #include "psiconv/data.h"
22 frodo 32 #include "gen.h"
23 frodo 187 #include "psiconv.h"
24 frodo 32 #include <string.h>
25 frodo 187 #include <stdlib.h>
26 frodo 32
27 frodo 142 #ifdef IMAGEMAGICK
28 frodo 52 #include "magick-aux.h"
29 frodo 32 #include <magick/magick.h>
30 frodo 187 #endif
31 frodo 32
32 frodo 142 #ifdef DMALLOC
33     #include "dmalloc.h"
34     #endif
35    
36 frodo 32 /* This is ridiculously simple using ImageMagick. Without it, it would
37 frodo 49 be quite somewhat harder - it will be left for later on.
38     Note that we ignore any errors. Dangerous... */
39 frodo 32
40 frodo 39 static Image *get_paint_data_section(psiconv_paint_data_section sec)
41 frodo 32 {
42     Image *image;
43 frodo 49 float *pixel, *p, *red, *green, *blue;
44     int x,y;
45 frodo 53 ExceptionInfo exc;
46 frodo 49
47 frodo 53 GetExceptionInfo(&exc);
48 frodo 49 red = sec->red;
49     green = sec->green;
50     blue = sec->blue;
51     p = pixel = malloc(sec->xsize * sec->ysize * 3 * sizeof(float));
52     for (y = 0; y < sec->ysize; y++) {
53     for (x = 0; x < sec->xsize; x++) {
54     *p++ = *red++;
55     *p++ = *green++;
56     *p++ = *blue++;
57     }
58     }
59 frodo 66
60 frodo 53 image = ConstituteImage(sec->xsize,sec->ysize,"RGB",FloatPixel,pixel,&exc);
61 frodo 187 if (! image || (exc.severity != UndefinedException)) {
62 frodo 118 MagickError(exc.severity,exc.reason,exc.description);
63 frodo 187 exit(1);
64 frodo 53 }
65 frodo 49 free(pixel);
66    
67 frodo 187 DestroyExceptionInfo(&exc);
68    
69 frodo 39 return image;
70 frodo 32 }
71    
72 frodo 39
73 frodo 187 static void image_to_list(psiconv_list list,Image *image,const char *dest)
74 frodo 44 {
75 frodo 53 ImageInfo *image_info;
76     ExceptionInfo exc;
77 frodo 187 size_t length;
78     char *data;
79 frodo 44 int i;
80 frodo 187
81     strcpy(image->magick,dest);
82     image_info = CloneImageInfo(NULL);
83 frodo 118 GetExceptionInfo(&exc);
84 frodo 187 data = ImageToBlob(image_info,image,&length,&exc);
85     if (!data || (exc.severity != UndefinedException)) {
86     MagickError(exc.severity,exc.reason,exc.description);
87     exit(1);
88     }
89     for (i = 0; i < length; i++) {
90     if (psiconv_list_add(list,data+i)) {
91     fprintf(stderr,"Out of memory error");
92     exit(1);
93 frodo 44 }
94     }
95 frodo 187 DestroyExceptionInfo(&exc);
96 frodo 53 DestroyImageInfo(image_info);
97 frodo 44 }
98    
99 frodo 187 static void gen_image_list(const psiconv_config config,psiconv_list list,
100     const psiconv_list sections, const char *dest)
101 frodo 32 {
102     psiconv_paint_data_section section;
103 frodo 138 const MagickInfo *mi;
104 frodo 53 ImageInfo *image_info;
105 frodo 39 Image *image = NULL;
106     Image *last_image = NULL;
107 frodo 53 Image *this_image, *images;
108     ExceptionInfo exc;
109 frodo 39 int i;
110 frodo 66
111 frodo 118 GetExceptionInfo(&exc);
112 frodo 187 mi = GetMagickInfo(dest,&exc);
113     if (!mi || (exc.severity != UndefinedException)) {
114     MagickError(exc.severity,exc.reason,exc.description);
115     exit(1);
116     }
117 frodo 118
118 frodo 187 if ((psiconv_list_length(sections) < 1) ||
119     ((psiconv_list_length(sections)) > 1 && ! (mi->adjoin))) {
120     fprintf(stderr,"This image type supports only one image\n");
121     exit(1);
122     }
123    
124     for (i = 0; i < psiconv_list_length(sections); i++) {
125     if (!(section = psiconv_list_get(sections,i))) {
126     fprintf(stderr,"Internal data structures corrupted\n");
127     exit(1);
128     }
129 frodo 39 this_image = get_paint_data_section(section);
130     if (! image) {
131 frodo 40 image = this_image;
132 frodo 39 } else {
133     last_image->next=this_image;
134     this_image->previous=last_image;
135     }
136 frodo 53 last_image = this_image;
137 frodo 39 }
138 frodo 40
139 frodo 53 image_info = CloneImageInfo(NULL);
140 frodo 187 if (image->next) {
141 frodo 66 images = CoalesceImages(image,&exc);
142 frodo 187 if (!images || (exc.severity != UndefinedException)) {
143     MagickError(exc.severity,exc.reason,exc.description);
144     exit(1);
145     }
146     } else
147 frodo 66 images = image;
148 frodo 53
149 frodo 187 image_to_list(list,image,dest);
150    
151     DestroyExceptionInfo(&exc);
152 frodo 53 DestroyImageInfo(image_info);
153 frodo 66 if (image != images)
154     DestroyImages(image);
155 frodo 53 DestroyImages(images);
156 frodo 32 }
157    
158 frodo 187 static void gen_image_clipart(const psiconv_config config,psiconv_list list,
159     const psiconv_clipart_f f, const char *dest)
160 frodo 32 {
161 frodo 187 int i;
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     {
193 frodo 39 Image *image;
194    
195     image = get_paint_data_section(f->sketch_sec->picture);
196 frodo 187 image_to_list(list,image,dest);
197 frodo 53 DestroyImage(image);
198 frodo 32 }
199    
200 frodo 187
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 frodo 32 {
205 frodo 38 if (file->type == psiconv_mbm_file)
206 frodo 187 gen_image_mbm(config,list,(psiconv_mbm_f) file->file,dest);
207 frodo 66 else if (file->type == psiconv_clipart_file)
208 frodo 187 gen_image_clipart(config,list,
209     (psiconv_clipart_f) file->file,dest);
210     else
211     if (file->type == psiconv_sketch_file) {
212     gen_image_sketch(config, list,(psiconv_sketch_f) file->file,dest);
213 frodo 34 } else
214     return -1;
215 frodo 53 return 0;
216 frodo 32 }
217    
218 frodo 34 void init_image(void)
219     {
220 frodo 187 struct fileformat_s ff;
221 frodo 34 #if IMAGEMAGICK
222 frodo 138 const MagickInfo *mi;
223 frodo 187 ff.output = gen_image;
224 frodo 52 for (mi = GetMagickFileList(); mi ; mi = mi->next) {
225 frodo 34 if (mi->encoder) {
226 frodo 138 ff.name = strdup(mi->name);
227 frodo 34 ff.description = strdup(mi->description);
228     psiconv_list_add(fileformat_list,&ff);
229     }
230     }
231     #endif
232     }
233    

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26