9#include <BufferStream.h>
18#define VTFPP_CHECK_SIZE(format) \
19 static_assert(sizeof(format) == ImageFormatDetails::bpp(ImageFormat::format) / 8)
21#define VTFPP_FORMAT_INHERITED(format, parent) \
22 struct format : parent { \
23 static constexpr auto FORMAT = ImageFormat::format; \
25 VTFPP_CHECK_SIZE(format)
272#undef VTFPP_FORMAT_INHERITED
273#undef VTFPP_CHECK_SIZE
277 std::same_as<T, RGBA8888> ||
278 std::same_as<T, ABGR8888> ||
279 std::same_as<T, RGB888> ||
280 std::same_as<T, BGR888> ||
281 std::same_as<T, RGB565> ||
282 std::same_as<T, I8> ||
283 std::same_as<T, IA88> ||
284 std::same_as<T, P8> ||
285 std::same_as<T, A8> ||
286 std::same_as<T, RGB888_BLUESCREEN> ||
287 std::same_as<T, BGR888_BLUESCREEN> ||
288 std::same_as<T, ARGB8888> ||
289 std::same_as<T, BGRA8888> ||
290 std::same_as<T, BGRX8888> ||
291 std::same_as<T, BGR565> ||
292 std::same_as<T, BGRX5551> ||
293 std::same_as<T, BGRA4444> ||
294 std::same_as<T, BGRA5551> ||
295 std::same_as<T, UV88> ||
296 std::same_as<T, UVWQ8888> ||
297 std::same_as<T, RGBA16161616F> ||
298 std::same_as<T, RGBA16161616> ||
299 std::same_as<T, UVLX8888> ||
300 std::same_as<T, R32F> ||
301 std::same_as<T, RGB323232F> ||
302 std::same_as<T, RGBA32323232F> ||
303 std::same_as<T, RG1616F> ||
304 std::same_as<T, RG3232F> ||
305 std::same_as<T, RGBX8888> ||
306 std::same_as<T, RGBA1010102> ||
307 std::same_as<T, BGRA1010102> ||
308 std::same_as<T, R16F> ||
309 std::same_as<T, CONSOLE_BGRX8888_LINEAR> ||
310 std::same_as<T, CONSOLE_RGBA8888_LINEAR> ||
311 std::same_as<T, CONSOLE_ABGR8888_LINEAR> ||
312 std::same_as<T, CONSOLE_ARGB8888_LINEAR> ||
313 std::same_as<T, CONSOLE_BGRA8888_LINEAR> ||
314 std::same_as<T, CONSOLE_RGB888_LINEAR> ||
315 std::same_as<T, CONSOLE_BGR888_LINEAR> ||
316 std::same_as<T, CONSOLE_BGRX5551_LINEAR> ||
317 std::same_as<T, CONSOLE_I8_LINEAR> ||
318 std::same_as<T, CONSOLE_RGBA16161616_LINEAR> ||
319 std::same_as<T, CONSOLE_BGRX8888_LE> ||
320 std::same_as<T, CONSOLE_BGRA8888_LE> ||
325namespace ImageConversion {
331[[nodiscard]] std::vector<std::byte>
convertSeveralImageDataToFormat(std::span<const std::byte> imageData,
ImageFormat oldFormat,
ImageFormat newFormat, uint8_t mipCount, uint16_t frameCount, uint8_t faceCount, uint16_t width, uint16_t height, uint16_t sliceCount);
337[[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);
396[[nodiscard]] std::vector<std::byte>
resizeImageDataStrict(std::span<const std::byte> imageData,
ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t& widthOut,
ResizeMethod widthResize, uint16_t height, uint16_t newHeight, uint16_t& heightOut,
ResizeMethod heightResize,
bool srgb,
ResizeFilter filter,
ResizeEdge edge =
ResizeEdge::CLAMP);
399[[nodiscard]] std::vector<std::byte>
cropImageData(std::span<const std::byte> imageData,
ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t xOffset, uint16_t height, uint16_t newHeight, uint16_t yOffset);
405template<ImagePixel::PixelType P>
408 if (imageData.empty() || imageData.size() %
sizeof(P) != 0) {
412 std::span pixels{
reinterpret_cast<const P*
>(imageData.data()), imageData.size() /
sizeof(P)};
414 std::vector<std::byte> out(imageData.size() /
sizeof(P) *
sizeof(C));
415 BufferStream stream{out,
false};
416 for (
const auto& pixel : pixels) {
417 stream << pixel.*channel;
426template<ImagePixel::PixelType P>
429 if (imageData.empty() || imageData.size() %
sizeof(P) != 0 || channelData.empty() || channelData.size() %
sizeof(C) != 0 || imageData.size() /
sizeof(P) != channelData.size() /
sizeof(C)) {
433 std::span pixels{
reinterpret_cast<P*
>(imageData.data()), imageData.size() /
sizeof(P)};
434 std::span values{
reinterpret_cast<C*
>(channelData.data()), channelData.size() /
sizeof(C)};
436 for (
int i = 0; i < pixels.size(); i++) {
437 pixels[i].*channel = values[i];
#define VTFPP_CHECK_SIZE(format)
#define VTFPP_FORMAT_INHERITED(format, parent)
typename member_type< T >::type member_type_t
std::vector< std::byte > convertFileToImageData(std::span< const std::byte > fileData, ImageFormat &format, int &width, int &height, int &frameCount)
std::vector< std::byte > convertImageDataToFile(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, FileFormat fileFormat=FileFormat::DEFAULT)
Converts image data to the given file format (PNG or EXR by default).
void setResizedDims(uint16_t &width, ResizeMethod widthResize, uint16_t &height, ResizeMethod heightResize)
Set the new image dimensions given a resize method.
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)
Converts an HDRI into a cubemap.
uint16_t getResizedDim(uint16_t n, ResizeMethod method)
Get the new image size given a resize method.
FileFormat getDefaultFileFormatForImageFormat(ImageFormat format)
PNG for integer formats, EXR for floating point formats.
std::vector< std::byte > extractChannelFromImageData(std::span< const std::byte > imageData, auto P::*channel)
Extracts a single channel from the given image data.
std::vector< std::byte > cropImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t xOffset, uint16_t height, uint16_t newHeight, uint16_t yOffset)
Crops the given image to the new dimensions. If the image format is compressed it will be converted t...
std::vector< std::byte > convertSeveralImageDataToFormat(std::span< const std::byte > imageData, ImageFormat oldFormat, ImageFormat newFormat, uint8_t mipCount, uint16_t frameCount, uint8_t faceCount, uint16_t width, uint16_t height, uint16_t sliceCount)
Converts several images from one format to another.
std::vector< std::byte > resizeImageDataStrict(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t &widthOut, ResizeMethod widthResize, uint16_t height, uint16_t newHeight, uint16_t &heightOut, ResizeMethod heightResize, bool srgb, ResizeFilter filter, ResizeEdge edge=ResizeEdge::CLAMP)
Resize given image data to the new dimensions, where the new width and height are governed by the res...
std::vector< std::byte > convertImageDataToFormat(std::span< const std::byte > imageData, ImageFormat oldFormat, ImageFormat newFormat, uint16_t width, uint16_t height)
Converts an image from one format to another.
bool applyChannelToImageData(std::span< std::byte > imageData, std::span< const std::byte > channelData, auto P::*channel)
Applies a single channel to the given image data.
std::vector< std::byte > resizeImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t height, uint16_t newHeight, bool srgb, ResizeFilter filter, ResizeEdge edge=ResizeEdge::CLAMP)
Resize given image data to the new dimensions.
@ CONSOLE_ARGB8888_LINEAR
@ CONSOLE_BGRX8888_LINEAR
@ CONSOLE_RGBA8888_LINEAR
@ CONSOLE_ABGR8888_LINEAR
@ CONSOLE_BGRX5551_LINEAR
@ CONSOLE_BGRA8888_LINEAR
@ CONSOLE_RGBA16161616_LINEAR
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT
static constexpr auto FORMAT