rewrite matrixsave

uses new target API
This commit is contained in:
John Cupitt 2020-02-22 17:55:04 +00:00
parent ce240b1ca2
commit 1a0e61510b
6 changed files with 246 additions and 491 deletions

View File

@ -5,7 +5,8 @@
- allow \ as an escape character in vips_break_token() [akemrir]
- tiffsave has a "depth" param to set max pyr depth
- libtiff LOGLUV images load and save as libvips XYZ
- add gifload_source, csvload_source, csvsave_target, matrixload_source
- add gifload_source, csvload_source, csvsave_target, matrixload_source,
matrixsave_source
- revise vipsthumbnail flags
- add VIPS_LEAK env var
- add vips_pipe_read_limit_set(), --vips-pipe-read-limit,

View File

@ -18,7 +18,6 @@ libforeign_la_SOURCES = \
radsave.c \
ppmload.c \
ppmsave.c \
csv.c \
csvload.c \
csvsave.c \
matrixload.c \

View File

@ -1,431 +0,0 @@
/* Read/write csv files.
*
* 19/12/05 JC
* - hacked from ppm reader
* 9/6/06
* - hacked from im_debugim
* 11/9/06
* - now distingushes whitespace and separators, so we can have blank
* fields
* 20/9/06
* - oop, unquoted trailing columns could get missed
* 23/10/06
* - allow separator to be specified (default "\t", <tab>)
* 17/11/06
* - oops, was broken
* 17/5/07
* - added im_csv2vips_header()
* 4/2/10
* - gtkdoc
* 1/3/10
* - allow lines that end with EOF
* 23/9/11
* - allow quoted strings, including escaped quotes
* 16/12/11
* - rework as a set of fns ready for wrapping as a class
* 23/2/12
* - report positions for EOF/EOL errors
* 2/7/13
* - add matrix read/write
* 4/6/15
* - try to support DOS files under linux ... we have to look for \r\n
* linebreaks
* 12/8/16
* - allow missing offset and scale in matrix header
*/
/*
This file is part of VIPS.
VIPS 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 of the License, or
(at your option) any later version.
This program 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 this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
/*
These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <vips/intl.h>
#include <ctype.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <vips/vips.h>
#include "pforeign.h"
/* Skip to the start of the next line (ie. read until we see a '\n'), return
* zero if we are at EOF.
*
* Files can end with EOF or with \nEOF. Tricky!
*/
static int
skip_line( FILE *fp )
{
int ch;
/* Are we at a delayed EOF? See below.
*/
if( (ch = vips__fgetc( fp )) == EOF )
return( 0 );
ungetc( ch, fp );
/* If we hit EOF and no \n, wait until the next call to report EOF.
*/
while( (ch = vips__fgetc( fp )) != '\n' &&
ch != EOF )
;
return( -1 );
}
static int
skip_white( FILE *fp, const char whitemap[256] )
{
int ch;
do {
ch = vips__fgetc( fp );
} while( ch != EOF &&
ch != '\n' &&
whitemap[ch] );
ungetc( ch, fp );
return( ch );
}
/* Read to non-whitespace, or buffer overflow.
*/
static int
fetch_nonwhite( FILE *fp, const char whitemap[256], char *buf, int max )
{
int ch;
int i;
for( i = 0; i < max - 1; i++ ) {
ch = vips__fgetc( fp );
if( ch == EOF ||
ch == '\n' ||
whitemap[ch] )
break;
buf[i] = ch;
}
buf[i] = '\0';
/* We mustn't skip the terminator.
*/
ungetc( ch, fp );
return( ch );
}
/* Read a single double in ascii (not locale) encoding.
*
* Return the char that caused failure on fail (EOF or \n).
*/
static int
read_ascii_double( FILE *fp, const char whitemap[256], double *out )
{
int ch;
char buf[256];
char *p;
*out = 0.0;
ch = skip_white( fp, whitemap );
if( ch == EOF ||
ch == '\n' )
return( ch );
fetch_nonwhite( fp, whitemap, buf, 256 );
/* The str we fetched must contain at least 1 digit. This helps stop
* us trying to convert "MATLAB" (for example) to a number and
* getting zero.
*/
for( p = buf; *p; p++ )
if( isdigit( *p ) )
break;
if( !*p )
return( *buf );
*out = g_ascii_strtod( buf, NULL );
return( 0 );
}
/* Read the header. Two numbers for width and height, and two optional
* numbers for scale and offset.
*
* We can have scale and no offset, in which case we assume offset = 0.
*/
static int
vips__matrix_header( char *whitemap, FILE *fp,
int *width, int *height, double *scale, double *offset )
{
double header[4];
double d;
int i;
int ch;
for( i = 0; i < 4 &&
(ch = read_ascii_double( fp, whitemap, &header[i] )) == 0;
i++ )
;
if( i < 4 )
header[3] = 0.0;
if( i < 3 )
header[2] = 1.0;
if( i < 2 ) {
vips_error( "mask2vips", "%s", _( "no width / height" ) );
return( -1 );
}
if( VIPS_FLOOR( header[0] ) != header[0] ||
VIPS_FLOOR( header[1] ) != header[1] ) {
vips_error( "mask2vips", "%s", _( "width / height not int" ) );
return( -1 );
}
*width = header[0];
*height = header[1];
if( *width <= 0 ||
*width > 100000 ||
*height <= 0 ||
*height > 100000 ) {
vips_error( "mask2vips",
"%s", _( "width / height out of range" ) );
return( -1 );
}
if( (ch = read_ascii_double( fp, whitemap, &d )) != '\n' ) {
vips_error( "mask2vips", "%s", _( "extra chars in header" ) );
return( -1 );
}
if( header[2] == 0.0 ) {
vips_error( "mask2vips", "%s", _( "zero scale" ) );
return( -1 );
}
*scale = header[2];
*offset = header[3];
skip_line( fp );
return( 0 );
}
#define WHITESPACE " \"\t\n;,"
/* Get the header from an matrix file.
*
* Also read the first line and make sure there are the right number of
* entries.
*/
int
vips__matrix_read_header( const char *filename,
int *width, int *height, double *scale, double *offset )
{
char whitemap[256];
int i;
char *p;
FILE *fp;
int ch;
double d;
for( i = 0; i < 256; i++ )
whitemap[i] = 0;
for( p = WHITESPACE; *p; p++ )
whitemap[(int) *p] = 1;
if( !(fp = vips__file_open_read( filename, NULL, TRUE )) )
return( -1 );
if( vips__matrix_header( whitemap, fp,
width, height, scale, offset ) ) {
fclose( fp );
return( -1 );
}
for( i = 0; i < *width; i++ ) {
ch = read_ascii_double( fp, whitemap, &d );
if( ch ) {
fclose( fp );
vips_error( "mask2vips", "%s", _( "line too short" ) );
return( -1 );
}
}
/* Deliberately don't check for line too long.
*/
fclose( fp );
return( 0 );
}
int
vips__matrix_ismatrix( const char *filename )
{
int width;
int height;
double scale;
double offset;
int result;
vips_error_freeze();
result = vips__matrix_read_header( filename,
&width, &height, &scale, &offset );
vips_error_thaw();
return( result == 0 );
}
static int
vips__matrix_body( char *whitemap, VipsImage *out, FILE *fp )
{
int x, y;
for( y = 0; y < out->Ysize; y++ ) {
for( x = 0; x < out->Xsize; x++ ) {
int ch;
double d;
ch = read_ascii_double( fp, whitemap, &d );
if( ch == EOF ||
ch == '\n' ) {
vips_error( "mask2vips",
_( "line %d too short" ), y + 1 );
return( -1 );
}
*VIPS_MATRIX( out, x, y ) = d;
/* Deliberately don't check for line too long.
*/
}
skip_line( fp );
}
return( 0 );
}
VipsImage *
vips__matrix_read_file( FILE *fp )
{
char whitemap[256];
int i;
char *p;
int width;
int height;
double scale;
double offset;
VipsImage *out;
for( i = 0; i < 256; i++ )
whitemap[i] = 0;
for( p = WHITESPACE; *p; p++ )
whitemap[(int) *p] = 1;
if( vips__matrix_header( whitemap, fp,
&width, &height, &scale, &offset ) )
return( NULL );
if( !(out = vips_image_new_matrix( width, height )) )
return( NULL );
vips_image_set_double( out, "scale", scale );
vips_image_set_double( out, "offset", offset );
if( vips__matrix_body( whitemap, out, fp ) ) {
g_object_unref( out );
return( NULL );
}
return( out );
}
VipsImage *
vips__matrix_read( const char *filename )
{
FILE *fp;
VipsImage *out;
if( !(fp = vips__file_open_read( filename, NULL, TRUE )) )
return( NULL );
out = vips__matrix_read_file( fp );
fclose( fp );
return( out );
}
int
vips__matrix_write_file( VipsImage *in, FILE *fp )
{
VipsImage *mask;
int x, y;
if( vips_check_matrix( "vips2mask", in, &mask ) )
return( -1 );
fprintf( fp, "%d %d ", mask->Xsize, mask->Ysize );
if( vips_image_get_typeof( mask, "scale" ) &&
vips_image_get_typeof( mask, "offset" ) )
fprintf( fp, "%g %g ",
vips_image_get_scale( mask ),
vips_image_get_offset( mask ) );
fprintf( fp, "\n" );
for( y = 0; y < mask->Ysize; y++ ) {
for( x = 0; x < mask->Xsize; x++ )
fprintf( fp, "%g ", *VIPS_MATRIX( mask, x, y ) );
fprintf( fp, "\n" );
}
g_object_unref( mask );
return( 0 );
}
int
vips__matrix_write( VipsImage *in, const char *filename )
{
FILE *fp;
int result;
if( !(fp = vips__file_open_write( filename, TRUE )) )
return( -1 );
result = vips__matrix_write_file( in, fp );
fclose( fp );
return( result );
}
const char *vips__foreign_matrix_suffs[] = { ".mat", NULL };

View File

@ -2041,7 +2041,8 @@ vips_foreign_operation_init( void )
extern GType vips_foreign_load_matrix_file_get_type( void );
extern GType vips_foreign_load_matrix_source_get_type( void );
extern GType vips_foreign_save_matrix_get_type( void );
extern GType vips_foreign_save_matrix_file_get_type( void );
extern GType vips_foreign_save_matrix_target_get_type( void );
extern GType vips_foreign_print_matrix_get_type( void );
extern GType vips_foreign_load_fits_get_type( void );
@ -2118,7 +2119,8 @@ vips_foreign_operation_init( void )
vips_foreign_save_csv_target_get_type();
vips_foreign_load_matrix_file_get_type();
vips_foreign_load_matrix_source_get_type();
vips_foreign_save_matrix_get_type();
vips_foreign_save_matrix_file_get_type();
vips_foreign_save_matrix_target_get_type();
vips_foreign_print_matrix_get_type();
vips_foreign_load_raw_get_type();
vips_foreign_save_raw_get_type();

View File

@ -1,7 +1,9 @@
/* save to matrix
*
* 2/7/13
* 2/12/11
* - wrap a class around the matrix writer
* 21/2/20
* - rewrite for the VipsTarget API
*/
/*
@ -52,10 +54,9 @@
typedef struct _VipsForeignSaveMatrix {
VipsForeignSave parent_object;
/* Filename for save.
*/
char *filename;
VipsTarget *target;
const char *separator;
} VipsForeignSaveMatrix;
typedef VipsForeignSaveClass VipsForeignSaveMatrixClass;
@ -63,17 +64,76 @@ typedef VipsForeignSaveClass VipsForeignSaveMatrixClass;
G_DEFINE_TYPE( VipsForeignSaveMatrix, vips_foreign_save_matrix,
VIPS_TYPE_FOREIGN_SAVE );
static void
vips_foreign_save_matrix_dispose( GObject *gobject )
{
VipsForeignSaveMatrix *matrix = (VipsForeignSaveMatrix *) gobject;
if( matrix->target )
vips_target_finish( matrix->target );
VIPS_UNREF( matrix->target );
G_OBJECT_CLASS( vips_foreign_save_matrix_parent_class )->
dispose( gobject );
}
static int
vips_foreign_save_matrix_block( VipsRegion *region, VipsRect *area, void *a )
{
VipsForeignSaveMatrix *matrix = (VipsForeignSaveMatrix *) a;
int x, y;
for( y = 0; y < area->height; y++ ) {
double *p = (double *)
VIPS_REGION_ADDR( region, 0, area->top + y );
char buf[G_ASCII_DTOSTR_BUF_SIZE];
for( x = 0; x < area->width; x++ ) {
if( x > 0 )
vips_target_writes( matrix->target, " " );
g_ascii_dtostr( buf, G_ASCII_DTOSTR_BUF_SIZE, p[x] );
vips_target_writes( matrix->target, buf );
}
if( vips_target_writes( matrix->target, "\n" ) )
return( -1 );
}
return( 0 );
}
static int
vips_foreign_save_matrix_build( VipsObject *object )
{
VipsForeignSave *save = (VipsForeignSave *) object;
VipsForeignSaveMatrix *matrix = (VipsForeignSaveMatrix *) object;
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
double scale;
double offset;
if( VIPS_OBJECT_CLASS( vips_foreign_save_matrix_parent_class )->
build( object ) )
return( -1 );
if( vips__matrix_write( save->ready, matrix->filename ) )
if( vips_check_mono( class->nickname, save->ready ) ||
vips_check_uncoded( class->nickname, save->ready ) )
return( -1 );
vips_target_writef( matrix->target, "%d %d",
save->ready->Xsize, save->ready->Ysize );
scale = vips_image_get_scale( save->ready );
offset = vips_image_get_offset( save->ready );
if( scale != 1.0 || offset != 0.0 )
vips_target_writef( matrix->target, " %g %g", scale, offset );
if( vips_target_writes( matrix->target, "\n" ) )
return( -1 );
if( vips_sink_disc( save->ready,
vips_foreign_save_matrix_block, matrix ) )
return( -1 );
return( 0 );
@ -102,31 +162,175 @@ vips_foreign_save_matrix_class_init( VipsForeignSaveMatrixClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignSaveClass *save_class = (VipsForeignSaveClass *) class;
gobject_class->dispose = vips_foreign_save_matrix_dispose;
object_class->nickname = "matrixsave_base";
object_class->description = _( "save image to matrix" );
object_class->build = vips_foreign_save_matrix_build;
save_class->saveable = VIPS_SAVEABLE_MONO;
save_class->format_table = bandfmt_matrix;
}
static void
vips_foreign_save_matrix_init( VipsForeignSaveMatrix *matrix )
{
}
typedef struct _VipsForeignSaveMatrixFile {
VipsForeignSaveMatrix parent_object;
char *filename;
} VipsForeignSaveMatrixFile;
typedef VipsForeignSaveMatrixClass VipsForeignSaveMatrixFileClass;
G_DEFINE_TYPE( VipsForeignSaveMatrixFile, vips_foreign_save_matrix_file,
vips_foreign_save_matrix_get_type() );
static int
vips_foreign_save_matrix_file_build( VipsObject *object )
{
VipsForeignSaveMatrix *matrix = (VipsForeignSaveMatrix *) object;
VipsForeignSaveMatrixFile *file = (VipsForeignSaveMatrixFile *) object;
if( file->filename &&
!(matrix->target = vips_target_new_to_file( file->filename )) )
return( -1 );
return( VIPS_OBJECT_CLASS(
vips_foreign_save_matrix_file_parent_class )->build( object ) );
}
static const char *vips_foreign_save_matrix_file_suffs[] = {
".mat",
NULL
};
static void
vips_foreign_save_matrix_file_class_init( VipsForeignSaveMatrixFileClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "matrixsave";
object_class->description = _( "save image to matrix file" );
object_class->build = vips_foreign_save_matrix_build;
object_class->build = vips_foreign_save_matrix_file_build;
foreign_class->suffs = vips__foreign_matrix_suffs;
save_class->saveable = VIPS_SAVEABLE_MONO;
save_class->format_table = bandfmt_matrix;
foreign_class->suffs = vips_foreign_save_matrix_file_suffs;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveMatrix, filename ),
G_STRUCT_OFFSET( VipsForeignSaveMatrixFile, filename ),
NULL );
}
static void
vips_foreign_save_matrix_init( VipsForeignSaveMatrix *matrix )
vips_foreign_save_matrix_file_init( VipsForeignSaveMatrixFile *file )
{
}
typedef struct _VipsForeignSaveMatrixTarget {
VipsForeignSaveMatrix parent_object;
VipsTarget *target;
} VipsForeignSaveMatrixTarget;
typedef VipsForeignSaveMatrixClass VipsForeignSaveMatrixTargetClass;
G_DEFINE_TYPE( VipsForeignSaveMatrixTarget, vips_foreign_save_matrix_target,
vips_foreign_save_matrix_get_type() );
static int
vips_foreign_save_matrix_target_build( VipsObject *object )
{
VipsForeignSaveMatrix *matrix = (VipsForeignSaveMatrix *) object;
VipsForeignSaveMatrixTarget *target =
(VipsForeignSaveMatrixTarget *) object;
if( target->target ) {
matrix->target = target->target;
g_object_ref( matrix->target );
}
return( VIPS_OBJECT_CLASS(
vips_foreign_save_matrix_target_parent_class )->
build( object ) );
}
static void
vips_foreign_save_matrix_target_class_init(
VipsForeignSaveMatrixTargetClass *class )
{
GObjectClass *gobject_class = G_OBJECT_CLASS( class );
VipsObjectClass *object_class = (VipsObjectClass *) class;
gobject_class->set_property = vips_object_set_property;
gobject_class->get_property = vips_object_get_property;
object_class->nickname = "matrixsave_target";
object_class->build = vips_foreign_save_matrix_target_build;
VIPS_ARG_OBJECT( class, "target", 1,
_( "Target" ),
_( "Target to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveMatrixTarget, target ),
VIPS_TYPE_TARGET );
}
static void
vips_foreign_save_matrix_target_init( VipsForeignSaveMatrixTarget *target )
{
}
typedef struct _VipsForeignPrintMatrix {
VipsForeignSaveMatrix parent_object;
} VipsForeignPrintMatrix;
typedef VipsForeignSaveClass VipsForeignPrintMatrixClass;
G_DEFINE_TYPE( VipsForeignPrintMatrix, vips_foreign_print_matrix,
vips_foreign_save_matrix_get_type() );
static int
vips_foreign_print_matrix_build( VipsObject *object )
{
VipsForeignSaveMatrix *matrix = (VipsForeignSaveMatrix *) object;
if( !(matrix->target = vips_target_new_to_descriptor( 0 )) )
return( -1 );
if( VIPS_OBJECT_CLASS( vips_foreign_print_matrix_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_print_matrix_class_init( VipsForeignPrintMatrixClass *class )
{
VipsObjectClass *object_class = (VipsObjectClass *) class;
object_class->nickname = "matrixprint";
object_class->description = _( "print matrix" );
object_class->build = vips_foreign_print_matrix_build;
}
static void
vips_foreign_print_matrix_init( VipsForeignPrintMatrix *matrix )
{
}
@ -156,51 +360,29 @@ vips_matrixsave( VipsImage *in, const char *filename, ... )
return( result );
}
typedef struct _VipsForeignPrintMatrix {
VipsForeignSave parent_object;
} VipsForeignPrintMatrix;
typedef VipsForeignSaveClass VipsForeignPrintMatrixClass;
G_DEFINE_TYPE( VipsForeignPrintMatrix, vips_foreign_print_matrix,
VIPS_TYPE_FOREIGN_SAVE );
static int
vips_foreign_print_matrix_build( VipsObject *object )
/**
* vips_matrixsave_target: (method)
* @in: image to save
* @target: save image to this target
* @...: %NULL-terminated list of optional named arguments
*
* As vips_matrixsave(), but save to a target.
*
* See also: vips_matrixsave().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_matrixsave_target( VipsImage *in, VipsTarget *target, ... )
{
VipsForeignSave *save = (VipsForeignSave *) object;
va_list ap;
int result;
if( VIPS_OBJECT_CLASS( vips_foreign_print_matrix_parent_class )->
build( object ) )
return( -1 );
va_start( ap, target );
result = vips_call_split( "matrixsave_target", ap, in, target );
va_end( ap );
if( vips__matrix_write_file( save->ready, stdout ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_print_matrix_class_init( VipsForeignPrintMatrixClass *class )
{
VipsObjectClass *object_class = (VipsObjectClass *) class;
VipsForeignClass *foreign_class = (VipsForeignClass *) class;
VipsForeignSaveClass *save_class = (VipsForeignSaveClass *) class;
object_class->nickname = "matrixprint";
object_class->description = _( "print matrix" );
object_class->build = vips_foreign_print_matrix_build;
foreign_class->suffs = vips__foreign_matrix_suffs;
save_class->saveable = VIPS_SAVEABLE_MONO;
save_class->format_table = bandfmt_matrix;
}
static void
vips_foreign_print_matrix_init( VipsForeignPrintMatrix *matrix )
{
return( result );
}
/**

View File

@ -537,6 +537,8 @@ int vips_matrixload_source( VipsSource *source, VipsImage **out, ... )
__attribute__((sentinel));
int vips_matrixsave( VipsImage *in, const char *filename, ... )
__attribute__((sentinel));
int vips_matrixsave_target( VipsImage *in, VipsTarget *target, ... )
__attribute__((sentinel));
int vips_matrixprint( VipsImage *in, ... )
__attribute__((sentinel));