2014.02.04 23:29 "[Tiff] LibTiffDelphi and 1bit tiff", by Martin Masci
Hi,
I'm using LibTiffDelphi to convert a bitmap image to 1bit tiff image.This is my code:
var OpenTiff: PTIFF; RowSize: Longword; RowsPerStrip: Longword; StripMemory: Pointer; StripIndex: Longword; StripRowOffset: Longword; StripRowCount: Longword; mb: PByte; ny: Longword; dpi: Double;begin if (Bitmap.PixelFormat<>pf1bit) then raise Exception.Create('WriteBitmapToTiff is designed for singlebit bitmaps only');
InvertRect(Bitmap.Canvas.Handle, Bitmap.Canvas.ClipRect); 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(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);
// deve essere un double, se si usa un integer non funziona dpi := 200; TIFFSetField(OpenTiff,TIFFTAG_XRESOLUTION, dpi); TIFFSetField(OpenTiff,TIFFTAG_YRESOLUTION, dpi); TIFFSetField(OpenTiff,TIFFTAG_RESOLUTIONUNIT,RESUNIT_INCH); 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;
Now I need control threshold to change contrast. In FreeImage for example I can use Threshold(Value) function to change threshold from 0 to 255 value.
How can I edit threshold with LibTiffDelphi?
Thanks