Skip to content

Commit 0661907

Browse files
Minorly tidy up image decoding code
1 parent 1085d61 commit 0661907

File tree

4 files changed

+76
-51
lines changed

4 files changed

+76
-51
lines changed

MCGalaxy/MCGalaxy_.csproj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -670,8 +670,9 @@
670670
<Compile Include="Server\Maintenance\ZipWriter.cs" />
671671
<Compile Include="util\CountryUtils.cs" />
672672
<Compile Include="util\Formatting\Wildcard.cs" />
673-
<Compile Include="util\ImageUtils.cs" />
674673
<Compile Include="util\DotnetBackend.cs" />
674+
<Compile Include="util\Imaging\Bitmap2D.cs" />
675+
<Compile Include="util\Imaging\ImageUtils.cs" />
675676
<Compile Include="util\NumberUtils.cs" />
676677
<Compile Include="util\OperatingSystem.cs" />
677678
<Compile Include="Server\Server.cs" />
@@ -726,7 +727,9 @@
726727
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
727728
</Content>
728729
</ItemGroup>
729-
<ItemGroup />
730+
<ItemGroup>
731+
<Folder Include="util\Imaging" />
732+
</ItemGroup>
730733
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
731734
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
732735
Other similar extension points exist, see Microsoft.Common.targets.
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public struct Pixel { public byte A, R, G, B; }
3838
/// <remarks> Backing implementation depends on whether running on dotnet or .NET </remarks>
3939
public abstract class IBitmap2D : IDisposable
4040
{
41-
public int Width, Height;
41+
public int Width;
42+
public int Height;
4243
public PixelGet Get;
4344
public PixelSet Set;
4445

@@ -62,38 +63,6 @@ public abstract class IBitmap2D : IDisposable
6263
#endif
6364
}
6465

65-
public static class ImageUtils
66-
{
67-
public static IBitmap2D DecodeImage(byte[] data, Player p) {
68-
IBitmap2D bmp = null;
69-
try {
70-
bmp = IBitmap2D.Create();
71-
bmp.Decode(data);
72-
return bmp;
73-
} catch (ArgumentException ex) {
74-
// GDI+ throws ArgumentException when data is not an image
75-
// This is a fairly expected error - e.g. when a user tries to /imgprint
76-
// the webpage an image is hosted on, instead of the actual image itself.
77-
// So don't bother logging a full error for this case
78-
Logger.Log(LogType.Warning, "Error decoding image: " + ex.Message);
79-
OnDecodeError(p, bmp);
80-
return null;
81-
} catch (Exception ex) {
82-
Logger.LogError("Error decoding image", ex);
83-
OnDecodeError(p, bmp);
84-
return null;
85-
}
86-
}
87-
88-
static void OnDecodeError(Player p, IBitmap2D bmp) {
89-
if (bmp != null) bmp.Dispose();
90-
// TODO failed to decode the image. make sure you are using the URL of the image directly, not just the webpage it is hosted on
91-
p.Message("&WThere was an error reading the downloaded image.");
92-
p.Message("&WThe url may need to end with its extension (such as .jpg).");
93-
}
94-
}
95-
96-
9766
#if !MCG_DOTNET
9867
unsafe sealed class GDIPlusBitmap : IBitmap2D
9968
{
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Copyright 2015-2024 MCGalaxy
3+
4+
Dual-licensed under the Educational Community License, Version 2.0 and
5+
the GNU General Public License, Version 3 (the "Licenses"); you may
6+
not use this file except in compliance with the Licenses. You may
7+
obtain a copy of the Licenses at
8+
9+
https://opensource.org/license/ecl-2-0/
10+
https://www.gnu.org/licenses/gpl-3.0.html
11+
12+
Unless required by applicable law or agreed to in writing,
13+
software distributed under the Licenses are distributed on an "AS IS"
14+
BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
15+
or implied. See the Licenses for the specific language governing
16+
permissions and limitations under the Licenses.
17+
*/
18+
using System;
19+
using System.IO;
20+
21+
namespace MCGalaxy.Util
22+
{
23+
public static class ImageUtils
24+
{
25+
public static IBitmap2D DecodeImage(byte[] data, Player p) {
26+
IBitmap2D bmp = null;
27+
try {
28+
bmp = IBitmap2D.Create();
29+
bmp.Decode(data);
30+
return bmp;
31+
} catch (ArgumentException ex) {
32+
// GDI+ throws ArgumentException when data is not an image
33+
// This is a fairly expected error - e.g. when a user tries to /imgprint
34+
// the webpage an image is hosted on, instead of the actual image itself.
35+
// So don't bother logging a full error for this case
36+
Logger.Log(LogType.Warning, "Error decoding image: " + ex.Message);
37+
OnDecodeError(p, bmp);
38+
return null;
39+
} catch (Exception ex) {
40+
Logger.LogError("Error decoding image", ex);
41+
OnDecodeError(p, bmp);
42+
return null;
43+
}
44+
}
45+
46+
static void OnDecodeError(Player p, IBitmap2D bmp) {
47+
if (bmp != null) bmp.Dispose();
48+
// TODO failed to decode the image. make sure you are using the URL of the image directly, not just the webpage it is hosted on
49+
p.Message("&WThere was an error reading the downloaded image.");
50+
p.Message("&WThe url may need to end with its extension (such as .jpg).");
51+
}
52+
}
53+
}

MCGalaxy/util/Utils.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,22 @@ public static class Utils
2929
public static string Hex(byte r, byte g, byte b) {
3030
return "#" + r.ToString("X2") + g.ToString("X2") + b.ToString("X2");
3131
}
32+
33+
public static string ToHexString(byte[] data) {
34+
char[] hex = new char[data.Length * 2];
35+
36+
for (int i = 0; i < data.Length; i++)
37+
{
38+
int value = data[i];
39+
hex[i * 2 + 0] = HexEncode(value >> 4);
40+
hex[i * 2 + 1] = HexEncode(value & 0x0F);
41+
}
42+
return new string(hex);
43+
}
44+
45+
static char HexEncode(int i) {
46+
return i < 10 ? (char)(i + '0') : (char)((i - 10) + 'a');
47+
}
3248

3349

3450
public static int Clamp(int value, int lo, int hi) {
@@ -49,22 +65,6 @@ public static List<string> ReadAllLinesList(string path) {
4965
}
5066

5167

52-
public static string ToHexString(byte[] data) {
53-
char[] hex = new char[data.Length * 2];
54-
55-
for (int i = 0; i < data.Length; i++)
56-
{
57-
int value = data[i];
58-
hex[i * 2 + 0] = HexEncode(value >> 4);
59-
hex[i * 2 + 1] = HexEncode(value & 0x0F);
60-
}
61-
return new string(hex);
62-
}
63-
64-
static char HexEncode(int i) {
65-
return i < 10 ? (char)(i + '0') : (char)((i - 10) + 'a');
66-
}
67-
6868
public static void SetBackgroundMode(Thread thread) {
6969
// Throws an exception when called on a dead thread,
7070
// which can very rarely happen

0 commit comments

Comments
 (0)