3

I'm using Delphitwain (delphitwain.sourceforge.net) to add scan functionality to my app. Scanning is working fine and I can save bmp and jpeg files. Now I need to:

  • save in 300dpi (the scanner is capable)
  • save in TIFF format

After digging around, I found 2 tips:

http://www.delphipraxis.net/132787-farbstich-nach-bitmap-operation.html

http://synopse.info/fossil/wiki?name=GDI%2B

Here is my final code:

procedure TForm1.GoAcquireClick(Sender: TObject);
begin
  Counter := 0;
  Twain.SourceManagerLoaded := TRUE;
  Twain.LoadSourceManager;
  Twain.TransferMode := ttmMemory;

  with Twain.Source[ 0 ] do
  begin
    Loaded := TRUE;
    SetIXResolution(300);
    SetIYResolution(300);
    SetIBitDepth(1);
    EnableSource(true, true);
    while Enabled do Application.ProcessMessages;
  end;
end;

procedure TForm1.TwainTwainAcquire(Sender: TObject; const Index: Integer;
   Image: TBitmap; var Cancel: Boolean);
var
   TiffHolder: TSynPicture;
begin
  Inc( Counter );
  Current := Counter;
  ImageHolder.Picture.Assign( Image );
  ImageHolder.Picture.Bitmap.Monochrome := true;
  ImageHolder.Picture.Bitmap.Pixelformat := pf1Bit;

  SynGDIPlus.SaveAs(ImageHolder.Picture, format('c:\temp\teste%d.tif',[ Counter ]), gptTIF );
end;

Result: the image still is 96dpi and were saved as BMP (even with TIF extension).

What am I missing?

Johan
  • 74,508
  • 24
  • 191
  • 319
Josir
  • 1,282
  • 22
  • 35
  • 1
    Great question. Please post your Delphi version. I would suggest you change the subject to how to change the DPI from 96 to 300. – Warren P Jul 07 '11 at 19:57
  • related: http://stackoverflow.com/questions/2059343/twain-scanning-components-for-delphi (not a duplicate because this new question boils down to how to change the DPI and file-format on the scan using these same Twain components, probably a property setup issue) – Warren P Jul 07 '11 at 19:57
  • Thanks for your tip Warren. I would keep the word "scan" instead of "change resolution" because I don´t know if the acquired image is already in 300dpi or it was changed after I manipulated it on Image parameter. – Josir Jul 07 '11 at 20:14
  • Delphi 7. But I have licences for newer versions if a new version solves my problem :) – Josir Jul 07 '11 at 20:24
  • 1
    GraphicsEx by Mike Liksche (of VirtualTreeView fame) can read and yes, write, TIFF files. I would give it a try - http://www.soft-gems.net/index.php?option=com_content&task=view&id=13&Itemid=33 – Leonardo Herrera Jul 08 '11 at 13:59
  • TIFF generations is working now but scan is not saving in 300dpi. I read question [Question 4536227](http://stackoverflow.com/questions/4536227/specific-area-scanning-by-delphi-twain) but I could not imagine where do I have to put that code. – Josir Jul 21 '11 at 18:29

2 Answers2

1

The GDI+ library is able to save tiff pictures.

The SynGdiPlus unit use {557CF405-1A04-11D3-9A73-0000F81EF32E} for its TIFF encoder.

From the TSynPicture.SaveAs code, I see two possibilities:

  • Either you don't have the corresponding TIFF encoder installed;
  • Either there is some missing parameter expected by the TIFF encoder.

Try this version:

type
  /// the optional TIFF compression levels
  // - use e.g. ord(evCompressionCCITT4) to save a TIFF picture as CCITT4
  TGDIPPEncoderValue = (
    evColorTypeCMYK,
    evColorTypeYCCK,
    evCompressionLZW,
    evCompressionCCITT3,
    evCompressionCCITT4,
    evCompressionRle,
    evCompressionNone,
    (...)
    evFrameDimensionPage);

const
  EncoderCompression: TGUID = '{e09d739d-ccd4-44ee-8eba-3fbf8be4fc58}';

function TSynPicture.SaveAs(Stream: TStream; Format: TGDIPPictureType;
  CompressionQuality: integer): TGdipStatus;
var fStream: IStream;
    Len,Dummy: Int64;
    tmp: pointer;
    Params: TEncoderParameters;
    PParams: pointer;
    MS: TMemoryStream absolute Stream;
begin
  if not Gdip.Exists or (Stream=nil) or (fImage=0) then begin
    result := stInvalidParameter;
    exit;
  end;
  Params.Count := 1;
  Params.Parameter[0].Type_ := EncoderParameterValueTypeLong;
  Params.Parameter[0].NumberOfValues := 1;
  Params.Parameter[0].Value := @CompressionQuality;
  PParams := nil;
  case Format of
  gptJPG: if CompressionQuality>=0 then begin
    Params.Parameter[0].Guid := EncoderQuality;
    PParams := @Params;
  end;
  gptTIF: begin
    if not (TGDIPPEncoderValue(CompressionQuality) in [
        evCompressionLZW, evCompressionCCITT3, evCompressionCCITT4,
        evCompressionRle, evCompressionNone]) then
      // default tiff compression is LZW
      CompressionQuality := ord(evCompressionLZW);
    Params.Parameter[0].Guid := EncoderCompression;
    PParams := @Params;
  end;
  end;
  CreateStreamOnHGlobal(0, true, fStream);
  (...)

It will add the EncoderCompression parameter for TIFF pictures, which seems to be required.

I've updated the source code repository version to include this correction.

Arnaud Bouchez
  • 42,305
  • 3
  • 71
  • 159
  • Merci Beaucoup! Tiff generation works great. But it didn´t save in 300dpi. But I think this is not related to your library. The error should be in Twain config. – Josir Jul 13 '11 at 15:30
0

Saving TIFF files is hard in Delphi. I don't know any free/open source modules that do this.

ImageEn works.

In the past I have used saving as bmp and converting to tiff with irfanview through createprocess with the commandline 'i_view32.exe c:\temp\scanned.bmp /bpp=1 /convert=c:\temp\scanned.tif'

Jan Oosting
  • 467
  • 2
  • 3
  • If you read better the OP question, you'll see it uses an Open Source wrapper to the GDI+ library for saving tiff pictures. – Arnaud Bouchez Jul 08 '11 at 05:47
  • He also said that the files came out internally as BMP files although they have the .tif extension – Jan Oosting Jul 08 '11 at 06:20
  • 1
    The fact that the wrapper has an issue does not mean that it would not be able to save TIFF. GDI+ is able to save TIFF pictures. It's the easiest way of doing it in Delphi. – Arnaud Bouchez Jul 08 '11 at 06:45
  • Currently it isn't the easiest way because it doesn't work, whereas I provided 2 working solutions that admittedly have their own drawbacks. – Jan Oosting Jul 08 '11 at 07:54
  • I will keep trying using open/free software before trying some more drastic solution using jailed software. – Josir Jul 13 '11 at 15:35
  • Unfortunatelly, the irfanview solution is not an option on this project because I intend to distribute it to several users and the irfanview installation could be a additional problem to solve. – Josir Jul 13 '11 at 15:40
  • After trying several free/open components, the only one that saves on 300dpi is ImageEn 3. It's not cheap but it works very well, scanning and generating TIFF with all its attributes. – Josir Sep 17 '11 at 10:31