DGtal  0.9.3beta
images/exampleArrayImageAdapter.cpp

Example of ArrayImageAdapter with subdomain.

See also
ArrayImageAdapter
ArrayImageAdapter_subImage_modifByImage.png
(9) modifying the first bottom-left 8x8 image through the sub-domain view with an image iterator.
#include <iostream>
#include <DGtal/base/Common.h>
#include <DGtal/helpers/StdDefs.h>
#include <DGtal/io/boards/Board2D.h>
#include <new>
#include <cmath>
#include <algorithm>
#include <DGtal/io/colormaps/HueShadeColorMap.h>
#include <DGtal/images/ImageContainerBySTLVector.h>
#include <DGtal/images/ArrayImageAdapter.h>
using namespace std;
using namespace DGtal;
void ArrayImageAdapter_example()
{
using Space = SpaceND<2>;
using Value = double;
const Domain domain{ {0, 1}, {4, 3} };
Value* data = new Value[ domain.size() ];
// Convert this allocated memory to a CImage model.
ArrayImageAdapter< Value*, Domain > image( data, domain );
// Alternative syntax using the helpers:
// auto image = makeArrayImageAdapterFromIterator( data, domain );
// Fill the image with first coordinate of the point
for ( auto it = image.begin(); it != image.end(); ++it )
{
*it = it.getPoint()[0];
}
// Get a constant view on a sub-domain.
const Domain sub_domain{ {1, 1}, {3, 2} };
ArrayImageAdapter< Value const*, Domain > cst_image( data, domain, sub_domain );
// Alternative syntax using the helpers:
// auto const cst_image = makeArrayImageAdapterFromImage( image, sub_domain );
// Display it.
for ( auto value : cst_image )
{
std::cout << value << " ";
}
std::cout << std::endl;
}
void moduleImages_example()
{
using namespace Z2i;
Board2D aBoard;
using Value = double; // value type of the image
using HueShadeDouble = HueShadeColorMap<Value>; // a simple HueShadeColorMap varying on 'double' values
trace.beginBlock("image");
const Domain domain(Point(1,1), Point(16,16));
Value* data = new Value[ domain.size() ];
ArrayImageAdapter< Value*, Domain > image( data, domain );
Value i = 0;
for ( auto & value : image )
value = i++;
aBoard.clear();
Display2DFactory::drawImage<HueShadeDouble>(aBoard, image, 0, domain.size()-1);
aBoard.saveSVG("ArrayImageAdapter_image.svg");
trace.beginBlock("subImage");
Domain subDomain(Point(1,1), Point(8,8));
ArrayImageAdapter< Value const*, Domain > constSubImage( data, domain, subDomain );
aBoard.clear();
Display2DFactory::drawImage<HueShadeDouble>(aBoard, constSubImage, 0, domain.size()-1);
aBoard.saveSVG("ArrayImageAdapter_subImage.svg");
trace.beginBlock("modifying subImage through domain iterator");
{
ArrayImageAdapter< Value*, Domain > subImage( data, domain, subDomain );
}
auto subImage = makeArrayImageAdapterFromIterator( data, domain, subDomain );
for ( auto point : subImage.domain() )
{
Value coord = (point - Point(4,4)).norm();
subImage.setValue( point, 25*(cos(coord)+1) );
}
aBoard.clear();
Display2DFactory::drawImage<HueShadeDouble>(aBoard, image, 0, domain.size()-1);
aBoard.saveSVG("ArrayImageAdapter_subImage_modifByDomain.svg");
trace.beginBlock("modifying subImage through image iterator");
for ( auto it = subImage.begin(), it_end = subImage.end(); it != it_end; ++it )
{
Value coord = (it.getPoint() - Point(4,4)).norm();
*it = 25*(sin(coord)+1);
}
aBoard.clear();
Display2DFactory::drawImage<HueShadeDouble>(aBoard, image, 0, domain.size()-1);
aBoard.saveSVG("ArrayImageAdapter_subImage_modifByImage.svg");
trace.beginBlock("subImage from an ImageContainerBySTLVector");
for (auto& value : anIterableImage)
value = 0;
{
ArrayImageAdapter< ImageContainerBySTLVector<Domain,Value>::Iterator, Domain > subImageSTL( anIterableImage.begin(), domain, subDomain );
}
auto subImageSTL = makeArrayImageAdapterFromImage( anIterableImage, subDomain );
trace.beginBlock("using std::copy on ArrayImageAdapter");
std::copy( subImage.cbegin(), subImage.cend(), subImageSTL.begin() );
aBoard.clear();
Display2DFactory::drawImage<HueShadeDouble>(aBoard, anIterableImage, 0, domain.size()-1);
aBoard.saveSVG("ArrayImageAdapter_subImage_copyToImageSTL.svg");
delete[] data;
}
int main()
{
ArrayImageAdapter_example();
moduleImages_example();
return 0;
}
// //