2002.12.18 13:14 "Raw data to tiff format", by Soujanya Rao

2002.12.18 13:28 "Re: Raw data to tiff format", by Thad Humphries

If you know the size, compression, resolution, etc. of the data, you can just put a header on it and be done. (Of course, 'header' is just a term--IIRC, libtiff puts the 'header' at the end of the file.)

The code below is such an example. I wrote it about three years ago. Basically, the "FormBackgroundRec" contained a block of raw Group 4 fax data and, independently, the data's size, resolution, etc. I opened a TIFF file, created the tags, then read in and wrote off the data.

Int32
CCOLDTextApp::WriteTIFF (
    const char              * filename,
    const FormBackgroundRec   bgimageRec,
    CDataStream               formDataStream )
{
    TIFF    *tif = TIFFOpen( filename, "wb" );

    if ( tif != NULL )
    {
        uint16      w = bgimageRec.pixWidth,
                  h = bgimageRec.pixLength;

        // Fields for all TIFF files.
        TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, w );
        TIFFSetField( tif, TIFFTAG_IMAGELENGTH, h );

        TIFFSetField( tif, TIFFTAG_COMPRESSION, COMPRESSION_CCITTFAX4 );

        TIFFSetField( tif, TIFFTAG_XRESOLUTION, (float)bgimageRec.resolution );
        TIFFSetField( tif, TIFFTAG_YRESOLUTION, (float)bgimageRec.resolution );
        TIFFSetField( tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH );

        // Called only for one bit images.
        TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISWHITE );
        TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
        TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 1 );

        // Required by libtiff.a
        TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );

        // Other fields.
        TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, h );

        // The image itself.
        tdata_t               buf = _TIFFmalloc( bgimageRec.backLen );
        if ( buf == NULL )
        {
            TIFFClose( tif );
            return kScanlineMallocErr;
        }

        formDataStream.ReadData( buf, bgimageRec.backLen );

        tsize_t               result = TIFFWriteRawStrip( tif, 0, buf, bgimageRec.backLen );

        if ( result != bgimageRec.backLen )
        {
            _TIFFfree( buf );
            TIFFClose( tif );
            return -666;
        }

        _TIFFfree( buf );
        TIFFClose( tif );
    }
    else
    {
        return kTIFFOpenErr;
    }

    return 0;
}

------------------------------------------------------------------------
Thad Humphries              "...no religious test shall ever be required
Web Development Manager      as a qualification to any office or public
Phone: 540/675-3015, x225    trust under the United States." -Article VI