Skip to content

Commit

Permalink
Fix accuracy loss when generating mipmap (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Looooong committed Dec 15, 2022
1 parent 96335b5 commit aa1c44f
Show file tree
Hide file tree
Showing 7 changed files with 150 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## [Unreleased]
### Fixed
- Fix accuracy loss when generating mipmap (#7)

## [0.1.2] - 2022-02-17
### Fixed
Expand Down
28 changes: 14 additions & 14 deletions Runtime/ImageImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,15 +294,15 @@ static void FilterMipmapRGB24(ref FilterMipmapJob job, int outputIndex) {
var inputX = min(mad(2, outputX, offsetX), job.inputDimensions.x - 1);
var inputY = min(mad(2, outputY, offsetY), job.inputDimensions.y - 1);
var inputIndex = mad(job.inputDimensions.x, inputY, inputX);
outputColor.x += (uint)job.inputMipmap[mad(3, inputIndex, 0)] >> 2;
outputColor.y += (uint)job.inputMipmap[mad(3, inputIndex, 1)] >> 2;
outputColor.z += (uint)job.inputMipmap[mad(3, inputIndex, 2)] >> 2;
outputColor.x += (uint)job.inputMipmap[mad(3, inputIndex, 0)];
outputColor.y += (uint)job.inputMipmap[mad(3, inputIndex, 1)];
outputColor.z += (uint)job.inputMipmap[mad(3, inputIndex, 2)];
}
}

job.outputMipmap[mad(3, outputIndex, 0)] = (byte)outputColor.x;
job.outputMipmap[mad(3, outputIndex, 1)] = (byte)outputColor.y;
job.outputMipmap[mad(3, outputIndex, 2)] = (byte)outputColor.z;
job.outputMipmap[mad(3, outputIndex, 0)] = (byte)(outputColor.x >> 2);
job.outputMipmap[mad(3, outputIndex, 1)] = (byte)(outputColor.y >> 2);
job.outputMipmap[mad(3, outputIndex, 2)] = (byte)(outputColor.z >> 2);
}

[BurstCompile(CompileSynchronously = true)]
Expand All @@ -316,17 +316,17 @@ static void FilterMipmapRGBA32(ref FilterMipmapJob job, int outputIndex) {
var inputX = min(mad(2, outputX, offsetX), job.inputDimensions.x - 1);
var inputY = min(mad(2, outputY, offsetY), job.inputDimensions.y - 1);
var inputIndex = mad(job.inputDimensions.x, inputY, inputX);
outputColor.x += (uint)job.inputMipmap[mad(4, inputIndex, 0)] >> 2;
outputColor.y += (uint)job.inputMipmap[mad(4, inputIndex, 1)] >> 2;
outputColor.z += (uint)job.inputMipmap[mad(4, inputIndex, 2)] >> 2;
outputColor.w += (uint)job.inputMipmap[mad(4, inputIndex, 3)] >> 2;
outputColor.x += (uint)job.inputMipmap[mad(4, inputIndex, 0)];
outputColor.y += (uint)job.inputMipmap[mad(4, inputIndex, 1)];
outputColor.z += (uint)job.inputMipmap[mad(4, inputIndex, 2)];
outputColor.w += (uint)job.inputMipmap[mad(4, inputIndex, 3)];
}
}

job.outputMipmap[mad(4, outputIndex, 0)] = (byte)outputColor.x;
job.outputMipmap[mad(4, outputIndex, 1)] = (byte)outputColor.y;
job.outputMipmap[mad(4, outputIndex, 2)] = (byte)outputColor.z;
job.outputMipmap[mad(4, outputIndex, 3)] = (byte)outputColor.w;
job.outputMipmap[mad(4, outputIndex, 0)] = (byte)(outputColor.x >> 2);
job.outputMipmap[mad(4, outputIndex, 1)] = (byte)(outputColor.y >> 2);
job.outputMipmap[mad(4, outputIndex, 2)] = (byte)(outputColor.z >> 2);
job.outputMipmap[mad(4, outputIndex, 3)] = (byte)(outputColor.w >> 2);
}

[ReadOnly]
Expand Down
8 changes: 8 additions & 0 deletions Tests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Tests/AsyncImageLoader.Tests.asmdef
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "AsyncImageLoader.Tests",
"references": [
"AsyncImageLoader.Runtime"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [],
"excludePlatforms": []
}
7 changes: 7 additions & 0 deletions Tests/AsyncImageLoader.Tests.asmdef.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 97 additions & 0 deletions Tests/TestMipmap.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
using NUnit.Framework;
using UnityEngine;
using UnityEngine.TestTools;

public class TestMipmap {
[Test]
public void TestPNGImage() {
var data = new byte[] {
0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x06, 0x00, 0x00, 0x00, 0x72, 0xB6, 0x0D,
0x24, 0x00, 0x00, 0x00, 0x1B, 0x49, 0x44, 0x41, 0x54, 0x08, 0xD7, 0x63, 0x60, 0xF8, 0xCF, 0xD0,
0xC0, 0xC0, 0xF0, 0xDF, 0x81, 0xE1, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x33, 0x30, 0xEC, 0x07, 0x00,
0x43, 0x18, 0x08, 0x79, 0xEB, 0xE3, 0x55, 0xB4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4E, 0x44,
0xAE, 0x42, 0x60, 0x82
};

var texture = AsyncImageLoader.CreateFromImage(data);
var mipmapLevel0 = texture.GetPixelData<int>(0);
var mipmapLevel1 = texture.GetPixelData<int>(1);

Assert.That(texture.format, Is.EqualTo(TextureFormat.RGBA32));
Assert.That(texture.width, Is.EqualTo(2));
Assert.That(texture.height, Is.EqualTo(2));
Assert.That(texture.mipmapCount, Is.EqualTo(2));

Assert.That(mipmapLevel0.Length, Is.EqualTo(4));
Assert.That(mipmapLevel0[0], Is.EqualTo(unchecked((int)0xFF_FF_FF_FF)));
Assert.That(mipmapLevel0[1], Is.EqualTo(unchecked((int)0xBF_00_00_FF)));
Assert.That(mipmapLevel0[2], Is.EqualTo(unchecked((int)0x80_00_FF_00)));
Assert.That(mipmapLevel0[3], Is.EqualTo(unchecked((int)0x40_FF_00_00)));

Assert.That(mipmapLevel1.Length, Is.EqualTo(1));
Assert.That(mipmapLevel1[0], Is.EqualTo(unchecked((int)0x9F_7F_7F_7F)));
}

[Test]
public void TestJPEGImage() {
var data = new byte[] {
0xFF, 0xD8, 0xFF, 0xE0, 0x00, 0x10, 0x4A, 0x46, 0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x01, 0x2C,
0x01, 0x2C, 0x00, 0x00, 0xFF, 0xDB, 0x00, 0x43, 0x00, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFF, 0xDB, 0x00, 0x43, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0xFF, 0xC9,
0x00, 0x11, 0x08, 0x00, 0x02, 0x00, 0x02, 0x03, 0x01, 0x11, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11,
0x01, 0xFF, 0xCC, 0x00, 0x0A, 0x00, 0x10, 0x10, 0x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 0xDC, 0xDC, 0xC2, 0x2F, 0x35, 0xF1, 0xEB, 0x2F, 0xA2, 0x75, 0x61, 0x01, 0xDF, 0x11, 0xF4,
0xD0, 0xC1, 0x62, 0x5A, 0x28, 0x22, 0xEC, 0x05, 0x2A, 0x41, 0x14, 0xC2, 0xA0, 0x4D, 0xB7, 0x9F,
0x3C, 0xF5, 0xD8, 0x90, 0xF1, 0x6F, 0x87, 0x09, 0xBE, 0xA9, 0xFD, 0x44, 0xBD, 0x59, 0x0C, 0x8B,
0x03, 0xF8, 0xB1, 0x0E, 0x07, 0x73, 0x3C, 0x82, 0xDA, 0x09, 0x83, 0x35, 0x03, 0x90, 0x3E, 0xBC,
0x65, 0x4B, 0x44, 0xAF, 0x2C, 0x80, 0xFF, 0xD9
};

var texture = AsyncImageLoader.CreateFromImage(data);
var mipmapLevel0 = texture.GetPixelData<byte>(0);
var mipmapLevel1 = texture.GetPixelData<byte>(1);

Assert.That(texture.format, Is.EqualTo(TextureFormat.RGB24));
Assert.That(texture.width, Is.EqualTo(2));
Assert.That(texture.height, Is.EqualTo(2));
Assert.That(texture.mipmapCount, Is.EqualTo(2));

Assert.That(mipmapLevel0.Length, Is.EqualTo(3 * 4));
Assert.That(mipmapLevel0[0], Is.EqualTo(0XF7));
Assert.That(mipmapLevel0[1], Is.EqualTo(0XFF));
Assert.That(mipmapLevel0[2], Is.EqualTo(0XFF));
Assert.That(mipmapLevel0[3], Is.EqualTo(0XF7));
Assert.That(mipmapLevel0[4], Is.EqualTo(0X07));
Assert.That(mipmapLevel0[5], Is.EqualTo(0X08));
Assert.That(mipmapLevel0[6], Is.EqualTo(0X00));
Assert.That(mipmapLevel0[7], Is.EqualTo(0XFF));
Assert.That(mipmapLevel0[8], Is.EqualTo(0X00));
Assert.That(mipmapLevel0[9], Is.EqualTo(0X00));
Assert.That(mipmapLevel0[10], Is.EqualTo(0X08));
Assert.That(mipmapLevel0[11], Is.EqualTo(0XFF));

Assert.That(mipmapLevel1.Length, Is.EqualTo(3));
Assert.That(mipmapLevel1[0], Is.EqualTo(0x7B));
Assert.That(mipmapLevel1[1], Is.EqualTo(0x83));
Assert.That(mipmapLevel1[2], Is.EqualTo(0x81));
}
}
11 changes: 11 additions & 0 deletions Tests/TestMipmap.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit aa1c44f

Please sign in to comment.