1 /*
2 * This file is part of XForms.
3 *
4 * XForms is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License as
6 * published by the Free Software Foundation; either version 2.1, or
7 * (at your option) any later version.
8 *
9 * XForms is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with XForms; see the file COPYING. If not, write to
16 * the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19
20
21 /*
22 * Convert an image file using the image support of Forms Library.
23 *
24 * Usage: iconvert [-version][-verbose][-help] inputimage outimage [fmt]
25 * output image format is determined by the extension or
26 * by fmt if present.
27 *
28 * Exit status: 0 (success) 1 (bad command line) 3 (conversion failed)
29 *
30 * T.C. Zhao (03/1999)
31 *
32 */
33
34 #ifdef HAVE_CONFIG_H
35 #include "config.h"
36 #endif
37
38 #include <stdlib.h>
39
40 #include "include/forms.h"
41 #include "image/flimage.h"
42
43 static void initialize( void );
44 static void usage( const char *,
45 int );
46 static int parse_command_line( int *,
47 char ** );
48
49
50 /***************************************
51 ***************************************/
52
53 int
main(int argc,char * argv[])54 main( int argc,
55 char * argv[ ] )
56 {
57 FL_IMAGE *im;
58 const char *fmt = 0;
59 char *const *args;
60
61 initialize( );
62 args = argv + parse_command_line( &argc, argv );
63
64 if ( argc < 3 ||
65 ! ( fmt = ( argc >= 4 ? args[ 3 ] : strrchr( args[ 2 ], '.' ) ) ) )
66 {
67 usage( argv[ 0 ], argc >= 3 );
68 exit( 1 );
69 }
70
71 fmt += fmt[ 0 ] == '.';
72 im = flimage_load( strdup( args[ 1 ] ) );
73
74 return flimage_dump( im, args[ 2 ], fmt ) < 0 ? 3 : 0;
75 }
76
77
78 /***************************************
79 ***************************************/
80
81 static void
usage(const char * cmd,int more)82 usage( const char * cmd,
83 int more )
84 {
85 const FLIMAGE_FORMAT_INFO *info;
86 int n,
87 i,
88 k;
89
90 fprintf( stderr, "Usage: %s [-verbose][-help] infile outfile [fmt]\n",
91 cmd );
92
93 if ( ! more )
94 exit( 1 );
95
96 fputs( " The output format is determined by the file extension or fmt.\n"
97 " fmt or extension must be one of the following:\n", stderr );
98
99 n = flimage_get_number_of_formats( );
100 for ( i = 1, k = 0; i <= n; i++ )
101 {
102 info = flimage_get_format_info( i );
103 if ( info->read_write & FLIMAGE_WRITABLE )
104 {
105 fprintf( stderr, "\t%s", info->extension );
106 if ( ++k % 6 == 0 )
107 fputc( '\n', stderr );
108 }
109 }
110
111 if ( k % 6 )
112 fputc( '\n', stderr );
113
114 exit( 1 );
115 }
116
117
118 /* shut up visual_cue */
119
120 /***************************************
121 ***************************************/
122
123 static int
noop(FL_IMAGE * im FL_UNUSED_ARG,const char * s FL_UNUSED_ARG)124 noop( FL_IMAGE * im FL_UNUSED_ARG,
125 const char * s FL_UNUSED_ARG )
126 {
127 return 0;
128 }
129
130
131 /***************************************
132 ***************************************/
133
134 static int
parse_command_line(int * argc,char * argv[])135 parse_command_line( int * argc,
136 char * argv[ ] )
137 {
138 int i;
139 static FLIMAGE_SETUP setup;
140
141 setup.visual_cue = noop;
142
143 for ( i = 1; i < *argc && *argv[ i ] == '-'; )
144 {
145 if ( strncmp( argv[ i ], "-verb", 5 ) == 0 )
146 {
147 setup.visual_cue = 0;
148 i++;
149 }
150 else if ( strncmp( argv[ i ], "-h", 2 ) == 0 )
151 {
152 usage( argv[ 0 ], 1 );
153 i++;
154 }
155 else
156 usage( argv[ 0 ], 0 );
157 }
158
159 flimage_setup( &setup );
160 *argc -= i - 1;
161
162 return i - 1;
163 }
164
165
166 /***************************************
167 ***************************************/
168
169 static void
initialize(void)170 initialize( void )
171 {
172 flimage_enable_xpm( );
173 flimage_enable_gif( );
174 flimage_enable_bmp( );
175 flimage_enable_sgi( );
176 flimage_enable_fits( );
177 flimage_enable_png( );
178 flimage_enable_xwd( );
179 flimage_enable_tiff( );
180 flimage_enable_ps( );
181 #ifndef NO_JPEG
182 flimage_enable_jpeg( );
183 #endif
184 }
185
186
187 /*
188 * Local variables:
189 * tab-width: 4
190 * indent-tabs-mode: nil
191 * End:
192 */
193