簡體   English   中英

CIMG中的Append圖像並保存在stb_image_write中不起作用

[英]Append image in CIMG and save in stb_image_write not working

我正在使用帶有以下代碼的 stb_image stb_image_write 和 CImg

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

它只是創建 10 像素的黑線並在我顯示它時附加到圖像的底部它工作正常但是當我保存它時顯示失真

我需要在底部添加邊框是我做錯了什么還是有更好的方法?

原來的

已保存

下面的代碼可以很好地使用 stb 讀取圖像在 cimg 中進行一些更改並使用 stb 保存它將小圖像覆蓋到大圖像

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

覆蓋

好的,我得到了 Mark Setchell 在關於交錯的評論中提到的工作,所以我必須按照下面的帖子中提到的那樣置換緩沖區結構

CImg 庫在旋轉時創建扭曲的圖像

所以如果我需要使用這三個庫而不是我的代碼如下

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM