SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
ImageConversion.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstddef>
5#include <span>
6#include <vector>
7
8#include <BufferStream.h>
9
10#include "ImageFormats.h"
11
13
14// Negative means use default quality
15// BC7, BC6H: 0.1
16// Everything else: 1.0
17constexpr float DEFAULT_COMPRESSED_QUALITY = -1.f;
18
20[[nodiscard]] std::vector<std::byte> convertImageDataToFormat(std::span<const std::byte> imageData, ImageFormat oldFormat, ImageFormat newFormat, uint16_t width, uint16_t height, float quality = DEFAULT_COMPRESSED_QUALITY);
21
23[[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 depth, float quality = DEFAULT_COMPRESSED_QUALITY);
24
30[[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, bool skybox = false);
31
33[[nodiscard]] std::vector<std::byte> compressBGRA8888HDR(std::span<const std::byte> imageData, float overbrightFactor = 16.f);
34
36[[nodiscard]] std::vector<std::byte> decompressBGRA8888HDR(std::span<const std::byte> imageData, float overbrightFactor = 16.f);
37
39[[nodiscard]] std::vector<std::byte> compressRGBA16161616HDR(std::span<const std::byte> imageData, bool flipExponentAndSignificand = false);
40
42[[nodiscard]] std::vector<std::byte> decompressRGBA16161616HDR(std::span<const std::byte> imageData, bool flipExponentAndSignificand = false);
43
44enum class FileFormat {
46 PNG = 1,
47 JPG = 2,
48 BMP = 3,
49 TGA = 4,
50#ifdef VTFPP_SUPPORT_WEBP
51 WEBP = 5,
52#endif
53#ifdef VTFPP_SUPPORT_QOI
54 QOI = 6,
55#endif
56 HDR = 7,
57#ifdef VTFPP_SUPPORT_EXR
58 EXR = 8,
59#endif
60};
61
64
66[[nodiscard]] std::vector<std::byte> convertImageDataToFile(std::span<const std::byte> imageData, ImageFormat format, uint16_t width, uint16_t height, FileFormat fileFormat = FileFormat::DEFAULT);
67
68[[nodiscard]] std::vector<std::byte> convertFileToImageData(std::span<const std::byte> fileData, ImageFormat& format, int& width, int& height, int& frameCount);
69
70enum class ResizeEdge {
71 // Matches stbir_edge
72 CLAMP = 0,
76};
77
78enum class ResizeFilter {
79 // Matches stbir_filter
87
88 // User-defined
89 KAISER = 100,
91 NICE = 101,
92};
93
100
102 uint16_t resizeMinWidth = 0;
103 uint16_t resizeMaxWidth = 0;
104 uint16_t resizeMinHeight = 0;
105 uint16_t resizeMaxHeight = 0;
106
107 ResizeBounds() = default;
108 explicit ResizeBounds(uint16_t size) : resizeMinWidth{size}, resizeMaxWidth{size}, resizeMinHeight{size}, resizeMaxHeight{size} {}
109 ResizeBounds(uint16_t width, uint16_t height) : resizeMinWidth{width}, resizeMaxWidth{width}, resizeMinHeight{height}, resizeMaxHeight{height} {}
110 ResizeBounds(uint16_t minWidth, uint16_t maxWidth, uint16_t minHeight, uint16_t maxHeight) : resizeMinWidth{minWidth}, resizeMaxWidth{maxWidth}, resizeMinHeight{minHeight}, resizeMaxHeight{maxHeight} {}
111
112 [[nodiscard]] std::pair<uint16_t, uint16_t> clamp(uint16_t width, uint16_t height) const;
113};
114
116[[nodiscard]] uint16_t getResizedDim(uint16_t n, ResizeMethod method);
117
119void setResizedDims(uint16_t& width, ResizeMethod widthResize, uint16_t& height, ResizeMethod heightResize);
120
122[[nodiscard]] 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);
123
125[[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);
126
128[[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);
129
131[[nodiscard]] std::vector<std::byte> padImageData(std::span<const std::byte> imageData, ImageFormat format, uint16_t width, uint16_t widthPad, uint16_t height, uint16_t heightPad);
132
134void gammaCorrectImageData(std::span<std::byte> imageData, ImageFormat format, uint16_t width, uint16_t height, float gamma);
135
137void invertGreenChannelForImageData(std::span<std::byte> imageData, ImageFormat format, uint16_t width, uint16_t height);
138
140void hableTonemapImageData(std::span<std::byte> imageData, ImageFormat format, uint16_t width, uint16_t height);
141
142} // namespace vtfpp::ImageConversion
void invertGreenChannelForImageData(std::span< std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height)
Invert the green channel on the given image data. Meant for converting normal maps between OpenGL and...
std::vector< std::byte > convertFileToImageData(std::span< const std::byte > fileData, ImageFormat &format, int &width, int &height, int &frameCount)
std::vector< std::byte > compressBGRA8888HDR(std::span< const std::byte > imageData, float overbrightFactor=16.f)
Takes in RGBA32323232F format image data, returns SOURCEPP_BGRA8888_HDR compressed HDR image data (al...
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.
void hableTonemapImageData(std::span< std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height)
Perform Hable tonemapping on the given image data.
std::vector< std::byte > convertImageDataToFormat(std::span< const std::byte > imageData, ImageFormat oldFormat, ImageFormat newFormat, uint16_t width, uint16_t height, float quality=DEFAULT_COMPRESSED_QUALITY)
Converts an image from one format to another.
uint16_t getResizedDim(uint16_t n, ResizeMethod method)
Get the new image size given a resize method.
constexpr float DEFAULT_COMPRESSED_QUALITY
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 depth, float quality=DEFAULT_COMPRESSED_QUALITY)
Converts several images from one format to another.
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, bool skybox=false)
Converts an HDRI into six cubemap (or skybox) faces.
@ NICE
Valve NICE filtering, equivalent to Lanczos-3.
FileFormat getDefaultFileFormatForImageFormat(ImageFormat format)
PNG for integer formats, EXR for floating point formats (or HDR if EXR support is disabled).
std::vector< std::byte > decompressBGRA8888HDR(std::span< const std::byte > imageData, float overbrightFactor=16.f)
Takes in SOURCEPP_BGRA8888_HDR compressed HDR image data (alias for BGRA8888), returns RGBA32323232F ...
std::vector< std::byte > padImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t widthPad, uint16_t height, uint16_t heightPad)
Pad the given image with pixels that are the same color as the edge. Padding is applied to the right ...
std::vector< std::byte > compressRGBA16161616HDR(std::span< const std::byte > imageData, bool flipExponentAndSignificand=false)
Takes in RGBA32323232F format image data, returns SOURCEPP_RGBA16161616_HDR compressed HDR image data...
void gammaCorrectImageData(std::span< std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, float gamma)
Perform gamma correction on the given image data. Will not perform gamma correction if the input imag...
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 > 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 > decompressRGBA16161616HDR(std::span< const std::byte > imageData, bool flipExponentAndSignificand=false)
Takes in SOURCEPP_RGBA16161616_HDR compressed HDR image data (alias for RGBA16161616),...
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.
ResizeBounds(uint16_t width, uint16_t height)
ResizeBounds(uint16_t minWidth, uint16_t maxWidth, uint16_t minHeight, uint16_t maxHeight)
std::pair< uint16_t, uint16_t > clamp(uint16_t width, uint16_t height) const