libvips/cplusplus/VImage.cc

197 lines
4.1 KiB
C++
Raw Normal View History

2014-10-20 12:50:34 +02:00
// Object part of VImage class
/*
Copyright (C) 1991-2001 The National Gallery
This program 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 <cstdlib>
#include <cstring>
#include <cstdio>
#include <vips/vips.h>
#include <vips/internal.h>
#include <vips/debug.h>
#include <vips/vipscpp.h>
#ifdef WITH_DMALLOC
#include <dmalloc.h>
#endif /*WITH_DMALLOC*/
/*
#define DEBUG
*/
VIPS_NAMESPACE_START
2014-10-20 15:54:03 +02:00
/* Useful to have these as namespaced C++ functions.
2014-10-20 12:50:34 +02:00
*/
2014-10-20 15:54:03 +02:00
void init( const char *argv0 )
2014-10-20 12:50:34 +02:00
{
2014-10-20 15:54:03 +02:00
if( vips_init( argv0 ) )
throw VError();
2014-10-20 12:50:34 +02:00
}
void shutdown()
{
vips_shutdown();
}
void thread_shutdown()
{
vips_thread_shutdown();
}
2014-10-20 15:54:03 +02:00
// see vips_image_new_from_file()
VImage::VImage( const char *name, ... )
__attribute__((sentinel)) throw( VError )
2014-10-20 12:50:34 +02:00
{
2014-10-20 15:54:03 +02:00
char filename[VIPS_PATH_MAX];
char option_string[VIPS_PATH_MAX];
const char *operation_name;
va_list ap;
int result;
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
vips_check_init();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
vips__filename_split8( name, filename, option_string );
if( !(operation_name = vips_foreign_find_load( filename )) )
throw VError();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
va_start( ap, name );
result = vips_call_split_option_string( operation_name,
option_string, ap, filename, &im );
va_end( ap );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
if( result )
throw VError();
2014-10-20 12:50:34 +02:00
}
2014-10-20 15:54:03 +02:00
// see vips_image_new_from_buffer()
VImage::VImage( void *buffer, size_t length, const char *option_string, ... )
2014-10-20 12:50:34 +02:00
{
2014-10-20 15:54:03 +02:00
const char *operation_name;
VipsBlob *blob;
va_list ap;
int result;
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
vips_check_init();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
if( !(operation_name =
vips_foreign_find_load_buffer( buffer, length )) )
throw VError();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
/* We don't take a copy of the data or free it.
2014-10-20 12:50:34 +02:00
*/
2014-10-20 15:54:03 +02:00
blob = vips_blob_new( NULL, buffer, length );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
va_start( ap, option_string );
result = vips_call_split_option_string( operation_name,
option_string, ap, blob, &im );
va_end( ap );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
vips_area_unref( VIPS_AREA( blob ) );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
if( result )
throw VError();
2014-10-20 12:50:34 +02:00
}
2014-10-20 15:54:03 +02:00
// see vips_image_write_to_file()
void VImage::write( const char *name, ... )
2014-10-20 12:50:34 +02:00
throw( VError )
{
2014-10-20 15:54:03 +02:00
char filename[VIPS_PATH_MAX];
char option_string[VIPS_PATH_MAX];
const char *operation_name;
va_list ap;
2014-10-20 12:50:34 +02:00
int result;
2014-10-20 15:54:03 +02:00
vips__filename_split8( name, filename, option_string );
if( !(operation_name = vips_foreign_find_save( filename )) )
throw VError();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
va_start( ap, name );
result = vips_call_split_option_string( operation_name, option_string,
ap, this.im, filename );
2014-10-20 15:54:03 +02:00
va_end( ap );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
if( result )
throw VError();
2014-10-20 12:50:34 +02:00
}
2014-10-20 15:54:03 +02:00
// see vips_image_write_to_buffer()
void *VImage::write( const char *suffix, size_t *size, ... )
2014-10-20 12:50:34 +02:00
throw( VError )
{
2014-10-20 15:54:03 +02:00
char filename[VIPS_PATH_MAX];
char option_string[VIPS_PATH_MAX];
const char *operation_name;
va_list ap;
VipsBlob *blob;
int result;
void *buf;
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
vips__filename_split8( suffix, filename, option_string );
if( !(operation_name = vips_foreign_find_save_buffer( filename )) )
throw VError();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
va_start( ap, size );
result = vips_call_split_option_string( operation_name, option_string,
ap, this.im, &blob );
2014-10-20 15:54:03 +02:00
va_end( ap );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
if( result )
throw VError();
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
g_assert( blob );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
buf = VIPS_AREA( blob )->data;
VIPS_AREA( blob )->free_fn = NULL;
if( size )
*size = VIPS_AREA( blob )->length;
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
vips_area_unref( VIPS_AREA( blob ) );
2014-10-20 12:50:34 +02:00
2014-10-20 15:54:03 +02:00
return( buf );
2014-10-20 12:50:34 +02:00
}
2014-10-20 15:54:03 +02:00
/* Insert automatically generated wrappers for vips operators.
2014-10-20 12:50:34 +02:00
*/
2014-10-20 15:54:03 +02:00
#include "vips-operators.cc"
2014-10-20 12:50:34 +02:00
VIPS_NAMESPACE_END