Trying to learn a bit of C++ and I am very new at it. My learning project is to generate images using various algorythms (like fractals). I am looking at the boost GIL library, since boost seems to be the most common and established C++ library around. So, I am trying to create an image in memory, iterate over pixels and set the RGB value based on some formula. My current code looks like this
int main() {
rgb8_image_t img(IMAGE_W, IMAGE_H);
auto b = view(img).begin();
while (b != view(img).end()) {
/* set the pixel value here */
b++;
}
write_view("image.png", view(img), png_tag());
return 0;
}
The iteration seems to work, but I cannot seem to figure out from the GIL docs, how to actually set the pixel in this loop. I could do a nested for loop and just set pixels using x and y coordinates, but I kinda want to use iterators since it seems neater and maybe I can later refactor this to use transform(). How do I proceed from here? How do I set a pixel to some RGB value?
Simply, create value of type of
rgb8_pixel_twith desired channel values and assign it to the pixel pointed by iterator.For a simple example, this will fill your image with solid orange:
For a more complex example, here is how to use a custom generator of pixel channel values:
UPDATE: The
static_generateis one of GIL algorithms to operate on color bases (e.g. pixels). Thestruct random_valueis a functor class because it encapsulates data elements of the random number generator. I simply copied it form GIL's test/core/image/test_fixture.hpp, but it does not have to be a class. It can be anything callable, usable as a functor. I have updated the code snippets withgil::namespace qualification to make it clearer where things come from.