I\'m trying to move towards Boost GIL to replace some similar functionality I\'ve implemented that is reaching the end of its maintainable life.
I have
A complete, simplified form of the actual code I ended up using is:
#include
#include
#include
#include
#include
struct dimension {
int w,h;
};
namespace {
struct PixelInserter {
std::vector* storage;
PixelInserter(std::vector* s) : storage(s) {}
void operator()(boost::gil::rgb8_pixel_t p) const {
using boost::gil::at_c;
storage->push_back(at_c<0>(p));
storage->push_back(at_c<1>(p));
storage->push_back(at_c<2>(p));
}
};
// This could probably share code with the PixelInserter
struct PixelWriter {
const uint8_t *pixels;
PixelWriter(const uint8_t *pixels) : pixels(pixels) {}
void operator()(boost::gil::rgb8_pixel_t& p) {
using boost::gil::at_c;
at_c<0>(p) = *pixels++;
at_c<1>(p) = *pixels++;
at_c<2>(p) = *pixels++;
}
};
}
void savePNG(const std::string& filename, const uint8_t *pixels, const dimension& d) {
boost::gil::rgb8_image_t img(d.w, d.h);
for_each_pixel(view(img), PixelWriter(pixels));
boost::gil::png_write_view(filename, view(img));
}
std::vector readPNG(const std::string& fn, dimension& d) {
boost::gil::rgb8_image_t image_type;
image_type img;
png_read_image(fn, img);
d.w = img.width();
d.h = img.height();
std::vector storage;
storage.reserve(d.w*d.h*boost::gil::num_channels());
for_each_pixel(const_view(img), PixelInserter(&storage));
return storage;
}
int main(int argc, char **argv) {
dimension d;
const std::vector pixels = readPNG(argv[1], d);
savePNG(argv[2], &pixels[0], d);
}
I originally had the following before I included any of the GIL headers:
#define png_infopp_NULL (png_infopp)NULL
#define int_p_NULL (int*)NULL
I'm not sure exactly what issue they fixed with the version of boost I had at the time, but they don't seem to be required with 1.48.