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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 147 Revision 196
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-2004 Frodo Looijaard <frodol@dds.nl>
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#include <magick/magick.h>
30#endif
29 31
30#ifdef DMALLOC 32#ifdef DMALLOC
31#include "dmalloc.h" 33#include "dmalloc.h"
32#endif 34#endif
33 35
34static void set_filename(char *dest,const char *type, const char *name) 36static Image *get_paint_data_section(psiconv_paint_data_section sec);
35{ 37static void image_to_list(psiconv_list list,Image *image,const char *dest);
36 int len; 38static void gen_image_list(const psiconv_config config,psiconv_list list,
37 strcpy(dest,type); 39 const psiconv_list sections, const char *dest);
38 len = strlen(dest); 40static void gen_clipart(const psiconv_config config,psiconv_list list,
39 dest[len] = ':'; 41 const psiconv_clipart_f f, const char *dest);
40 strcpy(dest + len + 1,name); 42static void gen_mbm(const psiconv_config config,psiconv_list list,
41} 43 const psiconv_mbm_f f, const char *dest);
44static void gen_sketch(const psiconv_config config,psiconv_list list,
45 const psiconv_sketch_f f, const char *dest);
46static 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
47static Image *get_paint_data_section(psiconv_paint_data_section sec) 54Image *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
78static int psiconv_gen_image_clipart(const char *filename, psiconv_clipart_f f, 87void 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
133static int psiconv_gen_image_mbm(const char *filename, psiconv_mbm_f f, 113void 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
190static void psiconv_gen_image_sketch(const char *filename, psiconv_sketch_f f, 172void 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
197void 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
204void 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
206static int psiconv_gen_image(const char * filename, const psiconv_file file, 214
207 const char *dest, 215int gen_image(psiconv_config config, psiconv_list list,
216 const psiconv_file file, const char *dest,
208 const psiconv_encoding encoding_type) 217 const encoding encoding_type)
209 /* Here the encoding_type is a dummy */
210{ 218{
211 if (file->type == psiconv_mbm_file) 219 if (file->type == psiconv_mbm_file)
212 psiconv_gen_image_mbm(filename,(psiconv_mbm_f) file->file,dest); 220 gen_mbm(config,list,(psiconv_mbm_f) file->file,dest);
213 else if (file->type == psiconv_clipart_file) 221 else if (file->type == psiconv_clipart_file)
214 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
215 else if (file->type == psiconv_sketch_file) { 225 if (file->type == psiconv_sketch_file) {
216 psiconv_gen_image_sketch(filename,(psiconv_sketch_f) file->file,dest); 226 gen_sketch(config, list,(psiconv_sketch_f) file->file,dest);
217 } else 227 } else
218 return -1; 228 return -1;
219 return 0; 229 return 0;
220} 230}
221 231
222void init_image(void) 232void init_image(void)
223{ 233{
224 struct psiconv_fileformat_s ff; 234 struct fileformat_s ff;
225#if IMAGEMAGICK 235#if IMAGEMAGICK
226 const MagickInfo *mi; 236 const MagickInfo *mi;
227 ff.output = psiconv_gen_image; 237 ff.output = gen_image;
228 for (mi = GetMagickFileList(); mi ; mi = mi->next) { 238 for (mi = GetMagickFileList(); mi ; mi = mi->next) {
229 if (mi->encoder) { 239 if (mi->encoder) {
230 ff.name = strdup(mi->name); 240 ff.name = strdup(mi->name);
231 ff.description = strdup(mi->description); 241 ff.description = strdup(mi->description);
242 ff.supported_format = FORMAT_CLIPART_SINGLE | FORMAT_MBM_SINGLE |
243 FORMAT_SKETCH;
244 if (mi->adjoin)
245 ff.supported_format |= FORMAT_MBM_MULTIPLE | FORMAT_CLIPART_MULTIPLE;
232 psiconv_list_add(fileformat_list,&ff); 246 psiconv_list_add(fileformat_list,&ff);
233 } 247 }
234 } 248 }
235#endif 249#endif
236} 250}

Legend:
Removed from v.147  
changed lines
  Added in v.196

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