Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions include/vtfpp/ImageConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ constexpr float DEFAULT_COMPRESSED_QUALITY = -1.f;
/// Fails (returns empty vectors) if the input data is empty, the given width is not 2x the height, or an error was encountered.
[[nodiscard]] std::array<std::vector<std::byte>, 6> convertHDRIToCubeMap(std::span<const std::byte> imageData, ImageFormat format, uint16_t width, uint16_t height, uint16_t resolution = 0, bool bilinear = true);

/// Takes in RGBA32323232F format image data, returns SOURCEPP_BGRA8888_HDR compressed HDR image data (alias for BGRA8888)
[[nodiscard]] std::vector<std::byte> compressBGRA8888HDR(std::span<const std::byte> imageData, float overbrightFactor = 16.f);

/// Takes in SOURCEPP_BGRA8888_HDR compressed HDR image data (alias for BGRA8888), returns RGBA32323232F format image data
[[nodiscard]] std::vector<std::byte> decompressBGRA8888HDR(std::span<const std::byte> imageData, float overbrightFactor = 16.f);

/// Takes in RGBA32323232F format image data, returns SOURCEPP_RGBA16161616_HDR compressed HDR image data (alias for RGBA16161616)
[[nodiscard]] std::vector<std::byte> compressRGBA16161616HDR(std::span<const std::byte> imageData, bool flipExponentAndSignificand = false);

/// Takes in SOURCEPP_RGBA16161616_HDR compressed HDR image data (alias for RGBA16161616), returns RGBA32323232F format image data
[[nodiscard]] std::vector<std::byte> decompressRGBA16161616HDR(std::span<const std::byte> imageData, bool flipExponentAndSignificand = false);

enum class FileFormat {
DEFAULT = 0,
PNG = 1,
Expand Down
94 changes: 92 additions & 2 deletions include/vtfpp/ImageFormats.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,12 @@ enum class ImageFormat : int32_t {
STRATA_BC7,
STRATA_BC6H,
// endregion

// region SourcePP Virtual Formats
SOURCEPP_BGRA8888_HDR = 10000,
SOURCEPP_RGBA16161616_HDR,
SOURCEPP_CONSOLE_RGBA16161616_HDR,
// endregion
};

namespace ImageFormatDetails {
Expand Down Expand Up @@ -157,6 +163,9 @@ namespace ImageFormatDetails {
case TFALL2_BC7:
case STRATA_BC7:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return -1;
}
return 0;
Expand All @@ -182,6 +191,9 @@ namespace ImageFormatDetails {
return 8;
case TFALL2_BC6H:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return 16;
default:
break;
Expand Down Expand Up @@ -262,6 +274,9 @@ namespace ImageFormatDetails {
case TFALL2_BC7:
case STRATA_BC7:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return -1;
}
return 0;
Expand All @@ -287,6 +302,9 @@ namespace ImageFormatDetails {
return 8;
case TFALL2_BC6H:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return 16;
default:
break;
Expand Down Expand Up @@ -366,6 +384,9 @@ namespace ImageFormatDetails {
case TFALL2_BC7:
case STRATA_BC7:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return -1;
}
return 0;
Expand All @@ -391,6 +412,9 @@ namespace ImageFormatDetails {
return 8;
case TFALL2_BC6H:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return 16;
default:
break;
Expand Down Expand Up @@ -470,6 +494,9 @@ namespace ImageFormatDetails {
case TFALL2_BC7:
case STRATA_BC7:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return -1;
}
return 0;
Expand Down Expand Up @@ -497,6 +524,9 @@ namespace ImageFormatDetails {
case ATI1N:
case TFALL2_BC6H:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return 0;
default:
break;
Expand All @@ -520,6 +550,8 @@ namespace ImageFormatDetails {
case RGBA16161616F:
case RGBA16161616:
case CONSOLE_RGBA16161616_LINEAR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
case RG3232F:
return 64;
case RGBA8888:
Expand All @@ -531,6 +563,7 @@ namespace ImageFormatDetails {
case BGRA8888:
case CONSOLE_BGRA8888_LINEAR:
case CONSOLE_BGRA8888_LE:
case SOURCEPP_BGRA8888_HDR:
case BGRX8888:
case CONSOLE_BGRX8888_LINEAR:
case CONSOLE_BGRX8888_LE:
Expand Down Expand Up @@ -602,6 +635,9 @@ namespace ImageFormatDetails {
case RGBA32323232F:
case TFALL2_BC6H:
case STRATA_BC6H:
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return RGBA32323232F;
case RGBA16161616:
case CONSOLE_RGBA16161616_LINEAR:
Expand Down Expand Up @@ -676,14 +712,32 @@ namespace ImageFormatDetails {
}

/**
* Check if the given format is a compressed format (DXT1, DXT3, DXT5, ATI1N, ATI2N, BC7, BC6H).
* Check if the given format is a compressed format (DXT1, DXT3, DXT5, ATI1N, ATI2N, BC7, BC6H, BGRA8888 HDR, RGBA16161616 HDR).
* @param format The format to check.
* @return True if the given format is compressed.
*/
[[nodiscard]] constexpr bool compressed(ImageFormat format) {
return red(format) == -1;
}

/**
* Check if the given format is a compressed HDR format (not counting BC6H).
* @param format The format to check.
* @return True if the format is a compressed HDR format..
*/
[[nodiscard]] constexpr bool compressedHDR(ImageFormat format) {
switch (format) {
using enum ImageFormat;
case SOURCEPP_BGRA8888_HDR:
case SOURCEPP_RGBA16161616_HDR:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return true;
default:
break;
}
return false;
}

/**
* Check if the given format can store transparency.
* @param format The format to check.
Expand Down Expand Up @@ -757,6 +811,42 @@ namespace ImageFormatDetails {
case CONSOLE_RGBA16161616_LINEAR:
case CONSOLE_BGRX8888_LE:
case CONSOLE_BGRA8888_LE:
case SOURCEPP_CONSOLE_RGBA16161616_HDR:
return true;
default:
break;
}
return false;
}

/**
* Check if the given format is exclusively used by Titanfall 2.
* @param format The format to check.
* @return True if the format is exclusively used by Titanfall 2.
*/
[[nodiscard]] constexpr bool tfall2(ImageFormat format) {
switch (format) {
using enum ImageFormat;
case TFALL2_BC6H:
case TFALL2_BC7:
return true;
default:
break;
}
return false;
}

/**
* Check if the given format is exclusively used by Strata Source.
* @param format The format to check.
* @return True if the format is exclusively used by Strata Source.
*/
[[nodiscard]] constexpr bool strata(ImageFormat format) {
switch (format) {
using enum ImageFormat;
case STRATA_R8:
case STRATA_BC7:
case STRATA_BC6H:
return true;
default:
break;
Expand Down Expand Up @@ -869,7 +959,7 @@ namespace ImageFormatDetails {
* @return The length in bytes of a texture containing the given format, width, height, and depth.
*/
[[nodiscard]] constexpr uint32_t getDataLength(ImageFormat format, uint16_t width, uint16_t height, uint16_t depth = 1) {
if (ImageFormatDetails::compressed(format)) {
if (ImageFormatDetails::compressed(format) && !ImageFormatDetails::compressedHDR(format)) {
return ((width + 3) / 4) * ((height + 3) / 4) * depth * bpp(format) * 2;
}
return width * height * depth * (bpp(format) / 8);
Expand Down
26 changes: 25 additions & 1 deletion include/vtfpp/ImagePixel.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,27 @@ VTFPP_FORMAT(
VTFPP_R
);

VTFPP_FORMAT(
SOURCEPP_BGRA8888_HDR,
VTFPP_FORMAT_LE(uint8_t r, g, b, a)
VTFPP_FORMAT_BE(uint8_t a, b, g, r),
VTFPP_R VTFPP_G VTFPP_B VTFPP_A
);

VTFPP_FORMAT(
SOURCEPP_RGBA16161616_HDR,
VTFPP_FORMAT_LE(uint16_t r, g, b, a)
VTFPP_FORMAT_BE(uint16_t a, b, g, r),
VTFPP_R VTFPP_G VTFPP_B VTFPP_A
);

VTFPP_FORMAT(
SOURCEPP_CONSOLE_RGBA16161616_HDR,
VTFPP_FORMAT_LE(uint16_t r, g, b, a)
VTFPP_FORMAT_BE(uint16_t a, b, g, r),
VTFPP_R VTFPP_G VTFPP_B VTFPP_A
);

#undef VTFPP_FORMAT
#undef VTFPP_FORMAT_BE
#undef VTFPP_FORMAT_LE
Expand Down Expand Up @@ -487,7 +508,10 @@ concept PixelType =
std::same_as<T, CONSOLE_RGBA16161616_LINEAR> ||
std::same_as<T, CONSOLE_BGRX8888_LE> ||
std::same_as<T, CONSOLE_BGRA8888_LE> ||
std::same_as<T, STRATA_R8>;
std::same_as<T, STRATA_R8> ||
std::same_as<T, SOURCEPP_BGRA8888_HDR> ||
std::same_as<T, SOURCEPP_RGBA16161616_HDR> ||
std::same_as<T, SOURCEPP_CONSOLE_RGBA16161616_HDR>;

/// Extracts a single channel from the given image data.
/// May have unexpected behavior if called on formats that use bitfields like BGRA5551!
Expand Down
4 changes: 2 additions & 2 deletions include/vtfpp/VTF.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ class VTF {

VTF();

explicit VTF(std::vector<std::byte>&& vtfData, bool parseHeaderOnly = false);
explicit VTF(std::vector<std::byte>&& vtfData, bool parseHeaderOnly = false, bool hdr = false);

explicit VTF(std::span<const std::byte> vtfData, bool parseHeaderOnly = false);
explicit VTF(std::span<const std::byte> vtfData, bool parseHeaderOnly = false, bool hdr = false);

explicit VTF(const std::filesystem::path& vtfPath, bool parseHeaderOnly = false);

Expand Down
7 changes: 5 additions & 2 deletions lang/c/include/vtfppc/ImageConversion.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@ VTFPP_EXTERNVAR const float VTFPP_IMAGE_CONVERSION_DEFAULT_COMPRESSED_QUALITY;

VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_convert_image_data_to_format(const unsigned char* buffer, size_t bufferLen, vtfpp_image_format_e oldFormat, vtfpp_image_format_e newFormat, uint16_t width, uint16_t height, float quality); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_convert_several_image_data_to_format(const unsigned char* buffer, size_t bufferLen, vtfpp_image_format_e oldFormat, vtfpp_image_format_e newFormat, uint8_t mipCount, uint16_t frameCount, uint8_t faceCount, uint16_t width, uint16_t height, uint16_t depth, float quality); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_convert_hdri_to_cubemap(const unsigned char* buffer, size_t bufferLen, vtfpp_image_format_e format, uint16_t width, uint16_t height); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_convert_hdri_to_cubemap_ex(const unsigned char* buffer, size_t bufferLen, vtfpp_image_format_e format, uint16_t width, uint16_t height, uint16_t resolution, int bilinear); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_convert_hdri_to_cubemap(const unsigned char* buffer, size_t bufferLen, vtfpp_image_format_e format, uint16_t width, uint16_t height, uint16_t resolution, int bilinear); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_compress_bgra8888_hdr(const unsigned char* buffer, size_t bufferLen, float overbrightFactor); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_decompress_bgra8888_hdr(const unsigned char* buffer, size_t bufferLen, float overbrightFactor); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_compress_rgba16161616_hdr(const unsigned char* buffer, size_t bufferLen, int flipExponentAndSignificand); // REQUIRES MANUAL FREE: sourcepp_buffer_free
VTFPP_API sourcepp_buffer_t vtfpp_image_conversion_decompress_rgba16161616_hdr(const unsigned char* buffer, size_t bufferLen, int flipExponentAndSignificand); // REQUIRES MANUAL FREE: sourcepp_buffer_free

VTFPP_EXTERN typedef enum {
VTFPP_IMAGE_CONVERSION_FILE_FORMAT_DEFAULT = 0,
Expand Down
Loading
Loading