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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
/* save to vips
*
* 24/11/11
*/
/*
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
*/
/*
#define DEBUG_VERBOSE
#define DEBUG
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vips/vips.h>
#include <vips/internal.h>
typedef struct _VipsForeignSaveVips {
VipsForeignSave parent_object;
VipsTarget *target;
} VipsForeignSaveVips;
typedef VipsForeignSaveClass VipsForeignSaveVipsClass;
G_DEFINE_ABSTRACT_TYPE( VipsForeignSaveVips, vips_foreign_save_vips,
VIPS_TYPE_FOREIGN_SAVE );
static void
vips_foreign_save_vips_dispose( GObject *gobject )
{
VipsForeignSaveVips *vips = (VipsForeignSaveVips *) gobject;
VIPS_UNREF( vips->target );
G_OBJECT_CLASS( vips_foreign_save_vips_parent_class )->
dispose( gobject );
}
static int
vips_foreign_save_vips_build( VipsObject *object )
{
VipsForeignSaveVips *vips = (VipsForeignSaveVips *) object;
const char *filename;
if( VIPS_OBJECT_CLASS( vips_foreign_save_vips_parent_class )->
build( object ) )
return( -1 );
if( (filename =
vips_connection_filename( VIPS_CONNECTION( vips->target ) )) ) {
VipsForeignSave *save = (VipsForeignSave *) object;
VipsImage *x;
/* vips_image_build() has some magic for "w"
* preventing recursion and sending this directly to the
* saver built into iofuncs.
*/
if( !(x = vips_image_new_mode( filename, "w" )) )
return( -1 );
if( vips_image_write( save->ready, x ) ) {
g_object_unref( x );
return( -1 );
}
g_object_unref( x );
}
else {
VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
/* We could add load vips from memory, fd, via mmap etc. here.
* We should perhaps move iofuncs/vips.c into this file.
*
* For now, just fail unless there's a filename associated
* with this source.
*/
vips_error( class->nickname,
"%s", _( "no filename associated with target" ) );
return( -1 );
}
if( vips_target_end( vips->target ) )
return( -1 );
return( 0 );
}
/* From vipsload.c.
*/
extern const char *vips__suffs[];
static void
vips_foreign_save_vips_class_init( VipsForeignSaveVipsClass *class )
{
int i;
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_vips_dispose;
object_class->nickname = "vipssave_base";
object_class->description = _( "save vips base class" );
object_class->build = vips_foreign_save_vips_build;
foreign_class->suffs = vips__suffs;
save_class->saveable = VIPS_SAVEABLE_ANY;
for( i = 0; i < VIPS_CODING_LAST; i++ )
save_class->coding[i] = TRUE;
}
static void
vips_foreign_save_vips_init( VipsForeignSaveVips *vips )
{
}
typedef struct _VipsForeignSaveVipsFile {
VipsForeignSaveVips parent_object;
char *filename;
} VipsForeignSaveVipsFile;
typedef VipsForeignSaveVipsClass VipsForeignSaveVipsFileClass;
G_DEFINE_TYPE( VipsForeignSaveVipsFile, vips_foreign_save_vips_file,
vips_foreign_save_vips_get_type() );
static int
vips_foreign_save_vips_file_build( VipsObject *object )
{
VipsForeignSaveVips *vips = (VipsForeignSaveVips *) object;
VipsForeignSaveVipsFile *file = (VipsForeignSaveVipsFile *) object;
if( !(vips->target = vips_target_new_to_file( file->filename )) )
return( -1 );
if( VIPS_OBJECT_CLASS( vips_foreign_save_vips_file_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_save_vips_file_class_init( VipsForeignSaveVipsFileClass *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 = "vipssave";
object_class->description = _( "save image to file in vips format" );
object_class->build = vips_foreign_save_vips_file_build;
VIPS_ARG_STRING( class, "filename", 1,
_( "Filename" ),
_( "Filename to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveVipsFile, filename ),
NULL );
}
static void
vips_foreign_save_vips_file_init( VipsForeignSaveVipsFile *file )
{
}
typedef struct _VipsForeignSaveVipsTarget {
VipsForeignSaveVips parent_object;
VipsTarget *target;
} VipsForeignSaveVipsTarget;
typedef VipsForeignSaveVipsClass VipsForeignSaveVipsTargetClass;
G_DEFINE_TYPE( VipsForeignSaveVipsTarget, vips_foreign_save_vips_target,
vips_foreign_save_vips_get_type() );
static int
vips_foreign_save_vips_target_build( VipsObject *object )
{
VipsForeignSaveVips *vips = (VipsForeignSaveVips *) object;
VipsForeignSaveVipsTarget *target =
(VipsForeignSaveVipsTarget *) object;
vips->target = target->target;
g_object_ref( vips->target );
if( VIPS_OBJECT_CLASS( vips_foreign_save_vips_target_parent_class )->
build( object ) )
return( -1 );
return( 0 );
}
static void
vips_foreign_save_vips_target_class_init(
VipsForeignSaveVipsTargetClass *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 = "vipssave_target";
object_class->description = _( "save image to target in vips format" );
object_class->build = vips_foreign_save_vips_target_build;
VIPS_ARG_OBJECT( class, "target", 1,
_( "Target" ),
_( "Target to save to" ),
VIPS_ARGUMENT_REQUIRED_INPUT,
G_STRUCT_OFFSET( VipsForeignSaveVipsTarget, target ),
VIPS_TYPE_TARGET );
}
static void
vips_foreign_save_vips_target_init( VipsForeignSaveVipsTarget *target )
{
}
/**
* vips_vipssave: (method)
* @in: image to save
* @filename: file to write to
* @...: %NULL-terminated list of optional named arguments
*
* Write @in to @filename in VIPS format.
*
* See also: vips_vipsload().
*
* Returns: 0 on success, -1 on error.
*/
int
vips_vipssave( VipsImage *in, const char *filename, ... )
{
va_list ap;
int result;
va_start( ap, filename );
result = vips_call_split( "vipssave", ap, in, filename );
va_end( ap );
return( result );
}
/**
* vips_vipssave_target: (method)
* @in: image to save
* @target: save image to this target
* @...: %NULL-terminated list of optional named arguments
*
* As vips_vipssave(), but save to a target.
*
* Returns: 0 on success, -1 on error.
*/
int
vips_vipssave_target( VipsImage *in, VipsTarget *target, ... )
{
va_list ap;
int result;
va_start( ap, target );
result = vips_call_split( "vipssave_target", ap, in, target );
va_end( ap );
return( result );
}
|