[Gdal-dev] How to read a 1 bit TIFF file

Frank Warmerdam warmerdam at pobox.com
Tue Feb 4 10:53:49 EST 2003


Harry Ly wrote:
> Hi,
>  
> Can anyone show me how to read a portion of a 1 bit TIFF image into a 
> char array using scanline or ReadStripEncoded function.
>  

Harry,

If you want to use the libtiff functions directly you would be better off
emailing the tiff mailing list.   I have attached some code out of
gdal/frmts/gtiff/geotiff.cpp for loading blocks from a TIFF file that
work for 1 bit files.

The key issues in calling TIFFReadEncodedStrip() like this:

   TIFFReadEncodedStrip(hTIFF, nBlockId, pabyBlockBuf, nBlockBufSize);

are:

  o The image is split into strips.  The number of strips will be
    (IMAGEHEIGHT + ROWSPERSTRIP - 1) / ROWSPERSTRIP with the blockids going
    from 0 to stripcount-1.

  o the size of the buffer required can be determined by calling TIFFStripSize().

The following code shows how to address the individual bits in the bitmap,
for instance for unpacking into one pixel per byte as I do.

Good luck,

-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam at pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | Geospatial Programmer for Rent

/* -------------------------------------------------------------------- */
/*      Get block size.                                                 */
/* -------------------------------------------------------------------- */
     if( TIFFIsTiled(hTIFF) )
         nBlockBufSize = TIFFTileSize( hTIFF );
     else
         nBlockBufSize = TIFFStripSize( hTIFF );

/* -------------------------------------------------------------------- */
/*      Allocate a temporary buffer for this strip.                     */
/* -------------------------------------------------------------------- */
     if( pabyBlockBuf == NULL )
     {
         pabyBlockBuf = (GByte *) VSICalloc( 1, nBlockBufSize );
         if( pabyBlockBuf == NULL )
         {
             CPLError( CE_Failure, CPLE_OutOfMemory,
                       "Unable to allocate %d bytes for a temporary strip buffer\n"
                       "in GeoTIFF driver.",
                       nBlockBufSize );

             return( CE_Failure );
         }
     }

/* -------------------------------------------------------------------- */
/*      If we don't have this block already loaded, and we know it      */
/*      doesn't yet exist on disk, just zero the memory buffer and      */
/*      pretend we loaded it.                                           */
/* -------------------------------------------------------------------- */
     if( eAccess == GA_Update && !IsBlockAvailable( nBlockId ) )
     {
         memset( pabyBlockBuf, 0, nBlockBufSize );
         nLoadedBlock = nBlockId;
         return CE_None;
     }

/* -------------------------------------------------------------------- */
/*      Load the block, if it isn't our current block.                  */
/* -------------------------------------------------------------------- */
     if( TIFFIsTiled( hTIFF ) )
     {
         if( TIFFReadEncodedTile(hTIFF, nBlockId, pabyBlockBuf,
                                 nBlockBufSize) == -1 )
         {
             /* Once TIFFError() is properly hooked, this can go away */
             CPLError( CE_Failure, CPLE_AppDefined,
                       "TIFFReadEncodedTile() failed." );

             memset( pabyBlockBuf, 0, nBlockBufSize );

             eErr = CE_Failure;
         }
     }
     else
     {
         if( TIFFReadEncodedStrip(hTIFF, nBlockId, pabyBlockBuf,
                                  nBlockBufSize) == -1 )
         {
             /* Once TIFFError() is properly hooked, this can go away */
             CPLError( CE_Failure, CPLE_AppDefined,
                       "TIFFReadEncodedStrip() failed." );

             memset( pabyBlockBuf, 0, nBlockBufSize );

             eErr = CE_Failure;
         }
     }





More information about the Gdal-dev mailing list