1 {
2 $Id: ImagingIO.pas 100 2007-06-28 21:09:52Z galfar $
3 Vampyre Imaging Library
4 by Marek Mauder
5 http://imaginglib.sourceforge.net
7 The contents of this file are used with permission, subject to the Mozilla
8 Public License Version 1.1 (the "License"); you may not use this file except
9 in compliance with the License. You may obtain a copy of the License at
10 http://www.mozilla.org/MPL/MPL-1.1.html
12 Software distributed under the License is distributed on an "AS IS" basis,
13 WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
14 the specific language governing rights and limitations under the License.
16 Alternatively, the contents of this file may be used under the terms of the
17 GNU Lesser General Public License (the "LGPL License"), in which case the
18 provisions of the LGPL License are applicable instead of those above.
19 If you wish to allow use of your version of this file only under the terms
20 of the LGPL License and not to allow others to use your version of this file
21 under the MPL, indicate your decision by deleting the provisions above and
22 replace them with the notice and other provisions required by the LGPL
23 License. If you do not delete the provisions above, a recipient may use
24 your version of this file under either the MPL or the LGPL License.
26 For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html
27 }
29 { This unit contains default IO functions for reading from/writting to
30 files, streams and memory.}
33 {$I ImagingOptions.inc}
35 interface
37 uses
40 type
48 var
54 { Helper function that returns size of input (from current position to the end)
55 represented by Handle (and opened and operated on by members of IOFunctions).}
57 { Helper function that initializes TMemoryIORec with given params.}
60 implementation
62 const
65 type
66 { Based on TaaBufferedStream
67 Copyright (c) Julian M Bucknall 1997, 1999 }
69 private
83 public
96 begin
109 begin
111 begin
112 Commit;
120 begin
125 begin
130 begin
135 var
137 begin
147 var
150 begin
160 begin
162 begin
163 WriteBuffer;
169 var
172 begin
173 // Calculate the actual number of bytes we can read - this depends on
174 // the current position and size of the stream as well as the number
175 // of bytes requested.
181 begin
183 Exit;
185 // Remember to return the result of our calculation
190 ReadBuffer;
191 // Calculate the number of bytes we can read prior to the loop
195 // Copy from the stream buffer to the caller's buffer
197 // Calculate the number of bytes still to read}
200 // while we have bytes to read, read them
202 begin
204 // As we've exhausted this buffer-full, advance to the next, check
205 // to see whether we need to write the buffer out first
207 begin
208 WriteBuffer;
213 ReadBuffer;
214 // Calculate the number of bytes we can read in this cycle
218 // Ccopy from the stream buffer to the caller's buffer
220 // Calculate the number of bytes still to read
223 // Remember our new position
226 begin
234 var
236 begin
237 // Calculate the new position
242 else
247 begin
248 //NewPos := ClampInt(NewPos, 0, FSize); don't do this - for writing
250 // Calculate which page of the file we need to be at
252 // If the new page is different than the old, mark the buffer as being
253 // ready to be replenished, and if need be write out any dirty data
255 begin
257 begin
258 WriteBuffer;
264 // Save the new position
270 var
273 begin
274 // When we write to this stream we always assume that we can write the
275 // requested number of bytes: if we can't (eg, the disk is full) we'll
276 // get an exception somewhere eventually.
278 // Remember to return the result of our calculation
283 ReadBuffer;
284 // Calculate the number of bytes we can write prior to the loop
288 // Copy from the caller's buffer to the stream buffer
290 // Mark our stream buffer as requiring a save to the actual stream,
291 // note that this will suffice for the rest of the routine as well: no
292 // inner routine will turn off the dirty flag.
294 // Calculate the number of bytes still to write
297 // While we have bytes to write, write them
299 begin
301 // As we've filled this buffer, write it out to the actual stream
302 // and advance to the next buffer, reading it if required
304 WriteBuffer;
309 ReadBuffer;
310 // Calculate the number of bytes we can write in this cycle
314 // Copy from the caller's buffer to our buffer
316 // Calculate the number of bytes still to write
319 // Remember our new position
321 // Make sure the count of valid bytes is correct
324 // Make sure the stream size is correct
327 // If we're at the end of the buffer, write it out and advance to the
328 // start of the next page
330 begin
331 WriteBuffer;
339 { File IO functions }
342 begin
347 begin
352 var
354 begin
361 begin
367 begin
372 begin
378 begin
384 begin
388 { Stream IO functions }
391 begin
396 begin
401 begin
405 begin
411 begin
416 begin
422 begin
428 begin
432 { Memory IO functions }
435 begin
440 begin
445 begin
449 begin
455 begin
462 //Result := ClampInt(Result, 0, PMemoryIORec(Handle).Size); don't do this - some file formats use it
467 begin
473 var
475 begin
486 var
488 begin
497 { Helper IO functions }
500 var
502 begin
510 begin
516 initialization
544 ResetFileIO;
546 {
547 File Notes:
549 -- TODOS ----------------------------------------------------
550 - nothing now
552 -- 0.23 Changes/Bug Fixes -----------------------------------
553 - Added merge between buffered read-only and write-only file
554 stream adapters - TIFF saving needed both reading and writing.
555 - Fixed bug causing wrong value of TBufferedWriteFile.Size
556 (needed to add buffer pos to size).
558 -- 0.21 Changes/Bug Fixes -----------------------------------
559 - Removed TMemoryIORec.Written, use Position to get proper memory
560 position (Written didn't take Seeks into account).
561 - Added TBufferedReadFile and TBufferedWriteFile classes for
562 buffered file reading/writting. File IO functions now use these
563 classes resulting in performance increase mainly in file formats
564 that read/write many small chunks.
565 - Added fmShareDenyWrite to FileOpenRead. You can now read
566 files opened for reading by Imaging from other apps.
567 - Added GetInputSize and PrepareMemIO helper functions.
569 -- 0.19 Changes/Bug Fixes -----------------------------------
570 - changed behaviour of MemorySeek to act as TStream
571 based Seeks
572 }