[ VIGRA Homepage | Function Index | Class Index | Namespaces | File List | Main Page ]

invert_tutorial.cxx |

Invert an image file (gray scale or color)
Usage: `invert infile outfile`

#include <iostream>
#include <vigra/multi_array.hxx>
#include <vigra/impex.hxx>
#include <vigra/multi_math.hxx>
using namespace vigra;
int main (int argc, char ** argv)
{
if(argc != 3)
{
std::cout << "Usage: " << argv[0] << " infile outfile" << std::endl;
std::cout << "(supported formats: " << impexListFormats() << ")" << std::endl;
return 1;
}
try
{
// use multi_math functions
using namespace multi_math;
// read image given as first argument
ImageImportInfo info(argv[1]);
// process grayscale image
if (info.isGrayscale())
{
// instantiate array for image data
MultiArray<2, UInt8> imageArray(info.shape());
// import image data into array
importImage(info, imageArray);
// invert image
imageArray = 255-imageArray;
// write image data to the file given as second argument
exportImage(imageArray, ImageExportInfo(argv[2]));
}
// process color image
else
{
// instantiate array for image data
MultiArray<2, RGBValue<UInt8> > imageArray(info.shape());
// copy image data into array
importImage(info, imageArray);
// invert image
RGBValue<UInt8> temp(255,255,255);
imageArray = temp - imageArray;
// write image data to the file given as second argument
exportImage(imageArray, ImageExportInfo(argv[2]));
}
}
catch (std::exception & e)
{
// catch any errors that might have occurred (e.g. "file not found") and print their reason
std::cout << e.what() << std::endl;
return 1;
}
return 0;
}
vigra::MultiArray
Main MultiArray class containing the memory management.
Definition: multi_array.hxx:2474
vigra
vigra::impexListFormats
std::string impexListFormats()
List the image formats VIGRA can read and write.
vigra::importImage
void importImage(...)
Read an image from a file.
vigra::ImageImportInfo::isGrayscale
bool isGrayscale() const
vigra::RGBValue
Class for a single RGB value.
Definition: accessor.hxx:938
impex.hxx
image import and export functions
vigra::ImageImportInfo::shape
MultiArrayShape< 2 >::type shape() const
vigra::ImageImportInfo
Argument object for the function importImage().
Definition: imageinfo.hxx:390
vigra::ImageExportInfo
Argument object for the function exportImage().
Definition: imageinfo.hxx:133
vigra::exportImage
void exportImage(...)
Write an image to a file.

© Ullrich Köthe (ullrich.koethe@iwr.uni-heidelberg.de) Heidelberg Collaboratory for Image Processing, University of Heidelberg, Germany | html generated using doxygen and Python vigra 1.11.1