DemoUtils.pas 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. unit DemoUtils;
  2. {$I ImagingOptions.inc}
  3. interface
  4. uses
  5. SysUtils,
  6. Classes,
  7. ImagingTypes,
  8. Imaging,
  9. ImagingUtility;
  10. const
  11. SDataDir = 'Data';
  12. SSourceDir = 'Source';
  13. function ExpandFileTo(const FileName, BasePath: string): string;
  14. function SwapPathDelims(const FileName: string; const NewDelim: string = PathDelim): string;
  15. function GetDataDir: string;
  16. function FileNameInDataDir(const BaseFileName: string): string; inline;
  17. function GetRootDir: string;
  18. // Returns next valid image format.
  19. function NextFormat(Format: TImageFormat): TImageFormat;
  20. implementation
  21. function ExpandFileTo(const FileName, BasePath: string): string;
  22. var
  23. OldPath: string;
  24. begin
  25. GetDir(0, OldPath);
  26. try
  27. if SysUtils.DirectoryExists(BasePath) then
  28. begin
  29. ChDir(BasePath);
  30. Result:= ExpandFileName(FileName);
  31. end
  32. else
  33. Result:=FileName;
  34. finally
  35. ChDir(OldPath);
  36. end;
  37. end;
  38. function SwapPathDelims(const FileName, NewDelim: string): string;
  39. begin
  40. Result := FileName;
  41. Result := StringReplace(Result, '\', NewDelim, [rfReplaceAll]);
  42. Result := StringReplace(Result, '/', NewDelim, [rfReplaceAll]);
  43. end;
  44. function GetDataDir: string;
  45. var
  46. Iter: Integer;
  47. begin
  48. Iter := 0;
  49. Result := GetAppDir;
  50. while not DirectoryExists(Result + PathDelim + SDataDir) and (Iter < 7) do
  51. begin
  52. Result := ExtractFileDir(Result);
  53. Inc(Iter);
  54. end;
  55. Result := Result + PathDelim + SDataDir;
  56. end;
  57. function FileNameInDataDir(const BaseFileName: string): string;
  58. begin
  59. Result := GetDataDir + PathDelim + BaseFileName;
  60. end;
  61. function GetRootDir: string;
  62. var
  63. Iter: Integer;
  64. begin
  65. Iter := 0;
  66. Result := GetAppDir;
  67. while not DirectoryExists(Result + PathDelim + SSourceDir) and (Iter < 7) do
  68. begin
  69. Result := ExtractFileDir(Result);
  70. Inc(Iter);
  71. end;
  72. end;
  73. function NextFormat(Format: TImageFormat): TImageFormat;
  74. var
  75. Info: TImageFormatInfo;
  76. begin
  77. repeat
  78. if Format < High(TImageFormat) then
  79. Format := Succ(Format)
  80. else
  81. Format := ifIndex8;
  82. until GetImageFormatInfo(Format, Info);
  83. Result := Format;
  84. end;
  85. end.