1

I am trying to figure out how to position an image within newly created image in C++ using GIL from Boost library.

#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_image_t img1;
    rgb8_image_t img2;
    png_read_image("img1.png", img1);//Code for loading an image
    png_read_image("img2.png", img2); //Code for loading 2nd image "img2.png" 
    //loading position of the images to an array or some kind of variable
    //passing in images and postions to the function to apply changes on newly created image with the  size of 512, 512 
    png_write_view("output.png", const_view(img)); //saving changes as "output.png"
}

image of what i want to do

mloskot
  • 37,086
  • 11
  • 109
  • 136
AESTHETICS
  • 989
  • 2
  • 14
  • 32

2 Answers2

3

You can just use the subimage_view to position your images and the copy_pixels to copy them.
You need to take care that the sizes of the input images and the output subview match. If they don't match you can also use the resize_view.
Something like that:

rgb8_image_t img1;
jpeg_read_image("img1.jpg", img1);
rgb8_image_t img2;
jpeg_read_image("img2.jpg", img2);

rgb8_image_t out_img(512, 512);
copy_pixels (view(img1), subimage_view(view(out_img), x, y, width, height));
copy_pixels (view(img2), subimage_view(view(out_img), x, y, width, height));
png_write_view("output.png", const_view(out_img));
mkaes
  • 13,781
  • 10
  • 52
  • 72
0

This is solution if anyone is curious.

How to install Boost

How to install LibPng (Needed for loading png)

#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_WARNINGS
#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 out_img(512, 512);
    rgb8_image_t img1;
    rgb8_image_t img2;
    png_read_image("img1.png", img1);//Code for loading img1
    png_read_image("img2.png", img2);//Code for loading img2
    copy_pixels(view(img1), subimage_view(view(out_img), 0, 0, 50, 50)); 
    copy_pixels(view(img2), subimage_view(view(out_img), 462, 462, 50, 50));
    png_write_view("output.png", const_view(out_img));

}

All of those #define are needed to stop visual studio from showing up errors.

Btw in the directory of the program there have to be img1.png and img2.png, otherwise memory errors will show up.

AESTHETICS
  • 989
  • 2
  • 14
  • 32