SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
ImageConversion.cpp File Reference
#include <vtfpp/ImageConversion.h>
#include <algorithm>
#include <bit>
#include <cstdlib>
#include <cstring>
#include <memory>
#include <span>
#include <string_view>
#include <unordered_map>
#include <Compressonator.h>
#include <sourcepp/Macros.h>
#include <sourcepp/MathExtended.h>
#include <stb_image.h>
#include <stb_image_resize2.h>
#include <stb_image_write.h>
#include <tinyexr.h>
Include dependency graph for ImageConversion.cpp:

Go to the source code of this file.

Macros

#define STB_IMAGE_IMPLEMENTATION
 
#define STB_IMAGE_STATIC
 
#define STBI_NO_FAILURE_STRINGS
 
#define STBI_NO_STDIO
 
#define STB_IMAGE_RESIZE_IMPLEMENTATION
 
#define STB_IMAGE_RESIZE_STATIC
 
#define STB_IMAGE_WRITE_IMPLEMENTATION
 
#define STB_IMAGE_WRITE_STATIC
 
#define STBI_WRITE_NO_STDIO
 
#define TINYEXR_IMPLEMENTATION   1
 
#define TINYEXR_USE_THREAD   0
 
#define VTFPP_REMAP_TO_8(value, shift)   math::remap<uint8_t>((value), (1 << (shift)) - 1, (1 << 8) - 1)
 
#define VTFPP_CONVERT_DETAIL(InputType, r, g, b, a, ...)
 
#define VTFPP_CONVERT(InputType, r, g, b, a)   VTFPP_CONVERT_DETAIL(InputType, r, g, b, a)
 
#define VTFPP_CASE_CONVERT_AND_BREAK(InputType, r, g, b, a)    case InputType: { VTFPP_CONVERT(InputType, r, g, b, a); } break
 
#define VTFPP_REMAP_FROM_8(value, shift)   math::remap<uint8_t>((value), (1 << 8) - 1, (1 << (shift)) - 1)
 
#define VTFPP_CONVERT(InputType, ...)
 
#define VTFPP_CASE_CONVERT_AND_BREAK(InputType, ...)    case InputType: { VTFPP_CONVERT(InputType, __VA_ARGS__); } break
 
#define VTFPP_REMAP_TO_16(value, shift)   math::remap<uint16_t>((value), (1 << (shift)) - 1, (1 << 16) - 1)
 
#define VTFPP_CONVERT_DETAIL(InputType, r, g, b, a, ...)
 
#define VTFPP_CONVERT(InputType, r, g, b, a)   VTFPP_CONVERT_DETAIL(InputType, r, g, b, a)
 
#define VTFPP_CASE_CONVERT_AND_BREAK(InputType, r, g, b, a)    case InputType: { VTFPP_CONVERT(InputType, r, g, b, a); } break
 
#define VTFPP_CONVERT_REMAP(InputType, r, g, b, a)
 
#define VTFPP_CASE_CONVERT_REMAP_AND_BREAK(InputType, r, g, b, a)
 
#define VTFPP_REMAP_FROM_16(value, shift)   static_cast<uint8_t>(math::remap<uint16_t>((value), (1 << 16) - 1, (1 << (shift)) - 1))
 
#define VTFPP_CONVERT(InputType, ...)
 
#define VTFPP_CASE_CONVERT_AND_BREAK(InputType, ...)    case InputType: { VTFPP_CONVERT(InputType, __VA_ARGS__); } break
 
#define VTFPP_CONVERT_DETAIL(InputType, r, g, b, a, ...)
 
#define VTFPP_CONVERT(InputType, r, g, b, a)   VTFPP_CONVERT_DETAIL(InputType, r, g, b, a)
 
#define VTFPP_CASE_CONVERT_AND_BREAK(InputType, r, g, b, a)    case InputType: { VTFPP_CONVERT(InputType, r, g, b, a); } break
 
#define VTFPP_CONVERT(InputType, ...)
 
#define VTFPP_CASE_CONVERT_AND_BREAK(InputType, ...)    case InputType: { VTFPP_CONVERT(InputType, __VA_ARGS__); } break
 

Macro Definition Documentation

◆ STB_IMAGE_IMPLEMENTATION

#define STB_IMAGE_IMPLEMENTATION

Definition at line 26 of file ImageConversion.cpp.

◆ STB_IMAGE_RESIZE_IMPLEMENTATION

#define STB_IMAGE_RESIZE_IMPLEMENTATION

Definition at line 32 of file ImageConversion.cpp.

◆ STB_IMAGE_RESIZE_STATIC

#define STB_IMAGE_RESIZE_STATIC

Definition at line 33 of file ImageConversion.cpp.

◆ STB_IMAGE_STATIC

#define STB_IMAGE_STATIC

Definition at line 27 of file ImageConversion.cpp.

◆ STB_IMAGE_WRITE_IMPLEMENTATION

#define STB_IMAGE_WRITE_IMPLEMENTATION

Definition at line 36 of file ImageConversion.cpp.

◆ STB_IMAGE_WRITE_STATIC

#define STB_IMAGE_WRITE_STATIC

Definition at line 37 of file ImageConversion.cpp.

◆ STBI_NO_FAILURE_STRINGS

#define STBI_NO_FAILURE_STRINGS

Definition at line 28 of file ImageConversion.cpp.

◆ STBI_NO_STDIO

#define STBI_NO_STDIO

Definition at line 29 of file ImageConversion.cpp.

◆ STBI_WRITE_NO_STDIO

#define STBI_WRITE_NO_STDIO

Definition at line 38 of file ImageConversion.cpp.

◆ TINYEXR_IMPLEMENTATION

#define TINYEXR_IMPLEMENTATION   1

Definition at line 41 of file ImageConversion.cpp.

◆ TINYEXR_USE_THREAD

#define TINYEXR_USE_THREAD   0

Definition at line 45 of file ImageConversion.cpp.

◆ VTFPP_CASE_CONVERT_AND_BREAK [1/6]

#define VTFPP_CASE_CONVERT_AND_BREAK (   InputType,
  r,
  g,
  b,
 
)     case InputType: { VTFPP_CONVERT(InputType, r, g, b, a); } break

◆ VTFPP_CASE_CONVERT_AND_BREAK [2/6]

#define VTFPP_CASE_CONVERT_AND_BREAK (   InputType,
  r,
  g,
  b,
 
)     case InputType: { VTFPP_CONVERT(InputType, r, g, b, a); } break

◆ VTFPP_CASE_CONVERT_AND_BREAK [3/6]

#define VTFPP_CASE_CONVERT_AND_BREAK (   InputType,
  r,
  g,
  b,
 
)     case InputType: { VTFPP_CONVERT(InputType, r, g, b, a); } break

◆ VTFPP_CASE_CONVERT_AND_BREAK [4/6]

#define VTFPP_CASE_CONVERT_AND_BREAK (   InputType,
  ... 
)     case InputType: { VTFPP_CONVERT(InputType, __VA_ARGS__); } break

◆ VTFPP_CASE_CONVERT_AND_BREAK [5/6]

#define VTFPP_CASE_CONVERT_AND_BREAK (   InputType,
  ... 
)     case InputType: { VTFPP_CONVERT(InputType, __VA_ARGS__); } break

◆ VTFPP_CASE_CONVERT_AND_BREAK [6/6]

#define VTFPP_CASE_CONVERT_AND_BREAK (   InputType,
  ... 
)     case InputType: { VTFPP_CONVERT(InputType, __VA_ARGS__); } break

◆ VTFPP_CASE_CONVERT_REMAP_AND_BREAK

#define VTFPP_CASE_CONVERT_REMAP_AND_BREAK (   InputType,
  r,
  g,
  b,
 
)
Value:
case InputType: { VTFPP_CONVERT_REMAP(InputType, r, g, b, a); } \
break
#define VTFPP_CONVERT_REMAP(InputType, r, g, b, a)

◆ VTFPP_CONVERT [1/6]

#define VTFPP_CONVERT (   InputType,
  r,
  g,
  b,
 
)    VTFPP_CONVERT_DETAIL(InputType, r, g, b, a)

◆ VTFPP_CONVERT [2/6]

#define VTFPP_CONVERT (   InputType,
  r,
  g,
  b,
 
)    VTFPP_CONVERT_DETAIL(InputType, r, g, b, a)

◆ VTFPP_CONVERT [3/6]

#define VTFPP_CONVERT (   InputType,
  r,
  g,
  b,
 
)    VTFPP_CONVERT_DETAIL(InputType, r, g, b, a)

◆ VTFPP_CONVERT [4/6]

#define VTFPP_CONVERT (   InputType,
  ... 
)
Value:
std::span<ImagePixel::InputType> newDataSpan{reinterpret_cast<ImagePixel::InputType*>(newData.data()), newData.size() / sizeof(ImagePixel::InputType)}; \
std::transform(imageDataSpan.begin(), imageDataSpan.end(), newDataSpan.begin(), [](ImagePixel::RGBA8888 pixel) -> ImagePixel::InputType { \
return __VA_ARGS__; \
})

◆ VTFPP_CONVERT [5/6]

#define VTFPP_CONVERT (   InputType,
  ... 
)
Value:
std::span<ImagePixel::InputType> newDataSpan{reinterpret_cast<ImagePixel::InputType*>(newData.data()), newData.size() / sizeof(ImagePixel::InputType)}; \
std::transform(imageDataSpan.begin(), imageDataSpan.end(), newDataSpan.begin(), [](ImagePixel::RGBA16161616 pixel) -> ImagePixel::InputType { \
return __VA_ARGS__; \
})

◆ VTFPP_CONVERT [6/6]

#define VTFPP_CONVERT (   InputType,
  ... 
)
Value:
std::span<ImagePixel::InputType> newDataSpan{reinterpret_cast<ImagePixel::InputType*>(newData.data()), newData.size() / sizeof(ImagePixel::InputType)}; \
std::transform(imageDataSpan.begin(), imageDataSpan.end(), newDataSpan.begin(), [](ImagePixel::RGBA32323232F pixel) -> ImagePixel::InputType { \
return __VA_ARGS__; \
})

◆ VTFPP_CONVERT_DETAIL [1/3]

#define VTFPP_CONVERT_DETAIL (   InputType,
  r,
  g,
  b,
  a,
  ... 
)
Value:
std::span<const ImagePixel::InputType> imageDataSpan{reinterpret_cast<const ImagePixel::InputType*>(imageData.data()), imageData.size() / sizeof(ImagePixel::InputType)}; \
std::transform(__VA_ARGS__ __VA_OPT__(,) imageDataSpan.begin(), imageDataSpan.end(), newDataSpan.begin(), [](ImagePixel::InputType pixel) -> ImagePixel::RGBA8888 { \
return {(r), (g), (b), (a)}; \
})

◆ VTFPP_CONVERT_DETAIL [2/3]

#define VTFPP_CONVERT_DETAIL (   InputType,
  r,
  g,
  b,
  a,
  ... 
)
Value:
std::span<const ImagePixel::InputType> imageDataSpan{reinterpret_cast<const ImagePixel::InputType*>(imageData.data()), imageData.size() / sizeof(ImagePixel::InputType)}; \
std::transform(__VA_ARGS__ __VA_OPT__(,) imageDataSpan.begin(), imageDataSpan.end(), newDataSpan.begin(), [](ImagePixel::InputType pixel) -> ImagePixel::RGBA16161616 { \
return { static_cast<uint16_t>(r), static_cast<uint16_t>(g), static_cast<uint16_t>(b), static_cast<uint16_t>(a) }; \
})

◆ VTFPP_CONVERT_DETAIL [3/3]

#define VTFPP_CONVERT_DETAIL (   InputType,
  r,
  g,
  b,
  a,
  ... 
)
Value:
std::span<const ImagePixel::InputType> imageDataSpan{reinterpret_cast<const ImagePixel::InputType*>(imageData.data()), imageData.size() / sizeof(ImagePixel::InputType)}; \
std::transform(__VA_ARGS__ __VA_OPT__(,) imageDataSpan.begin(), imageDataSpan.end(), newDataSpan.begin(), [](ImagePixel::InputType pixel) -> ImagePixel::RGBA32323232F { return {(r), (g), (b), (a)}; })

◆ VTFPP_CONVERT_REMAP

#define VTFPP_CONVERT_REMAP (   InputType,
  r,
  g,
  b,
 
)
Value:
do { \
if constexpr (ImageFormatDetails::alpha(ImageFormat::InputType) > 1) { \
VTFPP_CONVERT(InputType, \
VTFPP_REMAP_TO_16((r), ImageFormatDetails::red(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((g), ImageFormatDetails::green(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((b), ImageFormatDetails::blue(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((a), ImageFormatDetails::alpha(ImageFormat::InputType))); \
} else if constexpr (ImageFormatDetails::alpha(ImageFormat::InputType) == 1) { \
VTFPP_CONVERT(InputType, \
VTFPP_REMAP_TO_16((r), ImageFormatDetails::red(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((g), ImageFormatDetails::green(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((b), ImageFormatDetails::blue(ImageFormat::InputType)), \
(a) * 0xff); \
} else { \
VTFPP_CONVERT(InputType, \
VTFPP_REMAP_TO_16((r), ImageFormatDetails::red(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((g), ImageFormatDetails::green(ImageFormat::InputType)), \
VTFPP_REMAP_TO_16((b), ImageFormatDetails::blue(ImageFormat::InputType)), \
(a)); \
} \
} while (false)
#define VTFPP_REMAP_TO_16(value, shift)
constexpr int8_t alpha(ImageFormat format)
Definition: ImageFormats.h:345
constexpr int8_t green(ImageFormat format)
Definition: ImageFormats.h:166
constexpr int8_t red(ImageFormat format)
Definition: ImageFormats.h:77
constexpr int8_t blue(ImageFormat format)
Definition: ImageFormats.h:256

◆ VTFPP_REMAP_FROM_16

#define VTFPP_REMAP_FROM_16 (   value,
  shift 
)    static_cast<uint8_t>(math::remap<uint16_t>((value), (1 << 16) - 1, (1 << (shift)) - 1))

◆ VTFPP_REMAP_FROM_8

#define VTFPP_REMAP_FROM_8 (   value,
  shift 
)    math::remap<uint8_t>((value), (1 << 8) - 1, (1 << (shift)) - 1)

◆ VTFPP_REMAP_TO_16

#define VTFPP_REMAP_TO_16 (   value,
  shift 
)    math::remap<uint16_t>((value), (1 << (shift)) - 1, (1 << 16) - 1)

◆ VTFPP_REMAP_TO_8

#define VTFPP_REMAP_TO_8 (   value,
  shift 
)    math::remap<uint8_t>((value), (1 << (shift)) - 1, (1 << 8) - 1)