Skip to content

Commit 0ca1570

Browse files
author
kopaka
committed
fixed an alignment issue in ktx loader
1 parent c5d7131 commit 0ca1570

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

DxImageLoader/ktx_interface.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,10 +174,27 @@ std::unique_ptr<image::IImage> ktx_load_base(ktxTexture* ktex, gli::format forma
174174
ktxLvlSize *= res->getDepth(mip); // is not multiplied with depth layer
175175
size_t size;
176176
auto dstData = res->getData(dstLayer, mip, size);
177-
if (ktxLvlSize != size)
178-
throw std::runtime_error("suggested level size of gli does not match with ktx api");
179-
memcpy(dstData, ktex->pData + offset, size);
180-
177+
if(ktxLvlSize == size)
178+
{
179+
memcpy(dstData, ktex->pData + offset, size); // alignment matches
180+
}
181+
else if(size < ktxLvlSize)
182+
{
183+
// calculate size with alignment after each row
184+
size_t rows = res->getHeight(mip) * res->getDepth(mip);
185+
size_t unalignedRow = size / rows;
186+
size_t alignedRow = ktxLvlSize / rows;
187+
auto srcData = ktex->pData;
188+
//std::vector<uint8_t> debugSrc(srcData, srcData + ktxLvlSize);
189+
// copy row by row
190+
for(size_t r = 0; r < rows; ++r)
191+
{
192+
memcpy(dstData, srcData, unalignedRow);
193+
dstData += unalignedRow;
194+
srcData += alignedRow;
195+
}
196+
}
197+
else throw std::runtime_error("suggested level size of gli does not match with ktx api");
181198
}
182199
++dstLayer;
183200
}

FrameworkTests/ImageLoader/KtxSamples.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Runtime.InteropServices;
45
using System.Text;
56
using System.Threading.Tasks;
7+
using ImageFramework.DirectX;
68
using ImageFramework.ImageLoader;
79
using ImageFramework.Model;
10+
using ImageFramework.Utility;
811
using Microsoft.VisualStudio.TestTools.UnitTesting;
912
using SharpDX.DXGI;
1013

@@ -17,13 +20,31 @@ public class KtxSamples
1720

1821
public static string ImportBadDir = TestData.Directory + "ktx-software\\badktx2\\";
1922

23+
public static string ExportDir = TestData.Directory + "export/";
24+
25+
[ClassInitialize]
26+
public static void Init(TestContext context)
27+
{
28+
TestData.CreateOutputDirectory(ExportDir);
29+
}
30+
31+
private static string[] RemoveUnsupportedFile(string[] files)
32+
{
33+
// remove all files that have 'eac' or 'etc' in their name (these compressions are not supported yet)
34+
return files.Where(f =>
35+
!f.Contains("eac") && !f.Contains("EAC") &&
36+
!f.Contains("etc") && !f.Contains("ETC")
37+
).ToArray();
38+
}
39+
2040
[TestMethod]
2141
public void ImportTestImagesKtx()
2242
{
2343
// get all files in the directory
2444
var files = System.IO.Directory.GetFiles(ImportDir, "*.ktx", System.IO.SearchOption.TopDirectoryOnly);
2545
// filter files so they only end with .ktx (not ktx2)
2646
files = files.Where(f => f.EndsWith(".ktx")).ToArray();
47+
files = RemoveUnsupportedFile(files);
2748
TryImportAllFiles(files);
2849
}
2950

@@ -32,9 +53,41 @@ public void ImportTestImagesKtx2()
3253
{
3354
// get all files in the directory
3455
var files = System.IO.Directory.GetFiles(ImportDir, "*.ktx2", System.IO.SearchOption.TopDirectoryOnly);
56+
files = RemoveUnsupportedFile(files);
3557
TryImportAllFiles(files);
3658
}
3759

60+
[TestMethod]
61+
public void AlignmentKtx()
62+
{
63+
// this file uses the RGB format and the width of each row is not a multiple of 4 (this can be tricky to import/export)
64+
var filename = ImportDir + "hi_mark_sq.ktx";
65+
var model = new Models();
66+
model.AddImageFromFile(filename);
67+
68+
// the following needs to be fulfilled:
69+
Assert.AreEqual(145, model.Images.GetWidth(0));
70+
Assert.AreEqual(model.Images.Images[0].OriginalFormat, GliFormat.RGB8_UNORM);
71+
72+
// export and reimport
73+
var filename2 = ExportDir + "hi_mark_sq";
74+
model.ExportPipelineImage(filename2, "ktx", GliFormat.RGB8_UNORM);
75+
76+
// reimport
77+
model.AddImageFromFile(filename2 + ".ktx");
78+
79+
// compare colors
80+
var srcImg = model.Images.Images[0].Image as TextureArray2D;
81+
var expImg = model.Images.Images[1].Image as TextureArray2D;
82+
Assert.IsNotNull(srcImg);
83+
Assert.IsNotNull(expImg);
84+
85+
var srcColors = srcImg.GetPixelColors(LayerMipmapSlice.Mip0);
86+
var expColors = expImg.GetPixelColors(LayerMipmapSlice.Mip0);
87+
88+
TestData.CompareColors(srcColors, expColors);
89+
}
90+
3891
void TryImportAllFiles(string[] files)
3992
{
4093
string errors = "";
Binary file not shown.

0 commit comments

Comments
 (0)