ImagingTiff.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. {
  2. Vampyre Imaging Library
  3. by Marek Mauder
  4. https://github.com/galfar/imaginglib
  5. https://imaginglib.sourceforge.io
  6. - - - - -
  7. This Source Code Form is subject to the terms of the Mozilla Public
  8. License, v. 2.0. If a copy of the MPL was not distributed with this
  9. file, You can obtain one at https://mozilla.org/MPL/2.0.
  10. }
  11. unit ImagingTiff;
  12. {$I ImagingOptions.inc}
  13. interface
  14. uses
  15. SysUtils, Imaging, ImagingTypes, ImagingUtility, ImagingIO;
  16. type
  17. { TIFF (Tag Image File Format) loader/saver base class.}
  18. TBaseTiffFileFormat = class(TImageFileFormat)
  19. protected
  20. FCompression: Integer;
  21. FJpegQuality: Integer;
  22. procedure Define; override;
  23. public
  24. function TestFormat(Handle: TImagingHandle): Boolean; override;
  25. { Specifies compression scheme used when saving TIFF images. Supported values
  26. are 0 (Uncompressed), 1 (LZW), 2 (PackBits RLE), 3 (Deflate - ZLib), 4 (JPEG),
  27. 5 (CCITT Group 4 fax encoding - for binary images only).
  28. Default is 1 (LZW). Note that not all images can be stored with
  29. JPEG compression - these images will be saved with default compression if
  30. JPEG is set.}
  31. property Compression: Integer read FCompression write FCompression;
  32. { Controls compression quality when selected TIFF compression is Jpeg.
  33. It is number in range 1..100. 1 means small/ugly file,
  34. 100 means large/nice file. Accessible trough ImagingTiffJpegQuality option.}
  35. property JpegQuality: Integer read FJpegQuality write FJpegQuality;
  36. end;
  37. const
  38. TiffCompressionOptionNone = 0;
  39. TiffCompressionOptionLzw = 1;
  40. TiffCompressionOptionPackbitsRle = 2;
  41. TiffCompressionOptionDeflate = 3;
  42. TiffCompressionOptionJpeg = 4;
  43. TiffCompressionOptionGroup4 = 5;
  44. { Read only metadata info - name of compression scheme (LZW, none, JPEG, G4, ...)
  45. used in last loaded TIFF. }
  46. SMetaTiffCompressionName = 'TiffCompressionName';
  47. { Original resolution unit of loaded TIFF. Type is UInt.
  48. RESUNIT_NONE = 1; // no meaningful units
  49. RESUNIT_INCH = 2; // english
  50. RESUNIT_CENTIMETER = 3; // metric }
  51. SMetaTiffResolutionUnit = 'TiffResolutionUnit';
  52. implementation
  53. {$IFNDEF DONT_LINK_FILE_FORMATS}
  54. // So far we have only one TIFF support implementation - libtiff
  55. {$DEFINE USE_LIBTIFF}
  56. // libtiff for FPC ARM is disabled by default due to potential hardfp/softfp
  57. // ABI problems (without linking to any lib FPC generated binary does not call "ld"
  58. // and hardfp exe can run on softfp target). If you know what you're doing enable it.
  59. {$IF Defined(FPC) and Defined(CPUARM)}
  60. {$UNDEF USE_LIBTIFF}
  61. {$IFEND}
  62. // Not even dynamic linking works at the moment
  63. {$IF Defined(DELPHI) and Defined(MACOS))}
  64. {$UNDEF USE_LIBTIFF}
  65. {$IFEND}
  66. // Also disable for Delphi ARM targets
  67. {$IF Defined(DELPHI) and Defined(CPUARM))}
  68. {$UNDEF USE_LIBTIFF}
  69. {$IFEND}
  70. uses
  71. {$IFDEF USE_LIBTIFF}
  72. ImagingTiffLib,
  73. {$ENDIF}
  74. ImagingExtFileFormats;
  75. {$ENDIF}
  76. const
  77. STiffFormatName = 'Tagged Image File Format';
  78. STiffMasks = '*.tif,*.tiff';
  79. TiffDefaultCompression = 1;
  80. TiffDefaultJpegQuality = 90;
  81. const
  82. TiffBEMagic: TChar4 = 'MM'#0#42;
  83. TiffLEMagic: TChar4 = 'II'#42#0;
  84. {
  85. TBaseTiffFileFormat implementation
  86. }
  87. procedure TBaseTiffFileFormat.Define;
  88. begin
  89. inherited;
  90. FName := STiffFormatName;
  91. FFeatures := [ffLoad, ffSave, ffMultiImage];
  92. FCompression := TiffDefaultCompression;
  93. FJpegQuality := TiffDefaultJpegQuality;
  94. AddMasks(STiffMasks);
  95. RegisterOption(ImagingTiffCompression, @FCompression);
  96. RegisterOption(ImagingTiffJpegQuality, @FJpegQuality);
  97. end;
  98. function TBaseTiffFileFormat.TestFormat(Handle: TImagingHandle): Boolean;
  99. var
  100. Magic: TChar4;
  101. ReadCount: LongInt;
  102. begin
  103. Result := False;
  104. if Handle <> nil then
  105. begin
  106. ReadCount := GetIO.Read(Handle, @Magic, SizeOf(Magic));
  107. GetIO.Seek(Handle, -ReadCount, smFromCurrent);
  108. Result := (ReadCount >= SizeOf(Magic)) and
  109. ((Magic = TiffBEMagic) or (Magic = TiffLEMagic));
  110. end;
  111. end;
  112. end.