c++cimgstb-image

Append image in CIMG and save in stb_image_write not working


I am using stb_image stb_image_write and CImg with following code

    int width, height, bpp;
    img = stbi_load(imgPath, &width, &height, &bpp, 3);

    CImg<unsigned char> cimg(img, width, height, 1, bpp);
    CImg<unsigned char> wimg(width, 10, 1, 3, 0);
    cimg.append(wimg,'y');
    cimg.display();

    stbi_write_png("save.png", width, height, 3, cimg, width * 3);

It simply creating black line with 10 pixel and appending to bottom of image when I display this it works fine but when I save it showing distorted

I need to add border at bottom am I doing something wrong or any better way to do it ?

Original

Saved

Below code works fine read image with stb do some change in cimg and save with stb it overlay small image to big one

    int width, height, bpp;
    int width2, height2, bpp2;

    CImg<unsigned char> gradient(img1, bpp, width, height, 1);
    CImg<unsigned char> overlay(img2, bpp2, width2, height2, 1);

    CImg<unsigned char> gradient(img1, width, height, bpp, 1);
    CImg<unsigned char> overlay(img2, width2, height2, bpp2, 1);
    gradient.draw_image(0, 0, overlay);
    stbi_write_png("gradient.png", width, height, 3, gradient, width * 3);

Overlay


Solution

  • Ok I got it worked as mentioned by Mark Setchell in comments about interleave so I have to permute the buffer structure as mentioned in below post

    CImg library creates distorted images on rotation

    so if I need to used these three libs than my code would be as below

    int width, height, bpp;
        setHeader();
    
        img = stbi_load(imgPath, &width, &height, &bpp, 3);
        
        //Load with stb type
        CImg<unsigned char> cimg(img, bpp, width, height, 1);
        
        //Convert cimg type
        cimg.permute_axes("yzcx");
    
        //Can work with all type of cimg functions
        CImg<unsigned char> wimg(width, 10, 1, 3, 0);
        cimg.append(wimg,'y');
        
        //Convert back to stb type to save
        cimg.permute_axes("cxyz");
        stbi_write_png("save.png", width, height+10, 3, cimg, width * 3);