简体   繁体   中英

Writing a tif pixel by pixel using LibTiff?

Is it possible to create a new tif by iterating pixel by pixel and setting the RGB values for each pixel?

Let me explain what I'm attempting to do. I'm trying to open an existing tif, read it using TIFFReadRGBAImage , take the RGB values given by TIFFGetR / TIFFGetG / TIFFGetB , subtract them from 255, take those new values and use them to write each pixel one by one. In the end I'd like to end up with the original image and a new "complement" image that would be like a negative of the original.

Is there a way to do this using LibTiff? I've gone over the documentation and searched around Google but I've only seen very short examples of TIFFWriteScanline which provide so little lines of code/context/comments that I cannot figure out how to implement it in the way that I'd like it to work.

I'm still fairly new to programming so if someone could please either point me to a thorough example with plenty of explanatory comments or help me out directly with my code, I would appreciate it greatly. Thank you for taking the time to read this and help me learn.

What I have so far:

// 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 if you need it.

I went back and looked at my old code. It turns out that I didn't use libtiff. Nevertheless you are on the right track. You want something like;

    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
    }

Remember to set the tags appropriately. This example assumes 3 byte pixels. TIFF also allows for separate planes of 1 byte per pixel in each plane.

Alternately you can also write the whole image into a new buffer instead of one line at a time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM