1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290
|
/*
* This file is part of the XForms library package.
*
* XForms is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1, or
* (at your option) any later version.
*
* XForms is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with XForms. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* This file is part of the XForms library package.
* Copyright (c) 1993, 1998-2002 T.C. Zhao
* All rights reserved.
*
* Use one of the builtin image reader and an external filter to
* add new supported format.
*
* TODO: add signal handling to make sure the temp files are deleted.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "include/forms.h"
#include "flimage.h"
#include "flimage_int.h"
#include "private/flsnprintf.h"
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/stat.h>
#ifdef FL_WIN32
#include <process.h>
#include <io.h>
#include <X11/Xw32defs.h>
#endif
/***************************************
***************************************/
static char *
get_tmpf( char * obuf )
{
static int k,
seq;
static char buf[ 15 ][ 256 ];
int fd = -1,
tries = 0;
char *name;
k = ( k + 1 ) % 15;
name = obuf ? obuf : buf[ k ];
do
{
if ( ( fd = mkstemp( name ) ) >= 0 )
/* empty */ ;
else
{
fl_snprintf( buf[ k ], sizeof buf[ k ],
"%s/.FL%03d_%d.tmp", "/tmp", seq++,
( int ) getpid( ) );
name = buf[ k ];
/* create the file now in exclusive mode (for security) */
fd = open( name, O_RDWR | O_CREAT | O_EXCL, S_IREAD | S_IWRITE );
}
tries++;
} while ( fd < 0 && tries < 30 );
if ( fd >= 0 )
{
close( fd );
return name;
}
return 0;
}
/***************************************
***************************************/
int
flimage_description_via_filter( FL_IMAGE * im,
char * const * cmds,
const char * what,
int verbose )
{
char cmd[ 1024 ],
*tmpf;
char * const *q = cmds;
int status = 0,
n;
if ( ! ( tmpf = get_tmpf( 0 ) ) )
{
im->error_message( im, "can't get tmpfile!" );
return -1;
}
do
{
fl_snprintf( cmd, sizeof cmd - 12, *q, im->infile, tmpf );
if ( verbose )
fprintf( stderr, "executing %s\n", cmd );
else
strcat( cmd, " 2>/dev/null" );
status = system( cmd );
} while ( status && *++q );
if ( status )
{
M_err( "", "%s failed", cmd );
return -1;
}
/* first check if the converted image is something we can handle */
if ( ! ( n = flimage_is_supported( tmpf ) ) )
{
im->error_message( im, "unknown filtered output" );
remove( tmpf );
return -1;
}
im->visual_cue( im, what );
/* now we're really ok */
fclose( im->fpin );
im->fpin = fopen( tmpf, "rb" );
strcpy( cmd, im->infile );
strcpy( im->infile, tmpf );
/* if verbose, delay the file deletion in case we want to look at it */
--n;
if ( verbose )
fprintf( stderr, " reading %s via %s\n",
( ( FLIMAGE_IO * ) im->image_io )->short_name,
flimage_io[ n ].short_name );
if ( strcmp( im->fmt_name, "gzip" ) == 0 )
im->fmt_name = flimage_io[ n ].short_name;
im->image_io = flimage_io + n;
im->type = flimage_io[ n ].type;
flimage_io[ n ].identify( im->fpin ); /* advance file position */
status = flimage_io[ n ].read_description( im );
remove( tmpf );
strcpy( im->infile, cmd );
return status;
}
/**********************************************************************
* write via external filters
**********************************************************************/
/* Given several formats, say ppm, pgm, and pbm, check if the image
* type is the same as one of the requested. If yes, return the IO struct
* else return the first match. Therefore,
* caller should place the formats in decreasing order of generality,
* i.e., ppm, pgm, pbm, never pbm, pgm, ppm.
*/
static FLIMAGE_IO *
best_format( FL_IMAGE * im,
char * const formats[ ] )
{
FLIMAGE_IO *io = flimage_io;
char *const *fmts = formats;
FLIMAGE_IO *first = 0,
*best_sofar = 0;
int found;
for ( ; *fmts; fmts++ )
{
for ( io = flimage_io, found = 0; ! found && io->formal_name; io++ )
found = strcasecmp( io->formal_name, *fmts ) == 0
|| strcasecmp( io->short_name, *fmts ) == 0;
if ( found )
{
io--;
best_sofar = ( im->type & io->type ) ? io : 0;
if ( ! first )
first = best_sofar;
}
else
fprintf( stderr, "bad format name: %s\n", *fmts );
}
return best_sofar ? best_sofar : first;
}
/***************************************
***************************************/
int
flimage_write_via_filter( FL_IMAGE * im,
char * const * cmds, /* shell commands */
char *const formats[ ], /* formats */
int verbose )
{
char *tmpf;
char ofile[ 256 ],
cmd[ 1024 ];
char * const *shellcmd;
FLIMAGE_IO *io;
int err,
status;
/* check if we can handle the type */
if ( ! ( io = best_format( im, formats ) ) )
{
fprintf( stderr, "can't find format handler\n" );
return -1;
}
if ( ! ( io->type & im->type ) )
flimage_convert( im, io->type, 256 );
tmpf = get_tmpf( 0 );
strcpy( ofile, im->outfile );
strcpy( im->outfile, tmpf );
if ( ! ( im->fpout = fopen( im->outfile, "wb" ) ) )
{
fprintf( stderr, "can't open %s\n", tmpf );
remove( tmpf );
return -1;
}
/* skip the pre_write step */
err = io->write_image( im ) < 0;
fclose( im->fpout );
if ( verbose )
fprintf( stderr, "Done writing %s (%s) \n",
im->outfile, io->short_name );
/* now we have the intermediate image ready, generate the shell cmd */
for ( shellcmd = cmds, status = ! err; status && *shellcmd; shellcmd++ )
{
fl_snprintf( cmd, sizeof cmd, *shellcmd, tmpf, ofile );
if ( ! verbose )
strcat( cmd, " 2>/dev/null" );
else
fprintf( stderr, "executing %s\n", cmd );
status = system( cmd );
}
remove( tmpf );
strcpy( im->outfile, ofile );
if ( status )
fprintf( stderr, "%s failed\n", cmd );
return status ? -1 : 0;
}
/*
* Local variables:
* tab-width: 4
* indent-tabs-mode: nil
* End:
*/
|