adding new files

This commit is contained in:
Richard Geldreich
2026-01-19 01:59:35 -05:00
parent 9d87991078
commit ea6778b2b5
72 changed files with 31686 additions and 0 deletions

View File

@@ -0,0 +1,286 @@
// File: dds_defs.h
// DX9/10 .DDS file header definitions.
#pragma once
#define PIXEL_FMT_FOURCC(a, b, c, d) ((a) | ((b) << 8U) | ((c) << 16U) | ((d) << 24U))
enum pixel_format
{
PIXEL_FMT_INVALID = 0,
PIXEL_FMT_DXT1 = PIXEL_FMT_FOURCC('D', 'X', 'T', '1'),
PIXEL_FMT_DXT2 = PIXEL_FMT_FOURCC('D', 'X', 'T', '2'),
PIXEL_FMT_DXT3 = PIXEL_FMT_FOURCC('D', 'X', 'T', '3'),
PIXEL_FMT_DXT4 = PIXEL_FMT_FOURCC('D', 'X', 'T', '4'),
PIXEL_FMT_DXT5 = PIXEL_FMT_FOURCC('D', 'X', 'T', '5'),
PIXEL_FMT_3DC = PIXEL_FMT_FOURCC('A', 'T', 'I', '2'), // DXN_YX
PIXEL_FMT_DXN = PIXEL_FMT_FOURCC('A', '2', 'X', 'Y'), // DXN_XY
PIXEL_FMT_DXT5A = PIXEL_FMT_FOURCC('A', 'T', 'I', '1'), // ATI1N, http://developer.amd.com/media/gpu_assets/Radeon_X1x00_Programming_Guide.pdf
// Non-standard formats (some of these are supported by ATI's Compressonator)
PIXEL_FMT_DXT5_CCxY = PIXEL_FMT_FOURCC('C', 'C', 'x', 'Y'),
PIXEL_FMT_DXT5_xGxR = PIXEL_FMT_FOURCC('x', 'G', 'x', 'R'),
PIXEL_FMT_DXT5_xGBR = PIXEL_FMT_FOURCC('x', 'G', 'B', 'R'),
PIXEL_FMT_DXT5_AGBR = PIXEL_FMT_FOURCC('A', 'G', 'B', 'R'),
PIXEL_FMT_DXT1A = PIXEL_FMT_FOURCC('D', 'X', '1', 'A'),
PIXEL_FMT_ETC1 = PIXEL_FMT_FOURCC('E', 'T', 'C', '1'),
PIXEL_FMT_R8G8B8 = PIXEL_FMT_FOURCC('R', 'G', 'B', 'x'),
PIXEL_FMT_L8 = PIXEL_FMT_FOURCC('L', 'x', 'x', 'x'),
PIXEL_FMT_A8 = PIXEL_FMT_FOURCC('x', 'x', 'x', 'A'),
PIXEL_FMT_A8L8 = PIXEL_FMT_FOURCC('L', 'x', 'x', 'A'),
PIXEL_FMT_A8R8G8B8 = PIXEL_FMT_FOURCC('R', 'G', 'B', 'A')
};
const uint32_t cDDSMaxImageDimensions = 8192U;
// Total size of header is sizeof(uint32)+cDDSSizeofDDSurfaceDesc2;
const uint32_t cDDSSizeofDDSurfaceDesc2 = 124;
// "DDS "
const uint32_t cDDSFileSignature = 0x20534444;
struct DDCOLORKEY
{
uint32_t dwUnused0;
uint32_t dwUnused1;
};
struct DDPIXELFORMAT
{
uint32_t dwSize;
uint32_t dwFlags;
uint32_t dwFourCC;
uint32_t dwRGBBitCount; // ATI compressonator will place a FOURCC code here for swizzled/cooked DXTn formats
uint32_t dwRBitMask;
uint32_t dwGBitMask;
uint32_t dwBBitMask;
uint32_t dwRGBAlphaBitMask;
};
struct DDSCAPS2
{
uint32_t dwCaps;
uint32_t dwCaps2;
uint32_t dwCaps3;
uint32_t dwCaps4;
};
struct DDSURFACEDESC2
{
uint32_t dwSize;
uint32_t dwFlags;
uint32_t dwHeight;
uint32_t dwWidth;
union
{
int32_t lPitch;
uint32_t dwLinearSize;
};
uint32_t dwBackBufferCount;
uint32_t dwMipMapCount;
uint32_t dwAlphaBitDepth;
uint32_t dwUnused0;
uint32_t lpSurface;
DDCOLORKEY unused0;
DDCOLORKEY unused1;
DDCOLORKEY unused2;
DDCOLORKEY unused3;
DDPIXELFORMAT ddpfPixelFormat;
DDSCAPS2 ddsCaps;
uint32_t dwUnused1;
};
const uint32_t DDSD_CAPS = 0x00000001;
const uint32_t DDSD_HEIGHT = 0x00000002;
const uint32_t DDSD_WIDTH = 0x00000004;
const uint32_t DDSD_PITCH = 0x00000008;
const uint32_t DDSD_BACKBUFFERCOUNT = 0x00000020;
const uint32_t DDSD_ZBUFFERBITDEPTH = 0x00000040;
const uint32_t DDSD_ALPHABITDEPTH = 0x00000080;
const uint32_t DDSD_LPSURFACE = 0x00000800;
const uint32_t DDSD_PIXELFORMAT = 0x00001000;
const uint32_t DDSD_CKDESTOVERLAY = 0x00002000;
const uint32_t DDSD_CKDESTBLT = 0x00004000;
const uint32_t DDSD_CKSRCOVERLAY = 0x00008000;
const uint32_t DDSD_CKSRCBLT = 0x00010000;
const uint32_t DDSD_MIPMAPCOUNT = 0x00020000;
const uint32_t DDSD_REFRESHRATE = 0x00040000;
const uint32_t DDSD_LINEARSIZE = 0x00080000;
const uint32_t DDSD_TEXTURESTAGE = 0x00100000;
const uint32_t DDSD_FVF = 0x00200000;
const uint32_t DDSD_SRCVBHANDLE = 0x00400000;
const uint32_t DDSD_DEPTH = 0x00800000;
const uint32_t DDSD_ALL = 0x00fff9ee;
const uint32_t DDPF_ALPHAPIXELS = 0x00000001;
const uint32_t DDPF_ALPHA = 0x00000002;
const uint32_t DDPF_FOURCC = 0x00000004;
const uint32_t DDPF_PALETTEINDEXED8 = 0x00000020;
const uint32_t DDPF_RGB = 0x00000040;
const uint32_t DDPF_LUMINANCE = 0x00020000;
const uint32_t DDSCAPS_COMPLEX = 0x00000008;
const uint32_t DDSCAPS_TEXTURE = 0x00001000;
const uint32_t DDSCAPS_MIPMAP = 0x00400000;
const uint32_t DDSCAPS2_CUBEMAP = 0x00000200;
const uint32_t DDSCAPS2_CUBEMAP_POSITIVEX = 0x00000400;
const uint32_t DDSCAPS2_CUBEMAP_NEGATIVEX = 0x00000800;
const uint32_t DDSCAPS2_CUBEMAP_POSITIVEY = 0x00001000;
const uint32_t DDSCAPS2_CUBEMAP_NEGATIVEY = 0x00002000;
const uint32_t DDSCAPS2_CUBEMAP_POSITIVEZ = 0x00004000;
const uint32_t DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x00008000;
const uint32_t DDSCAPS2_VOLUME = 0x00200000;
typedef enum DXGI_FORMAT
{
DXGI_FORMAT_UNKNOWN = 0,
DXGI_FORMAT_R32G32B32A32_TYPELESS = 1,
DXGI_FORMAT_R32G32B32A32_FLOAT = 2,
DXGI_FORMAT_R32G32B32A32_UINT = 3,
DXGI_FORMAT_R32G32B32A32_SINT = 4,
DXGI_FORMAT_R32G32B32_TYPELESS = 5,
DXGI_FORMAT_R32G32B32_FLOAT = 6,
DXGI_FORMAT_R32G32B32_UINT = 7,
DXGI_FORMAT_R32G32B32_SINT = 8,
DXGI_FORMAT_R16G16B16A16_TYPELESS = 9,
DXGI_FORMAT_R16G16B16A16_FLOAT = 10,
DXGI_FORMAT_R16G16B16A16_UNORM = 11,
DXGI_FORMAT_R16G16B16A16_UINT = 12,
DXGI_FORMAT_R16G16B16A16_SNORM = 13,
DXGI_FORMAT_R16G16B16A16_SINT = 14,
DXGI_FORMAT_R32G32_TYPELESS = 15,
DXGI_FORMAT_R32G32_FLOAT = 16,
DXGI_FORMAT_R32G32_UINT = 17,
DXGI_FORMAT_R32G32_SINT = 18,
DXGI_FORMAT_R32G8X24_TYPELESS = 19,
DXGI_FORMAT_D32_FLOAT_S8X24_UINT = 20,
DXGI_FORMAT_R32_FLOAT_X8X24_TYPELESS = 21,
DXGI_FORMAT_X32_TYPELESS_G8X24_UINT = 22,
DXGI_FORMAT_R10G10B10A2_TYPELESS = 23,
DXGI_FORMAT_R10G10B10A2_UNORM = 24,
DXGI_FORMAT_R10G10B10A2_UINT = 25,
DXGI_FORMAT_R11G11B10_FLOAT = 26,
DXGI_FORMAT_R8G8B8A8_TYPELESS = 27,
DXGI_FORMAT_R8G8B8A8_UNORM = 28,
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB = 29,
DXGI_FORMAT_R8G8B8A8_UINT = 30,
DXGI_FORMAT_R8G8B8A8_SNORM = 31,
DXGI_FORMAT_R8G8B8A8_SINT = 32,
DXGI_FORMAT_R16G16_TYPELESS = 33,
DXGI_FORMAT_R16G16_FLOAT = 34,
DXGI_FORMAT_R16G16_UNORM = 35,
DXGI_FORMAT_R16G16_UINT = 36,
DXGI_FORMAT_R16G16_SNORM = 37,
DXGI_FORMAT_R16G16_SINT = 38,
DXGI_FORMAT_R32_TYPELESS = 39,
DXGI_FORMAT_D32_FLOAT = 40,
DXGI_FORMAT_R32_FLOAT = 41,
DXGI_FORMAT_R32_UINT = 42,
DXGI_FORMAT_R32_SINT = 43,
DXGI_FORMAT_R24G8_TYPELESS = 44,
DXGI_FORMAT_D24_UNORM_S8_UINT = 45,
DXGI_FORMAT_R24_UNORM_X8_TYPELESS = 46,
DXGI_FORMAT_X24_TYPELESS_G8_UINT = 47,
DXGI_FORMAT_R8G8_TYPELESS = 48,
DXGI_FORMAT_R8G8_UNORM = 49,
DXGI_FORMAT_R8G8_UINT = 50,
DXGI_FORMAT_R8G8_SNORM = 51,
DXGI_FORMAT_R8G8_SINT = 52,
DXGI_FORMAT_R16_TYPELESS = 53,
DXGI_FORMAT_R16_FLOAT = 54,
DXGI_FORMAT_D16_UNORM = 55,
DXGI_FORMAT_R16_UNORM = 56,
DXGI_FORMAT_R16_UINT = 57,
DXGI_FORMAT_R16_SNORM = 58,
DXGI_FORMAT_R16_SINT = 59,
DXGI_FORMAT_R8_TYPELESS = 60,
DXGI_FORMAT_R8_UNORM = 61,
DXGI_FORMAT_R8_UINT = 62,
DXGI_FORMAT_R8_SNORM = 63,
DXGI_FORMAT_R8_SINT = 64,
DXGI_FORMAT_A8_UNORM = 65,
DXGI_FORMAT_R1_UNORM = 66,
DXGI_FORMAT_R9G9B9E5_SHAREDEXP = 67,
DXGI_FORMAT_R8G8_B8G8_UNORM = 68,
DXGI_FORMAT_G8R8_G8B8_UNORM = 69,
DXGI_FORMAT_BC1_TYPELESS = 70,
DXGI_FORMAT_BC1_UNORM = 71,
DXGI_FORMAT_BC1_UNORM_SRGB = 72,
DXGI_FORMAT_BC2_TYPELESS = 73,
DXGI_FORMAT_BC2_UNORM = 74,
DXGI_FORMAT_BC2_UNORM_SRGB = 75,
DXGI_FORMAT_BC3_TYPELESS = 76,
DXGI_FORMAT_BC3_UNORM = 77,
DXGI_FORMAT_BC3_UNORM_SRGB = 78,
DXGI_FORMAT_BC4_TYPELESS = 79,
DXGI_FORMAT_BC4_UNORM = 80,
DXGI_FORMAT_BC4_SNORM = 81,
DXGI_FORMAT_BC5_TYPELESS = 82,
DXGI_FORMAT_BC5_UNORM = 83,
DXGI_FORMAT_BC5_SNORM = 84,
DXGI_FORMAT_B5G6R5_UNORM = 85,
DXGI_FORMAT_B5G5R5A1_UNORM = 86,
DXGI_FORMAT_B8G8R8A8_UNORM = 87,
DXGI_FORMAT_B8G8R8X8_UNORM = 88,
DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM = 89,
DXGI_FORMAT_B8G8R8A8_TYPELESS = 90,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB = 91,
DXGI_FORMAT_B8G8R8X8_TYPELESS = 92,
DXGI_FORMAT_B8G8R8X8_UNORM_SRGB = 93,
DXGI_FORMAT_BC6H_TYPELESS = 94,
DXGI_FORMAT_BC6H_UF16 = 95,
DXGI_FORMAT_BC6H_SF16 = 96,
DXGI_FORMAT_BC7_TYPELESS = 97,
DXGI_FORMAT_BC7_UNORM = 98,
DXGI_FORMAT_BC7_UNORM_SRGB = 99,
DXGI_FORMAT_AYUV = 100,
DXGI_FORMAT_Y410 = 101,
DXGI_FORMAT_Y416 = 102,
DXGI_FORMAT_NV12 = 103,
DXGI_FORMAT_P010 = 104,
DXGI_FORMAT_P016 = 105,
DXGI_FORMAT_420_OPAQUE = 106,
DXGI_FORMAT_YUY2 = 107,
DXGI_FORMAT_Y210 = 108,
DXGI_FORMAT_Y216 = 109,
DXGI_FORMAT_NV11 = 110,
DXGI_FORMAT_AI44 = 111,
DXGI_FORMAT_IA44 = 112,
DXGI_FORMAT_P8 = 113,
DXGI_FORMAT_A8P8 = 114,
DXGI_FORMAT_B4G4R4A4_UNORM = 115,
DXGI_FORMAT_P208 = 130,
DXGI_FORMAT_V208 = 131,
DXGI_FORMAT_V408 = 132,
DXGI_FORMAT_FORCE_UINT = 0xffffffff
} DXGI_FORMAT;
enum D3D10_RESOURCE_DIMENSION
{
D3D10_RESOURCE_DIMENSION_UNKNOWN = 0,
D3D10_RESOURCE_DIMENSION_BUFFER = 1,
D3D10_RESOURCE_DIMENSION_TEXTURE1D = 2,
D3D10_RESOURCE_DIMENSION_TEXTURE2D = 3,
D3D10_RESOURCE_DIMENSION_TEXTURE3D = 4
};
struct DDS_HEADER_DXT10
{
DXGI_FORMAT dxgiFormat;
D3D10_RESOURCE_DIMENSION resourceDimension;
uint32_t miscFlag;
uint32_t arraySize;
uint32_t miscFlags2;
};

View File

@@ -0,0 +1,100 @@
// example_transcoding.cpp: Very simple transcoding-only example. Does not depend on the basisu encoder library at all, just basisu_transcoder.cpp.
// You can use AMD Compressonator or Microsoft's DirectXTex tools on github to view the written DX10 .DDS file.
#include <stdlib.h>
#include <stdio.h>
// for testing
//#define BASISD_SUPPORT_XUASTC (0)
//#define BASISD_SUPPORT_KTX2_ZSTD (0)
#include "../transcoder/basisu_transcoder.h"
#include "utils.h"
int main()
{
basist::basisu_transcoder_init();
// Read the .KTX2 file's data into memory.
utils::uint8_vec ktx2_file_data;
if (!utils::read_file("../test_files/base_xuastc_arith.ktx2", ktx2_file_data))
{
if (!utils::read_file("base_xuastc_arith.ktx2", ktx2_file_data))
{
fprintf(stderr, "Can't read file ../test_files/base_xuastc_arith.ktx2 or base_xuastc_arith.ktx2\n");
return EXIT_FAILURE;
}
}
printf("Read file base_xuastc_arith.ktx2\n");
if (ktx2_file_data.size() > UINT32_MAX)
{
fprintf(stderr, "KTX2 file too large\n");
return EXIT_FAILURE;
}
basist::ktx2_transcoder transcoder;
// Initialize the transcoder.
if (!transcoder.init(ktx2_file_data.data(), (uint32_t)ktx2_file_data.size()))
return EXIT_FAILURE;
const uint32_t width = transcoder.get_width();
const uint32_t height = transcoder.get_height();
const uint32_t num_levels = transcoder.get_levels();
const bool is_srgb = transcoder.is_srgb();
printf("KTX2 dimensions: %ux%u, num mip levels: %u, sRGB: %u\n", width, height, num_levels, is_srgb);
// Can't transcode HDR to LDR formats.
if (transcoder.is_hdr())
{
fprintf(stderr, "Expected LDR KTX2 file\n");
return EXIT_FAILURE;
}
// Ensure BC7 support was enabled at compilation time (it will be enabled by default).
const basist::transcoder_texture_format tex_fmt = basist::transcoder_texture_format::cTFBC7_RGBA;
if (!basist::basis_is_format_supported(tex_fmt, transcoder.get_basis_tex_format()))
{
printf("BC7 was disabled in the transcoder at compilation\n");
return EXIT_FAILURE;
}
// Begin transcoding (this will be a no-op with UASTC HDR textures, but you still need to do it. For ETC1S it'll unpack the global codebooks).
transcoder.start_transcoding();
// Transcode to BC7 and write a BC7 .DDS file.
// Bytes per block (8 or 16 for BC1-7)
const uint32_t bytes_per_block = basist::basis_get_bytes_per_block_or_pixel(tex_fmt);
// Compute total bytes needed to transcode the slice
const uint32_t total_bytes = basist::basis_compute_transcoded_image_size_in_bytes(tex_fmt, width, height);
// Derive the total number of blocks the output buffer can hold. The transcoder will use this to verify the buffer is large enough.
const uint32_t total_blocks = total_bytes / bytes_per_block;
// Allocate the buffer to hold the blocks
utils::uint8_vec tex_buffer(total_bytes);
// Transcode the level
bool status = transcoder.transcode_image_level(0, 0, 0,
tex_buffer.data(), total_blocks,
tex_fmt, 0);
if (!status)
{
fprintf(stderr, "transcoder.transcode_image_level() failed\n");
return EXIT_FAILURE;
}
// Write an sRGB DX10-style .DDS file.
if (!utils::save_dds("out.dds", width, height, tex_buffer.data(), 8, DXGI_FORMAT_BC7_UNORM_SRGB, true, true))
{
fprintf(stderr, "save_dds() failed\n");
return EXIT_FAILURE;
}
printf("Wrote out.dds\n");
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
<assemblyIdentity type="win32" name="..." version="6.0.0.0"/>
<application>
<windowsSettings>
<activeCodePage xmlns="http://schemas.microsoft.com/SMI/2019/WindowsSettings">UTF-8</activeCodePage>
</windowsSettings>
</application>
</assembly>

View File

@@ -0,0 +1,202 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|ARM64EC">
<Configuration>Debug</Configuration>
<Platform>ARM64EC</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|ARM64EC">
<Configuration>Release</Configuration>
<Platform>ARM64EC</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{13333092-fcfe-4d74-8e76-f10c6037593c}</ProjectGuid>
<RootNamespace>exampletranscoding</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v145</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>v145</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64EC'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64EC'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v145</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64EC'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64EC'" Label="PropertySheets">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<EnableEnhancedInstructionSet>AdvancedVectorExtensions</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<EnableEnhancedInstructionSet>AdvancedVectorExtensions</EnableEnhancedInstructionSet>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<WarningLevel>Level4</WarningLevel>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|ARM64EC'">
<ClCompile>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
<WarningLevel>Level4</WarningLevel>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|ARM64EC'">
<ClCompile>
<WarningLevel>Level4</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<LanguageStandard>stdcpp17</LanguageStandard>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\transcoder\basisu_transcoder.cpp" />
<ClCompile Include="..\zstd\zstddeclib.c" />
<ClCompile Include="example_transcoding.cpp" />
<ClCompile Include="utils.cpp" />
</ItemGroup>
<ItemGroup>
<Manifest Include="example_transcoding.manifest" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="dds_defs.h" />
<ClInclude Include="utils.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="Source Files">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="Header Files">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="Resource Files">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
<Filter Include="Source Files\utils">
<UniqueIdentifier>{db43163f-6d1b-46cf-90ad-24650d502e6a}</UniqueIdentifier>
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="example_transcoding.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\transcoder\basisu_transcoder.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="utils.cpp">
<Filter>Source Files\utils</Filter>
</ClCompile>
<ClCompile Include="..\zstd\zstddeclib.c">
<Filter>Source Files\utils</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<Manifest Include="example_transcoding.manifest">
<Filter>Source Files</Filter>
</Manifest>
</ItemGroup>
<ItemGroup>
<ClInclude Include="utils.h">
<Filter>Source Files\utils</Filter>
</ClInclude>
<ClInclude Include="dds_defs.h">
<Filter>Source Files\utils</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@@ -0,0 +1,948 @@
// File: utils.cpp
#include "utils.h"
//#include "lodepng.h"
//#include "miniz.h"
namespace utils
{
#define FLOOD_PUSH(y, xl, xr, dy) if (((y + (dy)) >= 0) && ((y + (dy)) < (int)m_height)) { stack.push_back(fill_segment(y, xl, xr, dy)); }
// See http://www.realtimerendering.com/resources/GraphicsGems/gems/SeedFill.c
uint32_t image_u8::flood_fill(int x, int y, const color_quad_u8& c, const color_quad_u8& b, std::vector<pixel_coord>* pSet_pixels)
{
uint32_t total_set = 0;
if (!flood_fill_is_inside(x, y, b))
return 0;
std::vector<fill_segment> stack;
stack.reserve(64);
FLOOD_PUSH(y, x, x, 1);
FLOOD_PUSH(y + 1, x, x, -1);
while (stack.size())
{
fill_segment s = stack.back();
stack.pop_back();
int x1 = s.m_xl, x2 = s.m_xr, dy = s.m_dy;
y = s.m_y + s.m_dy;
for (x = x1; (x >= 0) && flood_fill_is_inside(x, y, b); x--)
{
(*this)(x, y) = c;
total_set++;
if (pSet_pixels)
pSet_pixels->push_back(pixel_coord(x, y));
}
int l;
if (x >= x1)
goto skip;
l = x + 1;
if (l < x1)
FLOOD_PUSH(y, l, x1 - 1, -dy);
x = x1 + 1;
do
{
for (; x <= ((int)m_width - 1) && flood_fill_is_inside(x, y, b); x++)
{
(*this)(x, y) = c;
total_set++;
if (pSet_pixels)
pSet_pixels->push_back(pixel_coord(x, y));
}
FLOOD_PUSH(y, l, x - 1, dy);
if (x > (x2 + 1))
FLOOD_PUSH(y, x2 + 1, x - 1, -dy);
skip:
for (x++; x <= x2 && !flood_fill_is_inside(x, y, b); x++)
;
l = x;
} while (x <= x2);
}
return total_set;
}
void image_u8::draw_line(int xs, int ys, int xe, int ye, const color_quad_u8& color)
{
if (xs > xe)
{
std::swap(xs, xe);
std::swap(ys, ye);
}
int dx = xe - xs, dy = ye - ys;
if (!dx)
{
if (ys > ye)
std::swap(ys, ye);
for (int i = ys; i <= ye; i++)
set_pixel_clipped(xs, i, color);
}
else if (!dy)
{
for (int i = xs; i < xe; i++)
set_pixel_clipped(i, ys, color);
}
else if (dy > 0)
{
if (dy <= dx)
{
int e = 2 * dy - dx, e_no_inc = 2 * dy, e_inc = 2 * (dy - dx);
rasterize_line(xs, ys, xe, ye, 0, 1, e, e_inc, e_no_inc, color);
}
else
{
int e = 2 * dx - dy, e_no_inc = 2 * dx, e_inc = 2 * (dx - dy);
rasterize_line(xs, ys, xe, ye, 1, 1, e, e_inc, e_no_inc, color);
}
}
else
{
dy = -dy;
if (dy <= dx)
{
int e = 2 * dy - dx, e_no_inc = 2 * dy, e_inc = 2 * (dy - dx);
rasterize_line(xs, ys, xe, ye, 0, -1, e, e_inc, e_no_inc, color);
}
else
{
int e = 2 * dx - dy, e_no_inc = (2 * dx), e_inc = 2 * (dx - dy);
rasterize_line(xe, ye, xs, ys, 1, -1, e, e_inc, e_no_inc, color);
}
}
}
void image_u8::rasterize_line(int xs, int ys, int xe, int ye, int pred, int inc_dec, int e, int e_inc, int e_no_inc, const color_quad_u8& color)
{
int start, end, var;
if (pred)
{
start = ys;
end = ye;
var = xs;
for (int i = start; i <= end; i++)
{
set_pixel_clipped(var, i, color);
if (e < 0)
e += e_no_inc;
else
{
var += inc_dec;
e += e_inc;
}
}
}
else
{
start = xs;
end = xe;
var = ys;
for (int i = start; i <= end; i++)
{
set_pixel_clipped(i, var, color);
if (e < 0)
e += e_no_inc;
else
{
var += inc_dec;
e += e_inc;
}
}
}
}
#if 0
bool load_png(const char* pFilename, image_u8& img)
{
img.clear();
std::vector<unsigned char> pixels;
unsigned int w = 0, h = 0;
unsigned int e = lodepng::decode(pixels, w, h, pFilename);
if (e != 0)
{
fprintf(stderr, "Failed loading PNG file %s\n", pFilename);
return false;
}
img.init(w, h);
memcpy(&img.get_pixels()[0], &pixels[0], w * h * sizeof(uint32_t));
return true;
}
bool save_png(const char* pFilename, const image_u8& img, bool save_alpha)
{
const uint32_t w = img.width();
const uint32_t h = img.height();
std::vector<unsigned char> pixels;
if (save_alpha)
{
pixels.resize(w * h * sizeof(color_quad_u8));
memcpy(&pixels[0], &img.get_pixels()[0], w * h * sizeof(color_quad_u8));
}
else
{
pixels.resize(w * h * 3);
unsigned char* pDst = &pixels[0];
for (uint32_t y = 0; y < h; y++)
for (uint32_t x = 0; x < w; x++, pDst += 3)
pDst[0] = img(x, y)[0], pDst[1] = img(x, y)[1], pDst[2] = img(x, y)[2];
}
return lodepng::encode(pFilename, pixels, w, h, save_alpha ? LCT_RGBA : LCT_RGB) == 0;
}
#endif
static float gauss(int x, int y, float sigma_sqr)
{
float pow = expf(-((x * x + y * y) / (2.0f * sigma_sqr)));
float g = (1.0f / (sqrtf((float)(2.0f * M_PI * sigma_sqr)))) * pow;
return g;
}
// size_x/y should be odd
void compute_gaussian_kernel(float* pDst, int size_x, int size_y, float sigma_sqr, uint32_t flags)
{
assert(size_x & size_y & 1);
if (!(size_x | size_y))
return;
int mid_x = size_x / 2;
int mid_y = size_y / 2;
double sum = 0;
for (int x = 0; x < size_x; x++)
{
for (int y = 0; y < size_y; y++)
{
float g;
if ((x > mid_x) && (y < mid_y))
g = pDst[(size_x - x - 1) + y * size_x];
else if ((x < mid_x) && (y > mid_y))
g = pDst[x + (size_y - y - 1) * size_x];
else if ((x > mid_x) && (y > mid_y))
g = pDst[(size_x - x - 1) + (size_y - y - 1) * size_x];
else
g = gauss(x - mid_x, y - mid_y, sigma_sqr);
pDst[x + y * size_x] = g;
sum += g;
}
}
if (flags & cComputeGaussianFlagNormalizeCenterToOne)
{
sum = pDst[mid_x + mid_y * size_x];
}
if (flags & (cComputeGaussianFlagNormalizeCenterToOne | cComputeGaussianFlagNormalize))
{
double one_over_sum = 1.0f / sum;
for (int i = 0; i < size_x * size_y; i++)
pDst[i] = static_cast<float>(pDst[i] * one_over_sum);
if (flags & cComputeGaussianFlagNormalizeCenterToOne)
pDst[mid_x + mid_y * size_x] = 1.0f;
}
if (flags & cComputeGaussianFlagPrint)
{
printf("{\n");
for (int y = 0; y < size_y; y++)
{
printf(" ");
for (int x = 0; x < size_x; x++)
{
printf("%f, ", pDst[x + y * size_x]);
}
printf("\n");
}
printf("}");
}
}
void gaussian_filter(imagef& dst, const imagef& orig_img, uint32_t odd_filter_width, float sigma_sqr, bool wrapping, uint32_t width_divisor, uint32_t height_divisor)
{
assert(odd_filter_width && (odd_filter_width & 1));
odd_filter_width |= 1;
std::vector<float> kernel(odd_filter_width * odd_filter_width);
compute_gaussian_kernel(&kernel[0], odd_filter_width, odd_filter_width, sigma_sqr, cComputeGaussianFlagNormalize);
const int dst_width = orig_img.get_width() / width_divisor;
const int dst_height = orig_img.get_height() / height_divisor;
const int H = odd_filter_width / 2;
const int L = -H;
dst.crop(dst_width, dst_height);
//#pragma omp parallel for
for (int oy = 0; oy < dst_height; oy++)
{
for (int ox = 0; ox < dst_width; ox++)
{
vec4F c(0.0f);
for (int yd = L; yd <= H; yd++)
{
int y = oy * height_divisor + (height_divisor >> 1) + yd;
for (int xd = L; xd <= H; xd++)
{
int x = ox * width_divisor + (width_divisor >> 1) + xd;
const vec4F& p = orig_img.get_clamped_or_wrapped(x, y, wrapping, wrapping);
float w = kernel[(xd + H) + (yd + H) * odd_filter_width];
c[0] += p[0] * w;
c[1] += p[1] * w;
c[2] += p[2] * w;
c[3] += p[3] * w;
}
}
dst(ox, oy).set(c[0], c[1], c[2], c[3]);
}
}
}
static void pow_image(const imagef& src, imagef& dst, const vec4F& power)
{
dst.resize(src);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& p = src(x, y);
if ((power[0] == 2.0f) && (power[1] == 2.0f) && (power[2] == 2.0f) && (power[3] == 2.0f))
dst(x, y).set(p[0] * p[0], p[1] * p[1], p[2] * p[2], p[3] * p[3]);
else
dst(x, y).set(powf(p[0], power[0]), powf(p[1], power[1]), powf(p[2], power[2]), powf(p[3], power[3]));
}
}
}
#if 0
static void mul_image(const imagef& src, imagef& dst, const vec4F& mul)
{
dst.resize(src);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& p = src(x, y);
dst(x, y).set(p[0] * mul[0], p[1] * mul[1], p[2] * mul[2], p[3] * mul[3]);
}
}
}
#endif
static void scale_image(const imagef& src, imagef& dst, const vec4F& scale, const vec4F& shift)
{
dst.resize(src);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& p = src(x, y);
vec4F d;
for (uint32_t c = 0; c < 4; c++)
d[c] = scale[c] * p[c] + shift[c];
dst(x, y).set(d[0], d[1], d[2], d[3]);
}
}
}
static void add_weighted_image(const imagef& src1, const vec4F& alpha, const imagef& src2, const vec4F& beta, const vec4F& gamma, imagef& dst)
{
dst.resize(src1);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& s1 = src1(x, y);
const vec4F& s2 = src2(x, y);
dst(x, y).set(
s1[0] * alpha[0] + s2[0] * beta[0] + gamma[0],
s1[1] * alpha[1] + s2[1] * beta[1] + gamma[1],
s1[2] * alpha[2] + s2[2] * beta[2] + gamma[2],
s1[3] * alpha[3] + s2[3] * beta[3] + gamma[3]);
}
}
}
static void add_image(const imagef& src1, const imagef& src2, imagef& dst)
{
dst.resize(src1);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& s1 = src1(x, y);
const vec4F& s2 = src2(x, y);
dst(x, y).set(s1[0] + s2[0], s1[1] + s2[1], s1[2] + s2[2], s1[3] + s2[3]);
}
}
}
static void adds_image(const imagef& src, const vec4F& value, imagef& dst)
{
dst.resize(src);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& p = src(x, y);
dst(x, y).set(p[0] + value[0], p[1] + value[1], p[2] + value[2], p[3] + value[3]);
}
}
}
static void mul_image(const imagef& src1, const imagef& src2, imagef& dst, const vec4F& scale)
{
dst.resize(src1);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& s1 = src1(x, y);
const vec4F& s2 = src2(x, y);
vec4F d;
for (uint32_t c = 0; c < 4; c++)
{
float v1 = s1[c];
float v2 = s2[c];
d[c] = v1 * v2 * scale[c];
}
dst(x, y) = d;
}
}
}
static void div_image(const imagef& src1, const imagef& src2, imagef& dst, const vec4F& scale)
{
dst.resize(src1);
//#pragma omp parallel for
for (int y = 0; y < (int)dst.get_height(); y++)
{
for (uint32_t x = 0; x < dst.get_width(); x++)
{
const vec4F& s1 = src1(x, y);
const vec4F& s2 = src2(x, y);
vec4F d;
for (uint32_t c = 0; c < 4; c++)
{
float v = s2[c];
if (v == 0.0f)
d[c] = 0.0f;
else
d[c] = (s1[c] * scale[c]) / v;
}
dst(x, y) = d;
}
}
}
static vec4F avg_image(const imagef& src)
{
vec4F avg(0.0f);
for (uint32_t y = 0; y < src.get_height(); y++)
{
for (uint32_t x = 0; x < src.get_width(); x++)
{
const vec4F& s = src(x, y);
avg += vec4F(s[0], s[1], s[2], s[3]);
}
}
avg /= static_cast<float>(src.get_total_pixels());
return avg;
}
// Reference: https://ece.uwaterloo.ca/~z70wang/research/ssim/index.html
vec4F compute_ssim(const imagef& a, const imagef& b)
{
imagef axb, a_sq, b_sq, mu1, mu2, mu1_sq, mu2_sq, mu1_mu2, s1_sq, s2_sq, s12, smap, t1, t2, t3;
const float C1 = 6.50250f, C2 = 58.52250f;
pow_image(a, a_sq, vec4F(2));
pow_image(b, b_sq, vec4F(2));
mul_image(a, b, axb, vec4F(1.0f));
gaussian_filter(mu1, a, 11, 1.5f * 1.5f);
gaussian_filter(mu2, b, 11, 1.5f * 1.5f);
pow_image(mu1, mu1_sq, vec4F(2));
pow_image(mu2, mu2_sq, vec4F(2));
mul_image(mu1, mu2, mu1_mu2, vec4F(1.0f));
gaussian_filter(s1_sq, a_sq, 11, 1.5f * 1.5f);
add_weighted_image(s1_sq, vec4F(1), mu1_sq, vec4F(-1), vec4F(0), s1_sq);
gaussian_filter(s2_sq, b_sq, 11, 1.5f * 1.5f);
add_weighted_image(s2_sq, vec4F(1), mu2_sq, vec4F(-1), vec4F(0), s2_sq);
gaussian_filter(s12, axb, 11, 1.5f * 1.5f);
add_weighted_image(s12, vec4F(1), mu1_mu2, vec4F(-1), vec4F(0), s12);
scale_image(mu1_mu2, t1, vec4F(2), vec4F(0));
adds_image(t1, vec4F(C1), t1);
scale_image(s12, t2, vec4F(2), vec4F(0));
adds_image(t2, vec4F(C2), t2);
mul_image(t1, t2, t3, vec4F(1));
add_image(mu1_sq, mu2_sq, t1);
adds_image(t1, vec4F(C1), t1);
add_image(s1_sq, s2_sq, t2);
adds_image(t2, vec4F(C2), t2);
mul_image(t1, t2, t1, vec4F(1));
div_image(t3, t1, smap, vec4F(1));
return avg_image(smap);
}
vec4F compute_ssim(const image_u8& a, const image_u8& b, bool luma)
{
image_u8 ta(a), tb(b);
if ((ta.width() != tb.width()) || (ta.height() != tb.height()))
{
fprintf(stderr, "compute_ssim: Cropping input images to equal dimensions\n");
const uint32_t w = std::min(a.width(), b.width());
const uint32_t h = std::min(a.height(), b.height());
ta.crop(w, h);
tb.crop(w, h);
}
if (!ta.width() || !ta.height())
{
assert(0);
return vec4F(0);
}
if (luma)
{
for (uint32_t y = 0; y < ta.height(); y++)
{
for (uint32_t x = 0; x < ta.width(); x++)
{
ta(x, y).set((uint8_t)ta(x, y).get_luma(), ta(x, y).a);
tb(x, y).set((uint8_t)tb(x, y).get_luma(), tb(x, y).a);
}
}
}
imagef fta, ftb;
fta.set(ta);
ftb.set(tb);
return compute_ssim(fta, ftb);
}
bool save_dds(const char* pFilename, uint32_t width, uint32_t height, const void* pBlocks, uint32_t pixel_format_bpp, DXGI_FORMAT dxgi_format, bool srgb, bool force_dx10_header)
{
(void)srgb;
FILE* pFile = NULL;
#ifdef _MSC_VER
fopen_s(&pFile, pFilename, "wb");
#else
pFile = fopen(pFilename, "wb");
#endif
if (!pFile)
{
fprintf(stderr, "Failed creating file %s!\n", pFilename);
return false;
}
fwrite("DDS ", 4, 1, pFile);
DDSURFACEDESC2 desc;
memset(&desc, 0, sizeof(desc));
desc.dwSize = sizeof(desc);
desc.dwFlags = DDSD_WIDTH | DDSD_HEIGHT | DDSD_PIXELFORMAT | DDSD_CAPS;
desc.dwWidth = width;
desc.dwHeight = height;
desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
desc.ddpfPixelFormat.dwSize = sizeof(desc.ddpfPixelFormat);
desc.ddpfPixelFormat.dwFlags |= DDPF_FOURCC;
desc.lPitch = (((desc.dwWidth + 3) & ~3) * ((desc.dwHeight + 3) & ~3) * pixel_format_bpp) >> 3;
desc.dwFlags |= DDSD_LINEARSIZE;
desc.ddpfPixelFormat.dwRGBBitCount = 0;
if ((!force_dx10_header) &&
((dxgi_format == DXGI_FORMAT_BC1_UNORM) ||
(dxgi_format == DXGI_FORMAT_BC3_UNORM) ||
(dxgi_format == DXGI_FORMAT_BC4_UNORM) ||
(dxgi_format == DXGI_FORMAT_BC5_UNORM)))
{
if (dxgi_format == DXGI_FORMAT_BC1_UNORM)
desc.ddpfPixelFormat.dwFourCC = (uint32_t)PIXEL_FMT_FOURCC('D', 'X', 'T', '1');
else if (dxgi_format == DXGI_FORMAT_BC3_UNORM)
desc.ddpfPixelFormat.dwFourCC = (uint32_t)PIXEL_FMT_FOURCC('D', 'X', 'T', '5');
else if (dxgi_format == DXGI_FORMAT_BC4_UNORM)
desc.ddpfPixelFormat.dwFourCC = (uint32_t)PIXEL_FMT_FOURCC('A', 'T', 'I', '1');
else if (dxgi_format == DXGI_FORMAT_BC5_UNORM)
desc.ddpfPixelFormat.dwFourCC = (uint32_t)PIXEL_FMT_FOURCC('A', 'T', 'I', '2');
fwrite(&desc, sizeof(desc), 1, pFile);
}
else
{
desc.ddpfPixelFormat.dwFourCC = (uint32_t)PIXEL_FMT_FOURCC('D', 'X', '1', '0');
fwrite(&desc, sizeof(desc), 1, pFile);
DDS_HEADER_DXT10 hdr10;
memset(&hdr10, 0, sizeof(hdr10));
// Not all tools support DXGI_FORMAT_BC7_UNORM_SRGB (like NVTT), but ddsview in DirectXTex pays attention to it. So not sure what to do here.
// For best compatibility just write DXGI_FORMAT_BC7_UNORM.
//hdr10.dxgiFormat = srgb ? DXGI_FORMAT_BC7_UNORM_SRGB : DXGI_FORMAT_BC7_UNORM;
hdr10.dxgiFormat = dxgi_format; // DXGI_FORMAT_BC7_UNORM;
hdr10.resourceDimension = D3D10_RESOURCE_DIMENSION_TEXTURE2D;
hdr10.arraySize = 1;
fwrite(&hdr10, sizeof(hdr10), 1, pFile);
}
fwrite(pBlocks, desc.lPitch, 1, pFile);
if (fclose(pFile) == EOF)
{
fprintf(stderr, "Failed writing to DDS file %s!\n", pFilename);
return false;
}
return true;
}
void strip_extension(std::string& s)
{
for (int32_t i = (int32_t)s.size() - 1; i >= 0; i--)
{
if (s[i] == '.')
{
s.resize(i);
break;
}
}
}
void strip_path(std::string& s)
{
for (int32_t i = (int32_t)s.size() - 1; i >= 0; i--)
{
if ((s[i] == '/') || (s[i] == ':') || (s[i] == '\\'))
{
s.erase(0, i + 1);
break;
}
}
}
uint32_t hash_hsieh(const uint8_t* pBuf, size_t len)
{
if (!pBuf || !len)
return 0;
uint32_t h = static_cast<uint32_t>(len);
const uint32_t bytes_left = len & 3;
len >>= 2;
while (len--)
{
const uint16_t* pWords = reinterpret_cast<const uint16_t*>(pBuf);
h += pWords[0];
const uint32_t t = (pWords[1] << 11) ^ h;
h = (h << 16) ^ t;
pBuf += sizeof(uint32_t);
h += h >> 11;
}
switch (bytes_left)
{
case 1:
h += *reinterpret_cast<const signed char*>(pBuf);
h ^= h << 10;
h += h >> 1;
break;
case 2:
h += *reinterpret_cast<const uint16_t*>(pBuf);
h ^= h << 11;
h += h >> 17;
break;
case 3:
h += *reinterpret_cast<const uint16_t*>(pBuf);
h ^= h << 16;
h ^= (static_cast<signed char>(pBuf[sizeof(uint16_t)])) << 18;
h += h >> 11;
break;
default:
break;
}
h ^= h << 3;
h += h >> 5;
h ^= h << 4;
h += h >> 17;
h ^= h << 25;
h += h >> 6;
return h;
}
float compute_block_max_std_dev(const color_quad_u8* pPixels, uint32_t block_width, uint32_t block_height, uint32_t num_comps)
{
tracked_stat comp_stats[4];
for (uint32_t y = 0; y < block_height; y++)
{
for (uint32_t x = 0; x < block_width; x++)
{
const color_quad_u8* pPixel = pPixels + x + y * block_width;
for (uint32_t c = 0; c < num_comps; c++)
comp_stats[c].update(pPixel->m_c[c]);
}
}
float max_std_dev = 0.0f;
for (uint32_t i = 0; i < num_comps; i++)
max_std_dev = std::max(max_std_dev, comp_stats[i].get_std_dev());
return max_std_dev;
}
const uint32_t ASTC_SIG = 0x5CA1AB13;
#pragma pack(push, 1)
struct astc_header
{
uint32_t m_sig;
uint8_t m_block_x;
uint8_t m_block_y;
uint8_t m_block_z;
uint8_t m_width[3];
uint8_t m_height[3];
uint8_t m_depth[3];
};
#pragma pack(pop)
bool save_astc_file(const char* pFilename, block16_vec& blocks, uint32_t width, uint32_t height, uint32_t block_width, uint32_t block_height)
{
FILE* pFile = nullptr;
#ifdef _MSC_VER
fopen_s(&pFile, pFilename, "wb");
#else
pFile = fopen(pFilename, "wb");
#endif
if (!pFile)
return false;
astc_header hdr;
memset(&hdr, 0, sizeof(hdr));
hdr.m_sig = ASTC_SIG;
hdr.m_block_x = (uint8_t)block_width;
hdr.m_block_y = (uint8_t)block_height;
hdr.m_block_z = 1;
hdr.m_width[0] = (uint8_t)(width);
hdr.m_width[1] = (uint8_t)(width >> 8);
hdr.m_width[2] = (uint8_t)(width >> 16);
hdr.m_height[0] = (uint8_t)(height);
hdr.m_height[1] = (uint8_t)(height >> 8);
hdr.m_height[2] = (uint8_t)(height >> 16);
hdr.m_depth[0] = 1;
fwrite(&hdr, sizeof(hdr), 1, pFile);
fwrite(blocks.data(), 16, blocks.size(), pFile);
if (fclose(pFile) == EOF)
return false;
return true;
}
bool load_astc_file(const char* pFilename, block16_vec& blocks, uint32_t& width, uint32_t& height, uint32_t& block_width, uint32_t& block_height)
{
FILE* pFile = nullptr;
#ifdef _MSC_VER
fopen_s(&pFile, pFilename, "rb");
#else
pFile = fopen(pFilename, "rb");
#endif
if (!pFile)
return false;
astc_header hdr;
if (fread(&hdr, sizeof(hdr), 1, pFile) != 1)
{
fclose(pFile);
return false;
}
if (hdr.m_sig != ASTC_SIG)
{
fclose(pFile);
return false;
}
width = hdr.m_width[0] + (hdr.m_width[1] << 8) + (hdr.m_width[2] << 16);
height = hdr.m_height[0] + (hdr.m_height[1] << 8) + (hdr.m_height[2] << 16);
uint32_t depth = hdr.m_depth[0] + (hdr.m_depth[1] << 8) + (hdr.m_depth[2] << 16);
if ((width < 1) || (width > 32768) || (height < 1) || (height > 32768))
return false;
if ((hdr.m_block_z != 1) || (depth != 1))
return false;
block_width = hdr.m_block_x;
block_height = hdr.m_block_y;
if ((block_width < 4) || (block_width > 12) || (block_height < 4) || (block_height > 12))
return false;
uint32_t blocks_x = (width + block_width - 1) / block_width;
uint32_t blocks_y = (height + block_height - 1) / block_height;
uint32_t total_blocks = blocks_x * blocks_y;
blocks.resize(total_blocks);
if (fread(blocks.data(), 16, total_blocks, pFile) != total_blocks)
{
fclose(pFile);
return false;
}
fclose(pFile);
return true;
}
#if 0
uint32_t get_deflate_size(const void* pData, size_t data_size)
{
size_t comp_size = 0;
void* pPre_RDO_Comp_data = tdefl_compress_mem_to_heap(pData, data_size, &comp_size, TDEFL_MAX_PROBES_MASK);// TDEFL_DEFAULT_MAX_PROBES);
mz_free(pPre_RDO_Comp_data);
if (comp_size > UINT32_MAX)
return UINT32_MAX;
return (uint32_t)comp_size;
}
#endif
bool read_file(const char* pFilename, uint8_vec& buf)
{
buf.resize(0);
FILE* pFile = nullptr;
#if _MSC_VER
fopen_s(&pFile, pFilename, "rb");
#else
pFile = fopen(pFilename, "rb");
#endif
if (!pFile)
return false;
fseek(pFile, 0, SEEK_END);
long file_end_ofs = ftell(pFile);
if (file_end_ofs <= 0)
{
fclose(pFile);
return false;
}
size_t sz = static_cast<size_t>(file_end_ofs);
if (sz != (unsigned long)file_end_ofs)
{
fclose(pFile);
return false;
}
fseek(pFile, 0, SEEK_SET);
buf.resize(sz);
if (fread(buf.data(), sizeof(uint8_t), sz, pFile) != sz)
{
fclose(pFile);
return false;
}
fclose(pFile);
return true;
}
} // namespace utils

2621
example_transcoding/utils.h Normal file

File diff suppressed because it is too large Load Diff