2022-12-28 16:21:58 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
|
using Wabbajack.DTOs.Texture;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
namespace Compression.BSA;
|
|
|
|
|
/*
|
|
|
|
|
* Copied from https://raw.githubusercontent.com/AlexxEG/BSA_Browser/master/Sharp.BSA.BA2/BA2Util/DDS.cs
|
|
|
|
|
* which is also GPL3 code. Modified slightly for Wabbajack
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Copied from dds.h. Includes (almost) only stuff I need in this project.
|
|
|
|
|
*
|
|
|
|
|
* Link: https://github.com/digitalutopia1/BA2Lib/blob/master/BA2Lib/dds.h
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
public class DDS
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public const int DDS_MAGIC = 0x20534444; // "DDS "
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public const int DDS_FOURCC = 0x00000004; // DDPF_FOURCC
|
|
|
|
|
public const int DDS_RGB = 0x00000040; // DDPF_RGB
|
|
|
|
|
public const int DDS_RGBA = 0x00000041; // DDPF_RGB | DDPF_ALPHAPIXELS
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public const int
|
|
|
|
|
DDS_HEADER_FLAGS_TEXTURE = 0x00001007; // DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT
|
2021-09-27 12:42:46 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public const int DDS_HEADER_FLAGS_MIPMAP = 0x00020000; // DDSD_MIPMAPCOUNT
|
|
|
|
|
public const int DDS_HEADER_FLAGS_LINEARSIZE = 0x00080000; // DDSD_LINEARSIZE
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public const int DDS_SURFACE_FLAGS_TEXTURE = 0x00001000; // DDSCAPS_TEXTURE
|
|
|
|
|
public const int DDS_SURFACE_FLAGS_MIPMAP = 0x00400008; // DDSCAPS_COMPLEX | DDSCAPS_MIPMAP
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public const int DDS_ALPHA_MODE_UNKNOWN = 0x0;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public static uint HeaderSizeForFormat(DXGI_FORMAT fmt)
|
|
|
|
|
{
|
|
|
|
|
switch (fmt)
|
2021-09-27 12:42:46 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
case DXGI_FORMAT.BC1_UNORM_SRGB:
|
|
|
|
|
case DXGI_FORMAT.BC3_UNORM_SRGB:
|
|
|
|
|
case DXGI_FORMAT.BC4_UNORM:
|
|
|
|
|
case DXGI_FORMAT.BC5_SNORM:
|
|
|
|
|
case DXGI_FORMAT.BC6H_UF16:
|
|
|
|
|
case DXGI_FORMAT.BC7_UNORM:
|
|
|
|
|
case DXGI_FORMAT.BC7_UNORM_SRGB:
|
|
|
|
|
return DDS_HEADER_DXT10.Size + DDS_HEADER.Size;
|
|
|
|
|
default:
|
|
|
|
|
return DDS_HEADER.Size;
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public static uint MAKEFOURCC(char ch0, char ch1, char ch2, char ch3)
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// This is alien to me...
|
|
|
|
|
return (byte) ch0 | ((uint) (byte) ch1 << 8) | ((uint) (byte) ch2 << 16) | ((uint) (byte) ch3 << 24);
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public enum DXT10_RESOURCE_DIMENSION
|
|
|
|
|
{
|
|
|
|
|
DIMENSION_TEXTURE1D = 2,
|
|
|
|
|
DIMENSION_TEXTURE2D = 3,
|
|
|
|
|
DIMENSION_TEXTURE3D = 4
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2022-12-28 16:21:58 +00:00
|
|
|
|
[Flags]
|
|
|
|
|
public enum DDSCAPS2 : uint
|
|
|
|
|
{
|
|
|
|
|
CUBEMAP = 0x200,
|
|
|
|
|
CUBEMAP_POSITIVEX = 0x400,
|
|
|
|
|
CUBEMAP_NEGATIVEX = 0x800,
|
|
|
|
|
CUBEMAP_POSITIVEY = 0x1000,
|
|
|
|
|
CUBEMAP_NEGATIVEY = 0x2000,
|
|
|
|
|
CUBEMAP_POSITIVEZ = 0x4000,
|
|
|
|
|
CUBEMAP_NEGATIVEZ = 0x8000,
|
|
|
|
|
CUBEMAP_ALLFACES = 0xFC00
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
public struct DDS_HEADER
|
|
|
|
|
{
|
|
|
|
|
public uint dwSize;
|
|
|
|
|
public uint dwHeaderFlags;
|
|
|
|
|
public uint dwHeight;
|
|
|
|
|
public uint dwWidth;
|
|
|
|
|
public uint dwPitchOrLinearSize;
|
|
|
|
|
public uint dwDepth; // only if DDS_HEADER_FLAGS_VOLUME is set in dwHeaderFlags
|
|
|
|
|
public uint dwMipMapCount;
|
|
|
|
|
public uint dwReserved1; // [11]
|
|
|
|
|
public DDS_PIXELFORMAT PixelFormat; // ddspf
|
|
|
|
|
public uint dwSurfaceFlags;
|
|
|
|
|
public uint dwCubemapFlags;
|
|
|
|
|
public uint dwReserved2; // [3]
|
|
|
|
|
|
|
|
|
|
public uint GetSize()
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
// 9 uint + DDS_PIXELFORMAT uints + 2 uint arrays with 14 uints total
|
|
|
|
|
// each uint 4 bytes each
|
|
|
|
|
return 9 * 4 + PixelFormat.GetSize() + 14 * 4;
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2022-09-12 23:06:14 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public void Write(BinaryWriter bw)
|
|
|
|
|
{
|
|
|
|
|
bw.Write(dwSize);
|
|
|
|
|
bw.Write(dwHeaderFlags);
|
|
|
|
|
bw.Write(dwHeight);
|
|
|
|
|
bw.Write(dwWidth);
|
|
|
|
|
bw.Write(dwPitchOrLinearSize);
|
|
|
|
|
bw.Write(dwDepth);
|
|
|
|
|
bw.Write(dwMipMapCount);
|
|
|
|
|
|
|
|
|
|
// Just write it multiple times, since it's never assigned a value anyway
|
|
|
|
|
for (var i = 0; i < 11; i++)
|
|
|
|
|
bw.Write(dwReserved1);
|
|
|
|
|
|
|
|
|
|
// DDS_PIXELFORMAT
|
|
|
|
|
bw.Write(PixelFormat.dwSize);
|
|
|
|
|
bw.Write(PixelFormat.dwFlags);
|
|
|
|
|
bw.Write(PixelFormat.dwFourCC);
|
|
|
|
|
bw.Write(PixelFormat.dwRGBBitCount);
|
|
|
|
|
bw.Write(PixelFormat.dwRBitMask);
|
|
|
|
|
bw.Write(PixelFormat.dwGBitMask);
|
|
|
|
|
bw.Write(PixelFormat.dwBBitMask);
|
|
|
|
|
bw.Write(PixelFormat.dwABitMask);
|
|
|
|
|
|
|
|
|
|
bw.Write(dwSurfaceFlags);
|
|
|
|
|
bw.Write(dwCubemapFlags);
|
|
|
|
|
|
|
|
|
|
// Just write it multiple times, since it's never assigned a value anyway
|
|
|
|
|
for (var i = 0; i < 3; i++)
|
|
|
|
|
bw.Write(dwReserved2);
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public static uint Size
|
|
|
|
|
{
|
|
|
|
|
get
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
unsafe
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
return (uint) (sizeof(DDS_HEADER) + sizeof(int) * 10 + sizeof(int) * 2);
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
|
|
;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
public struct DDS_HEADER_DXT10
|
|
|
|
|
{
|
|
|
|
|
public uint dxgiFormat;
|
|
|
|
|
public uint resourceDimension;
|
|
|
|
|
public uint miscFlag;
|
|
|
|
|
public uint arraySize;
|
|
|
|
|
public uint miscFlags2;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public void Write(BinaryWriter bw)
|
|
|
|
|
{
|
|
|
|
|
bw.Write(dxgiFormat);
|
|
|
|
|
bw.Write(resourceDimension);
|
|
|
|
|
bw.Write(miscFlag);
|
|
|
|
|
bw.Write(arraySize);
|
|
|
|
|
bw.Write(miscFlags2);
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public static uint Size
|
|
|
|
|
{
|
|
|
|
|
get
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
unsafe
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
return (uint) sizeof(DDS_HEADER_DXT10);
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
|
|
|
|
|
;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-23 16:51:17 +00:00
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
|
|
|
|
public struct DDS_PIXELFORMAT
|
|
|
|
|
{
|
|
|
|
|
public uint dwSize;
|
|
|
|
|
public uint dwFlags;
|
|
|
|
|
public uint dwFourCC;
|
|
|
|
|
public uint dwRGBBitCount;
|
|
|
|
|
public uint dwRBitMask;
|
|
|
|
|
public uint dwGBitMask;
|
|
|
|
|
public uint dwBBitMask;
|
|
|
|
|
public uint dwABitMask;
|
|
|
|
|
|
|
|
|
|
public DDS_PIXELFORMAT(uint size, uint flags, uint fourCC, uint rgbBitCount, uint rBitMask, uint gBitMask,
|
|
|
|
|
uint bBitMask, uint aBitMask)
|
2019-10-06 21:58:36 +00:00
|
|
|
|
{
|
2021-10-23 16:51:17 +00:00
|
|
|
|
dwSize = size;
|
|
|
|
|
dwFlags = flags;
|
|
|
|
|
dwFourCC = fourCC;
|
|
|
|
|
dwRGBBitCount = rgbBitCount;
|
|
|
|
|
dwRBitMask = rBitMask;
|
|
|
|
|
dwGBitMask = gBitMask;
|
|
|
|
|
dwBBitMask = bBitMask;
|
|
|
|
|
dwABitMask = aBitMask;
|
|
|
|
|
}
|
2019-10-06 21:58:36 +00:00
|
|
|
|
|
2021-10-23 16:51:17 +00:00
|
|
|
|
public uint GetSize()
|
|
|
|
|
{
|
|
|
|
|
// 8 uints, each 4 bytes each
|
|
|
|
|
return 8 * 4;
|
2019-10-06 21:58:36 +00:00
|
|
|
|
}
|
2021-09-27 12:42:46 +00:00
|
|
|
|
}
|