2009-08-16 17:00:08 +02:00
/* S W I G i n t e r f a c e f i l e f o r v i p s C C 7
*
* 5 / 9 / 07
* - use g _ o p t i o n _ c o n t e x t _ s e t _ i g n o r e _ u n k n o w n _ o p t i o n s ( ) s o w e d o n ' t f a i l
* on u n r e c o g n i e d - a r g s ( t h a n k s S i m o n )
* 3 / 8 / 08
* - add . t o b u f f e r ( ) / . f r o m b u f f e r ( ) , . t o s t r i n g ( ) , . f r o m s t r i n g ( )
* m e t h o d s
* - add P I L _ m o d e _ f r o m _ v i p s ( ) and v i p s _ f r o m _ P I L _ m o d e ( ) u t i l i t y
* f u n c t i o n s
2009-11-06 14:41:11 +01:00
* 6 / 11 / 09
* - a r g , s t d : : v e c t o r < v i p s : : V I m a g e > w a s m i s s i n g t h e "vips::"
2009-08-16 17:00:08 +02:00
* /
% m o d u l e V I m a g e
% {
2009-11-10 14:54:15 +01:00
# i n c l u d e < v i p s / v i p s c p p . h >
2009-08-16 17:00:08 +02:00
/* W e n e e d t h e C A P I t o o f o r t h e a r g s i n i t a n d s o m e o f t h e
* f r o m b u f f e r / t o b u f f e r s t u f f .
* /
# i n c l u d e < v i p s / v i p s . h >
% }
/* N e e d t o o v e r r i d e a s s i g n m e n t t o g e t r e f c o u n t i n g w o r k i n g .
* /
% r e n a m e ( _ _ a s s i g n _ _ ) v i p s : : V I m a g e : : o p e r a t o r = ;
% i n c l u d e "std_list.i"
% i n c l u d e "std_complex.i"
% i n c l u d e "std_vector.i"
% i n c l u d e "std_except.i"
% i n c l u d e "std_string.i"
% i n c l u d e "cstring.i"
2012-02-03 11:37:10 +01:00
% i n c l u d e "typemaps.i"
2009-08-16 17:00:08 +02:00
% import "VError.i"
% import "VMask.i"
% import "VDisplay.i"
n a m e s p a c e s t d {
% t e m p l a t e ( I n t V e c t o r ) v e c t o r < int > ;
% t e m p l a t e ( D o u b l e V e c t o r ) v e c t o r < double > ;
2009-11-06 14:41:11 +01:00
% t e m p l a t e ( I m a g e V e c t o r ) v e c t o r < v i p s : : V I m a g e > ;
2009-08-16 17:00:08 +02:00
}
/* T o g e t i m a g e d a t a t o a n d f r o m V I m a g e ( e g . w h e n i n t e r f a c i n g w i t h P I L ) w e
* n e e d to b e a b l e to import and export P y t h o n buffer ( ) o b j e c t s . Add new
* m e t h o d s to c o n s t r u c t from and return p o i n t e r / length p a i r s , then w r a p t h e m
* o u r s e l v e s with a c o u p l e of t y p e m a p s .
* /
% {
s t r u c t V B u f f e r {
void * d a t a ;
s i z e _ t size ;
} ;
% }
% t y p e m a p ( o u t ) V B u f f e r {
$ r e s u l t = P y B u f f e r _ F r o m M e m o r y ( $ 1 . d a t a , $ 1 . size ) ;
}
% t y p e m a p ( in ) V B u f f e r {
c o n s t char * buffer ;
P y _ s s i z e _ t b u f f e r _ l e n ;
if ( P y O b j e c t _ A s C h a r B u f f e r ( $ input , &buffer, &buffer_len) == -1) {
P y E r r _ S e t S t r i n g ( P y E x c _ T y p e E r r o r , "Type error. Unable to get char pointer from buffer" ) ;
return NULL ;
}
$ 1 . d a t a = ( void * ) buffer ;
$ 1 . size = b u f f e r _ l e n ;
}
2012-02-06 15:54:54 +01:00
/* F u n c t i o n s w h i c h r e t u r n e x t r a v a l u e s t h o u g h t h e i r p a r a m e t e r s n e e d s p e c i a l
* t y p e m a p s .
* /
// double maxpos_avg( double& maxpos_avg_y, double& maxpos_avg_out )
% apply double * OUTPUT { d o u b l e & m a x p o s _ a v g _ y } ;
% apply double * OUTPUT { d o u b l e & m a x p o s _ a v g _ o u t } ;
// VImage system_image( char* system_image_in_format, char* system_image_out_format, char* system_image_command, char*& system_image_log )
% c s t r i n g _ o u t p u t _ a l l o c a t e ( char * * s y s t e m _ i m a g e _ l o g , g _ f r e e ( * $ 1 ) ) ;
// VImage segment( int& segment_segments )
% apply int * OUTPUT { i n t & s e g m e n t _ s e g m e n t s } ;
// VImage project( VImage& project_vout ) throw( VError );
// nope ... not sure how to handle this one
//%apply VImage *OUTPUT { VImage & project_vout };
// VImage label_regions( int& label_regions_segments )
% apply int * OUTPUT { i n t & l a b e l _ r e g i o n s _ s e g m e n t s } ;
// double correl( VImage correl_sec, int correl_xref, int correl_yref, int correl_xsec, int correl_ysec, int correl_hwindowsize, int correl_hsearchsize, int& correl_x, int& correl_y )
% apply int * OUTPUT { i n t & c o r r e l _ x } ;
% apply int * OUTPUT { i n t & c o r r e l _ y } ;
// int _find_lroverlap( VImage _find_lroverlap_sec, int _find_lroverlap_bandno, int _find_lroverlap_xr, int _find_lroverlap_yr, int _find_lroverlap_xs, int _find_lroverlap_ys, int _find_lroverlap_halfcorrelation, int _find_lroverlap_halfarea, int& _find_lroverlap_dy0, double& _find_lroverlap_scale1, double& _find_lroverlap_angle1, double& _find_lroverlap_dx1, double& _find_lroverlap_dy1 )
% apply int * OUTPUT { i n t & _ f i n d _ l r o v e r l a p _ d y 0 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ l r o v e r l a p _ s c a l e 1 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ l r o v e r l a p _ a n g l e 1 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ l r o v e r l a p _ d x 1 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ l r o v e r l a p _ d y 1 } ;
// int _find_tboverlap( VImage _find_tboverlap_sec, int _find_tboverlap_bandno, int _find_tboverlap_xr, int _find_tboverlap_yr, int _find_tboverlap_xs, int _find_tboverlap_ys, int _find_tboverlap_halfcorrelation, int _find_tboverlap_halfarea, int& _find_tboverlap_dy0, double& _find_tboverlap_scale1, double& _find_tboverlap_angle1, double& _find_tboverlap_dx1, double& _find_tboverlap_dy1 )
% apply int * OUTPUT { i n t & _ f i n d _ t b o v e r l a p _ d y 0 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ t b o v e r l a p _ s c a l e 1 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ t b o v e r l a p _ a n g l e 1 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ t b o v e r l a p _ d x 1 } ;
% apply double * OUTPUT { d o u b l e & _ f i n d _ t b o v e r l a p _ d y 1 } ;
// double maxpos_subpel( double& maxpos_subpel_y )
% apply double * OUTPUT { d o u b l e & m a x p o s _ s u b p e l _ y } ;
2009-08-16 17:00:08 +02:00
/* N e e d t h e e x p a n d e d V I m a g e . h i n t h i s d i r e c t o r y , r a t h e r t h a n t h e u s u a l
* v i p s / V I m a g e . h . S W I G b 0 r k s on # i n c l u d e i n s i d e class d e f i n i t i o n s .
* /
% i n c l u d e V I m a g e . h
% e x t e n d v i p s : : V I m a g e {
public :
V B u f f e r t o b u f f e r ( ) throw ( V E r r o r )
{
V B u f f e r buffer ;
buffer . d a t a = $ s e l f - > d a t a ( ) ;
buffer . size = ( s i z e _ t ) $ s e l f - > X s i z e ( ) * $ s e l f - > Y s i z e ( ) *
I M _ I M A G E _ S I Z E O F _ P E L ( $ s e l f - > image ( ) ) ;
return buffer ;
}
static V I m a g e f r o m b u f f e r ( V B u f f e r buffer , int width , int height ,
int b a n d s , T B a n d F m t format ) throw ( V E r r o r )
{
return V I m a g e ( buffer . d a t a , width , height , b a n d s , format ) ;
}
% c s t r i n g _ o u t p u t _ a l l o c a t e _ s i z e ( char * * buffer , int * b u f f e r _ l e n , i m _ f r e e ( * $ 1 ) )
void t o s t r i n g ( char * * buffer , int * b u f f e r _ l e n ) throw ( V E r r o r )
{
void * v i p s _ m e m o r y ;
/* E v a l t h e v i p s i m a g e f i r s t . T h i s m a y t h r o w a n e x c e p t i o n a n d w e w a n t t o
* m a k e s u r e w e do t h i s b e f o r e w e t r y to m a l l o c ( ) space for t h e c o p y .
* /
v i p s _ m e m o r y = $ s e l f - > d a t a ( ) ;
/* W e h a v e t o c o p y t h e i m a g e d a t a t o m a k e a s t r i n g t h a t P y t h o n c a n
* m a n a g e . Use f r o m b u f f e r ( ) / t o b u f f e r ( ) if y o u w a n t to a v o i d t h e c o p y
* and m a n a g e m e m o r y l i f e t i m e y o u r s e l f .
* /
* b u f f e r _ l e n = ( s i z e _ t ) $ s e l f - > X s i z e ( ) * $ s e l f - > Y s i z e ( ) *
I M _ I M A G E _ S I Z E O F _ P E L ( $ s e l f - > image ( ) ) ;
if ( ! ( * buffer = ( char * ) i m _ m a l l o c ( NULL , * b u f f e r _ l e n ) ) )
v e r r o r ( "Unable to allocate memory for image copy." ) ;
m e m c p y ( * buffer , v i p s _ m e m o r y , * b u f f e r _ l e n ) ;
}
static V I m a g e f r o m s t r i n g ( s t d : : string buffer , int width , int height ,
int b a n d s , T B a n d F m t format ) throw ( V E r r o r )
{
void * v i p s _ m e m o r y ;
V I m a g e r e s u l t ;
/* W e h a v e t o c o p y t h e s t r i n g , t h e n a d d a c a l l b a c k t o t h e V I m a g e t o f r e e
* i t when w e f r e e t h e V I m a g e . Use f r o m b u f f e r ( ) / t o b u f f e r ( ) if y o u w a n t
* to a v o i d t h e c o p y and m a n a g e m e m o r y l i f e t i m e y o u r s e l f .
* /
if ( ! ( v i p s _ m e m o r y = i m _ m a l l o c ( NULL , buffer . length ( ) ) ) )
v e r r o r ( "Unable to allocate memory for image copy." ) ;
/* W e h a v e t o u s e . c _ s t r ( ) s i n c e t h e s t r i n g m a y n o t b e c o n t i g u o u s .
* /
m e m c p y ( v i p s _ m e m o r y , buffer . c _ s t r ( ) , buffer . length ( ) ) ;
r e s u l t = V I m a g e ( v i p s _ m e m o r y , width , height , b a n d s , format ) ;
if ( i m _ a d d _ c l o s e _ c a l l b a c k ( r e s u l t . image ( ) ,
( i m _ c a l l b a c k _ f n ) i m _ f r e e , v i p s _ m e m o r y , NULL ) )
v e r r o r ( ) ;
return r e s u l t ;
}
}
% p y t h o n c o d e % {
# t r y to g u e s s a P I L m o d e string from a V I P S image
def P I L _ m o d e _ f r o m _ v i p s ( v i m ) :
if v i m . B a n d s ( ) = = 3 and v i m . B a n d F m t ( ) = = V I m a g e . F M T U C H A R :
return 'RGB'
2013-06-07 11:42:47 +02:00
e l i f v i m . B a n d s ( ) = = 4 and v i m . B a n d F m t ( ) = = V I m a g e . F M T U C H A R and v i m . Type ( ) = = V I m a g e . R G B :
2009-08-16 17:00:08 +02:00
return 'RGBA'
2013-06-07 11:42:47 +02:00
e l i f v i m . B a n d s ( ) = = 4 and v i m . B a n d F m t ( ) = = V I m a g e . F M T U C H A R and v i m . Type ( ) = = V I m a g e . C M Y K :
2009-08-16 17:00:08 +02:00
return 'CMYK'
e l i f v i m . B a n d s ( ) = = 1 and v i m . B a n d F m t ( ) = = V I m a g e . F M T U C H A R :
return 'L'
e l i f v i m . B a n d s ( ) = = 1 and v i m . B a n d F m t ( ) = = V I m a g e . F M T I N T :
return 'I'
e l i f v i m . B a n d s ( ) = = 1 and v i m . B a n d F m t ( ) = = V I m a g e . F M T F L O A T :
return 'F'
e l i f v i m . B a n d s ( ) = = 2 and v i m . B a n d F m t ( ) = = V I m a g e . F M T U C H A R :
return 'LA'
else :
r a i s e V a l u e E r r o r ( 'unsupported vips -> pil image' )
# return v i p s ( b a n d s , format , type ) for a P I L m o d e
def v i p s _ f r o m _ P I L _ m o d e ( m o d e ) :
if m o d e = = 'RGB' :
return ( 3 , V I m a g e . F M T U C H A R , V I m a g e . R G B )
e l i f m o d e = = 'RGBA' :
return ( 4 , V I m a g e . F M T U C H A R , V I m a g e . R G B )
e l i f m o d e = = 'CMYK' :
return ( 4 , V I m a g e . F M T U C H A R , V I m a g e . C M Y K )
e l i f m o d e = = 'L' :
return ( 1 , V I m a g e . F M T U C H A R , V I m a g e . B _ W )
e l i f m o d e = = 'I' :
return ( 1 , V I m a g e . F M T I N T , V I m a g e . B _ W )
e l i f m o d e = = 'F' :
return ( 1 , V I m a g e . F M T F L O A T , V I m a g e . B _ W )
e l i f m o d e = = 'LA' :
return ( 2 , V I m a g e . F M T U C H A R , V I m a g e . B _ W )
else :
r a i s e V a l u e E r r o r ( 'unsupported pil -> vips image' )
% }
/* H e l p e r c o d e f o r v i p s _ i n i t ( ) .
* /
% {
/* T u r n o n t o p r i n t a r g s .
# define DEBUG
* /
/* C o m m a n d - l i n e a r g s d u r i n g p a r s e .
* /
t y p e def s t r u c t _ A r g s {
/* T h e n s t r i n g s w e a l l o c w h e n w e g e t f r o m P y t h o n .
* /
int n ;
char * * s t r ;
/* a r g c / a r g v a s p r o c e s s e d b y u s .
* /
int a r g c ;
char * * a r g v ;
} A r g s ;
# i f def DEBUG
static void
a r g s _ p r i n t ( A r g s * a r g s )
{
int i ;
p r i n t f ( "args_print: argc = %d\n" , a r g s - > a r g c ) ;
// +1 so we print the trailing NULL too
for ( i = 0 ; i < a r g s - > a r g c + 1 ; i + + )
p r i n t f ( "\t%2d)\t%s\n" , i , a r g s - > a r g v [ i ] ) ;
}
# e n d i f /* D E B U G */
static void
a r g s _ f r e e ( A r g s * a r g s )
{
int i ;
for ( i = 0 ; i < a r g s - > n ; i + + )
I M _ F R E E ( a r g s - > s t r [ i ] ) ;
a r g s - > n = 0 ;
a r g s - > a r g c = 0 ;
I M _ F R E E ( a r g s - > s t r ) ;
I M _ F R E E ( a r g s - > a r g v ) ;
I M _ F R E E ( a r g s ) ;
}
/* G e t a r g v / a r g c f r o m p y t h o n .
* /
static A r g s *
a r g s _ n e w ( void )
{
A r g s * a r g s ;
P y O b j e c t * a v ;
int i ;
int n ;
a r g s = g _ n e w ( A r g s , 1 ) ;
a r g s - > n = 0 ;
a r g s - > s t r = NULL ;
a r g s - > a r g c = 0 ;
a r g s - > a r g v = NULL ;
if ( ! ( a v = P y S y s _ G e t O b j e c t ( ( char * ) "argv" ) ) )
return ( a r g s ) ;
if ( ! P y L i s t _ C h e c k ( a v ) ) {
P y E r r _ W a r n ( P y E x c _ W a r n i n g , "ignoring sys.argv: "
"it must be a list of strings" ) ;
return a r g s ;
}
n = P y L i s t _ S i z e ( a v ) ;
a r g s - > s t r = g _ n e w ( char * , n ) ;
for ( i = 0 ; i < n ; i + + )
a r g s - > s t r [ i ] = g _ s t r d u p ( P y S t r i n g _ A s S t r i n g ( P y L i s t _ G e t I t e m ( a v , i ) ) ) ;
a r g s - > n = n ;
/* + 1 f o r N U L L t e r m i n a t i o n .
* /
a r g s - > a r g c = n ;
a r g s - > a r g v = g _ n e w ( char * , n + 1 ) ;
for ( i = 0 ; i < n ; i + + )
a r g s - > a r g v [ i ] = a r g s - > s t r [ i ] ;
a r g s - > a r g v [ i ] = NULL ;
return a r g s ;
}
static void
v i p s _ f a t a l ( c o n s t char * m s g )
{
char b u f [ 256 ] ;
i m _ s n p r i n t f ( b u f , 256 , "%s\n%s" , m s g , i m _ e r r o r _ b u f f e r ( ) ) ;
i m _ e r r o r _ c l e a r ( ) ;
P y _ F a t a l E r r o r ( b u f ) ;
}
% }
% init % {
{
A r g s * a r g s ;
a r g s = a r g s _ n e w ( ) ;
# i f def DEBUG
p r i n t f ( "on startup:\n" ) ;
a r g s _ p r i n t ( a r g s ) ;
# e n d i f /* D E B U G */
if ( i m _ i n i t _ w o r l d ( a r g s - > a r g v [ 0 ] ) ) {
a r g s _ f r e e ( a r g s ) ;
v i p s _ f a t a l ( "can't initialise module vips" ) ;
}
/* N o w p a r s e a n y G O p t i o n s .
* /
G E r r o r * error = NULL ;
G O p t i o n C o n t e x t * context ;
context = g _ o p t i o n _ c o n t e x t _ n e w ( "- vips" ) ;
g _ o p t i o n _ c o n t e x t _ a d d _ g r o u p ( context , i m _ g e t _ o p t i o n _ g r o u p ( ) ) ;
g _ o p t i o n _ c o n t e x t _ s e t _ i g n o r e _ u n k n o w n _ o p t i o n s ( context , TRUE ) ;
if ( ! g _ o p t i o n _ c o n t e x t _ p a r s e ( context ,
&args->argc, &args->argv, &error)) {
g _ o p t i o n _ c o n t e x t _ f r e e ( context ) ;
a r g s _ f r e e ( a r g s ) ;
i m _ e r r o r ( "vipsmodule" , "%s" , e r r o r - > message ) ;
g _ e r r o r _ f r e e ( error ) ;
v i p s _ f a t a l ( "can't initialise module vips" ) ;
}
g _ o p t i o n _ c o n t e x t _ f r e e ( context ) ;
# i f def DEBUG
p r i n t f ( "after parse:\n" ) ;
a r g s _ p r i n t ( a r g s ) ;
# e n d i f /* D E B U G */
// Write (possibly) modified argc/argv back again.
if ( a r g s - > a r g v )
P y S y s _ S e t A r g v ( a r g s - > a r g c , a r g s - > a r g v ) ;
a r g s _ f r e e ( a r g s ) ;
}
% }