Exemple #1
0
StringsetChild *
stringset_child_new( Stringset *ss,
	const char *label, const char *text, const char *tooltip )
{
	StringsetChild *ssc = INEW( NULL, StringsetChild );

	ssc->ss = ss;
	ssc->label = im_strdup( NULL, label );
	ssc->text = im_strdup( NULL, text );
	ssc->tooltip = im_strdup( NULL, tooltip );

	ss->children = g_slist_append( ss->children, ssc );

	return( ssc );
}
Exemple #2
0
static VipsFits *
vips_fits_new_read( const char *filename, VipsImage *out, int band_select )
{
	VipsFits *fits;
	int status;

	if( !(fits = VIPS_NEW( out, VipsFits )) )
		return( NULL );

	fits->filename = im_strdup( NULL, filename );
	fits->image = out;
	fits->fptr = NULL;
	fits->lock = NULL;
	fits->band_select = band_select;
	fits->buffer = NULL;
	g_signal_connect( out, "close", 
		G_CALLBACK( vips_fits_close_cb ), fits );

	status = 0;
	if( fits_open_file( &fits->fptr, filename, READONLY, &status ) ) {
		im_error( "fits", _( "unable to open \"%s\"" ), filename );
		vips_fits_error( status );
		return( NULL );
	}

	fits->lock = g_mutex_new();

	return( fits );
}
Exemple #3
0
/**
 * im_system:
 * @im: image to run command on
 * @cmd: command to run
 * @out: stdout of command is returned here
 *
 * im_system() runs a command on an image, returning the command's output as a
 * string. The command is executed with popen(), the first '%%s' in the 
 * command being substituted for a filename.
 *
 * If the IMAGE is a file on disc, then the filename will be the name of the 
 * real file. If the image is in memory, or the result of a computation, 
 * then a new file is created in the temporary area called something like 
 * "vips_XXXXXX.v", and that filename given to the command. The file is 
 * deleted when the command finishes.
 *
 * The environment variable TMPDIR can be used to set the temporary 
 * directory. If it is not set, it defaults to "/tmp".
 *
 * In all cases, @log must be freed with im_free().
 *
 * See also: im_system_image().
 *
 * Returns: 0 on success, -1 on error
 */
int
im_system( IMAGE *im, const char *cmd, char **out )
{
	FILE *fp;

	if( !im_isfile( im ) ) {
		IMAGE *disc;

		if( !(disc = im__open_temp( "%s.v" )) )
			return( -1 );
		if( im_copy( im, disc ) ||
			im_system( disc, cmd, out ) ) {
			im_close( disc );
			return( -1 );
		}
		im_close( disc );
	}
	else if( (fp = im_popenf( cmd, "r", im->filename )) ) {
		char line[IM_MAX_STRSIZE];
		char txt[IM_MAX_STRSIZE];
		VipsBuf buf = VIPS_BUF_STATIC( txt );

		while( fgets( line, IM_MAX_STRSIZE, fp ) ) 
			if( !vips_buf_appends( &buf, line ) )
				break; 
		pclose( fp );

		if( out )
			*out = im_strdup( NULL, vips_buf_all( &buf ) );
	}

	return( 0 );
}
/* Init function for string input.
 */
static int
input_string_init( im_object *obj, char *str )
{
	if( !(*obj = (im_object) im_strdup( NULL, str )) ) 
		return( -1 );

	return( 0 );
}
/* Init function for masks. "str" can be NULL for output masks.
 */
static int
mask_init( im_object *obj, char *str )
{
	im_mask_object *mo = *obj;

	/* Install string, clear mask.
	 */
	if( str && !(mo->name = im_strdup( NULL, str )) ) 
		return( -1 );
	mo->mask = NULL;

	return( 0 );
}
Exemple #6
0
static Read *
read_new( const char *filename, IMAGE *out )
{
	Read *read;

	if( !(read = IM_NEW( NULL, Read )) )
		return( NULL );

	read->filename = im_strdup( NULL, filename );
	read->out = out;
	read->mat = NULL;
	read->var = NULL;

	if( !(read->mat = Mat_Open( filename, MAT_ACC_RDONLY )) ) {
		im_error( "im_mat2vips", 
			_( "unable to open \"%s\"" ), filename );
		read_destroy( read );
		return( NULL );
	}

	for(;;) {
		if( !(read->var = Mat_VarReadNextInfo( read->mat )) ) {
			im_error( "im_mat2vips", 
				_( "no matrix variables in \"%s\"" ), 
				filename );
			read_destroy( read );
			return( NULL );
		}

#ifdef DEBUG
		printf( "im_mat2vips: seen:\n" );
		printf( "var->name == %s\n", read->var->name );
		printf( "var->class_type == %d\n", read->var->class_type );
		printf( "var->rank == %d\n", read->var->rank );
#endif /*DEBUG*/

		/* Vector to colour image is OK for us.
		 */
		if( read->var->rank >= 1 && read->var->rank <= 3 )
			break;

		IM_FREEF( Mat_VarFree, read->var );
	}

	return( read );
}
/* Make a leaf for a file.
 */
static JoinNode *
build_node( SymbolTable *st, char *name )
{
	JoinNode *node = IM_NEW( st->im, JoinNode );
	int n = hash( name );

	/* Fill fields.
	 */
	if( !node || !(node->name = im_strdup( st->im, name )) )
		return( NULL );

	node->type = JOIN_LEAF;
	node->dirty = 0;
	node->mwidth = -2;
	node->st = st;
	im__transform_init( &node->cumtrn );
	node->trnim = NULL;
	node->arg1 = NULL;
	node->arg2 = NULL;
	node->overlaps = NULL;
	node->im = NULL;
	node->index = 0;

        if( im_add_close_callback( st->im, 
		(im_callback_fn) junk_node, node, NULL ) ) 
                return( NULL );

	/* Try to open.
	 */
	if( (node->im = im__global_open_image( st, name )) ) {
		/* There is a file there - set width and height.
		 */
		node->cumtrn.oarea.width = node->im->Xsize;
		node->cumtrn.oarea.height = node->im->Ysize;
	}
	else {
		/* Clear the error buffer to lessen confusion.
		 */
		im_error_clear();
	}

	st->table[n] = g_slist_prepend( st->table[n], node );

	return( node );
}
Exemple #8
0
static Read *
read_new( const char *filename, IMAGE *im )
{
	Read *read;
	static int inited = 0;

	if( !inited ) {
#ifdef HAVE_MAGICKCOREGENESIS
		MagickCoreGenesis( im_get_argv0(), MagickFalse );
#else /*!HAVE_MAGICKCOREGENESIS*/
		InitializeMagick( "" );
#endif /*HAVE_MAGICKCOREGENESIS*/
		inited = 1;
	}

	if( !(read = IM_NEW( NULL, Read )) )
		return( NULL );
	read->filename = im_strdup( NULL, filename );
	read->im = im;
	read->image = NULL;
	read->image_info = CloneImageInfo( NULL );
	GetExceptionInfo( &read->exception );
	read->n_frames = 0;
	read->frames = NULL;
	read->frame_height = 0;
	read->lock = g_mutex_new();

	if( im_add_close_callback( im,
		(im_callback_fn) read_destroy, read, NULL ) ) {
		read_destroy( read );
		return( NULL );
	}

	if( !read->filename || !read->image_info ) 
		return( NULL );

	im_strncpy( read->image_info->filename, filename, MaxTextExtent );

#ifdef DEBUG
	printf( "im_magick2vips: read_new: %s\n", read->filename );
#endif /*DEBUG*/

	return( read );
}
Exemple #9
0
static Read *
read_new( const char *name, IMAGE *out )
{
	Read *read;

	if( !(read = IM_NEW( NULL, Read )) )
		return( NULL );

	read->name = im_strdup( NULL, name );
	read->out = out;
	read->fp = NULL;
	read->pPng = NULL;
	read->pInfo = NULL;
	read->row_pointer = NULL;
	read->data = NULL;

        if( !(read->fp = im__file_open_read( name, NULL )) ) {
		read_destroy( read );
		return( NULL );
	}

	if( !(read->pPng = png_create_read_struct( 
		PNG_LIBPNG_VER_STRING, NULL,
		user_error_function, user_warning_function )) ) {
		read_destroy( read );
		return( NULL );
	}

	/* Catch PNG errors from png_create_info_struct().
	 */
	if( setjmp( read->pPng->jmpbuf ) ) {
		read_destroy( read );
		return( NULL );
	}

	if( !(read->pInfo = png_create_info_struct( read->pPng )) ) {
		read_destroy( read );
		return( NULL );
	}

	return( read );
}
Exemple #10
0
/**
 * im_create_dmask:
 * @filename: set mask filename to this
 * @xsize: mask width
 * @ysize: mask height
 *
 * Create an empty dmask. You need to loop over @coeff to set the values. 
 *
 * See also: im_create_dmaskv(), im_vips2mask().
 *
 * Returns: The newly-allocated mask.
 */
DOUBLEMASK *
im_create_dmask( const char *filename, int xsize, int ysize )
{	
	DOUBLEMASK *out;
	int size = xsize * ysize;

	/* Check args.
	 */
	if( xsize <= 0 || ysize <= 0 || filename == NULL ) { 
		im_error( "im_create_dmask", "%s", _( "bad arguments" ) ); 
		return( NULL );
	}

	/* Allocate and initialise structure.
	 */
	if( !(out = IM_NEW( NULL, DOUBLEMASK )) ) 
		return( NULL );
	out->coeff = NULL;
	out->filename = NULL;
	out->scale = 1.0;
	out->offset = 0.0;
	out->xsize = 0;
	out->ysize = 0;

	if( !(out->coeff = IM_ARRAY( NULL, size, double )) ) {
		im_free_dmask( out );
		return( NULL );
	}
	(void) memset( (char *) out->coeff, 0, size * sizeof( double ) );
	if( !(out->filename = im_strdup( NULL, filename )) ) {
		im_free_dmask( out );
		return( NULL );
	}
	out->xsize = xsize; 
	out->ysize = ysize;

	return( out );
}
Exemple #11
0
/* Do "search".
 */
static void
apply_search_call( Reduce *rc, const char *name, HeapNode **arg, PElement *out )
{
	char buf[MAX_STRSIZE];
	PElement rhs;
	char *fn;

	/* Get string. 
	 */
	PEPOINTRIGHT( arg[0], &rhs );
	(void) reduce_get_string( rc, &rhs, buf, MAX_STRSIZE );

	if( !(fn = path_find_file( buf )) )
		/* If not found, return [].
		 */
		fn = im_strdup( NULL, "" );

	if( !heap_managedstring_new( rc->heap, fn, out ) ) {
		IM_FREE( fn );
		reduce_throw( rc );
	}
	IM_FREE( fn );
}
Exemple #12
0
static VipsFits *
vips_fits_new_write( VipsImage *in, const char *filename )
{
	VipsImage *flip;
	VipsImage *type;
	VipsFits *fits;
	int status;

	status = 0;

	if( im_check_noncomplex( "im_vips2fits", in ) ||
		im_check_uncoded( "im_vips2fits", in ) )
		return( NULL );

	/* Cast to a supported format.
	 */
	if( !(type = vips_image_new()) ||
		vips_object_local( in, type ) ||
		im_clip2fmt( in, type, vips_fits_bandfmt[in->BandFmt] ) )
		return( NULL );
	in = type;

	/* FITS has (0,0) in the bottom left, we need to flip.
	 */
	if( !(flip = vips_image_new()) ||
		vips_object_local( in, flip ) ||
		im_flipver( in, flip ) )
		return( NULL );
	in = flip;

	if( !(fits = VIPS_NEW( in, VipsFits )) )
		return( NULL );
	fits->filename = im_strdup( NULL, filename );
	fits->image = in;
	fits->fptr = NULL;
	fits->lock = NULL;
	fits->band_select = -1;
	fits->buffer = NULL;
	g_signal_connect( in, "close", 
		G_CALLBACK( vips_fits_close_cb ), fits );

	if( !(fits->filename = im_strdup( NULL, filename )) )
		return( NULL );

	/* We need to be able to hold one scanline of one band.
	 */
	if( !(fits->buffer = VIPS_ARRAY( NULL, 
		VIPS_IMAGE_SIZEOF_ELEMENT( in ) * in->Xsize, PEL )) )
		return( NULL );

	/* fits_create_file() will fail if there's a file of thet name, unless
	 * we put a "!" in front ofthe filename. This breaks conventions with
	 * the rest of vips, so just unlink explicitly.
	 */
	g_unlink( filename );

	if( fits_create_file( &fits->fptr, filename, &status ) ) {
		im_error( "fits", _( "unable to write to \"%s\"" ), filename );
		vips_fits_error( status );
		return( NULL );
	}

	fits->lock = g_mutex_new();

	return( fits );
}
Exemple #13
0
int
im_vips2tiff( IMAGE *in, const char *filename )
{
    char *p, *q, *r;
    char name[FILENAME_MAX];
    char mode[FILENAME_MAX];
    char buf[FILENAME_MAX];

    VipsForeignTiffCompression compression =
        VIPS_FOREIGN_TIFF_COMPRESSION_NONE;
    int Q = 75;
    VipsForeignTiffPredictor predictor = VIPS_FOREIGN_TIFF_PREDICTOR_NONE;
    char *profile = NULL;
    gboolean tile = FALSE;
    int tile_width = 128;
    int tile_height = 128;
    gboolean pyramid = FALSE;
    gboolean squash = FALSE;
    VipsForeignTiffResunit resunit = VIPS_FOREIGN_TIFF_RESUNIT_CM;
    double xres = in->Xres * 10.0;
    double yres = in->Yres * 10.0;
    gboolean bigtiff = FALSE;

    im_filename_split( filename, name, mode );
    strcpy( buf, mode );
    p = &buf[0];
    if( (q = im_getnextoption( &p )) ) {
        if( im_isprefix( "none", q ) )
            compression = VIPS_FOREIGN_TIFF_COMPRESSION_NONE;
        else if( im_isprefix( "packbits", q ) )
            compression = VIPS_FOREIGN_TIFF_COMPRESSION_PACKBITS;
        else if( im_isprefix( "ccittfax4", q ) )
            compression = VIPS_FOREIGN_TIFF_COMPRESSION_CCITTFAX4;
        else if( im_isprefix( "lzw", q ) ) {
            compression = VIPS_FOREIGN_TIFF_COMPRESSION_LZW;

            if( (r = im_getsuboption( q )) ) {
                int i;

                if( sscanf( r, "%d", &i ) != 1 ) {
                    im_error( "im_vips2tiff",
                              "%s", _( "bad predictor "
                                       "parameter" ) );
                    return( -1 );
                }
                predictor = i;
            }
        }
        else if( im_isprefix( "deflate", q ) ) {
            compression = VIPS_FOREIGN_TIFF_COMPRESSION_DEFLATE;

            if( (r = im_getsuboption( q )) ) {
                int i;

                if( sscanf( r, "%d", &i ) != 1 ) {
                    im_error( "im_vips2tiff",
                              "%s", _( "bad predictor "
                                       "parameter" ) );
                    return( -1 );
                }
                predictor = i;
            }
        }
        else if( im_isprefix( "jpeg", q ) ) {
            compression = VIPS_FOREIGN_TIFF_COMPRESSION_JPEG;

            if( (r = im_getsuboption( q )) )
                if( sscanf( r, "%d", &Q ) != 1 ) {
                    im_error( "im_vips2tiff",
                              "%s", _( "bad JPEG quality "
                                       "parameter" ) );
                    return( -1 );
                }
        }
        else {
            im_error( "im_vips2tiff", _( "unknown compression mode "
                                         "\"%s\"\nshould be one of \"none\", "
                                         "\"packbits\", \"ccittfax4\", \"lzw\", "
                                         "\"deflate\" or \"jpeg\"" ), q );
            return( -1 );
        }
    }

    if( (q = im_getnextoption( &p )) ) {
        if( im_isprefix( "tile", q ) ) {
            tile = TRUE;

            if( (r = im_getsuboption( q )) ) {
                if( sscanf( r, "%dx%d",
                            &tile_width, &tile_height ) != 2 ) {
                    im_error( "im_vips2tiff", "%s",
                              _( "bad tile sizes" ) );
                    return( -1 );
                }
            }
        }
        else if( im_isprefix( "strip", q ) )
            tile = FALSE;
        else {
            im_error( "im_vips2tiff", _( "unknown layout mode "
                                         "\"%s\"\nshould be one of \"tile\" or "
                                         "\"strip\"" ), q );
            return( -1 );
        }
    }

    if( (q = im_getnextoption( &p )) ) {
        if( im_isprefix( "pyramid", q ) )
            pyramid = TRUE;
        else if( im_isprefix( "flat", q ) )
            pyramid = FALSE;
        else {
            im_error( "im_vips2tiff", _( "unknown multi-res mode "
                                         "\"%s\"\nshould be one of \"flat\" or "
                                         "\"pyramid\"" ), q );
            return( -1 );
        }
    }

    if( (q = im_getnextoption( &p )) ) {
        if( im_isprefix( "onebit", q ) )
            squash = TRUE;
        else if( im_isprefix( "manybit", q ) )
            squash = FALSE;
        else {
            im_error( "im_vips2tiff", _( "unknown format "
                                         "\"%s\"\nshould be one of \"onebit\" or "
                                         "\"manybit\"" ), q );
            return( -1 );
        }
    }

    if( (q = im_getnextoption( &p )) ) {
        if( im_isprefix( "res_cm", q ) )
            resunit = VIPS_FOREIGN_TIFF_RESUNIT_CM;
        else if( im_isprefix( "res_inch", q ) )
            resunit = VIPS_FOREIGN_TIFF_RESUNIT_INCH;
        else {
            im_error( "im_vips2tiff", _( "unknown resolution unit "
                                         "\"%s\"\nshould be one of \"res_cm\" or "
                                         "\"res_inch\"" ), q );
            return( -1 );
        }

        if( (r = im_getsuboption( q )) ) {
            if( sscanf( r, "%lfx%lf", &xres, &yres ) != 2 ) {
                if( sscanf( r, "%lf", &xres ) != 1 ) {
                    im_error( "im_vips2tiff", "%s",
                              _( "bad resolution values" ) );
                    return( -1 );
                }

                yres = xres;
            }

            /* vips resolutions are always in pixels/mm. If the
             * user specifies ",res_inch:72x72" then they are
             * using pixels/inch instead and we must convert.
             */
            if( resunit == VIPS_FOREIGN_TIFF_RESUNIT_INCH ) {
                xres /= 2.54;
                yres /= 2.54;
            }
        }
    }

    if( (q = im_getnextoption( &p )) && strcmp( q, "" ) != 0 )
        profile = im_strdup( NULL, q );

    if( (q = im_getnextoption( &p )) && strcmp( q, "8" ) == 0 )
        bigtiff = TRUE;

    if( (q = im_getnextoption( &p )) ) {
        im_error( "im_vips2tiff",
                  _( "unknown extra options \"%s\"" ), q );
        return( -1 );
    }

    if( vips_tiffsave( in, name,
                       "compression", compression,
                       "Q", Q,
                       "predictor", predictor,
                       "profile", profile,
                       "tile", tile,
                       "tile_width", tile_width,
                       "tile_height", tile_height,
                       "pyramid", pyramid,
                       "squash", squash,
                       "resunit", resunit,
                       "xres", xres,
                       "yres", yres,
                       "bigtiff", bigtiff,
                       NULL ) )
        return( -1 );

    return( 0 );
}