Skip to content

Commit 9a431bd

Browse files
Implement basic bilinear resizing
1 parent 78888b1 commit 9a431bd

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

MCGalaxy/util/Imaging/ImageDecoder.cs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,58 @@ public sealed class SimpleBitmap //: IBitmap2D
3030
public void AllocatePixels() {
3131
pixels = new Pixel[Width * Height];
3232
}
33+
34+
public SimpleBitmap ResizeBilinear(int dstWidth, int dstHeight) {
35+
SimpleBitmap dst = new SimpleBitmap();
36+
dst.Width = dstWidth;
37+
dst.Height = dstHeight;
38+
dst.AllocatePixels();
39+
40+
float scaleX = (float)Width / dstWidth;
41+
float scaleY = (float)Height / dstHeight;
42+
43+
int maxX = Width - 1;
44+
int maxY = Height - 1;
45+
46+
Pixel[] pixels = this.pixels;
47+
int stride = this.Width;
48+
int dstI = 0;
49+
50+
for (int y = 0; y < dstHeight; y++)
51+
{
52+
float sy = (y + 0.5f) * scaleY;
53+
int sy0 = (int)sy; if (sy0 >= maxY) sy0 = maxY;
54+
int sy1 = sy0 + 1; if (sy1 >= maxY) sy1 = maxY;
55+
float dy = sy - sy0;
56+
57+
for (int x = 0; x < dstWidth; x++)
58+
{
59+
float sx = (x + 0.5f) * scaleX;
60+
int sx0 = (int)sx; if (sx0 >= maxX) sx0 = maxX;
61+
int sx1 = sx0 + 1; if (sx1 >= maxX) sx1 = maxX;
62+
float dx = sx - sx0;
63+
64+
Pixel p00 = pixels[sy0 * stride + sx0];
65+
Pixel p10 = pixels[sy0 * stride + sx1];
66+
Pixel p01 = pixels[sy1 * stride + sx0];
67+
Pixel p11 = pixels[sy1 * stride + sx1];
68+
69+
Pixel p;
70+
p.R = (byte)((p00.R * (1-dx) * (1-dy)) + (p10.R * dx * (1-dy)) + (p01.R * (1-dx) * dy) + (p11.R * dx * dy));
71+
p.G = (byte)((p00.G * (1-dx) * (1-dy)) + (p10.G * dx * (1-dy)) + (p01.G * (1-dx) * dy) + (p11.G * dx * dy));
72+
p.B = (byte)((p00.B * (1-dx) * (1-dy)) + (p10.B * dx * (1-dy)) + (p01.B * (1-dx) * dy) + (p11.B * dx * dy));
73+
p.A = (byte)((p00.A * (1-dx) * (1-dy)) + (p10.A * dx * (1-dy)) + (p01.A * (1-dx) * dy) + (p11.A * dx * dy));
74+
dst.pixels[dstI++] = p;
75+
}
76+
}
77+
return dst;
78+
}
3379
}
3480

3581
public abstract class ImageDecoder
3682
{
3783
protected byte[] buf_data;
38-
protected int buf_offset, buf_length;
84+
protected int buf_offset, buf_length;
3985

4086
/// <summary> Attempts to advance next read offset by 'amount', then returns current read offset </summary>
4187
protected int AdvanceOffset(int amount) {

MCGalaxy/util/Imaging/JpegDecoder.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -382,10 +382,12 @@ void DecodeMCUs(byte[] src, SimpleBitmap bmp) {
382382
}
383383
}
384384

385-
static byte ByteClamp(float v) {
386-
if (v < 0) return 0;
387-
if (v > 255) return 255;
388-
return (byte)v;
385+
static byte ByteClamp(float value) {
386+
int n = (int)value;
387+
388+
if (n < 0) return 0;
389+
if (n > 255) return 255;
390+
return (byte)n;
389391
}
390392

391393
void DecodeBlock(JpegComponent comp, byte[] src, int* block) {

0 commit comments

Comments
 (0)