ImagingIO.pas 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. {
  2. $Id: ImagingIO.pas,v 1.8 2006/08/31 14:53:33 galfar Exp $
  3. Vampyre Imaging Library
  4. by Marek Mauder ([email protected])
  5. http://imaginglib.sourceforge.net
  6. The contents of this file are used with permission, subject to the Mozilla
  7. Public License Version 1.1 (the "License"); you may not use this file except
  8. in compliance with the License. You may obtain a copy of the License at
  9. http://www.mozilla.org/MPL/MPL-1.1.html
  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 for
  12. the specific language governing rights and limitations under the License.
  13. Alternatively, the contents of this file may be used under the terms of the
  14. GNU Lesser General Public License (the "LGPL License"), in which case the
  15. provisions of the LGPL License are applicable instead of those above.
  16. If you wish to allow use of your version of this file only under the terms
  17. of the LGPL License and not to allow others to use your version of this file
  18. under the MPL, indicate your decision by deleting the provisions above and
  19. replace them with the notice and other provisions required by the LGPL
  20. License. If you do not delete the provisions above, a recipient may use
  21. your version of this file under either the MPL or the LGPL License.
  22. For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
  23. }
  24. { This unit contains default IO functions for reading from/writting to
  25. files, streams and memory.}
  26. unit ImagingIO;
  27. {$I ImagingOptions.inc}
  28. interface
  29. uses
  30. ImagingTypes, Imaging, SysUtils, Classes, ImagingUtility;
  31. type
  32. TMemoryIORec = record
  33. Data: ImagingUtility.PByteArray;
  34. Position: LongInt;
  35. Size: LongInt;
  36. Written: LongInt;
  37. end;
  38. PMemoryIORec = ^TMemoryIORec;
  39. var
  40. OriginalFileIO: TIOFunctions;
  41. FileIO: TIOFunctions;
  42. StreamIO: TIOFunctions;
  43. MemoryIO: TIOFunctions;
  44. implementation
  45. function FileOpenRead(FileName: PChar): TImagingHandle; cdecl;
  46. begin
  47. Result := TFileStream.Create(FileName, fmOpenRead);
  48. end;
  49. function FileOpenWrite(FileName: PChar): TImagingHandle; cdecl;
  50. begin
  51. Result := TFileStream.Create(FileName, fmCreate);
  52. end;
  53. procedure FileClose(Handle: TImagingHandle); cdecl;
  54. begin
  55. TFileStream(Handle).Free;
  56. end;
  57. function FileEof(Handle: TImagingHandle): Boolean; cdecl;
  58. begin
  59. Result := TFileStream(Handle).Position = TFileStream(Handle).Size;
  60. end;
  61. function FileSeek(Handle: TImagingHandle; Offset: LongInt; Mode: TSeekMode):
  62. LongInt; cdecl;
  63. begin
  64. Result := TFileStream(Handle).Seek(Offset, LongInt(Mode));
  65. end;
  66. function FileTell(Handle: TImagingHandle): LongInt; cdecl;
  67. begin
  68. Result := TFileStream(Handle).Position;
  69. end;
  70. function FileRead(Handle: TImagingHandle; Buffer: Pointer; Count: LongInt):
  71. LongInt; cdecl;
  72. begin
  73. Result := TFileStream(Handle).Read(Buffer^, Count);
  74. end;
  75. function FileWrite(Handle: TImagingHandle; Buffer: Pointer; Count: LongInt):
  76. LongInt; cdecl;
  77. begin
  78. Result := TFileStream(Handle).Write(Buffer^, Count);
  79. end;
  80. function StreamOpenRead(FileName: PChar): TImagingHandle; cdecl;
  81. begin
  82. Result := FileName;
  83. end;
  84. function StreamOpenWrite(FileName: PChar): TImagingHandle; cdecl;
  85. begin
  86. Result := FileName;
  87. end;
  88. procedure StreamClose(Handle: TImagingHandle); cdecl;
  89. begin
  90. end;
  91. function StreamEof(Handle: TImagingHandle): Boolean; cdecl;
  92. begin
  93. Result := TStream(Handle).Position = TStream(Handle).Size;
  94. end;
  95. function StreamSeek(Handle: TImagingHandle; Offset: LongInt; Mode: TSeekMode):
  96. LongInt; cdecl;
  97. begin
  98. Result := TStream(Handle).Seek(Offset, LongInt(Mode));
  99. end;
  100. function StreamTell(Handle: TImagingHandle): LongInt; cdecl;
  101. begin
  102. Result := TStream(Handle).Position;
  103. end;
  104. function StreamRead(Handle: TImagingHandle; Buffer: Pointer; Count: LongInt):
  105. LongInt; cdecl;
  106. begin
  107. Result := TStream(Handle).Read(Buffer^, Count);
  108. end;
  109. function StreamWrite(Handle: TImagingHandle; Buffer: Pointer; Count: LongInt):
  110. LongInt; cdecl;
  111. begin
  112. Result := TStream(Handle).Write(Buffer^, Count);
  113. end;
  114. function MemoryOpenRead(FileName: PChar): TImagingHandle; cdecl;
  115. begin
  116. Result := FileName;
  117. end;
  118. function MemoryOpenWrite(FileName: PChar): TImagingHandle; cdecl;
  119. begin
  120. Result := FileName;
  121. end;
  122. procedure MemoryClose(Handle: TImagingHandle); cdecl;
  123. begin
  124. end;
  125. function MemoryEof(Handle: TImagingHandle): Boolean; cdecl;
  126. begin
  127. Result := PMemoryIORec(Handle).Position = PMemoryIORec(Handle).Size;
  128. end;
  129. function MemorySeek(Handle: TImagingHandle; Offset: LongInt; Mode: TSeekMode):
  130. LongInt; cdecl;
  131. begin
  132. Result := 0;
  133. case Mode of
  134. smFromBeginning: Result := Offset;
  135. smFromCurrent: Result := PMemoryIORec(Handle).Position + Offset;
  136. smFromEnd: Result := PMemoryIORec(Handle).Size + Offset;
  137. end;
  138. Result := ClampInt(Result, 0, PMemoryIORec(Handle).Size);
  139. PMemoryIORec(Handle).Position := Result;
  140. end;
  141. function MemoryTell(Handle: TImagingHandle): LongInt; cdecl;
  142. begin
  143. Result := PMemoryIORec(Handle).Position;
  144. end;
  145. function MemoryRead(Handle: TImagingHandle; Buffer: Pointer; Count: LongInt):
  146. LongInt; cdecl;
  147. var
  148. Rec: PMemoryIORec;
  149. begin
  150. Rec := PMemoryIORec(Handle);
  151. Result := Count;
  152. if Rec.Position + Count > Rec.Size then
  153. Result := Rec.Size - Rec.Position;
  154. Move(Rec.Data[Rec.Position], Buffer^, Result);
  155. Rec.Position := Rec.Position + Result;
  156. end;
  157. function MemoryWrite(Handle: TImagingHandle; Buffer: Pointer; Count: LongInt):
  158. LongInt; cdecl;
  159. var
  160. Rec: PMemoryIORec;
  161. begin
  162. Rec := PMemoryIORec(Handle);
  163. Result := Count;
  164. if Rec.Position + Count > Rec.Size then
  165. Result := Rec.Size - Rec.Position;
  166. Move(Buffer^, Rec.Data[Rec.Position], Result);
  167. Rec.Position := Rec.Position + Result;
  168. Rec.Written := Rec.Written + Result;
  169. end;
  170. initialization
  171. OriginalFileIO.OpenRead := FileOpenRead;
  172. OriginalFileIO.OpenWrite := FileOpenWrite;
  173. OriginalFileIO.Close := FileClose;
  174. OriginalFileIO.Eof := FileEof;
  175. OriginalFileIO.Seek := FileSeek;
  176. OriginalFileIO.Tell := FileTell;
  177. OriginalFileIO.Read := FileRead;
  178. OriginalFileIO.Write := FileWrite;
  179. StreamIO.OpenRead := StreamOpenRead;
  180. StreamIO.OpenWrite := StreamOpenWrite;
  181. StreamIO.Close := StreamClose;
  182. StreamIO.Eof := StreamEof;
  183. StreamIO.Seek := StreamSeek;
  184. StreamIO.Tell := StreamTell;
  185. StreamIO.Read := StreamRead;
  186. StreamIO.Write := StreamWrite;
  187. MemoryIO.OpenRead := MemoryOpenRead;
  188. MemoryIO.OpenWrite := MemoryOpenWrite;
  189. MemoryIO.Close := MemoryClose;
  190. MemoryIO.Eof := MemoryEof;
  191. MemoryIO.Seek := MemorySeek;
  192. MemoryIO.Tell := MemoryTell;
  193. MemoryIO.Read := MemoryRead;
  194. MemoryIO.Write := MemoryWrite;
  195. ResetFileIO;
  196. {
  197. File Notes:
  198. -- TODOS ----------------------------------------------------
  199. - nothing now
  200. -- 0.19 Changes/Bug Fixes -----------------------------------
  201. - changed behaviour of MemorySeek to act as TStream
  202. based Seeks
  203. }
  204. end.