#include #include #include #include #include int Morton(int t, int sx, int sy) { int num1; int num2 = num1 = 1; int num3 = t; int num4 = sx; int num5 = sy; int num6 = 0; int num7 = 0; while (num4 > 1 || num5 > 1) { if (num4 > 1) { num6 += num2 * (num3 & 1); num3 >>= 1; num2 *= 2; num4 >>= 1; } if (num5 > 1) { num7 += num1 * (num3 & 1); num3 >>= 1; num1 *= 2; num5 >>= 1; } } return num7 * sx + num6; } std::vector DoSwizzle(std::vector data, int width, int height, int blockSize, bool unswizzle) { auto processed = std::vector(); processed.resize(data.size()); auto heightTexels = height / 4; auto heightTexelsAligned = (heightTexels + 7) / 8; int widthTexels = width / 4; auto widthTexelsAligned = (widthTexels + 7) / 8; auto dataIndex = 0; for (int y = 0; y < heightTexelsAligned; ++y) { for (int x = 0; x < widthTexelsAligned; ++x) { for (int t = 0; t < 64; ++t) { int pixelIndex = Morton(t, 8, 8); int num8 = pixelIndex / 8; int num9 = pixelIndex % 8; auto yOffset = (y * 8) + num8; auto xOffset = (x * 8) + num9; if (xOffset < widthTexels && yOffset < heightTexels) { auto destPixelIndex = yOffset * widthTexels + xOffset; int destIndex = blockSize * destPixelIndex; if (unswizzle) memcpy(processed.data() + destIndex, data.data() + dataIndex, blockSize); else memcpy(processed.data() + dataIndex, data.data() + destIndex, blockSize); } dataIndex += blockSize; } } } return processed; } std::vector Swizzle(std::vector data, int width, int height, int blockSize) { return DoSwizzle(data, width, height, blockSize, false); } std::vector UnSwizzle(std::vector data, int width, int height, int blockSize) { return DoSwizzle(data, width, height, blockSize, true); } int main() { DirectX::ScratchImage image; auto initResult = image.Initialize2D(DXGI_FORMAT::DXGI_FORMAT_BC4_UNORM, 1024, 1024, 1, 1); auto img = image.GetImage(0, 0, 0); auto cnt = image.GetImageCount(); auto images = image.GetImages(); for (int i = 0; i < cnt; ++i) { auto img = images + i; std::cout << "ID: " << i << " W: " << img->width << " H: " << img->height << " Row pitch: " << img->rowPitch << " Slice pitch: " << img->slicePitch << std::endl; } //2097152 //"" auto readSize = img->slicePitch; auto inFile = std::ifstream("D:/TextureExport/blt-armor-a-new-spec-mask.tga.1024_1024_unswizzled_page0_file103.dds", std::ios::binary); inFile.read((char*)img->pixels, readSize); inFile.close(); /* std::vector mem; mem.resize(img->slicePitch); memcpy(mem.data(), img->pixels, img->slicePitch); auto unswizzled = UnSwizzle(mem, 1024, 1024, 8); memcpy(img->pixels, unswizzled.data(), img->slicePitch);*/ DirectX::SaveToDDSFile(*img, DirectX::DDS_FLAGS::DDS_FLAGS_NONE, L"F:/out.dds"); //img->pixels std::cout << "Hello World!\n"; }