1
// ==========================================================
2
// FreeImage 3 .NET wrapper
3
// Original FreeImage 3 functions and .NET compatible derived functions
5
// Design and implementation by
6
// - Jean-Philippe Goerke (jpgoerke@users.sourceforge.net)
7
// - Carsten Klein (cklein05@users.sourceforge.net)
10
// - David Boland (davidboland@vodafone.ie)
12
// Main reference : MSDN Knowlede Base
14
// This file is part of FreeImage 3
16
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
17
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
18
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
19
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
20
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
21
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
22
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
23
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
26
// Use at your own risk!
27
// ==========================================================
29
// ==========================================================
32
// $Date: 2009/09/15 11:47:46 $
33
// $Id: FreeImageStreamIO.cs,v 1.5 2009/09/15 11:47:46 cklein05 Exp $
34
// ==========================================================
38
using System.Runtime.InteropServices;
39
using System.Diagnostics;
41
namespace FreeImageAPI.IO
44
/// Internal class wrapping stream io functions.
47
/// FreeImage can read files from a disk or a network drive but also allows the user to
48
/// implement their own loading or saving functions to load them directly from an ftp or web
49
/// server for example.
51
/// In .NET streams are a common way to handle data. The <b>FreeImageStreamIO</b> class handles
52
/// the loading and saving from and to streams. It implements the funtions FreeImage needs
53
/// to load data from an an arbitrary source.
55
/// The class is for internal use only.
57
internal static class FreeImageStreamIO
60
/// <see cref="FreeImageAPI.IO.FreeImageIO"/> structure that can be used to read from streams via
61
/// <see cref="FreeImageAPI.FreeImage.LoadFromHandle(FREE_IMAGE_FORMAT, ref FreeImageIO, fi_handle, FREE_IMAGE_LOAD_FLAGS)"/>.
63
public static readonly FreeImageIO io;
66
/// Initializes a new instances which can be used to
67
/// create a FreeImage compatible <see cref="FreeImageAPI.IO.FreeImageIO"/> structure.
69
static FreeImageStreamIO()
71
io.readProc = new ReadProc(streamRead);
72
io.writeProc = new WriteProc(streamWrite);
73
io.seekProc = new SeekProc(streamSeek);
74
io.tellProc = new TellProc(streamTell);
78
/// Reads the requested data from the stream and writes it to the given address.
80
static unsafe uint streamRead(IntPtr buffer, uint size, uint count, fi_handle handle)
82
Stream stream = handle.GetObject() as Stream;
83
if ((stream == null) || (!stream.CanRead))
88
byte* ptr = (byte*)buffer;
89
byte[] bufferTemp = new byte[size];
91
while (readCount < count)
93
read = stream.Read(bufferTemp, 0, (int)size);
94
if (read != (int)size)
96
stream.Seek(-read, SeekOrigin.Current);
99
for (int i = 0; i < read; i++, ptr++)
101
*ptr = bufferTemp[i];
105
return (uint)readCount;
109
/// Reads the given data and writes it into the stream.
111
static unsafe uint streamWrite(IntPtr buffer, uint size, uint count, fi_handle handle)
113
Stream stream = handle.GetObject() as Stream;
114
if ((stream == null) || (!stream.CanWrite))
119
byte[] bufferTemp = new byte[size];
120
byte* ptr = (byte*)buffer;
121
while (writeCount < count)
123
for (int i = 0; i < size; i++, ptr++)
125
bufferTemp[i] = *ptr;
129
stream.Write(bufferTemp, 0, bufferTemp.Length);
141
/// Moves the streams position.
143
static int streamSeek(fi_handle handle, int offset, SeekOrigin origin)
145
Stream stream = handle.GetObject() as Stream;
150
stream.Seek((long)offset, origin);
155
/// Returns the streams current position
157
static int streamTell(fi_handle handle)
159
Stream stream = handle.GetObject() as Stream;
164
return (int)stream.Position;
b'\\ No newline at end of file'