Skip to content

Commit bc3af5c

Browse files
feat(vtfpp): accelerate conversion functions
1 parent e3461bd commit bc3af5c

File tree

12 files changed

+1219
-865
lines changed

12 files changed

+1219
-865
lines changed

cmake/AddPrettyParser.cmake

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ function(add_pretty_parser TARGET)
2323
# Define DEBUG macro
2424
target_compile_definitions(${TARGET} PRIVATE "$<$<CONFIG:Debug>:DEBUG>")
2525

26+
# MSVC on its bullshit again
27+
if(MSVC)
28+
target_compile_options(${TARGET} PRIVATE "/Zc:preprocessor")
29+
endif()
30+
2631
# Set optimization flags
2732
if(CMAKE_BUILD_TYPE MATCHES "Debug")
2833
# Build with debug friendly optimizations and debug symbols (MSVC defaults are fine)

include/vtfpp/ImageConversion.h

Lines changed: 191 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,196 @@
44
#include <span>
55
#include <vector>
66

7+
#include <sourcepp/math/Float.h>
8+
79
#include "ImageFormats.h"
810

9-
namespace vtfpp::ImageConversion {
11+
namespace vtfpp {
12+
13+
namespace ImagePixel {
14+
15+
struct RGBA8888 {
16+
static constexpr auto FORMAT = ImageFormat::RGBA8888;
17+
uint8_t r;
18+
uint8_t g;
19+
uint8_t b;
20+
uint8_t a;
21+
};
22+
23+
struct ABGR8888 {
24+
static constexpr auto FORMAT = ImageFormat::ABGR8888;
25+
uint8_t a;
26+
uint8_t b;
27+
uint8_t g;
28+
uint8_t r;
29+
};
30+
31+
struct RGB888 {
32+
static constexpr auto FORMAT = ImageFormat::RGB888;
33+
uint8_t r;
34+
uint8_t g;
35+
uint8_t b;
36+
};
37+
38+
struct RGB888_BLUESCREEN : public RGB888 {
39+
static constexpr auto FORMAT = ImageFormat::RGB888_BLUESCREEN;
40+
};
41+
42+
struct BGR888 {
43+
static constexpr auto FORMAT = ImageFormat::BGR888;
44+
uint8_t b;
45+
uint8_t g;
46+
uint8_t r;
47+
};
48+
49+
struct BGR888_BLUESCREEN : public BGR888 {
50+
static constexpr auto FORMAT = ImageFormat::BGR888_BLUESCREEN;
51+
};
52+
53+
struct RGB565 {
54+
static constexpr auto FORMAT = ImageFormat::RGB565;
55+
uint16_t r : 5;
56+
uint16_t g : 6;
57+
uint16_t b : 5;
58+
};
59+
60+
struct I8 {
61+
static constexpr auto FORMAT = ImageFormat::I8;
62+
uint8_t i;
63+
};
64+
65+
struct IA88 {
66+
static constexpr auto FORMAT = ImageFormat::IA88;
67+
uint8_t i;
68+
uint8_t a;
69+
};
70+
71+
struct P8 {
72+
static constexpr auto FORMAT = ImageFormat::P8;
73+
uint8_t p;
74+
};
75+
76+
struct A8 {
77+
static constexpr auto FORMAT = ImageFormat::A8;
78+
uint8_t a;
79+
};
80+
81+
struct ARGB8888 {
82+
static constexpr auto FORMAT = ImageFormat::ARGB8888;
83+
uint8_t a;
84+
uint8_t r;
85+
uint8_t g;
86+
uint8_t b;
87+
};
88+
89+
struct BGRA8888 {
90+
static constexpr auto FORMAT = ImageFormat::BGRA8888;
91+
uint8_t b;
92+
uint8_t g;
93+
uint8_t r;
94+
uint8_t a;
95+
};
96+
97+
struct BGRX8888 {
98+
static constexpr auto FORMAT = ImageFormat::BGRX8888;
99+
uint8_t b;
100+
uint8_t g;
101+
uint8_t r;
102+
uint8_t x;
103+
};
104+
105+
struct BGR565 {
106+
static constexpr auto FORMAT = ImageFormat::BGR565;
107+
uint16_t b : 5;
108+
uint16_t g : 6;
109+
uint16_t r : 5;
110+
};
111+
112+
struct BGRX5551 {
113+
static constexpr auto FORMAT = ImageFormat::BGRX5551;
114+
uint16_t b : 5;
115+
uint16_t g : 5;
116+
uint16_t r : 5;
117+
uint16_t x : 1;
118+
};
119+
120+
struct BGRA4444 {
121+
static constexpr auto FORMAT = ImageFormat::BGRA4444;
122+
uint16_t b : 4;
123+
uint16_t g : 4;
124+
uint16_t r : 4;
125+
uint16_t a : 4;
126+
};
127+
128+
struct BGRA5551 {
129+
static constexpr auto FORMAT = ImageFormat::BGRA5551;
130+
uint16_t b : 5;
131+
uint16_t g : 5;
132+
uint16_t r : 5;
133+
uint16_t a : 1;
134+
};
135+
136+
struct UV88 {
137+
static constexpr auto FORMAT = ImageFormat::UV88;
138+
uint8_t u;
139+
uint8_t v;
140+
};
141+
142+
struct UVWQ8888 {
143+
static constexpr auto FORMAT = ImageFormat::UVWQ8888;
144+
uint8_t u;
145+
uint8_t v;
146+
uint8_t w;
147+
uint8_t q;
148+
};
149+
150+
struct RGBA16161616F {
151+
static constexpr auto FORMAT = ImageFormat::RGBA16161616F;
152+
sourcepp::math::FloatCompressed16 r;
153+
sourcepp::math::FloatCompressed16 g;
154+
sourcepp::math::FloatCompressed16 b;
155+
sourcepp::math::FloatCompressed16 a;
156+
};
157+
158+
struct RGBA16161616 {
159+
static constexpr auto FORMAT = ImageFormat::RGBA16161616;
160+
uint16_t r;
161+
uint16_t g;
162+
uint16_t b;
163+
uint16_t a;
164+
};
165+
166+
struct UVLX8888 {
167+
static constexpr auto FORMAT = ImageFormat::UVLX8888;
168+
uint8_t u;
169+
uint8_t v;
170+
uint8_t l;
171+
uint8_t x;
172+
};
173+
174+
struct R32F {
175+
static constexpr auto FORMAT = ImageFormat::R32F;
176+
float r;
177+
};
178+
179+
struct RGB323232F {
180+
static constexpr auto FORMAT = ImageFormat::R32F;
181+
float r;
182+
float g;
183+
float b;
184+
};
185+
186+
struct RGBA32323232F {
187+
static constexpr auto FORMAT = ImageFormat::RGBA32323232F;
188+
float r;
189+
float g;
190+
float b;
191+
float a;
192+
};
193+
194+
} // namespace ImagePixel
195+
196+
namespace ImageConversion {
10197

11198
/// Converts an image from one format to another.
12199
[[nodiscard]] std::vector<std::byte> convertImageDataToFormat(std::span<const std::byte> imageData, ImageFormat oldFormat, ImageFormat newFormat, uint16_t width, uint16_t height);
@@ -56,4 +243,6 @@ void setResizedDims(uint16_t& width, ResizeMethod widthResize, uint16_t& height,
56243
/// Resize given image data to the new dimensions, where the new width and height are governed by the resize methods
57244
[[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);
58245

59-
} // namespace vtfpp::ImageConversion
246+
} // namespace ImageConversion
247+
248+
} // namespace vtfpp

include/vtfpp/ImageFormats.h

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,10 @@ namespace ImageFormatDetails {
386386
return red(format) > 8 || bpp(format) > 32;
387387
}
388388

389+
[[nodiscard]] constexpr bool decimal(ImageFormat format) {
390+
return large(format) && format != ImageFormat::RGBA16161616;
391+
}
392+
389393
[[nodiscard]] constexpr bool transparent(ImageFormat format) {
390394
const auto a = alpha(format);
391395
if (a < 0) {
@@ -403,15 +407,71 @@ namespace ImageFormatDetails {
403407
break;
404408
}
405409
return false;
406-
} else {
407-
return a != 0;
408410
}
411+
switch (format) {
412+
using enum ImageFormat;
413+
case RGB888_BLUESCREEN:
414+
case BGR888_BLUESCREEN:
415+
return true;
416+
case BGRX8888:
417+
case BGRX5551:
418+
return false;
419+
default:
420+
break;
421+
}
422+
return a != 0;
409423
}
410424

411425
[[nodiscard]] constexpr bool opaque(ImageFormat format) {
412426
return !transparent(format);
413427
}
414428

429+
[[nodiscard]] constexpr ImageFormat containerFormat(ImageFormat format) {
430+
switch (format) {
431+
using enum ImageFormat;
432+
case R32F:
433+
case RGB323232F:
434+
case RGBA16161616F:
435+
case RGBA32323232F:
436+
case BC6H:
437+
return RGBA32323232F;
438+
case RGBA16161616:
439+
return RGBA16161616;
440+
case RGBA8888:
441+
case ABGR8888:
442+
case RGB888:
443+
case BGR888:
444+
case RGB888_BLUESCREEN:
445+
case BGR888_BLUESCREEN:
446+
case ARGB8888:
447+
case BGRA8888:
448+
case BGRX8888:
449+
case UVWQ8888:
450+
case UVLX8888:
451+
case RGB565:
452+
case BGR565:
453+
case BGRX5551:
454+
case BGRA5551:
455+
case BGRA4444:
456+
case I8:
457+
case IA88:
458+
case P8:
459+
case UV88:
460+
case A8:
461+
case DXT1:
462+
case DXT3:
463+
case DXT5:
464+
case DXT1_ONE_BIT_ALPHA:
465+
case ATI2N:
466+
case ATI1N:
467+
case BC7:
468+
return RGBA8888;
469+
case EMPTY:
470+
break;
471+
}
472+
return ImageFormat::EMPTY;
473+
}
474+
415475
} // namespace ImageFormatDetails
416476

417477
namespace ImageDimensions {

0 commit comments

Comments
 (0)