Skip to content

Instantly share code, notes, and snippets.

@mloskot
Created October 10, 2012 15:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mloskot/3866213 to your computer and use it in GitHub Desktop.
Save mloskot/3866213 to your computer and use it in GitHub Desktop.
How to use Boost.GIL views attached to bytes array with interleaved_ptr, versus view constructed with cast to pixel type
//////////////////////////////////////////////////////////////////////////////////////////////////////
// Problem reported to Boost mailing list: http://thread.gmane.org/gmane.comp.lib.boost.devel/234991
//
// How to use Boost.GIL views attached to bytes array with interleaved_ptr,
// versus view constructed with cast to pixel type
//////////////////////////////////////////////////////////////////////////////////////////////////////
std::size_t w = 512;
std::size_t h = 256;
std::size_t sizeRow = 1536; // calculated for Windows DIB
std::size_t sizeImage = sizeRow * h;
std::vector<unsigned char> bitmap(sizeImage);
std::vector<unsigned char> bitmapOut(sizeImage);
// here bitmap is filled with data (e.g. read from file)
// Simple test: iterate over pixels and rewrite from input array to output array
// using views created in two ways: 1) cast to pixel type pointer and 2) using interleaved_ptr
// 1) ////////////////////////////////////////////////////////////////////////////////////////////////
auto src = boost::gil::interleaved_view(w, h, (boost::gil::rgb8_pixel_t const*)(&bitmap[0]), sizeRow);
auto dst = boost::gil::interleaved_view(w, h, (boost::gil::rgb8_pixel_t*)(&bitmapOut[0]), sizeRow);
assert( !boost::gil::equal_pixels(src, dst) );
// loop taken from boost::gil::transform_pixels
for (std::ptrdiff_t y=0; y<src.height(); ++y)
{
auto srcIt=src.row_begin(y);
auto dstIt=dst.row_begin(y);
for (std::ptrdiff_t x=0; x<src.width(); ++x)
{
// Simple rgb8_pixel_t to rgb8_pixel_t assignment
dstIt[x] = srcIt[x];
}
}
assert( boost::gil::equal_pixels(src, dst) );
// The image output to bitmapOut looks like this:
// http://www.flickr.com/photos/mloskot/8074130396
// 2) ////////////////////////////////////////////////////////////////////////////////////////////////
typedef interleaved_ptr<unsigned char*, rgb_layout_t> rgb8_interleaved_ptr;
typedef interleaved_ptr<const unsigned char*, rgb_layout_t> rgb8c_interleaved_ptr
auto src = boost::gil::interleaved_view(w, h, gil::rgb8c_interleaved_ptr(&bitmap[0]), sizeRow);
auto dst = boost::gil::interleaved_view(w, h, gil::rgb8_interleaved_ptr(&bitmapOut[0]), sizeRow);
assert( !boost::gil::equal_pixels(src, dst) );
// loop taken from boost::gil::transform_pixels
for (std::ptrdiff_t y=0; y<src.height(); ++y)
{
auto srcIt=src.row_begin(y);
auto dstIt=dst.row_begin(y);
for (std::ptrdiff_t x=0; x<src.width(); ++x)
{
// Interleaved_ref points to first channel of current pixel, right?
// Is this access and assignment of colour components incorrect?
dstIt[x][0] = srcIt[x][0]; // R
dstIt[x][1] = srcIt[x][1]; // G
dstIt[x][2] = srcIt[x][2]; // B
}
}
assert( boost::gil::equal_pixels(src, dst) ); // This assertion fails!
// The image output to bitmapOut looks like this:
//http://www.flickr.com/photos/mloskot/8074130218/
// 1/2 to 1/3 of bitmapOut is black (pixels with value 0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment