GR32.ImageFormats.TIcon.pas 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. unit GR32.ImageFormats.TIcon;
  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. // FPC compatibility
  49. //
  50. //------------------------------------------------------------------------------
  51. {$ifdef FPC}
  52. resourcestring
  53. SVIcons = 'Icons';
  54. {$endif FPC}
  55. //------------------------------------------------------------------------------
  56. //
  57. // TImageFormatAdapterTIcon
  58. //
  59. //------------------------------------------------------------------------------
  60. // Implements IImageFormatAdapter for the TIcon class.
  61. //------------------------------------------------------------------------------
  62. type
  63. TImageFormatAdapterTIcon = class(TImageFormatReaderWriterTGraphic)
  64. strict protected
  65. // IImageFormatAdapter
  66. function AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean; override;
  67. end;
  68. //------------------------------------------------------------------------------
  69. // IImageFormatAdapter
  70. //------------------------------------------------------------------------------
  71. function TImageFormatAdapterTIcon.AssignFrom(Dest: TCustomBitmap32; Source: TPersistent): boolean;
  72. var
  73. I: Integer;
  74. P: PColor32Entry;
  75. ReassignFromMasked: Boolean;
  76. begin
  77. if (not (Source is TIcon)) then
  78. Exit(False);
  79. Result := True;
  80. AssignFromGraphicPlain(Dest, TIcon(Source), 0, False);
  81. if Dest.Empty then
  82. Exit;
  83. // Check if the icon was painted with a merged alpha channel.
  84. // That happens transparently for new-style 32-bit icons.
  85. // For all other bit depths GDI will reset our alpha channel to opaque.
  86. ReassignFromMasked := True;
  87. P := PColor32Entry(@Dest.Bits[0]);
  88. for I := 0 to Dest.Height * Dest.Width - 1 do
  89. begin
  90. if P.A > 0 then
  91. begin
  92. ReassignFromMasked := False;
  93. Break;
  94. end;
  95. Inc(P);
  96. end;
  97. // No alpha values found? Use masked approach...
  98. if ReassignFromMasked then
  99. AssignFromGraphicMasked(Dest, TIcon(Source));
  100. end;
  101. //------------------------------------------------------------------------------
  102. //------------------------------------------------------------------------------
  103. //------------------------------------------------------------------------------
  104. var
  105. ImageFormatHandle: integer = 0;
  106. initialization
  107. ImageFormatHandle := ImageFormatManager.RegisterImageFormat(
  108. TImageFormatAdapterTIcon.Create(TIcon, SVIcons, ['ico']),
  109. ImageFormatPriorityNormal);
  110. finalization
  111. ImageFormatManager.UnregisterImageFormat(ImageFormatHandle);
  112. end.