Compare commits

...

2 Commits

Author SHA1 Message Date
Adrian 34b3ff8722 Add UnsafeBitmap class for image manipulation 2016-02-22 16:33:04 +01:00
Adrian 3ba34b9bd0 Refactor binary reader/writer 2016-02-22 00:31:21 +01:00
5 changed files with 116 additions and 40 deletions

View File

@ -30,14 +30,17 @@ namespace AdrianKousz.Util
return ReadInt32() != 0;
}
public string ReadString(int len) { return ReadString(len, false); }
public string ReadStringInt16() { return ReadString(ReadInt16(), false); }
public string ReadStringInt32() { return ReadString(ReadInt32(), false); }
public string ReadZString(int len) { return ReadString(len, true); }
public string ReadZStringInt16() { return ReadString(ReadInt16(), true); }
public string ReadZStringInt32() { return ReadString(ReadInt32(), true); }
public string ReadStringInternal(int len) { return ReadStringInternal(len, false); }
public string ReadStringByte() { return ReadStringInternal(ReadByte(), false); }
public string ReadStringInt16() { return ReadStringInternal(ReadInt16(), false); }
public string ReadStringInt32() { return ReadStringInternal(ReadInt32(), false); }
private string ReadString(int len, bool expectNull)
public string ReadZString(int len) { return ReadStringInternal(len, true); }
public string ReadZStringByte() { return ReadStringInternal(ReadByte(), true); }
public string ReadZStringInt16() { return ReadStringInternal(ReadInt16(), true); }
public string ReadZStringInt32() { return ReadStringInternal(ReadInt32(), true); }
private string ReadStringInternal(int len, bool expectNull)
{
if (len == 0) return "";
var array = ReadBytes(len);

View File

@ -9,6 +9,7 @@ namespace AdrianKousz.Util
public Encoding Encoding { get; private set; }
private const string ErrorChooseMethod = "Choose string writing method";
private const string ErrorStringTooLong = "String too long";
public ExtendedBinaryWriter(Stream stream, Encoding enc)
: base(stream, enc)
@ -27,38 +28,17 @@ namespace AdrianKousz.Util
else Write((Int32)0);
}
public void WriteZStringRaw(string value) { WriteStringRaw(value + "\0"); }
public void WriteZStringInt16(string value) { WriteStringInt16(value + "\0"); }
public void WriteZStringInt32(string value) { WriteStringInt32(value + "\0"); }
public void WriteString(string value, int len) { WriteStringInternal(value, len, TypeCode.Empty); }
public void WriteStringByte(string value) { WriteStringInternal(value, 0, TypeCode.Byte); }
public void WriteStringInt16(string value) { WriteStringInternal(value, 0, TypeCode.Int16); }
public void WriteStringInt32(string value) { WriteStringInternal(value, 0, TypeCode.Int32); }
public void WriteStringRaw(string value) { WriteStringInternal(value, 0, TypeCode.Empty); }
public void WriteStringRaw(string value)
{
var array = Encoding.GetBytes(value);
Write(array);
}
public void WriteStringInt16(string value)
{
var array = Encoding.GetBytes(value);
var len = (Int16)array.Length;
Write(len);
Write(array);
}
public void WriteStringInt32(string value)
{
var array = Encoding.GetBytes(value);
var len = (Int32)array.Length;
Write(len);
Write(array);
}
public void WriteStringPadded(string value, int len)
{
var array = new byte[len];
Encoding.GetBytes(value, 0, value.Length, array, 0);
Write(array);
}
public void WriteZString(string value, int len) { WriteStringInternal(value + "\0", len, TypeCode.Empty); }
public void WriteZStringByte(string value) { WriteStringInternal(value + "\0", 0, TypeCode.Byte); }
public void WriteZStringInt16(string value) { WriteStringInternal(value + "\0", 0, TypeCode.Int16); }
public void WriteZStringInt32(string value) { WriteStringInternal(value + "\0", 0, TypeCode.Int32); }
public void WriteZStringRaw(string value) { WriteStringInternal(value + "\0", 0, TypeCode.Empty); }
public int GetByteCount(string value)
{
@ -69,5 +49,29 @@ namespace AdrianKousz.Util
{
return Encoding.GetByteCount(value + "\0");
}
private void WriteStringInternal(string value, int len, TypeCode typecode)
{
byte[] array;
if (len == 0) {
array = Encoding.GetBytes(value);
len = array.Length;
} else {
if (len < GetByteCount(value))
throw new ArgumentOutOfRangeException(ErrorStringTooLong);
array = new byte[len];
Encoding.GetBytes(value, 0, value.Length, array, 0);
}
switch (typecode) {
case TypeCode.Empty: break;
case TypeCode.Int32: Write(checked((Int32)len)); break;
case TypeCode.Int16: Write(checked((Int16)len)); break;
case TypeCode.Byte: Write(checked((Byte)len)); break;
default: throw new ArgumentException();
}
Write(array);
}
}
}

View File

@ -11,8 +11,6 @@ namespace AdrianKousz.Util
{
public sealed class NonDisposingStreamWrapper : Stream
{
private bool closed = false;
public Stream BaseStream { get; private set; }
public NonDisposingStreamWrapper(Stream stream)

View File

@ -0,0 +1,65 @@
using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace AdrianKousz.Util
{
public unsafe class UnsafeBitmap : IDisposable
{
private const string ErrorNotLocked = "Bitmap not locked";
private const string ErrorAlreadyLocked = "Bitmap already locked";
private Bitmap bmp;
private BitmapData bmpdat;
public UnsafeBitmap(Bitmap bmp)
: this(bmp, false, true) { }
public UnsafeBitmap(Bitmap bmp, bool write)
: this(bmp, write, true) { }
public UnsafeBitmap(Bitmap bmp, bool write, bool locknow)
{
this.bmp = bmp;
this.bmpdat = null;
if (locknow)
Lock(write ? ImageLockMode.ReadWrite : ImageLockMode.ReadOnly);
}
public void Dispose()
{
if (bmp == null) return;
Unlock();
bmp = null;
}
public void Lock(ImageLockMode mode)
{
if (bmpdat != null)
throw new InvalidOperationException(ErrorAlreadyLocked);
if (bmp == null)
throw new ObjectDisposedException(null);
var rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
bmpdat = bmp.LockBits(rect, mode, bmp.PixelFormat);
}
public void Unlock()
{
bmp.UnlockBits(bmpdat);
bmpdat = null;
}
public int Height { get { CheckLocked(); return bmpdat.Height; } }
public PixelFormat PixelFormat { get { CheckLocked(); return bmpdat.PixelFormat; } }
public byte* Scan0 { get { CheckLocked(); return (byte*)bmpdat.Scan0.ToPointer(); } }
public int Stride { get { CheckLocked(); return bmpdat.Stride; } }
public int Width { get { CheckLocked(); return bmpdat.Width; } }
private void CheckLocked()
{
if (bmpdat == null)
throw new InvalidOperationException(ErrorNotLocked);
}
}
}

View File

@ -18,6 +18,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
@ -26,6 +27,7 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
@ -39,5 +41,9 @@
<Compile Include="AdrianKousz.Util\IApp.cs" />
<Compile Include="AdrianKousz.Util\CmdApp.cs" />
<Compile Include="AdrianKousz.Util\NonDisposingStreamWrapper.cs" />
<Compile Include="AdrianKousz.Util\UnsafeBitmap.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="System.Drawing" />
</ItemGroup>
</Project>