11#include <unordered_map>
14#ifdef SOURCEPP_BUILD_WITH_TBB
18#ifdef SOURCEPP_BUILD_WITH_THREADS
23#include <BufferStream.h>
36[[nodiscard]] std::vector<std::byte> compressData(std::span<const std::byte> data, int16_t level,
CompressionMethod method) {
40 mz_ulong compressedSize = mz_compressBound(data.size());
41 std::vector<std::byte> out(compressedSize);
44 while ((status = mz_compress2(
reinterpret_cast<unsigned char*
>(out.data()), &compressedSize,
reinterpret_cast<const unsigned char*
>(data.data()), data.size(), level)) == MZ_BUF_ERROR) {
46 out.resize(compressedSize);
49 if (status != MZ_OK) {
52 out.resize(compressedSize);
60 const auto expectedSize = ZSTD_compressBound(data.size());
61 std::vector<std::byte> out(expectedSize);
63 const auto compressedSize = ZSTD_compress(out.data(), expectedSize, data.data(), data.size(), level);
64 if (ZSTD_isError(compressedSize)) {
68 out.resize(compressedSize);
81template<std::
unsigned_
integral T,
bool ExistingDataIsSwizzled>
82constexpr void swizzleUncompressedImageData(std::span<std::byte> inputData, std::span<std::byte> outputData,
ImageFormat format, uint16_t width, uint16_t height, uint16_t depth) {
88 ](uint32_t x, uint32_t y, uint32_t z) {
89 auto widthL2m = widthL2;
90 auto heightL2m = heightL2;
91 auto depthL2m = depthL2;
93 uint32_t shiftCount = 0;
96 offset |= (z & 1) << shiftCount++;
99 if (heightL2m --> 0) {
100 offset |= (y & 1) << shiftCount++;
103 if (widthL2m --> 0) {
104 offset |= (x & 1) << shiftCount++;
107 }
while (x || y || z);
111 const auto* inputPtr =
reinterpret_cast<const T*
>(inputData.data());
112 auto* outputPtr =
reinterpret_cast<T*
>(outputData.data());
113 for (uint16_t x = 0; x < width; x++) {
114 for (uint16_t y = 0; y < height; y++) {
115 for (uint16_t z = 0; z < depth; z++) {
116 if constexpr (ExistingDataIsSwizzled) {
117 *outputPtr++ =
reinterpret_cast<const T*
>(inputData.data())[zIndex(x, y, z)];
119 reinterpret_cast<T*
>(outputData.data())[zIndex(x, y, z)] = *inputPtr++;
126template<
bool ConvertingFromSource>
127void swapImageDataEndianForConsole(std::span<std::byte> imageData,
ImageFormat format, uint8_t mipCount, uint16_t frameCount, uint8_t faceCount, uint16_t width, uint16_t height, uint16_t depth,
VTF::Platform platform) {
140 std::ranges::copy(newData, imageData.begin());
148 std::span dxtData{
reinterpret_cast<uint16_t*
>(imageData.data()), imageData.size() /
sizeof(uint16_t)};
150#ifdef SOURCEPP_BUILD_WITH_TBB
151 std::execution::par_unseq,
153 dxtData.begin(), dxtData.end(), [](uint16_t& value) {
154 BufferStream::swap_endian(&value);
166 std::vector<std::byte> out(imageData.size());
167 for(
int mip = mipCount - 1; mip >= 0; mip--) {
169 for (
int frame = 0; frame < frameCount; frame++) {
170 for (
int face = 0; face < faceCount; face++) {
171 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, format, mip, mipCount, frame, frameCount, face, faceCount, width, height)) {
172 std::span imageDataSpan{imageData.data() + offset, length * mipDepth};
173 std::span outSpan{out.data() + offset, length * mipDepth};
175 ::swizzleUncompressedImageData<uint32_t, ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
177 ::swizzleUncompressedImageData<uint16_t, ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
179 ::swizzleUncompressedImageData<uint8_t, ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
185 std::memcpy(imageData.data(), out.data(), out.size());
189template<
bool ConvertingFromDDS>
190[[nodiscard]] std::vector<std::byte> convertBetweenDDSAndVTFMipOrderForXBOX(
bool padded, std::span<const std::byte> imageData,
ImageFormat format, uint8_t mipCount, uint16_t frameCount, uint8_t faceCount, uint16_t width, uint16_t height, uint16_t depth,
bool& ok) {
191 std::vector<std::byte> reorderedImageData;
193 BufferStream reorderedStream{reorderedImageData};
195 if constexpr (ConvertingFromDDS) {
196 for (
int i = mipCount - 1; i >= 0; i--) {
198 for (
int j = 0; j < frameCount; j++) {
199 for (
int k = 0; k < faceCount; k++) {
200 for (
int l = 0; l < mipDepth; l++) {
201 uint32_t oldOffset, length;
202 if (!
ImageFormatDetails::getDataPositionXbox(oldOffset, length, padded, format, i, mipCount, j, frameCount, k, faceCount, width, height, l, depth)) {
206 reorderedStream << imageData.subspan(oldOffset, length);
212 for (
int j = 0; j < frameCount; j++) {
213 for (
int k = 0; k < faceCount; k++) {
214 for (
int i = 0; i < mipCount; i++) {
216 for (
int l = 0; l < mipDepth; l++) {
217 uint32_t oldOffset, length;
218 if (!
ImageFormatDetails::getDataPosition(oldOffset, length, format, i, mipCount, j, frameCount, k, faceCount, width, height, l, depth)) {
222 reorderedStream << imageData.subspan(oldOffset, length);
226 if (padded && j + 1 != frameCount && reorderedStream.tell() > 512) {
233 return reorderedImageData;
239 switch (this->
type) {
241 if (this->data.size() <=
sizeof(uint32_t)) {
244 return SHT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
247 if (this->data.size() !=
sizeof(uint32_t)) {
250 return *
reinterpret_cast<const uint32_t*
>(this->data.data());
252 if (this->data.size() !=
sizeof(uint32_t)) {
255 return std::make_tuple(
256 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 0),
257 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 1),
258 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 2),
259 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 3));
261 if (this->data.size() <=
sizeof(uint32_t)) {
264 return std::string(
reinterpret_cast<const char*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data()));
266 if (this->data.size() <=
sizeof(uint32_t)) {
269 return HOT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
280VTF::VTF(std::vector<std::byte>&& vtfData,
bool parseHeaderOnly)
281 :
data(std::move(vtfData)) {
282 BufferStreamReadOnly stream{this->data};
284 if (
const auto signature = stream.read<uint32_t>(); signature ==
VTF_SIGNATURE) {
285 stream >> this->platform;
286 if (this->platform != PLATFORM_PC) {
289 stream >> this->version;
294 stream.set_big_endian(
true);
295 stream >> this->platform;
296 if (this->platform != PLATFORM_X360 && this->platform != PLATFORM_PS3_ORANGEBOX && this->platform != PLATFORM_PS3_PORTAL2) {
299 stream >> this->version;
300 if (this->version != 8) {
305 this->platform = PLATFORM_PS3_PORTAL2;
311 stream >> this->platform;
312 if (this->platform != PLATFORM_XBOX) {
315 stream >> this->version;
316 if (this->version != 0) {
325 const auto headerSize = stream.read<uint32_t>();
327 const auto readResources = [
this, &stream](uint32_t resourceCount) {
329 this->resources.reserve(resourceCount);
330 for (
int i = 0; i < resourceCount; i++) {
331 auto& [type, flags_, data_] = this->resources.emplace_back();
333 auto typeAndFlags = stream.read<uint32_t>();
334 if (stream.is_big_endian()) {
336 BufferStream::swap_endian(&typeAndFlags);
340 data_ = stream.read_span<std::byte>(4);
343 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(data_.data()));
352 std::ranges::sort(this->resources, [](
const Resource& lhs,
const Resource& rhs) {
362 return *
reinterpret_cast<uint32_t*
>(lhs.
data.data()) < *
reinterpret_cast<uint32_t*
>(rhs.
data.data());
367 for (
auto& resource : this->resources) {
370 const auto lastOffset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
371 const auto currentOffset = *
reinterpret_cast<uint32_t*
>(resource.data.data());
372 const auto curPos = stream.tell();
373 stream.seek(lastOffset);
374 lastResource->data = stream.read_span<std::byte>(currentOffset - lastOffset);
375 stream.seek(
static_cast<int64_t
>(curPos));
377 lastResource = &resource;
381 const auto offset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
382 const auto curPos = stream.tell();
384 lastResource->data = stream.read_span<std::byte>(stream.size() - offset);
385 stream.seek(
static_cast<int64_t
>(curPos));
390 const auto postReadTransform = [
this] {
394 if (this->format ==
ImageFormat::DXT1 && this->flags & (FLAG_V0_ONE_BIT_ALPHA | FLAG_V0_MULTI_BIT_ALPHA)) {
400 this->format =
static_cast<ImageFormat>(
static_cast<int32_t
>(this->format) - 3);
404 if (this->flags & FLAG_V0_NO_MIP && this->mipCount > 1) {
405 this->removeFlags(FLAG_V0_NO_MIP);
409 switch (this->platform) {
410 case PLATFORM_UNKNOWN:
417 .read(this->frameCount)
418 .read(this->startFrame)
420 .read(this->reflectivity[0])
421 .read(this->reflectivity[1])
422 .read(this->reflectivity[2])
424 .read(this->bumpMapScale)
426 .read(this->mipCount);
432 stream >> this->thumbnailWidth >> this->thumbnailHeight;
433 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
439 if (this->version < 2) {
442 stream.read(this->depth);
445 if (parseHeaderOnly) {
450 if (this->version >= 3) {
452 auto resourceCount = stream.read<uint32_t>();
454 readResources(resourceCount);
456 this->opened = stream.tell() == headerSize;
458 if (this->opened && this->version >= 6) {
461 if (auxResource && imageResource) {
462 if (auxResource->getDataAsAuxCompressionLevel() != 0) {
463 const auto faceCount = this->getFaceCount();
464 std::vector<std::byte> decompressedImageData(
ImageFormatDetails::getDataLength(this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth));
465 uint32_t oldOffset = 0;
466 for (
int i = this->mipCount - 1; i >= 0; i--) {
467 for (
int j = 0; j < this->frameCount; j++) {
468 for (
int k = 0; k < faceCount; k++) {
469 uint32_t oldLength = auxResource->getDataAsAuxCompressionLength(i, this->mipCount, j, this->frameCount, k, faceCount);
470 if (uint32_t newOffset, newLength;
ImageFormatDetails::getDataPosition(newOffset, newLength, this->format, i, this->mipCount, j, this->frameCount, k, faceCount, this->width, this->height, 0, this->getDepth())) {
472 mz_ulong decompressedImageDataSize = newLength * this->depth;
473 switch (auxResource->getDataAsAuxCompressionMethod()) {
476 if (mz_uncompress(
reinterpret_cast<unsigned char*
>(decompressedImageData.data() + newOffset), &decompressedImageDataSize,
reinterpret_cast<const unsigned char*
>(imageResource->data.data() + oldOffset), oldLength) != MZ_OK) {
477 this->opened =
false;
482 if (
const auto decompressedSize = ZSTD_decompress(decompressedImageData.data() + newOffset, decompressedImageDataSize, imageResource->data.data() + oldOffset, oldLength); ZSTD_isError(decompressedSize) || decompressedSize != decompressedImageDataSize) {
483 this->opened =
false;
493 oldOffset += oldLength;
503 this->opened = stream.tell() == headerSize;
505 this->resources.reserve(2);
507 if (this->hasThumbnailData()) {
508 this->resources.push_back({
514 if (this->hasImageData()) {
515 this->resources.push_back({
518 .data = stream.read_span<std::byte>(stream.size() - stream.tell()),
524 this->compressionLevel = resource->getDataAsAuxCompressionLevel();
525 this->compressionMethod = resource->getDataAsAuxCompressionMethod();
530 case PLATFORM_XBOX: {
531 if (this->platform == PLATFORM_XBOX) {
532 uint16_t preloadSize = 0, imageOffset = 0;
538 .read(this->frameCount)
541 .read(this->reflectivity[0])
542 .read(this->reflectivity[1])
543 .read(this->reflectivity[2])
544 .read(this->bumpMapScale)
546 .read(this->thumbnailWidth)
547 .read(this->thumbnailHeight)
548 .read(this->fallbackWidth)
549 .read(this->fallbackHeight)
550 .read(this->consoleMipScale)
553 const bool headerSizeIsAccurate = stream.tell() == headerSize;
561 const auto faceCount = (this->flags & FLAG_V0_ENVMAP) ? 6 : 1;
563 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
567 this->resources.push_back({
575 this->resources.push_back({
584 std::vector<std::byte> reorderedFallbackData;
585 if (this->hasFallbackData()) {
586 if (stream.tell() + fallbackSize != preloadSize) {
589 if (stream.tell() + fallbackSize != preloadSize) {
590 this->opened =
false;
593 this->fallbackMipCount = 1;
595 this->flags |= FLAG_V0_NO_MIP;
597 reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
false, stream.read_span<std::byte>(fallbackSize), this->format, this->fallbackMipCount, this->frameCount, faceCount, this->fallbackWidth, this->fallbackHeight, 1, ok);
599 this->opened =
false;
602 ::swapImageDataEndianForConsole<true>(reorderedFallbackData, this->format, this->fallbackMipCount, this->frameCount, faceCount, this->fallbackWidth, this->fallbackHeight, 1, this->platform);
605 this->opened = headerSizeIsAccurate;
606 if (parseHeaderOnly) {
611 std::vector<std::byte> reorderedImageData;
612 if (this->hasImageData()) {
613 reorderedImageData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
true, stream.seek(imageOffset).read_span<std::byte>(imageSize), this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth, ok);
615 this->opened =
false;
618 ::swapImageDataEndianForConsole<true>(reorderedImageData, this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth, this->platform);
622 if (this->hasFallbackData()) {
625 if (this->hasImageData()) {
632 case PLATFORM_PS3_ORANGEBOX:
633 case PLATFORM_PS3_PORTAL2: {
634 uint8_t resourceCount;
640 .read(this->frameCount)
642 .read(this->consoleMipScale)
644 .read(this->reflectivity[0])
645 .read(this->reflectivity[1])
646 .read(this->reflectivity[2])
647 .read(this->bumpMapScale)
649 .skip<math::Vec4ui8>()
655 if (this->platform == PLATFORM_PS3_PORTAL2) {
656 stream.skip<uint32_t>();
661 if (parseHeaderOnly) {
666 this->resources.reserve(resourceCount);
667 readResources(resourceCount);
669 this->opened = stream.tell() == headerSize;
672 for (
const auto& resource : this->resources) {
676 this->setResourceInternal(resource.type, *decompressedData);
686 ::swapImageDataEndianForConsole<true>(resource.data, this->thumbnailFormat, 1, 1, 1, this->thumbnailWidth, this->thumbnailHeight, 1, this->platform);
688 if (this->platform == PLATFORM_PS3_ORANGEBOX) {
690 const auto reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
false, resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
691 if (!ok || reorderedFallbackData.size() != resource.data.size()) {
692 this->opened =
false;
695 std::memcpy(resource.data.data(), reorderedFallbackData.data(), resource.data.size());
697 ::swapImageDataEndianForConsole<true>(resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, this->platform);
699 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resource.data.data()));
707VTF::VTF(std::span<const std::byte> vtfData,
bool parseHeaderOnly)
708 :
VTF(std::vector<std::byte>{vtfData.begin(), vtfData.end()}, parseHeaderOnly) {}
710VTF::VTF(
const std::filesystem::path& vtfPath,
bool parseHeaderOnly)
711 :
VTF(
fs::readFileBuffer(vtfPath), parseHeaderOnly) {}
719 this->data = other.
data;
721 this->width = other.
width;
722 this->height = other.
height;
728 this->format = other.
format;
737 this->depth = other.
depth;
739 this->resources.clear();
740 for (
const auto& [otherType, otherFlags, otherData] : other.
resources) {
741 auto& [
type, flags_, data_] = this->resources.emplace_back();
744 data_ = {this->data.data() + (otherData.data() - other.
data.data()), otherData.size()};
756VTF::operator bool()
const {
763 for (
int i = 0; i < writer.
mipCount; i++) {
766 for (
int l = 0; l < writer.
depth; l++) {
767 if (options.
invertGreenChannel && !writer.
setImage(
ImageConversion::invertGreenChannelForImageData(writer.
getImageDataRaw(i, j, k, l), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i)), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i),
ImageConversion::ResizeFilter::DEFAULT, i, j, k, l)) {
770 if (options.
gammaCorrection != 1.f && !writer.
setImage(
ImageConversion::gammaCorrectImageData(writer.
getImageDataRaw(i, j, k, l), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i), options.
gammaCorrection), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i),
ImageConversion::ResizeFilter::DEFAULT, i, j, k, l)) {
826 return writer.
bake(vtfPath);
830 std::vector<std::byte> imageData;
851 std::vector<std::byte> imageData;
867 return writer.
bake(vtfPath);
886 return this->platform;
890 if (this->platform == newPlatform) {
895 const auto oldPlatform = this->platform;
897 switch (newPlatform) {
904 this->platform = newPlatform;
916 this->platform = newPlatform;
930 this->thumbnailHeight = 0;
945 if (this->mipCount != maxMipCount) {
952 return this->version;
961 if (faceCount == 7 && (newVersion < 1 || newVersion > 4)) {
964 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, faceCount, this->depth);
968 const bool srgb = this->
isSRGB();
986 this->version = newVersion;
1027 if (newWidth == 0 || newHeight == 0) {
1037 if (this->width == newWidth && this->height == newHeight) {
1040 auto newMipCount = this->mipCount;
1042 newMipCount = maxMipCount;
1048 this->frameCount = 1;
1050 this->width = newWidth;
1051 this->height = newHeight;
1079 if (this->version >= 5) {
1081 }
else if (this->version == 4) {
1085 if (this->version >= 5) {
1087 }
else if (this->version == 4) {
1121 return this->format;
1132 this->format = newFormat;
1135 const auto oldFormat = this->format;
1137 this->
regenerateImageData(newFormat, this->width +
math::paddingForAlignment(4, this->width), this->height +
math::paddingForAlignment(4, this->height), this->mipCount, this->frameCount, this->
getFaceCount(), this->depth, filter, quality);
1139 this->
regenerateImageData(newFormat, this->width, this->height, this->mipCount, this->frameCount, this->
getFaceCount(), this->depth, filter, quality);
1143 const auto fallbackConverted =
ImageConversion::convertSeveralImageDataToFormat(fallbackResource->data, oldFormat, this->format,
ImageDimensions::getMaximumMipCount(this->fallbackWidth, this->fallbackHeight), this->frameCount, this->getFaceCount(), this->fallbackWidth, this->fallbackHeight, 1, quality);
1149 return this->mipCount;
1158 newMipCount = maxMipCount;
1177 if (this->mipCount <= 1) {
1183 auto* outputDataPtr = imageResource->data.data();
1186#ifdef SOURCEPP_BUILD_WITH_THREADS
1187 std::vector<std::future<void>> futures;
1188 futures.reserve(this->frameCount * faceCount * this->depth);
1190 for (
int j = 0; j < this->frameCount; j++) {
1191 for (
int k = 0; k < faceCount; k++) {
1192#ifdef SOURCEPP_BUILD_WITH_THREADS
1193 futures.push_back(std::async(std::launch::async, [
this, filter, outputDataPtr, faceCount, j, k] {
1195 for (
int i = 1; i < this->mipCount; i++) {
1198 for (
int l = 0; l < mipDepth; l++) {
1199 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(i - 1, j, k, l), this->format, mipWidthM1, mipWidth, mipHeightM1, mipHeight, this->
isSRGB(), filter);
1200 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, this->mipCount, j, this->frameCount, k, faceCount, this->width, this->height, l, this->depth) && mip.size() == length) {
1201 std::memcpy(outputDataPtr + offset, mip.data(), length);
1205#ifdef SOURCEPP_BUILD_WITH_THREADS
1207 if (futures.size() >= std::thread::hardware_concurrency()) {
1208 for (
auto& future : futures) {
1216#ifdef SOURCEPP_BUILD_WITH_THREADS
1217 for (
auto& future : futures) {
1224 return this->frameCount;
1246 if (this->platform ==
PLATFORM_XBOX || this->version >= 6) {
1252 if (this->version >= 1 && this->version <= 4 && expectedLength < image->
data.size()) {
1255 if (expectedLength == image->data.size()) {
1265 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, isCubeMap ? ((this->version >= 1 && this->version <= 4) ? 7 : 6) : 1, this->depth);
1285 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, newFrameCount, isCubeMap ? ((this->version >= 1 && this->version <= 4) ? 7 : 6) : 1, newDepth);
1306 static constexpr auto getReflectivityForImage = [](
const VTF& vtf, uint16_t frame, uint8_t face, uint16_t slice) {
1307 static constexpr auto getReflectivityForPixel = [](
const ImagePixel::RGBA8888* pixel) -> math::Vec3f {
1309 math::Vec3f ref{
static_cast<float>(pixel->r),
static_cast<float>(pixel->g),
static_cast<float>(pixel->b)};
1311 ref = ref / 255.f * 0.922f;
1320 for (uint64_t i = 0; i < rgba8888Data.size(); i += 4) {
1321 out += getReflectivityForPixel(
reinterpret_cast<ImagePixel::RGBA8888*
>(rgba8888Data.data() + i));
1328#ifdef SOURCEPP_BUILD_WITH_THREADS
1329 if (this->frameCount > 1 || faceCount > 1 || this->depth > 1) {
1330 std::vector<std::future<math::Vec3f>> futures;
1331 futures.reserve(this->frameCount * faceCount * this->depth);
1334 for (
int j = 0; j < this->frameCount; j++) {
1335 for (
int k = 0; k < faceCount; k++) {
1336 for (
int l = 0; l < this->depth; l++) {
1337 futures.push_back(std::async(std::launch::async, [
this, j, k, l] {
1338 return getReflectivityForImage(*
this, j, k, l);
1340 if (futures.size() >= std::thread::hardware_concurrency()) {
1341 for (
auto& future : futures) {
1350 for (
auto& future : futures) {
1353 this->
reflectivity /= this->frameCount * faceCount * this->depth;
1355 this->
reflectivity = getReflectivityForImage(*
this, 0, 0, 0);
1359 for (
int j = 0; j < this->frameCount; j++) {
1360 for (
int k = 0; k < faceCount; k++) {
1361 for (
int l = 0; l < this->depth; l++) {
1362 this->
reflectivity += getReflectivityForImage(*
this, j, k, l);
1366 this->
reflectivity /= this->frameCount * faceCount * this->depth;
1387 return this->thumbnailHeight;
1403 return this->resources;
1407 for (
const auto& resource : this->resources) {
1408 if (resource.type ==
type) {
1416 for (
auto& resource : this->resources) {
1417 if (resource.type ==
type) {
1425 if (
const auto* resource = this->
getResource(type); resource && resource->
data.size() == data_.size()) {
1426 std::memcpy(resource->data.data(), data_.data(), data_.size());
1431 std::unordered_map<Resource::Type, std::pair<std::vector<std::byte>, uint64_t>> resourceData;
1432 for (
const auto& [type_, flags_, dataSpan] : this->resources) {
1433 resourceData[type_] = {std::vector<std::byte>{dataSpan.begin(), dataSpan.end()}, 0};
1437 if (data_.empty()) {
1438 resourceData.erase(
type);
1440 resourceData[
type] = {{data_.begin(), data_.end()}, 0};
1445 BufferStream writer{this->data};
1447 for (
auto resourceType : resourceData | std::views::keys) {
1448 if (!resourceData.contains(resourceType)) {
1451 auto& [specificResourceData, offset] = resourceData[resourceType];
1452 if (resourceType ==
type) {
1456 {this->data.data() + offset, specificResourceData.size()},
1459 *resourcePtr = newResource;
1461 this->resources.push_back(newResource);
1463 }
else if (!resourceData.contains(resourceType)) {
1466 offset = writer.tell();
1467 writer.write(specificResourceData);
1469 this->data.resize(writer.size());
1471 for (
auto& [type_, flags_, dataSpan] : this->resources) {
1472 if (resourceData.contains(type_)) {
1473 const auto& [specificResourceData, offset] = resourceData[type_];
1474 dataSpan = {this->data.data() + offset, specificResourceData.size()};
1480 std::erase_if(this->resources, [
type](
const Resource& resource) {
return resource.
type ==
type; });
1484 if (!newWidth) { newWidth = 1; }
1485 if (!newHeight) { newHeight = 1; }
1486 if (!newMipCount) { newMipCount = 1; }
1487 if (!newFrameCount) { newFrameCount = 1; }
1488 if (!newFaceCount) { newFaceCount = 1; }
1489 if (!newDepth) { newDepth = 1; }
1491 if (newMipCount > 1) {
1494 if (
ImageFormatDetails::compressed(newFormat) && ((newWidth > 4 && !std::has_single_bit(newWidth)) || (newHeight > 4 && !std::has_single_bit(newHeight)))) {
1504 if (this->format == newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->depth == newDepth) {
1508 std::vector<std::byte> newImageData;
1510 if (this->format != newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->depth == newDepth) {
1514 for (
int i = newMipCount - 1; i >= 0; i--) {
1517 for (
int j = 0; j < newFrameCount; j++) {
1518 for (
int k = 0; k < newFaceCount; k++) {
1519 for (
int l = 0; l < newMipDepth; l++) {
1522 std::vector<std::byte> image{imageSpan.begin(), imageSpan.end()};
1523 if (this->width != newWidth || this->height != newHeight) {
1526 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, newMipCount, j, newFrameCount, k, newFaceCount, newWidth, newHeight, l, newDepth) && image.size() == length) {
1527 std::memcpy(newImageData.data() + offset, image.data(), length);
1534 if (this->format != newFormat) {
1542 this->format = newFormat;
1543 this->width = newWidth;
1544 this->height = newHeight;
1545 this->mipCount = newMipCount;
1546 this->frameCount = newFrameCount;
1547 if (newFaceCount > 1) {
1552 this->depth = newDepth;
1559 if (palette->data.size() != targetSize) {
1560 std::vector<std::byte> paletteData{palette->data.begin(), palette->data.end()};
1561 paletteData.resize(targetSize);
1573 return palette->getDataAsPalette(frame);
1578std::vector<std::byte>
VTF::getParticleSheetFrameDataRaw(uint16_t& spriteWidth, uint16_t& spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds, uint8_t mip, uint16_t frame, uint8_t face, uint16_t slice)
const {
1587 auto sht = shtResource->getDataAsParticleSheet();
1588 const auto* sequence = sht.getSequenceFromID(shtSequenceID);
1589 if (!sequence || sequence->frames.size() <= shtFrame || shtBounds >= sht.getFrameBoundsCount()) {
1597 const auto& bounds = sequence->frames[shtFrame].bounds[shtBounds];
1598 uint16_t x1 = std::clamp<uint16_t>(std::floor(bounds.x1 *
static_cast<float>(this->getWidth(mip))), 0, this->getWidth(mip));
1599 uint16_t y1 = std::clamp<uint16_t>(std::ceil( bounds.y1 *
static_cast<float>(this->getHeight(mip))), 0, this->getHeight(mip));
1600 uint16_t x2 = std::clamp<uint16_t>(std::ceil( bounds.x2 *
static_cast<float>(this->getWidth(mip))), 0, this->getHeight(mip));
1601 uint16_t y2 = std::clamp<uint16_t>(std::floor(bounds.y2 *
static_cast<float>(this->getHeight(mip))), 0, this->getWidth(mip));
1603 if (x1 > x2) [[unlikely]] {
1606 if (y1 > y2) [[unlikely]] {
1609 spriteWidth = x2 - x1;
1610 spriteWidth = y2 - y1;
1612 const auto out =
ImageConversion::cropImageData(this->
getImageDataRaw(mip, frame, face, slice), this->
getFormat(), this->
getWidth(mip), spriteWidth, x1, this->
getHeight(mip), spriteHeight, y1);
1620std::vector<std::byte>
VTF::getParticleSheetFrameDataAs(
ImageFormat newFormat, uint16_t& spriteWidth, uint16_t& spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds, uint8_t mip, uint16_t frame, uint8_t face, uint16_t slice)
const {
1621 return ImageConversion::convertImageDataToFormat(this->
getParticleSheetFrameDataRaw(spriteWidth, spriteHeight, shtSequenceID, shtFrame, shtBounds, mip, frame, face, slice), this->
getFormat(), newFormat, spriteWidth, spriteHeight);
1624std::vector<std::byte>
VTF::getParticleSheetFrameDataAsRGBA8888(uint16_t& spriteWidth, uint16_t& spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds, uint8_t mip, uint16_t frame, uint8_t face, uint16_t slice)
const {
1629 std::vector<std::byte> particleSheetData;
1630 BufferStream writer{particleSheetData};
1632 const auto bakedSheet = value.
bake();
1633 writer.write<uint32_t>(bakedSheet.size()).write(bakedSheet);
1634 particleSheetData.resize(writer.size());
1653 BufferStream writer{&lodData,
sizeof(lodData)};
1655 writer << u << v << u360 << v360;
1673 std::vector<std::byte> keyValuesData;
1674 BufferStream writer{keyValuesData};
1676 writer.write<uint32_t>(value.size()).write(value,
false);
1677 keyValuesData.resize(writer.size());
1687 std::vector<std::byte> hotspotData;
1688 BufferStream writer{hotspotData};
1690 const auto bakedHotspotData = value.
bake();
1691 writer.write<uint32_t>(bakedHotspotData.size()).write(bakedHotspotData);
1692 hotspotData.resize(writer.size());
1729 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->mipCount, frame, this->frameCount, face, this->
getFaceCount(), this->width, this->height, slice, this->depth)) {
1730 return imageResource->data.subspan(offset, length);
1737 const auto rawImageData = this->
getImageDataRaw(mip, frame, face, slice);
1738 if (rawImageData.empty()) {
1756 if (imageData_.empty()) {
1761 uint16_t resizedWidth = width_, resizedHeight = height_;
1768 mip = newMipCount - 1;
1773 this->
regenerateImageData(format_, resizedWidth, resizedHeight, mip + 1, frame + 1, face ? (face < 6 ? 6 : face) : 0, slice + 1);
1782 if (!imageResource) {
1785 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->mipCount, frame, this->frameCount, face, faceCount, this->width, this->height, slice, this->depth)) {
1786 std::vector<std::byte> image{imageData_.begin(), imageData_.end()};
1788 if (width_ != newWidth || height_ != newHeight) {
1791 if (format_ != this->format) {
1794 std::memcpy(imageResource->data.data() + offset, image.data(), image.size());
1801 int inputWidth, inputHeight, inputFrameCount;
1805 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
1810 if (inputFrameCount == 1) {
1811 return this->
setImage(imageData_, inputFormat, inputWidth, inputHeight, filter, mip, frame, face, slice, quality);
1815 bool allSuccess =
true;
1817 for (
int currentFrame = 0; currentFrame < inputFrameCount; currentFrame++) {
1818 if (!this->
setImage({imageData_.data() + currentFrame * frameSize, imageData_.data() + currentFrame * frameSize + frameSize}, inputFormat, inputWidth, inputHeight, filter, mip, frame + currentFrame, face, slice, quality)) {
1821 if (currentFrame == 0 && this->frameCount < frame + inputFrameCount) {
1834 if (
auto data_ = this->
saveImageToFile(mip, frame, face, slice, fileFormat); !data_.empty()) {
1846 return thumbnailResource->data;
1853 if (rawThumbnailData.empty()) {
1870 this->thumbnailHeight = height_;
1875 int inputWidth, inputHeight, inputFrameCount;
1879 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
1884 if (inputFrameCount == 1) {
1885 this->
setThumbnail(imageData_, inputFormat, inputWidth, inputHeight, quality);
1891 this->
setThumbnail({imageData_.data(), frameSize}, inputFormat, inputWidth, inputHeight, quality);
1903 this->thumbnailHeight = 1;
1904 std::array newThumbnail{
1905 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[0], 0.f, 1.f) * 255.f)),
1906 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[1], 0.f, 1.f) * 255.f)),
1907 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[2], 0.f, 1.f) * 255.f)),
1913 this->thumbnailHeight = 16;
1914 this->
setResourceInternal(
Resource::TYPE_THUMBNAIL_DATA,
ImageConversion::convertImageDataToFormat(
ImageConversion::resizeImageData(this->
getImageDataRaw(), this->format, this->width, this->
thumbnailWidth, this->height, this->thumbnailHeight, this->
isSRGB(), filter), this->format, this->
thumbnailFormat, this->
thumbnailWidth, this->thumbnailHeight, quality));
1921 this->thumbnailHeight = 0;
1942 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->
fallbackMipCount, frame, this->frameCount, face, this->
getFaceCount(), this->
fallbackWidth, this->
fallbackHeight)) {
1943 return fallbackResource->data.subspan(offset, length);
1951 if (rawFallbackData.empty()) {
1977 std::vector<std::byte> fallbackData;
1981 for (
int j = 0; j < this->frameCount; j++) {
1982 for (
int k = 0; k < faceCount; k++) {
1983 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(0, j, k, 0), this->format, this->width, mipWidth, this->height, mipHeight, this->
isSRGB(), filter);
1984 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, this->
fallbackMipCount, j, this->frameCount, k, faceCount, this->
fallbackWidth, this->
fallbackHeight) && mip.size() == length) {
1985 std::memcpy(fallbackData.data() + offset, mip.data(), length);
2006 if (
auto data_ = this->
saveFallbackToFile(mip, frame, face, fileFormat); !data_.empty()) {
2027 uint64_t vtfSize = 0;
2029 switch (this->platform) {
2033 switch (this->version) {
2036 vtfSize +=
sizeof(uint64_t);
2037 vtfSize +=
sizeof(uint32_t) * (2 + this->mipCount * this->frameCount * this->
getFaceCount());
2042 vtfSize += 11 +
sizeof(uint32_t) +
sizeof(uint64_t) * this->
getResources().size();
2043 for (
const auto& [resourceType, resourceFlags, resourceData] : this->
getResources()) {
2045 vtfSize += resourceData.size();
2049 vtfSize +=
sizeof(uint16_t);
2052 vtfSize +=
sizeof(uint32_t) * 9 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 4 +
sizeof(uint8_t) * 3;
2053 if (this->version < 3) {
2058 vtfSize += thumbnailResource->data.size();
2062 vtfSize += imageResource->data.size();
2065 vtfSize +=
static_cast<uint64_t
>(
static_cast<double>(imageResource->data.size()) / 1.75);
2074 vtfSize +=
sizeof(uint32_t) * 6 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 6 +
sizeof(uint8_t) * 6;
2077 vtfSize += thumbnailResource->data.size();
2081 vtfSize += paletteResource->data.size();
2091 if (vtfSize > 512) {
2096 vtfSize +=
sizeof(uint32_t);
2099 vtfSize +=
sizeof(uint32_t) * 7 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 5 +
sizeof(uint8_t) * 6;
2100 vtfSize +=
sizeof(uint64_t) * this->
getResources().size();
2102 for (
const auto& [resourceType, resourceFlags, resourceData] : this->
getResources()) {
2104 vtfSize += resourceData.size();
2109 vtfSize += imageResource->data.size();
2112 vtfSize +=
static_cast<uint64_t
>(
static_cast<double>(imageResource->data.size()) / 1.75);
2121 std::vector<std::byte> out;
2122 BufferStream writer{out};
2124 const auto writeResources = [&writer](uint64_t headerLengthPos,
const std::vector<Resource>& sortedResources) -> uint64_t {
2125 auto resourceHeaderCurPos = writer.tell();
2126 writer.pad<uint64_t>(sortedResources.size());
2127 auto resourceDataCurPos = writer.tell();
2128 writer.seek_u(headerLengthPos).write<uint32_t>(resourceDataCurPos);
2130 uint64_t resourceHeaderImagePos = 0;
2131 for (
const auto& resource : sortedResources) {
2132 writer.seek_u(resourceHeaderCurPos);
2134 uint32_t resourceType = resource.type;
2138 if (writer.is_big_endian()) {
2140 BufferStream::swap_endian(&resourceType);
2142 writer.write<uint32_t>(resourceType);
2145 resourceHeaderImagePos = writer.tell();
2146 writer.write<uint32_t>(0);
2147 resourceHeaderCurPos = writer.tell();
2152 writer.write(resource.data);
2153 resourceHeaderCurPos = writer.tell();
2155 writer.write<uint32_t>(resourceDataCurPos);
2156 resourceHeaderCurPos = writer.tell();
2157 writer.seek_u(resourceDataCurPos).write(resource.data);
2158 resourceDataCurPos = writer.tell();
2161 if (resourceHeaderImagePos) {
2162 writer.seek_u(resourceHeaderImagePos).write<uint32_t>(resourceDataCurPos);
2163 for (
auto& resource : sortedResources) {
2165 writer.seek_u(resourceDataCurPos).write(resource.data);
2169 return resourceDataCurPos;
2175 auto bakeFormat = this->format;
2176 auto bakeFlags = this->
flags;
2183 bakeFormat =
static_cast<ImageFormat>(
static_cast<int32_t
>(this->format) + 3);
2186 switch (this->platform) {
2193 .write(this->version);
2195 const auto headerLengthPos = writer.tell();
2196 writer.write<uint32_t>(0);
2200 .write(this->height)
2202 .write(this->frameCount)
2209 .write(this->mipCount)
2212 .write(this->thumbnailHeight);
2214 if (this->version >= 2) {
2215 writer << this->depth;
2218 if (this->version < 3) {
2220 const auto headerSize = writer.tell();
2221 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(headerSize);
2224 writer.write(thumbnailResource->data);
2227 writer.write(imageResource->data);
2230 std::vector<std::byte> auxCompressionResourceData;
2231 std::vector<std::byte> compressedImageResourceData;
2232 bool hasAuxCompression =
false;
2235 if (hasAuxCompression) {
2237 auxCompressionResourceData.resize((this->mipCount * this->frameCount * faceCount + 2) *
sizeof(uint32_t));
2238 BufferStream auxWriter{auxCompressionResourceData,
false};
2245 .write<uint32_t>(auxCompressionResourceData.size() -
sizeof(uint32_t))
2249 for (
int i = this->mipCount - 1; i >= 0; i--) {
2250 for (
int j = 0; j < this->frameCount; j++) {
2251 for (
int k = 0; k < faceCount; k++) {
2252 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, this->mipCount, j, this->frameCount, k, faceCount, this->width, this->height, 0, this->depth)) {
2254 compressedImageResourceData.insert(compressedImageResourceData.end(), compressedData.begin(), compressedData.end());
2255 auxWriter.write<uint32_t>(compressedData.size());
2263 writer.pad(3).write<uint32_t>(this->
getResources().size() + hasAuxCompression).pad(8);
2265 std::vector<Resource> sortedResources = this->
getResources();
2266 if (hasAuxCompression) {
2267 for (
auto& resource : sortedResources) {
2269 resource.data = compressedImageResourceData;
2273 sortedResources.push_back({
2276 .data = auxCompressionResourceData,
2279 std::ranges::sort(sortedResources, [](
const Resource& lhs,
const Resource& rhs) {
2282 writeResources(headerLengthPos, sortedResources);
2288 writer.write<uint32_t>(0);
2290 const auto headerSizePos = writer.tell();
2295 .write(this->height)
2297 .write(this->frameCount);
2298 const auto preloadSizePos = writer.tell();
2299 writer.write<uint16_t>(0);
2300 const auto imageOffsetPos = writer.tell();
2307 .write(this->format)
2309 .write(this->thumbnailHeight)
2315 const auto headerSize = writer.tell();
2316 writer.seek_u(headerSizePos).write<uint32_t>(headerSize).seek_u(headerSize);
2319 writer.write(thumbnailResource->data);
2324 writer.write(paletteResource->data);
2328 bool hasFallbackResource =
false;
2330 hasFallbackResource =
true;
2332 auto reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
false, fallbackResource->data, this->format, this->fallbackMipCount, this->frameCount, this->getFaceCount(), this->fallbackWidth, this->fallbackHeight, 1, ok);
2335 writer.write(reorderedFallbackData);
2337 writer.pad(fallbackResource->data.size());
2341 const auto preloadSize = writer.tell();
2342 writer.seek_u(preloadSizePos).write<uint32_t>(preloadSize).seek_u(preloadSize);
2344 if (hasFallbackResource) {
2347 const auto imageOffset = writer.tell();
2348 writer.seek_u(imageOffsetPos).write<uint16_t>(imageOffset).seek_u(imageOffset);
2352 auto reorderedImageData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
true, imageResource->data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
2354 ::swapImageDataEndianForConsole<false>(reorderedImageData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, this->platform);
2355 writer.write(reorderedImageData);
2357 writer.pad(imageResource->data.size());
2360 if (writer.tell() > 512) {
2370 writer.set_big_endian(
true);
2374 writer.set_big_endian(
true);
2375 writer << this->platform;
2377 writer.write<uint32_t>(8);
2379 const auto headerLengthPos = writer.tell();
2384 .write(this->height)
2386 .write(this->frameCount);
2387 const auto preloadPos = writer.tell();
2391 .write<uint8_t>(this->resources.size())
2397 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[0] * 255)), 0, 255))
2398 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[1] * 255)), 0, 255))
2399 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[2] * 255)), 0, 255))
2400 .write<uint8_t>(255);
2401 const auto compressionPos = writer.tell();
2402 writer.write<uint32_t>(0);
2406 writer.write<uint32_t>(0);
2409 std::vector<std::byte> thumbnailResourceData;
2410 std::vector<std::byte> imageResourceData;
2411 std::vector<Resource> sortedResources = this->
getResources();
2412 for (
auto& resource : sortedResources) {
2414 thumbnailResourceData = {resource.data.begin(), resource.data.end()};
2415 ::swapImageDataEndianForConsole<false>(thumbnailResourceData, this->
thumbnailFormat, 1, 1, 1, this->
thumbnailWidth, this->thumbnailHeight, 1, this->platform);
2416 resource.data = thumbnailResourceData;
2420 imageResourceData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
false, resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
2421 if (!ok || imageResourceData.size() != resource.data.size()) {
2425 imageResourceData = {resource.data.begin(), resource.data.end()};
2427 ::swapImageDataEndianForConsole<false>(imageResourceData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, this->platform);
2437 fixedCompressionLevel = 6;
2440 imageResourceData = std::move(*compressedData);
2441 const auto curPos = writer.tell();
2442 writer.seek_u(compressionPos).write<uint32_t>(imageResourceData.size()).seek_u(curPos);
2446 resource.data = imageResourceData;
2450 std::ranges::sort(sortedResources, [](
const Resource& lhs,
const Resource& rhs) {
2453 const auto resourceDataImagePos = writeResources(headerLengthPos, sortedResources);
2454 writer.seek_u(preloadPos).write(std::max<uint16_t>(resourceDataImagePos, 2048));
2458 out.resize(writer.size());
#define SOURCEPP_DEBUG_BREAK
Create a breakpoint in debug.
std::vector< std::byte > bake() const
std::vector< std::byte > bake() const
void setImageHeightResizeMethod(ImageConversion::ResizeMethod imageHeightResizeMethod_)
bool setFrameFaceAndDepth(uint16_t newFrameCount, bool isCubeMap, uint16_t newDepth=1)
void removeKeyValuesDataResource()
void computeFallback(ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT)
uint8_t getFallbackHeight() const
CompressionMethod compressionMethod
void computeReflectivity()
uint8_t getThumbnailWidth() const
std::vector< std::byte > saveThumbnailToFile(ImageConversion::FileFormat fileFormat=ImageConversion::FileFormat::DEFAULT) const
ImageFormat getFormat() const
void setPlatform(Platform newPlatform)
uint16_t getHeight(uint8_t mip=0) const
VTF & operator=(const VTF &other)
uint8_t getFallbackWidth() const
sourcepp::math::Vec3f reflectivity
uint16_t getWidth(uint8_t mip=0) const
void setThumbnail(std::span< const std::byte > imageData_, ImageFormat format_, uint16_t width_, uint16_t height_, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
bool setRecommendedMipCount()
static bool createInternal(VTF &writer, CreationOptions options)
std::vector< std::byte > getParticleSheetFrameDataAsRGBA8888(uint16_t &spriteWidth, uint16_t &spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds=0, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
This is a convenience function. You're best off uploading the bounds to the GPU and scaling the UV th...
void regenerateImageData(ImageFormat newFormat, uint16_t newWidth, uint16_t newHeight, uint8_t newMipCount, uint16_t newFrameCount, uint8_t newFaceCount, uint16_t newDepth, ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
bool hasFallbackData() const
void computeMips(ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT)
void setImageWidthResizeMethod(ImageConversion::ResizeMethod imageWidthResizeMethod_)
void setCompressionLevel(int16_t newCompressionLevel)
ImageConversion::ResizeMethod imageHeightResizeMethod
void setFormat(ImageFormat newFormat, ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
void setImageResizeMethods(ImageConversion::ResizeMethod imageWidthResizeMethod_, ImageConversion::ResizeMethod imageHeightResizeMethod_)
std::vector< std::byte > getParticleSheetFrameDataAs(ImageFormat newFormat, uint16_t &spriteWidth, uint16_t &spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds=0, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
This is a convenience function. You're best off uploading the bounds to the GPU and scaling the UV th...
float getBumpMapScale() const
void setKeyValuesDataResource(std::string_view value)
void computeTransparencyFlags()
std::vector< std::byte > saveFallbackToFile(uint8_t mip=0, uint16_t frame=0, uint8_t face=0, ImageConversion::FileFormat fileFormat=ImageConversion::FileFormat::DEFAULT) const
std::vector< std::byte > data
Platform getPlatform() const
std::vector< std::byte > getPaletteResourceFrame(uint16_t frame=0) const
void addFlags(uint32_t flags_)
uint8_t getFaceCount() const
ImageFormat thumbnailFormat
void setFlags(uint32_t flags_)
void setSize(uint16_t newWidth, uint16_t newHeight, ImageConversion::ResizeFilter filter)
ImageConversion::ResizeMethod getImageHeightResizeMethod() const
void setBumpMapScale(float newBumpMapScale)
ImageFormat getThumbnailFormat() const
uint16_t getStartFrame() const
static constexpr auto FORMAT_DEFAULT
This value is only valid when passed to VTF::create through CreationOptions or VTF::setFormat.
bool hasThumbnailData() const
void setReflectivity(sourcepp::math::Vec3f newReflectivity)
const std::vector< Resource > & getResources() const
void removeHotspotDataResource()
void setVersion(uint32_t newVersion)
std::vector< std::byte > saveImageToFile(uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0, ImageConversion::FileFormat fileFormat=ImageConversion::FileFormat::DEFAULT) const
Resource * getResourceInternal(Resource::Type type)
void computeThumbnail(ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
std::vector< std::byte > bake() const
bool hasImageData() const
ImageConversion::ResizeMethod imageWidthResizeMethod
std::span< const std::byte > getFallbackDataRaw(uint8_t mip=0, uint16_t frame=0, uint8_t face=0) const
void removeParticleSheetResource()
std::vector< std::byte > getFallbackDataAs(ImageFormat newFormat, uint8_t mip=0, uint16_t frame=0, uint8_t face=0) const
static bool create(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, const std::filesystem::path &vtfPath, const CreationOptions &options)
std::vector< std::byte > getImageDataAsRGBA8888(uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
uint16_t getFrameCount() const
uint8_t getMipCount() const
void setLODResource(uint8_t u, uint8_t v, uint8_t u360=0, uint8_t v360=0)
void removeResourceInternal(Resource::Type type)
std::vector< std::byte > getFallbackDataAsRGBA8888(uint8_t mip=0, uint16_t frame=0, uint8_t face=0) const
bool setFrameCount(uint16_t newFrameCount)
ImageConversion::ResizeMethod getImageWidthResizeMethod() const
std::vector< std::byte > getImageDataAs(ImageFormat newFormat, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
static ImageFormat getDefaultCompressedFormat(ImageFormat inputFormat, uint32_t version, bool isCubeMap)
void setStartFrame(uint16_t newStartFrame)
std::vector< std::byte > getThumbnailDataAs(ImageFormat newFormat) const
std::span< const std::byte > getImageDataRaw(uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
sourcepp::math::Vec3f getReflectivity() const
uint8_t getFallbackMipCount() const
std::vector< std::byte > getThumbnailDataAsRGBA8888() const
uint8_t getConsoleMipScale() const
uint16_t getDepth(uint8_t mip=0) const
uint16_t getPaddedHeight(uint8_t mip=0) const
static constexpr auto FORMAT_UNCHANGED
This value is only valid when passed to VTF::create through CreationOptions.
std::span< const std::byte > getThumbnailDataRaw() const
bool setMipCount(uint8_t newMipCount)
void setExtendedFlagsResource(uint32_t value)
CompressionMethod getCompressionMethod() const
uint16_t getPaddedWidth(uint8_t mip=0) const
void setCRCResource(uint32_t value)
void setCompressionMethod(CompressionMethod newCompressionMethod)
bool setFaceCount(bool isCubeMap)
uint8_t getThumbnailHeight() const
void setConsoleMipScale(uint8_t consoleMipScale_)
const Resource * getResource(Resource::Type type) const
uint32_t getFlags() const
void setParticleSheetResource(const SHT &value)
@ FLAG_V0_MULTI_BIT_ALPHA
uint32_t getVersion() const
int16_t getCompressionLevel() const
void setHotspotDataResource(const HOT &value)
void removeFlags(uint32_t flags_)
void setResourceInternal(Resource::Type type, std::span< const std::byte > data_)
bool setDepth(uint16_t newDepth)
uint64_t estimateBakeSize() const
std::vector< Resource > resources
bool setImage(std::span< const std::byte > imageData_, ImageFormat format_, uint16_t width_, uint16_t height_, ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
std::vector< std::byte > getParticleSheetFrameDataRaw(uint16_t &spriteWidth, uint16_t &spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds=0, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
This is a convenience function. You're best off uploading the bounds to the GPU and scaling the UV th...
void removeExtendedFlagsResource()
std::optional< std::vector< std::byte > > compressValveLZMA(std::span< const std::byte > data, uint8_t compressLevel=6)
constexpr auto VALVE_LZMA_SIGNATURE
std::optional< std::vector< std::byte > > decompressValveLZMA(std::span< const std::byte > data)
bool writeFileBuffer(const std::filesystem::path &filepath, std::span< const std::byte > buffer)
std::vector< std::byte > readFileBuffer(const std::filesystem::path &filepath, std::size_t startOffset=0)
constexpr uint16_t paddingForAlignment(uint16_t alignment, uint64_t n)
constexpr uint32_t log2ceil(uint32_t value)
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::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.
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::vector< std::byte > gammaCorrectImageData(std::span< const 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 > invertGreenChannelForImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height)
Invert the green channel. Meant for converting normal maps between OpenGL and DirectX formats.
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.
constexpr uint8_t getMaximumMipCount(uint16_t width, uint16_t height, uint16_t depth=1)
Calculate the largest mip count a texture with the given width, height, and depth can contain.
constexpr std::pair< uint16_t, uint16_t > getMipDims(uint8_t mip, uint16_t width, uint16_t height, bool addCompressedFormatPadding=false)
Get the width and height at a given mip level.
constexpr uint16_t getMipDim(uint8_t mip, uint16_t dim, bool addCompressedFormatPadding=false)
Get the dimension at a given mip level.
std::vector< std::byte > convertP8ImageDataToBGRA8888(std::span< const std::byte > paletteData, std::span< const std::byte > imageData)
Converts a paletted image to something usable.
constexpr uint32_t VTF_SIGNATURE
constexpr uint32_t XTF_SIGNATURE
constexpr uint32_t VTFX_SIGNATURE
constexpr uint32_t VTF3_SIGNATURE
ConvertedData convertData() const
std::variant< std::monostate, SHT, uint32_t, std::tuple< uint8_t, uint8_t, uint8_t, uint8_t >, std::string, HOT > ConvertedData
@ TYPE_PARTICLE_SHEET_DATA
std::span< std::byte > data
uint16_t initialFrameCount
float compressedFormatQuality
bool computeTransparencyFlags
ImageConversion::ResizeFilter filter
ImageConversion::ResizeMethod heightResizeMethod
CompressionMethod compressionMethod
ImageConversion::ResizeMethod widthResizeMethod