AWARE [SYSTEMS] Imaging expertise for the Delphi developer
AWare Systems, Imaging expertise for the Delphi developer, Home TIFF and LibTiff Mailing List Archive

LibTiff Mailing List

TIFF and LibTiff Mailing List Archive
April 2006

Previous Thread
Next Thread

Previous by Thread
Next by Thread

Previous by Date
Next by Date

Contact

The TIFF Mailing List Homepage
This list is run by Frank Warmerdam
Archive maintained by AWare Systems



Valid HTML 4.01!



Thread

2006.04.06 19:40 "LibTiffDelphi problem with WriteEncodedStrip Newbee", by Thorsten Hagelstein
2006.04.07 09:56 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Joris Van Damme
2006.04.11 16:43 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Thorsten Hagelstein
2006.04.11 22:07 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Joris Van Damme
2006.04.13 15:02 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Thorsten Hagelstein
2006.04.13 19:53 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Bernie Pallek
2006.04.14 17:02 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Joris Van Damme
2006.04.15 10:46 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Joris Van Damme
2006.04.18 09:22 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Thorsten Hagelstein

2006.04.15 10:46 "Re: LibTiffDelphi problem with WriteEncodedStrip Newbee", by Joris Van Damme

Thorsten,

Joris wrote:
> Try again. If it still doesn't work, mail me your zipped bitmap and
> relevant code, and I'll take a look.

I've received your code and testfile, made sure I used the distributed
version of LibTiffDelphi, and... Sure enough, I can reproduce your
problem.

Apparently, you need to set the planar config tag, even though
SamplesPerPixel equals 1. I can only assume this is no longer a
requirement in my own LibTiffDelphi, and I thus didn't see the problem.
It is a bit silly requirement anyway, probably it got removed at some
later time.

As after inserting the line...

  TIFFSetField(OpenTiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);

...before the setting of TIFFTAG_COMPRESSION, all is fine.

Since you had a problem, I was intriged by the fact that the proposed
procedure didn't raise an exception. It was designed to do that, as is
usual in Delphi code. The reason is, I got confused about error return
value of TIFFWriteEncodedStrip (again!), and tested against 0 instead
of -1.

One more thing I noticed in your own code, is that you write with G3
compression, instead of G4. There should be no advantage to G3 over G4,
except for better error recovery in some situations that only possibly
arise after corruption. But it's not as efficient. So I'd recommend you
use G4 instead.

With these corrections in place, the code for writing a pf1bit Delphi
TBitmap to a G4 compressed TIFF should be looking like this:

procedure WriteBitmapToTiff(Bitmap: TBitmap; Filename: String);
var
  OpenTiff: PTIFF;
  RowSize: Longword;
  RowsPerStrip: Longword;
  StripMemory: Pointer;
  StripIndex: Longword;
  StripRowOffset: Longword;
  StripRowCount: Longword;
  mb: PByte;
  ny: Longword;
begin
  if (Bitmap.PixelFormat<>pf1bit) then
    raise Exception.Create('WriteBitmapToTiff is designed for singlebit
                    bitmaps only');
  RowSize:=((Bitmap.Width+7) div 8);
  RowsPerStrip:=((256*1024) div RowSize);
  if RowsPerStrip>Bitmap.Height then
    RowsPerStrip:=Bitmap.Height
  else if RowsPerStrip=0 then
    RowsPerStrip:=1;
  StripMemory:=GetMemory(RowsPerStrip*RowSize);
  OpenTiff:=TIFFOpen(PAnsiChar(Filename),'w');
  if OpenTiff=nil then
  begin
    FreeMemory(StripMemory);
    raise Exception.Create('Unable to create file '''+Filename+'''');
  end;
  TIFFSetField(OpenTiff,TIFFTAG_IMAGEWIDTH,Bitmap.Width);
  TIFFSetField(OpenTiff,TIFFTAG_IMAGELENGTH,Bitmap.Height);
  TIFFSetField(OpenTiff,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_MINISWHITE);
  TIFFSetField(OpenTiff,TIFFTAG_SAMPLESPERPIXEL,1);
  TIFFSetField(OpenTiff,TIFFTAG_BITSPERSAMPLE,1);
  TIFFSetField(OpenTiff,TIFFTAG_PLANARCONFIG,PLANARCONFIG_CONTIG);
  TIFFSetField(OpenTiff,TIFFTAG_COMPRESSION,COMPRESSION_CCITTFAX4);
  TIFFSetField(OpenTiff,TIFFTAG_ROWSPERSTRIP,RowsPerStrip);
  StripIndex:=0;
  StripRowOffset:=0;
  while StripRowOffset<Bitmap.Height do
  begin
    StripRowCount:=RowsPerStrip;
    if StripRowCount>Bitmap.Height-StripRowOffset then
      StripRowCount:=Bitmap.Height-StripRowOffset;
    mb:=StripMemory;
    for ny:=StripRowOffset to StripRowOffset+StripRowCount-1 do
    begin
      CopyMemory(mb,Bitmap.ScanLine[ny],RowSize);
      Inc(mb,RowSize);
    end;
    if TIFFWriteEncodedStrip(OpenTiff,StripIndex,
        StripMemory,StripRowCount*RowSize)=-1 then
    begin
      TIFFClose(OpenTiff);
      FreeMemory(StripMemory);
      raise Exception.Create('Failed to write '''+Filename+'''');
    end;
    Inc(StripIndex);
    Inc(StripRowOffset,StripRowCount);
  end;
  TIFFClose(OpenTiff);
  FreeMem(StripMemory);
end;


Best regards,

Joris Van Damme
info@awaresystems.be
http://www.awaresystems.be/
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html