Skip to content

Commit 3bdd7ff

Browse files
Merge pull request #843 from ClassiCube/PNGDecoder
WIP Png decoder
2 parents 56b74bb + 09e2f0d commit 3bdd7ff

File tree

4 files changed

+524
-1
lines changed

4 files changed

+524
-1
lines changed

MCGalaxy/MCGalaxy_.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,9 @@
672672
<Compile Include="util\Formatting\Wildcard.cs" />
673673
<Compile Include="util\DotnetBackend.cs" />
674674
<Compile Include="util\Imaging\Bitmap2D.cs" />
675+
<Compile Include="util\Imaging\ImageDecoder.cs" />
675676
<Compile Include="util\Imaging\ImageUtils.cs" />
677+
<Compile Include="util\Imaging\PngDecoder.cs" />
676678
<Compile Include="util\NumberUtils.cs" />
677679
<Compile Include="util\OperatingSystem.cs" />
678680
<Compile Include="Server\Server.cs" />

MCGalaxy/util/Imaging/Bitmap2D.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,16 @@ namespace MCGalaxy.Util
3232
{
3333
public delegate Pixel PixelGet(int x, int y);
3434
public delegate void PixelSet(int x, int y, Pixel pixel);
35-
public struct Pixel { public byte A, R, G, B; }
35+
36+
public struct Pixel
37+
{
38+
public byte A, R, G, B;
39+
40+
public Pixel(byte r, byte g, byte b, byte a)
41+
{
42+
R = r; G = g; B = b; A = a;
43+
}
44+
}
3645

3746
/// <summary> Represents a 2D image </summary>
3847
/// <remarks> Backing implementation depends on whether running on dotnet or .NET </remarks>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
using System;
2+
using System.IO;
3+
using System.IO.Compression;
4+
using MCGalaxy.Util;
5+
6+
namespace MCGalaxy
7+
{
8+
public sealed class SimpleBitmap //: IBitmap2D
9+
{
10+
public int Width, Height;
11+
public Pixel[] pixels;
12+
}
13+
14+
public abstract class ImageDecoder
15+
{
16+
protected byte[] buf_data;
17+
protected int buf_offset, buf_length;
18+
19+
protected int AdvanceOffset(int amount) {
20+
int offset = buf_offset;
21+
22+
buf_offset += amount;
23+
if (buf_offset > buf_length)
24+
throw new EndOfStreamException("End of stream reading data");
25+
return offset;
26+
}
27+
28+
protected static void Fail(string reason) {
29+
throw new InvalidDataException(reason);
30+
}
31+
32+
protected static bool MatchesSignature(byte[] data, byte[] sig) {
33+
if (data.Length < sig.Length) return false;
34+
35+
for (int i = 0; i < sig.Length; i++)
36+
{
37+
if (data[i] != sig[i]) return false;
38+
}
39+
return true;
40+
}
41+
42+
43+
public static SimpleBitmap DecodeFrom(byte[] src) {
44+
if (PngDecoder.DetectHeader(src))
45+
return new PngDecoder().Decode(src);
46+
47+
throw new InvalidDataException("Unsupported or invalid image format");
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)