GLPictureRegisteredFormats.pas 2.9 KB

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