DemoUtils.pas 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 GetRootDir: string;
  17. // Returns next valid image format.
  18. function NextFormat(Format: TImageFormat): TImageFormat;
  19. implementation
  20. function ExpandFileTo(const FileName, BasePath: string): string;
  21. var
  22. OldPath: string;
  23. begin
  24. GetDir(0, OldPath);
  25. try
  26. if SysUtils.DirectoryExists(BasePath) then
  27. begin
  28. ChDir(BasePath);
  29. Result:= ExpandFileName(FileName);
  30. end
  31. else
  32. Result:=FileName;
  33. finally
  34. ChDir(OldPath);
  35. end;
  36. end;
  37. function SwapPathDelims(const FileName, NewDelim: string): string;
  38. begin
  39. Result := FileName;
  40. Result := StringReplace(Result, '\', NewDelim, [rfReplaceAll]);
  41. Result := StringReplace(Result, '/', NewDelim, [rfReplaceAll]);
  42. end;
  43. function GetDataDir: string;
  44. var
  45. Iter: Integer;
  46. begin
  47. Iter := 0;
  48. Result := GetAppDir;
  49. while not DirectoryExists(Result + PathDelim + SDataDir) and (Iter < 7) do
  50. begin
  51. Result := ExtractFileDir(Result);
  52. Inc(Iter);
  53. end;
  54. Result := Result + PathDelim + SDataDir;
  55. end;
  56. function GetRootDir: string;
  57. var
  58. Iter: Integer;
  59. begin
  60. Iter := 0;
  61. Result := GetAppDir;
  62. while not DirectoryExists(Result + PathDelim + SSourceDir) and (Iter < 7) do
  63. begin
  64. Result := ExtractFileDir(Result);
  65. Inc(Iter);
  66. end;
  67. end;
  68. function NextFormat(Format: TImageFormat): TImageFormat;
  69. var
  70. Info: TImageFormatInfo;
  71. begin
  72. repeat
  73. if Format < High(TImageFormat) then
  74. Format := Succ(Format)
  75. else
  76. Format := ifIndex8;
  77. until GetImageFormatInfo(Format, Info);
  78. Result := Format;
  79. end;
  80. end.