GR32.ImageFormats.TWICImage.pas 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. unit GR32.ImageFormats.TWICImage;
  2. (* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1 or LGPL 2.1 with linking exception
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * Alternatively, the contents of this file may be used under the terms of the
  16. * Free Pascal modified version of the GNU Lesser General Public License
  17. * Version 2.1 (the "FPC modified LGPL License"), in which case the provisions
  18. * of this license are applicable instead of those above.
  19. * Please see the file LICENSE.txt for additional information concerning this
  20. * license.
  21. *
  22. * The Original Code is image format support for Graphics32
  23. *
  24. * The Initial Developer of the Original Code is
  25. * Anders Melander <[email protected]>
  26. *
  27. * Portions created by the Initial Developer are Copyright (C) 2008-2022
  28. * the Initial Developer. All Rights Reserved.
  29. *
  30. * Contributor(s):
  31. *
  32. * ***** END LICENSE BLOCK ***** *)
  33. interface
  34. {$include GR32.inc}
  35. implementation
  36. uses
  37. Classes,
  38. Graphics,
  39. SysUtils,
  40. {$ifndef FPC}
  41. Consts,
  42. {$endif FPC}
  43. GR32,
  44. GR32.ImageFormats.TGraphic,
  45. GR32.ImageFormats;
  46. //------------------------------------------------------------------------------
  47. //
  48. // TImageFormatAdapterTWICImage
  49. //
  50. //------------------------------------------------------------------------------
  51. // Implements IImageFormatAdapter for the TIFF file format via the TWICImage
  52. // class.
  53. //------------------------------------------------------------------------------
  54. type
  55. TImageFormatAdapterTWICImage = class(TImageFormatReaderTGraphic,
  56. IImageFormatAdapter)
  57. strict protected
  58. // IImageFormatAdapter
  59. function AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean; override;
  60. strict protected
  61. // IImageFormatReader
  62. function CanLoadFromStream(AStream: TStream): boolean; override;
  63. end;
  64. const
  65. FileSignatureTIFFLittle : AnsiString = #$49#$49#$2A#$00;// Little endian
  66. FileSignatureTIFFBig : AnsiString = #$4D#$4D#$00#$2A;// Big endian
  67. FileSignatureTIFFMask: AnsiString = #$ff#$ff#$ff#$ff;
  68. //------------------------------------------------------------------------------
  69. // IImageFormatAdapter
  70. //------------------------------------------------------------------------------
  71. function TImageFormatAdapterTWICImage.AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean;
  72. begin
  73. {$IFDEF PLATFORM_INDEPENDENT}
  74. Result := inherited;
  75. {$ELSE}
  76. if (not (Source is TWICImage)) then
  77. Exit(False);
  78. Result := True;
  79. AssignFromGraphicPlain(Dest, TGraphic(Source), 0, False);
  80. {$ENDIF}
  81. end;
  82. //------------------------------------------------------------------------------
  83. // IImageFormatReader
  84. //------------------------------------------------------------------------------
  85. function TImageFormatAdapterTWICImage.CanLoadFromStream(AStream: TStream): boolean;
  86. begin
  87. // TWICImage does not implement CanLoadFromStream
  88. Result := CheckFileSignature(AStream, FileSignatureTIFFLittle, FileSignatureTIFFMask) or
  89. CheckFileSignature(AStream, FileSignatureTIFFBig, FileSignatureTIFFMask);
  90. end;
  91. //------------------------------------------------------------------------------
  92. //------------------------------------------------------------------------------
  93. //------------------------------------------------------------------------------
  94. var
  95. ImageFormatHandle: integer = 0;
  96. initialization
  97. {$IFNDEF PLATFORM_INDEPENDENT}
  98. ImageFormatHandle := ImageFormatManager.RegisterImageFormat(
  99. TImageFormatAdapterTWICImage.Create(TWICImage, SVTIFFImages, ['tif', 'tiff']),
  100. ImageFormatPriorityNormal);
  101. {$ENDIF}
  102. finalization
  103. ImageFormatManager.UnregisterImageFormat(ImageFormatHandle);
  104. end.