GXS.PictureRegisteredFormats.pas 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // The graphics engine GXScene https://github.com/glscene
  3. //
  4. unit GXS.PictureRegisteredFormats;
  5. (* Hacks into the FMX to access the list of TPicture registered TGraphic formats *)
  6. interface
  7. {$I GLScene.Defines.inc}
  8. uses
  9. System.Classes,
  10. FMX.Graphics,
  11. GXS.ImageUtils;
  12. {$DEFINE PRF_HACK_PASSES}
  13. (* Returns the TGraphicClass associated to the extension, if any.
  14. Accepts anExtension with or without the '.' *)
  15. function GraphicClassForExtension(const anExtension: string): TGraphicClass;
  16. (* Adds to the passed TStrings the list of registered Formatx.
  17. Convention is "extension=description" for the string, the Objects hold
  18. the corresponding TGraphicClass (extensions do not include the '.'). *)
  19. procedure HackTPictureRegisteredFormats(destList: TStrings);
  20. implementation // -----------------------------------------------------------
  21. type
  22. PInteger = ^integer;
  23. function GraphicClassForExtension(const anExtension: string): TGraphicClass;
  24. var
  25. i: integer;
  26. sl: TStringList;
  27. buf: string;
  28. begin
  29. Result := nil;
  30. if anExtension = '' then
  31. Exit;
  32. if anExtension[1] = '.' then
  33. buf := Copy(anExtension, 2, MaxInt)
  34. else
  35. buf := anExtension;
  36. sl := TStringList.Create;
  37. try
  38. HackTPictureRegisteredFormats(sl);
  39. i := sl.IndexOfName(buf);
  40. if i >= 0 then
  41. Result := TGraphicClass(sl.Objects[i]);
  42. finally
  43. sl.Free;
  44. end;
  45. end;
  46. type
  47. PFileFormat = ^TFileFormat;
  48. TFileFormat = record
  49. GraphicClass: TGraphicClass;
  50. Extension: string;
  51. Description: string;
  52. DescResID: integer;
  53. end;
  54. // HackTPictureRegisteredFormats
  55. {$ifopt R+}
  56. {$define HackTPictureRegisteredFormats_Disable_RangeCheck}
  57. {$R-}
  58. {$endif}
  59. procedure HackTPictureRegisteredFormats(destList: TStrings);
  60. var
  61. pRegisterFileFormat, pCallGetFileFormat, pGetFileFormats, pFileFormats: PAnsiChar;
  62. iCall: cardinal;
  63. i: integer;
  64. list: TList;
  65. fileFormat: PFileFormat;
  66. begin
  67. {$MESSAGE WARN 'HackTPictureRegisteredFormats will crash when Graphics.pas is compiled with the 'Use Debug DCUs' option'}
  68. // TODO -oPW : FMX.Graphics.TImage has no RegisterFileFormat as VCL.Graphics.TPicture
  69. (*pRegisterFileFormat := PAnsiChar(@TPicture.RegisterFileFormat);*)
  70. if pRegisterFileFormat[0] = #$FF then // in case of BPL redirector
  71. pRegisterFileFormat := PAnsiChar(PCardinal(PCardinal(@pRegisterFileFormat[2])^)^);
  72. pCallGetFileFormat := @pRegisterFileFormat[16];
  73. iCall := PCardinal(pCallGetFileFormat)^;
  74. pGetFileFormats := @pCallGetFileFormat[iCall + 4];
  75. pFileFormats := PAnsiChar(PCardinal(@pGetFileFormats[2])^);
  76. list := TList(PCardinal(pFileFormats)^);
  77. if list <> nil then
  78. begin
  79. for i := 0 to list.Count - 1 do
  80. begin
  81. fileFormat := PFileFormat(list[i]);
  82. destList.AddObject(fileFormat.Extension + '=' + fileFormat.Description,
  83. TObject(fileFormat.GraphicClass));
  84. end;
  85. end;
  86. end;
  87. {$ifdef HackTPictureRegisteredFormats_Disable_RangeCheck}
  88. {$R+}
  89. {$undef HackTPictureRegisteredFormats_Disable_RangeCheck}
  90. {$endif}
  91. end.