Candrayba
Candrayba

Reputation: 11

How to display a TIFF?

I want to display .tif into delphi using pascal and I'm already using LibTiff

var
  OpenTiff: PTIFF;
  FirstPageWidth,FirstPageHeight: Cardinal;
  FirstPageBitmap: TBitmap;
begin
  OpenTiff:=TIFFOpen('C:\World.tif','r');
  TIFFGetField(OpenTiff,TIFFTAG_IMAGEWIDTH,@FirstPageWidth);
  TIFFGetField(OpenTiff,TIFFTAG_IMAGELENGTH,@FirstPageHeight);
  FirstPageBitmap:=TBitmap.Create;
  FirstPageBitmap.PixelFormat:=pf32bit;
  FirstPageBitmap.Width:=FirstPageWidth;
  FirstPageBitmap.Height:=FirstPageHeight;
  TIFFReadRGBAImage(OpenTiff,FirstPageWidth,FirstPageHeight,
               FirstPageBitmap.Scanline[FirstPageHeight-1],0);
  TIFFClose(OpenTiff);
  TIFFReadRGBAImageSwapRB(FirstPageWidth,FirstPageheight,
               FirstPageBitmap.Scanline[FirstPageHeight-1]);

end;

But why the image not displaying? Anyone have solution? And Sorry for my bad english.

Upvotes: 0

Views: 1280

Answers (1)

Johan
Johan

Reputation: 76753

Convert the tiff into a bitmap using the code below.
Then display the bitmap as usual.
Here is the complete function you're using (with a few alterations).

function ReadTiffIntoBitmap(const Filename: string): TBitmap;
var
  OpenTiff: PTIFF;
  FirstPageWidth, FirstPageHeight: Cardinal;
begin
  Result:= nil;  //in case you want to tweak code to not raise exceptions.
  OpenTiff:= TIFFOpen(Filename,'r');
  if OpenTiff = nil then raise Exception.Create(
           'Unable to open file '''+Filename+'''');
  try 
    TIFFGetField(OpenTiff, TIFFTAG_IMAGEWIDTH, @FirstPageWidth);
    TIFFGetField(OpenTiff, TIFFTAG_IMAGELENGTH, @FirstPageHeight);
    Result:= TBitmap.Create;
    try 
      Result.PixelFormat:= pf32bit;
      Result.Width:= FirstPageWidth;
      Result.Height:= FirstPageHeight;
    except
      FreeAndNil(Result);
      raise Exception.Create('Unable to create TBitmap buffer');
    end;
    TIFFReadRGBAImage(OpenTiff, FirstPageWidth, FirstPageHeight,
                 Result.Scanline[FirstPageHeight-1],0);
    TIFFReadRGBAImageSwapRB(FirstPageWidth, FirstPageheight,
                 Result.Scanline[FirstPageHeight-1]);
  finally
    TIFFClose(OpenTiff);
  end;
end;

Now use it in the following context:
Put a button and an Image on the form.
Double click on the button and fill the OnClick handler for the button like so:

Form1.Button1Click(sender: TObject);
var
  MyBitmap: TBitmap;
begin
  MyBitmap:= ReadTiffIntoBitmap('c:\test.tiff');
  try
    //uncomment if..then if ReadTiffIntoBitmap does not raise exceptions
    //but returns nil on error instead.
    {if Assigned(MyBitmap) then} Image1.Picture.Assign(MyBitmap);
  finally
    MyBitmap.Free;
  end;
end;

I located the full version of your snippet at: http://www.asmail.be/msg0055571626.html

Upvotes: 1

Related Questions