使用LibTiff逐像素写入tif

Writing a tif pixel by pixel using LibTiff?

本文关键字:tif 像素 LibTiff 使用      更新时间:2023-10-16

是否可以通过逐像素迭代并设置每个像素的RGB值来创建新的tif?

让我解释一下我试图做什么。我试图打开一个现有的tif,使用TIFFReadRGBAImage读取它,取TIFFGetR/TIFFGetG/TIFFGetB给出的RGB值,从255中减去它们,取这些新值,并用它们逐个写入每个像素。最后,我想以原始图像和一个新的"补充"图像结束,就像原始图像的负片一样。

有没有一种方法可以使用LibTiff做到这一点?我浏览了文档并在谷歌上搜索了一下,但我只看到了TIFFWriteScanline的很短的例子,这些例子提供了太少的代码行/上下文/注释,以至于我不知道如何以我希望的方式实现它。

我对编程还很陌生,所以如果有人能给我指一个有大量解释性注释的完整例子,或者直接帮助我编写代码,我将不胜感激。谢谢你花时间阅读并帮助我学习。

到目前为止我所拥有的:

// Other unrelated code here...
    //Invert color values and write to new image file
    for (e = height - 1; e != -1; e--)
    {
        for (c = 0; c < width; c++)
        {
            red = TIFFGetR(raster[c]);
            newRed = 255 - red;
            green = TIFFGetG(raster[c]);
            newGreen = 255 - green;
            blue = TIFFGetB(raster[c]);
            newBlue = 255 - blue;
            // What to do next? Is this feasible?
        }
    }
// Other unrelated code here...

如果您需要完整的代码。

我返回并查看了我的旧代码。事实证明我没有使用libtiff。尽管如此,你还是走在了正确的轨道上。你想要这样的东西;

    lineBuffer = (char *)malloc(width * 3) // 3 bytes per pixel
    for all lines
    {
       ptr = lineBuffer
       // modify your line code above so that you make a new line
       for all pixels in line
       {
            *ptr++ = newRed;
            *ptr++ = newGreen;
            *ptr++ = newBlue
       }
       // write the line using libtiff scanline write
       write a line here
    }

记住要适当地设置标签。本例假设像素为3字节。TIFF还允许在每个平面中每个像素有1个字节的单独平面。

或者,您也可以将整个图像写入新的缓冲区,而不是一次写入一行。