1

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

  • Why are you even trying to use `stbi`? Just use **CImg** `save_png()`. The answer is that the interleave on **CImg** is planar not by pixel. – Mark Setchell Feb 23 '21 at 08:02
  • I want add simple image processing code in mobile so want to create a simple and small c++ shared code for both android and ios i tried diff functions in cimg and saving it with stb all work fine but if image size is change than its having problem – shuwair sardar Feb 23 '21 at 08:07
  • I haven't use `stbi` but find it hard to imagine it understands `CImg` images. Have you got code for `stbi` to correctly save a simple colour `CImg` as a PNG? (Not in the comments please, but by clicking `edit` button) – Mark Setchell Feb 23 '21 at 08:32

1 Answers1

1

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);