AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

Thread

2004.11.22 06:32 "[Tiff] Unexpected TIFF output", by Zac Bond
2004.11.22 09:25 "Re: [Tiff] Unexpected TIFF output", by Eric VERGNAUD

2004.11.22 06:32 "[Tiff] Unexpected TIFF output", by Zac Bond

I am attempting to write a program that extracts text from an old document based on the color of the ink. I am having a lot of trouble figuring out how to use libTIFF to write my image to a file. Here are the results:

Original: http://home.cwru.edu/~zwb2/smallblock.tif Result: http://home.cwru.edu/~zwb2/binresult.tif

As you can see, the second image looks like a squashed version of part of the original, with odd vetical lines. I don't understand how this is happening; the contents of my image buffer are correct. Also, I notice that the output has only values of 255 and 0, whereas my buffer used 250 and 10. Huh?

Here is my code. I'm guesing I am using TIFFWriteScanline wrong, or else perhaps I forgot to set an important tag? (I deleted the errorchecking for this post, but there were no errors when run)

int main(int argc, char* argv[])
{
        // Open file
        TIFF* infile = TIFFOpen(argv[1], "r");

        // Read in tags
        uint32 rows, columns;
        size_t imagesize;
        int errorval;

        TIFFGetField(infile, TIFFTAG_IMAGELENGTH, &rows);
        TIFFGetField(infile, TIFFTAG_IMAGEWIDTH, &columns);

        // Now we read in the color image using RGBA

        imagesize = rows * columns;
        uint32* raster = (uint32*) _TIFFmalloc(imagesize * sizeof(uint32));

        TIFFReadRGBAImageOriented(infile, columns, rows,                                                                        raster,

ORIENTATION_TOPLEFT, 1);

        // Now create an output file & set fields for an 8-bit grayscale
image

        TIFF* outfile = TIFFOpen("binresult.tif", "w");
        TIFFSetField(outfile, TIFFTAG_IMAGELENGTH, rows);
        TIFFSetField(outfile, TIFFTAG_IMAGEWIDTH, columns);
        TIFFSetField(outfile, TIFFTAG_BITSPERSAMPLE, 8);
        TIFFSetField(outfile, TIFFTAG_COMPRESSION, 1);
        TIFFSetField(outfile, TIFFTAG_FILLORDER, 1);
        TIFFSetField(outfile, TIFFTAG_ORIENTATION, 1);
        TIFFSetField(outfile, TIFFTAG_PHOTOMETRIC, 1);
        TIFFSetField(outfile, TIFFTAG_PLANARCONFIG, 1);
        TIFFSetField(outfile, TIFFTAG_SAMPLESPERPIXEL, 1);

        // Allocate memory the new image
        uint32* binimage = (uint32*) _TIFFmalloc(imagesize * sizeof(uint32));

        for (int r = 0; r < rows; r++)
                for (int c = 0; c < columns; c++)
                {
                        // Various logic to threshold the image
                        // based on pixel color goes on in here
                        if (fill_loc)
                                binimage[r * columns + c] = 10;
                        else
                                binimage[r * columns + c] = 250;
                }

        // Write the new file
        for (int r = 0; r < rows; r++)
          TIFFWriteScanline(outfile, &binimage[r * columns], r);

        // Clean up memory, etc.
}

Thanks to anyone who can help me out. I'm a bit new to this, and I'm sure I'm missing something simple.

-Zac Bod