/// <summary> /// Reads from an unmanaged stream. /// </summary> protected unsafe int Read(FreeImageIO io, fi_handle handle, uint size, uint count, ref byte[] buffer) { fixed(byte *ptr = buffer) { return((int)io.readProc(new IntPtr(ptr), size, count, handle)); } }
/// <summary> /// Reads a single byte from an unmanaged stream. /// </summary> protected unsafe int ReadByte(FreeImageIO io, fi_handle handle) { byte buffer = 0; return((int)io.readProc(new IntPtr(&buffer), 1, 1, handle) > 0 ? buffer : -1); }
public void FreeImageStreamIO() { Random rand = new Random(); byte[] bBuffer = new byte[256]; IntPtr buffer = Marshal.AllocHGlobal(256); MemoryStream stream = new MemoryStream(); Assert.IsNotNull(stream); using (fi_handle handle = new fi_handle(stream)) { FreeImageIO io = FreeImageAPI.IO.FreeImageStreamIO.io; Assert.IsNotNull(io.readProc); Assert.IsNotNull(io.writeProc); Assert.IsNotNull(io.seekProc); Assert.IsNotNull(io.tellProc); // // Procs // rand.NextBytes(bBuffer); stream.Write(bBuffer, 0, bBuffer.Length); Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); Assert.That(io.tellProc(handle) == stream.Position); // Read one block Assert.That(io.readProc(buffer, (uint)bBuffer.Length, 1, handle) == 1); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == stream.Position); // Read 1 byte block Assert.That(io.readProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == stream.Position); rand.NextBytes(bBuffer); for (int i = 0; i < bBuffer.Length; i++) { Marshal.WriteByte(buffer, i, bBuffer[i]); } // Write one block Assert.That(io.writeProc(buffer, (uint)bBuffer.Length, 1, handle) == 1); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); // write 1 byte block Assert.That(io.writeProc(buffer, 1, (uint)bBuffer.Length, handle) == bBuffer.Length); for (int i = 0; i < bBuffer.Length; i++) { Assert.That(Marshal.ReadByte(buffer, i) == bBuffer[i]); } Assert.That(io.tellProc(handle) == stream.Position); // Seek and tell Assert.That(io.seekProc(handle, 0, SeekOrigin.Begin) == 0); Assert.That(io.tellProc(handle) == 0); Assert.That(io.seekProc(handle, 127, SeekOrigin.Current) == 0); Assert.That(io.tellProc(handle) == 127); Assert.That(io.seekProc(handle, 0, SeekOrigin.End) == 0); Assert.That(io.tellProc(handle) == 256); Marshal.FreeHGlobal(buffer); stream.Dispose(); } }