c++: add [], change ()

() used to return element zero, just return a vector

add [] overload
This commit is contained in:
John Cupitt 2015-03-01 10:43:02 +00:00
parent 367819b5cc
commit ded01df512
5 changed files with 48 additions and 12 deletions

View File

@ -11,6 +11,7 @@
- rewritten tiff writer is about 3 - 4x faster at making pyramids
- jpg, magick, png, tiff readers now use only 1 fd per input image
- python: use [] to index and slice image bands
- c++: use [] to band index, () returns a vector<double>
6/2/15 started 7.42.3
- bump version for back-compat ABI change

15
TODO
View File

@ -5,20 +5,15 @@
- need to add [] overload to Ruby bindings
- add
- overload (x, y) to be getpoint()? py/rb
VImage &operator[]( int index )
throw( VError )
{
VImage result = this->extract_band( index );
c++ has:
return( result );
std::vector<double> operator()( int x, int y )
{
return( this->getpoint( x, y ) );
}
to cpp bindings
- overload (x, y) to be getpoint()? c++/py/rb

View File

@ -5,6 +5,10 @@
*
* g++ -g -Wall test_overloads.cpp `pkg-config vips-cpp --cflags --libs`
*
* run with:
*
* valgrind --leak-check=yes ./a.out ~/pics/k2.jpg ~/pics/shark.jpg
*
*/
/*
@ -256,6 +260,8 @@ main( int argc, char **argv )
vips_error_exit( NULL );
}
g_option_context_free( context );
{
VImage left = VImage::new_from_file( argv[1] );
VImage right = VImage::new_from_file( argv[2] );
@ -264,6 +270,10 @@ main( int argc, char **argv )
test_binary_test_subtract( left, right );
test_binary_test_multiply( left, right );
test_binary_test_divide( left, right );
VImage band_one = left[1];
std::vector<double> point = left(0, 0);
}
vips_shutdown();

View File

@ -776,9 +776,15 @@ public:
// Operator overloads
double operator()( int x, int y, int z = 0 )
VImage operator[]( int index )
throw( VError )
{
return( this->getpoint( x, y )[z] );
return( this->extract_band( index ) );
}
std::vector<double> operator()( int x, int y )
{
return( this->getpoint( x, y ) );
}
friend VImage operator+( VImage a, VImage b )

View File

@ -256,6 +256,30 @@ VImage a = (a &lt; 128).ifthenelse( 128, a );
The C++ API includes the usual range of arithmetic operator overloads.
You can mix constants, vectors and images freely.
</para>
<para>
The API overloads <code>[]</code> to be vips_extract_band(). You can
write:
<programlisting language="C++">
VImage xyz = VImage::xyz( 256, 256 ) - VImage::to_vectorv( 2, 128.0, 128.0 );
VImage mask = (xyz[0].pow( 2 ) + xyz[1].pow( 2 )).pow( 0.5 ) &lt; 100;
</programlisting>
to make a circular mask, for example.
</para>
<para>
The API overloads <code>()<code> to be vips_getpoint(). You can
write:
<programlisting language="C++">
VImage xyz = VImage::xyz( 256, 256 ) - VImage::to_vectorv( 2, 128.0, 128.0 );
// this will have the value [0, 0]
std::vector<double> point = xyz(128, 128);
</programlisting>
</para>
</refsect3>
<refsect3 id="cpp-enum">