GR32.ImageFormats.BMP.pas 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. unit GR32.ImageFormats.BMP;
  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 BMP 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. {$ifndef FPC}
  40. Windows,
  41. {$else FPC}
  42. LCLType,
  43. {$endif FPC}
  44. GR32,
  45. GR32_Clipboard,
  46. GR32.ImageFormats;
  47. const
  48. FileSignatureBMP : AnsiString = #$42#$4d;//#$00#$00#$00#$00#$00#$00#$00#$00; // BM...and then some
  49. FileSignatureBMPMask: AnsiString = #$ff#$ff;//#$00#$00#$00#$00#$ff#$ff#$ff#$ff;
  50. resourcestring
  51. sImageFormatBMPName = 'Bitmaps';
  52. //------------------------------------------------------------------------------
  53. //
  54. // TImageFormatAdapterBMP
  55. //
  56. //------------------------------------------------------------------------------
  57. // Implements reader and writer for the BMP image format.
  58. // Uses the built-in bitmap support of TCustomBitmap32.
  59. //------------------------------------------------------------------------------
  60. type
  61. TImageFormatAdapterBMP = class(TCustomImageFormat,
  62. IImageFormat,
  63. IImageFormatFileInfo,
  64. IImageFormatReader,
  65. IImageFormatWriter,
  66. IImageFormatResourceReader,
  67. IImageFormatClipboardFormat)
  68. strict private
  69. // IImageFormatFileInfo
  70. function ImageFormatDescription: string;
  71. function ImageFormatFileTypes: TFileTypes;
  72. strict private
  73. // IImageFormatReader
  74. function CanLoadFromStream(AStream: TStream): boolean;
  75. function LoadFromStream(ADest: TCustomBitmap32; AStream: TStream): boolean;
  76. strict private
  77. // IImageFormatWriter
  78. procedure SaveToStream(ASource: TCustomBitmap32; AStream: TStream);
  79. strict private
  80. // IImageFormatClipboardFormat
  81. function SupportsClipboardFormat(AFormat: TClipboardFormat): Boolean;
  82. function PasteFromClipboard(ADest: TCustomBitmap32): boolean;
  83. function LoadFromClipboardFormat(ADest: TCustomBitmap32; AFormat: TClipboardFormat; AData: THandle; APalette: THandle): boolean;
  84. strict private
  85. // IImageFormatResourceReader
  86. function LoadFromResource(ADest: TCustomBitmap32; AResourceType: TResourceType; AStream: TStream): boolean;
  87. end;
  88. TBitmap32Cracker = class(TCustomBitmap32);
  89. //------------------------------------------------------------------------------
  90. // IImageFormatFileInfo
  91. //------------------------------------------------------------------------------
  92. function TImageFormatAdapterBMP.ImageFormatFileTypes: TFileTypes;
  93. begin
  94. Result := ['bmp', 'dib', 'rle'];
  95. end;
  96. function TImageFormatAdapterBMP.ImageFormatDescription: string;
  97. begin
  98. Result := sImageFormatBMPName;
  99. end;
  100. //------------------------------------------------------------------------------
  101. // IImageFormatReader
  102. //------------------------------------------------------------------------------
  103. function TImageFormatAdapterBMP.CanLoadFromStream(AStream: TStream): boolean;
  104. begin
  105. Result := CheckFileSignature(AStream, FileSignatureBMP, FileSignatureBMPMask);
  106. end;
  107. function TImageFormatAdapterBMP.LoadFromStream(ADest: TCustomBitmap32; AStream: TStream): boolean;
  108. begin
  109. // Use LoadFromBMPStream instead of LoadFromStream to avoid recursion
  110. Result := TBitmap32Cracker(ADest).LoadFromBMPStream(AStream, AStream.Size);
  111. end;
  112. //------------------------------------------------------------------------------
  113. // IImageFormatWriter
  114. //------------------------------------------------------------------------------
  115. procedure TImageFormatAdapterBMP.SaveToStream(ASource: TCustomBitmap32; AStream: TStream);
  116. begin
  117. ASource.SaveToStream(AStream);
  118. end;
  119. //------------------------------------------------------------------------------
  120. // IImageFormatClipboard
  121. //------------------------------------------------------------------------------
  122. function TImageFormatAdapterBMP.SupportsClipboardFormat(AFormat: TClipboardFormat): Boolean;
  123. begin
  124. {$ifdef FPC}
  125. Result := (TPredefinedClipboardFormat(AFormat) = pcfBitmap);
  126. {$else FPC}
  127. Result := (AFormat in [CF_BITMAP, CF_DIBV5]);
  128. {$endif FPC}
  129. end;
  130. function TImageFormatAdapterBMP.PasteFromClipboard(ADest: TCustomBitmap32): boolean;
  131. begin
  132. Result := PasteBitmap32FromClipboard(ADest);
  133. end;
  134. function TImageFormatAdapterBMP.LoadFromClipboardFormat(ADest: TCustomBitmap32; AFormat: TClipboardFormat;
  135. AData: THandle; APalette: THandle): boolean;
  136. begin
  137. Result := False;
  138. end;
  139. //------------------------------------------------------------------------------
  140. // IImageFormatResourceReader
  141. //------------------------------------------------------------------------------
  142. function TImageFormatAdapterBMP.LoadFromResource(ADest: TCustomBitmap32; AResourceType: TResourceType;
  143. AStream: TStream): boolean;
  144. begin
  145. Result := TBitmap32Cracker(ADest).LoadFromDIBStream(AStream, AStream.Size);
  146. end;
  147. //------------------------------------------------------------------------------
  148. //------------------------------------------------------------------------------
  149. //------------------------------------------------------------------------------
  150. var
  151. ImageFormatHandle: integer = 0;
  152. initialization
  153. ImageFormatHandle := ImageFormatManager.RegisterImageFormat(TImageFormatAdapterBMP.Create, ImageFormatPriorityBest);
  154. finalization
  155. ImageFormatManager.UnregisterImageFormat(ImageFormatHandle);
  156. end.