16

I've been trying to read the boost::gil documentation, but it's somewhere between lacking, and convoluted.

Ranting aside, I need an example on how to do the following:

Create an image of, say 512x512. Fill it with red pixels. Write to PNG.

I can't find anything about doing any of that, at all, in the documentation for gil. Particularly the creating an image or filling it with pixels part.

If anyone can help, thanks.

mloskot
  • 37,086
  • 11
  • 109
  • 136
Brian
  • 331
  • 3
  • 12
  • 1
    I had this same problem when I first came to GIL too (before it was in boost even)... the tutorial material shows you a ton of clever stuff with the view types... but then you come to try it yourself and realize you've still been given no idea how to actually create an concrete image for a view to refer to! Anyway, stick with it, it really is an incredibly elegant library for dealing with images. – timday May 04 '11 at 21:19
  • @timday Some good comments. We're trying to improve GIL including the docs. If you notice problems or have comments, please consider opening an issue at https://github.com/boostorg/gil – mloskot Jun 07 '19 at 08:59

1 Answers1

28

I haven't used GIL yet, but I want to learn it as well. Having looked at the design guide and having googled up the error related to libpng, looks like the simplest example is

#define png_infopp_NULL (png_infopp)NULL
#define int_p_NULL (int*)NULL
#include <boost/gil/gil_all.hpp>
#include <boost/gil/extension/io/png_dynamic_io.hpp>
using namespace boost::gil;
int main()
{
    rgb8_image_t img(512, 512);
    rgb8_pixel_t red(255, 0, 0);
    fill_pixels(view(img), red);
    png_write_view("redsquare.png", const_view(img));
}

works for me, with -lpng on command line, producing this image

enter image description here

ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97
Cubbi
  • 46,567
  • 13
  • 103
  • 169
  • Excellently simple and effective solution. Thanks for the warning about the libpng error too. I found in Visual C++ I have to include the libs and directories, something else that was either missing or buried in its documentation :p – Brian May 04 '11 at 20:47
  • 8
    For future readers that may want to control each pixel's color : just take a View of your image (`rgb8_image_t::view_t v = view(img)`) and set the pixel on it : `v(10,20) = rgb8_pixel_t(255, 0, 123)`. Or use iterators on the view, as in the docs. – Arthur Aug 31 '13 at 19:03
  • 1
    The answer provided by @cubbi is perfectly correct for GIL released in Boost 1.67 or earlier. Since, the I/O extensions have been entirely rewritten in Boost.GIL released in Boost 1.68, the example will need adjustments. Feel free to ask https://github.com/boostorg/gil#community for any questions – mloskot Jun 07 '19 at 08:57