使用 LibTiff 逐像素写入 tif?

Writing a tif pixel by pixel using LibTiff?

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

让我解释一下我正在尝试做什么。我正在尝试打开现有的 tif,使用 TIFFReadRGBAImage 读取它,获取 TIFFGetR/TIFFGetG/TIFFGetB 给出的 RGB 值,从 255 中减去它们,获取那些新值并使用它们逐个写入每个像素。最后,我想得到原始图像和一个新的 "complement" 图像,就像原始图像的底片一样。

有没有办法使用 LibTiff 做到这一点?我浏览了文档并搜索了 Google,但我只看到了非常短的 TIFFWriteScanline 示例,其中提供的 code/context/comments 行太少,以至于我无法弄清楚如何实现它以我希望的方式工作。

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

我目前拥有的:

// 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...

Full code需要的话

我回去查看了我的旧代码。原来我没有用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 字节的单独平面。

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