11#include <unordered_map>
14#ifdef SOURCEPP_BUILD_WITH_TBB
18#ifdef SOURCEPP_BUILD_WITH_THREADS
23#include <BufferStream.h>
37[[nodiscard]] std::vector<std::byte> compressData(std::span<const std::byte> data, int16_t level,
CompressionMethod method) {
41 mz_ulong compressedSize = mz_compressBound(data.size());
42 std::vector<std::byte> out(compressedSize);
45 while ((status = mz_compress2(
reinterpret_cast<unsigned char*
>(out.data()), &compressedSize,
reinterpret_cast<const unsigned char*
>(data.data()), data.size(), level)) == MZ_BUF_ERROR) {
47 out.resize(compressedSize);
50 if (status != MZ_OK) {
53 out.resize(compressedSize);
61 const auto expectedSize = ZSTD_compressBound(data.size());
62 std::vector<std::byte> out(expectedSize);
64 const auto compressedSize = ZSTD_compress(out.data(), expectedSize, data.data(), data.size(), level);
65 if (ZSTD_isError(compressedSize)) {
69 out.resize(compressedSize);
82template<std::
unsigned_
integral T,
bool ExistingDataIsSwizzled>
83constexpr void swizzleUncompressedImageData(std::span<std::byte> inputData, std::span<std::byte> outputData,
ImageFormat format, uint16_t width, uint16_t height, uint16_t depth) {
90 ](uint32_t x, uint32_t y, uint32_t z) {
91 auto widthL2m = widthL2;
92 auto heightL2m = heightL2;
93 auto depthL2m = depthL2;
95 uint32_t shiftCount = 0;
98 offset |= (z & 1) << shiftCount++;
101 if (heightL2m --> 0) {
102 offset |= (y & 1) << shiftCount++;
105 if (widthL2m --> 0) {
106 offset |= (x & 1) << shiftCount++;
109 }
while (x || y || z);
113 const auto* inputPtr =
reinterpret_cast<const T*
>(inputData.data());
114 auto* outputPtr =
reinterpret_cast<T*
>(outputData.data());
116 for (uint16_t x = 0; x < width; x++) {
117 for (uint16_t y = 0; y < height; y++) {
118 for (uint16_t z = 0; z < depth; z++) {
119 if constexpr (ExistingDataIsSwizzled) {
120 *outputPtr++ =
reinterpret_cast<const T*
>(inputData.data())[zIndex(x, y, z)];
122 reinterpret_cast<T*
>(outputData.data())[zIndex(x, y, z)] = *inputPtr++;
129template<
bool ExistingDataIsSwizzled>
130void swizzleUncompressedImageDataXBOX(std::span<std::byte> inputData, std::span<std::byte> outputData,
ImageFormat format, uint16_t width, uint16_t height, uint16_t depth) {
131 const auto zIndex = [
135 ](int32_t x, int32_t y, int32_t z) {
136 int widthL2m = widthL2;
137 int heightL2m = heightL2;
138 int depthL2m = depthL2;
140 uint32_t shiftCount = 0;
142 while (widthL2m > 0 || heightL2m > 0 || depthL2m > 0) {
144 offset |= (x & 1) << shiftCount++;
149 offset |= (y & 1) << shiftCount++;
154 offset |= (z & 1) << shiftCount++;
164 uint32_t linearIndex = 0;
165 for (uint16_t z = 0; z < depth; z++) {
166 for (uint16_t y = 0; y < height; y++) {
167 for (uint16_t x = 0; x < width; x++) {
168 const auto codedIndex = zIndex(x, y, z);
169 for (uint32_t b = 0; b < stride; b++) {
170 if constexpr (ExistingDataIsSwizzled) {
171 outputData[linearIndex * stride + b] = inputData[codedIndex * stride + b];
173 outputData[codedIndex * stride + b] = inputData[linearIndex * stride + b];
182template<
bool ConvertingFromSource>
183void 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) {
196 std::ranges::copy(newData, imageData.begin());
204 std::span dxtData{
reinterpret_cast<uint16_t*
>(imageData.data()), imageData.size() /
sizeof(uint16_t)};
206#ifdef SOURCEPP_BUILD_WITH_TBB
207 std::execution::par_unseq,
209 dxtData.begin(), dxtData.end(), [](uint16_t& value) {
210 BufferStream::swap_endian(&value);
222 std::vector<std::byte> out(imageData.size());
223 for(
int mip = mipCount - 1; mip >= 0; mip--) {
225 for (
int frame = 0; frame < frameCount; frame++) {
226 for (
int face = 0; face < faceCount; face++) {
227 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, format, mip, mipCount, frame, frameCount, face, faceCount, width, height)) {
228 std::span imageDataSpan{imageData.data() + offset, length * mipDepth};
229 std::span outSpan{out.data() + offset, length * mipDepth};
231 ::swizzleUncompressedImageDataXBOX<ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
233 ::swizzleUncompressedImageData<uint32_t, ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
235 ::swizzleUncompressedImageData<uint16_t, ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
237 ::swizzleUncompressedImageData<uint8_t, ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, mipDepth);
243 std::memcpy(imageData.data(), out.data(), out.size());
247template<
bool ConvertingFromDDS>
248[[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) {
249 std::vector<std::byte> reorderedImageData;
251 BufferStream reorderedStream{reorderedImageData};
253 if constexpr (ConvertingFromDDS) {
254 for (
int i = mipCount - 1; i >= 0; i--) {
256 for (
int j = 0; j < frameCount; j++) {
257 for (
int k = 0; k < faceCount; k++) {
258 for (
int l = 0; l < mipDepth; l++) {
259 uint32_t oldOffset, length;
260 if (!
ImageFormatDetails::getDataPositionXBOX(oldOffset, length, padded, format, i, mipCount, j, frameCount, k, faceCount, width, height, l, depth)) {
264 reorderedStream << imageData.subspan(oldOffset, length);
270 for (
int j = 0; j < frameCount; j++) {
271 for (
int k = 0; k < faceCount; k++) {
272 for (
int i = 0; i < mipCount; i++) {
274 for (
int l = 0; l < mipDepth; l++) {
275 uint32_t oldOffset, length;
276 if (!
ImageFormatDetails::getDataPosition(oldOffset, length, format, i, mipCount, j, frameCount, k, faceCount, width, height, l, depth)) {
280 reorderedStream << imageData.subspan(oldOffset, length);
284 if (padded && j + 1 != frameCount && reorderedStream.tell() > 512) {
291 return reorderedImageData;
297 switch (this->
type) {
299 if (this->data.size() <=
sizeof(uint32_t)) {
302 return SHT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
306 if (this->data.size() !=
sizeof(uint32_t)) {
309 return *
reinterpret_cast<const uint32_t*
>(this->data.data());
311 if (this->data.size() !=
sizeof(uint32_t)) {
314 return std::make_tuple(
315 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 0),
316 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 1),
317 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 2),
318 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 3));
321 if (this->data.size() <=
sizeof(uint32_t)) {
324 return std::string(
reinterpret_cast<const char*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data()));
326 if (this->data.size() <=
sizeof(uint32_t)) {
329 return HOT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
337 static constexpr auto PALETTE_FRAME_SIZE = 256 *
sizeof(ImagePixel::BGRA8888);
338 if (this->data.size() % PALETTE_FRAME_SIZE != 0 || PALETTE_FRAME_SIZE * frame > this->data.size()) {
341 return {this->data.data() + PALETTE_FRAME_SIZE * frame, this->data.data() + PALETTE_FRAME_SIZE * (frame + 1)};
357 return std::get<std::tuple<uint8_t, uint8_t, uint8_t, uint8_t>>(this->
convertData());
373 if (this->data.size() <
sizeof(uint32_t) * 2) {
376 return static_cast<int16_t
>(BufferStream{this->data}.skip<uint32_t>().read<uint32_t>() & 0xffff);
380 if (this->data.size() <
sizeof(uint32_t) * 2) {
383 const auto method =
static_cast<int16_t
>((BufferStream{this->data}.skip<uint32_t>().read<uint32_t>() & 0xffff0000) >> 16);
391 if (this->data.size() < ((mipCount - 1 - mip) * frameCount * faceCount + frame * faceCount + face + 2) *
sizeof(uint32_t)) {
394 return BufferStream{this->data}.skip<uint32_t>((mipCount - 1 - mip) * frameCount * faceCount + frame * faceCount + face + 2).read<uint32_t>();
401VTF::VTF(std::vector<std::byte>&& vtfData,
bool parseHeaderOnly,
bool hdr)
402 :
data(std::move(vtfData)) {
403 BufferStreamReadOnly stream{this->data};
405 if (
const auto signature = stream.read<uint32_t>(); signature ==
VTF_SIGNATURE) {
406 stream >> this->platform;
407 if (this->platform != PLATFORM_PC) {
410 stream >> this->version;
415 stream.set_big_endian(
true);
416 stream >> this->platform;
417 if (this->platform != PLATFORM_X360 && this->platform != PLATFORM_PS3_ORANGEBOX && this->platform != PLATFORM_PS3_PORTAL2) {
420 stream >> this->version;
421 if (this->version != 8) {
426 this->platform = PLATFORM_PS3_PORTAL2;
432 stream >> this->platform;
433 if (this->platform != PLATFORM_XBOX) {
436 stream >> this->version;
437 if (this->version != 0) {
446 const auto headerSize = stream.read<uint32_t>();
448 const auto readResources = [
this, &stream](uint32_t resourceCount) {
450 this->resources.reserve(resourceCount);
451 for (
int i = 0; i < resourceCount; i++) {
452 auto& [type, flags_, data_] = this->resources.emplace_back();
454 auto typeAndFlags = stream.read<uint32_t>();
455 if (stream.is_big_endian()) {
457 BufferStream::swap_endian(&typeAndFlags);
461 data_ = stream.read_span<std::byte>(4);
464 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(data_.data()));
473 std::ranges::sort(this->resources, [](
const Resource& lhs,
const Resource& rhs) {
483 return *
reinterpret_cast<uint32_t*
>(lhs.
data.data()) < *
reinterpret_cast<uint32_t*
>(rhs.
data.data());
488 for (
auto& resource : this->resources) {
491 const auto lastOffset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
492 const auto currentOffset = *
reinterpret_cast<uint32_t*
>(resource.data.data());
493 const auto curPos = stream.tell();
494 stream.seek(lastOffset);
495 lastResource->data = stream.read_span<std::byte>(currentOffset - lastOffset);
496 stream.seek(
static_cast<int64_t
>(curPos));
498 lastResource = &resource;
502 const auto offset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
503 const auto curPos = stream.tell();
505 lastResource->data = stream.read_span<std::byte>(stream.size() - offset);
506 stream.seek(
static_cast<int64_t
>(curPos));
511 const auto postHeaderReadTransform = [
this, hdr] {
515 if (this->format ==
ImageFormat::DXT1 && this->flags & (FLAG_V0_ONE_BIT_ALPHA | FLAG_V0_MULTI_BIT_ALPHA)) {
521 this->format =
static_cast<ImageFormat>(
static_cast<int32_t
>(this->format) - 3);
528 if (this->platform == PLATFORM_PC) {
537 if (this->flags & FLAG_V0_NO_MIP && this->mipCount > 1) {
538 this->removeFlags(FLAG_V0_NO_MIP);
542 switch (this->platform) {
543 case PLATFORM_UNKNOWN:
550 .read(this->frameCount)
551 .read(this->startFrame)
553 .read(this->reflectivity[0])
554 .read(this->reflectivity[1])
555 .read(this->reflectivity[2])
557 .read(this->bumpMapScale)
559 .read(this->mipCount);
561 postHeaderReadTransform();
565 stream >> this->thumbnailWidth >> this->thumbnailHeight;
566 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
572 if (this->version < 2) {
575 stream.read(this->depth);
578 if (parseHeaderOnly) {
583 if (this->version >= 3) {
585 auto resourceCount = stream.read<uint32_t>();
587 readResources(resourceCount);
589 this->opened = stream.tell() == headerSize;
591 if (this->opened && this->version >= 6) {
594 if (auxResource && imageResource) {
595 if (auxResource->getDataAsAuxCompressionLevel() != 0) {
596 const auto faceCount = this->getFaceCount();
597 std::vector<std::byte> decompressedImageData(
ImageFormatDetails::getDataLength(this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth));
598 uint32_t oldOffset = 0;
599 for (
int i = this->mipCount - 1; i >= 0; i--) {
600 for (
int j = 0; j < this->frameCount; j++) {
601 for (
int k = 0; k < faceCount; k++) {
602 uint32_t oldLength = auxResource->getDataAsAuxCompressionLength(i, this->mipCount, j, this->frameCount, k, faceCount);
603 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())) {
605 mz_ulong decompressedImageDataSize = newLength * this->depth;
606 switch (auxResource->getDataAsAuxCompressionMethod()) {
609 if (mz_uncompress(
reinterpret_cast<unsigned char*
>(decompressedImageData.data() + newOffset), &decompressedImageDataSize,
reinterpret_cast<const unsigned char*
>(imageResource->data.data() + oldOffset), oldLength) != MZ_OK) {
610 this->opened =
false;
615 if (
const auto decompressedSize = ZSTD_decompress(decompressedImageData.data() + newOffset, decompressedImageDataSize, imageResource->data.data() + oldOffset, oldLength); ZSTD_isError(decompressedSize) || decompressedSize != decompressedImageDataSize) {
616 this->opened =
false;
626 oldOffset += oldLength;
636 this->opened = stream.tell() == headerSize;
638 this->resources.reserve(2);
640 if (this->hasThumbnailData()) {
641 this->resources.push_back({
647 if (this->hasImageData()) {
648 this->resources.push_back({
651 .data = stream.read_span<std::byte>(stream.size() - stream.tell()),
657 this->flagsExtra = resource->getDataAsFlags();
661 this->compressionLevel = resource->getDataAsAuxCompressionLevel();
662 this->compressionMethod = resource->getDataAsAuxCompressionMethod();
667 case PLATFORM_XBOX: {
668 if (this->platform == PLATFORM_XBOX) {
669 uint16_t preloadSize = 0, imageOffset = 0;
675 .read(this->frameCount)
678 .read(this->reflectivity[0])
679 .read(this->reflectivity[1])
680 .read(this->reflectivity[2])
681 .read(this->bumpMapScale)
683 .read(this->thumbnailWidth)
684 .read(this->thumbnailHeight)
685 .read(this->fallbackWidth)
686 .read(this->fallbackHeight)
687 .read(this->consoleMipScale)
690 const bool headerSizeIsAccurate = stream.tell() == headerSize;
695 postHeaderReadTransform();
698 const auto faceCount = (this->flags & FLAG_V0_ENVMAP) ? 6 : 1;
700 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
705 if (!parseHeaderOnly) {
706 this->resources.push_back({
709 .data = stream.read_span<std::byte>(thumbnailSize),
712 stream.skip(thumbnailSize);
717 const auto paletteSize = 256 *
sizeof(ImagePixel::BGRA8888) * this->frameCount;
718 if (!parseHeaderOnly) {
719 this->resources.push_back({
722 .data = stream.read_span<std::byte>(paletteSize),
725 stream.skip_u(paletteSize);
731 std::vector<std::byte> reorderedFallbackData;
732 if (this->hasFallbackData()) {
733 if (stream.tell() + fallbackSize != preloadSize) {
736 if (stream.tell() + fallbackSize != preloadSize) {
737 this->opened =
false;
740 this->fallbackMipCount = 1;
742 this->flags |= FLAG_V0_NO_MIP;
744 reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
false, stream.read_span<std::byte>(fallbackSize), this->format, this->fallbackMipCount, this->frameCount, faceCount, this->fallbackWidth, this->fallbackHeight, 1, ok);
746 this->opened =
false;
749 ::swapImageDataEndianForConsole<true>(reorderedFallbackData, this->format, this->fallbackMipCount, this->frameCount, faceCount, this->fallbackWidth, this->fallbackHeight, 1, this->platform);
752 this->opened = headerSizeIsAccurate;
753 if (parseHeaderOnly) {
758 std::vector<std::byte> reorderedImageData;
759 if (this->hasImageData()) {
760 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);
762 this->opened =
false;
765 ::swapImageDataEndianForConsole<true>(reorderedImageData, this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth, this->platform);
769 if (this->hasFallbackData()) {
772 if (this->hasImageData()) {
779 case PLATFORM_PS3_ORANGEBOX:
780 case PLATFORM_PS3_PORTAL2: {
781 uint8_t resourceCount;
787 .read(this->frameCount)
789 .read(this->consoleMipScale)
791 .read(this->reflectivity[0])
792 .read(this->reflectivity[1])
793 .read(this->reflectivity[2])
794 .read(this->bumpMapScale)
796 .skip<math::Vec4ui8>()
799 postHeaderReadTransform();
802 if (this->platform == PLATFORM_PS3_PORTAL2) {
803 stream.skip<uint32_t>();
808 if (parseHeaderOnly) {
813 this->resources.reserve(resourceCount);
814 readResources(resourceCount);
816 this->opened = stream.tell() == headerSize;
819 for (
const auto& resource : this->resources) {
823 this->setResourceInternal(resource.type, *decompressedData);
833 ::swapImageDataEndianForConsole<true>(resource.data, this->thumbnailFormat, 1, 1, 1, this->thumbnailWidth, this->thumbnailHeight, 1, this->platform);
835 if (this->platform == PLATFORM_PS3_ORANGEBOX) {
837 const auto reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
false, resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
838 if (!ok || reorderedFallbackData.size() != resource.data.size()) {
839 this->opened =
false;
842 std::memcpy(resource.data.data(), reorderedFallbackData.data(), resource.data.size());
844 ::swapImageDataEndianForConsole<true>(resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, this->platform);
846 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resource.data.data()));
854VTF::VTF(std::span<const std::byte> vtfData,
bool parseHeaderOnly,
bool hdr)
855 :
VTF(std::vector<std::byte>{vtfData.begin(), vtfData.end()}, parseHeaderOnly, hdr) {}
857VTF::VTF(
const std::filesystem::path& vtfPath,
bool parseHeaderOnly)
858 :
VTF(
fs::readFileBuffer(vtfPath), parseHeaderOnly, [](std::
string path) {
860 return path.ends_with(
".hdr.vtf") || path.ends_with(
".hdr.360.vtf") || path.ends_with(
".hdr.ps3.vtf");
861 }(vtfPath.filename().
string())) {}
869 this->data = other.
data;
871 this->width = other.
width;
872 this->height = other.
height;
878 this->format = other.
format;
887 this->depth = other.
depth;
889 this->resources.clear();
890 for (
const auto& [otherType, otherFlags, otherData] : other.
resources) {
891 auto& [
type, flags_, data_] = this->resources.emplace_back();
894 data_ = {this->data.data() + (otherData.data() - other.
data.data()), otherData.size()};
906VTF::operator bool()
const {
913 for (
int i = 0; i < writer.
mipCount; i++) {
916 for (
int l = 0; l < writer.
depth; l++) {
976 if (!writer.
setImage(imageDataResized,
format, requestedResizeWidth, requestedResizeHeight, options.
filter)) {
985 return writer.
bake(vtfPath);
989 std::vector<std::byte> imageData;
992 return create(imageData,
format, requestedResizeWidth, requestedResizeHeight, vtfPath, options);
1003 if (!writer.
setImage(imageDataResized,
format, requestedResizeWidth, requestedResizeHeight, options.
filter)) {
1018 std::vector<std::byte> imageData;
1021 return create(imageData,
format, requestedResizeWidth, requestedResizeHeight, options);
1036 return writer.
bake(vtfPath);
1056 return this->platform;
1060 if (this->platform == newPlatform) {
1065 const auto oldPlatform = this->platform;
1067 switch (newPlatform) {
1071 switch (oldPlatform) {
1098 this->platform = newPlatform;
1102 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, 6, this->depth);
1117 this->thumbnailHeight = 0;
1132 if (this->mipCount != maxMipCount) {
1139 return this->version;
1147 const auto faceCount = (this->
flags &
FLAG_V0_ENVMAP) ? (newVersion < 1 || newVersion > 4 ? 6 : 7) : 1;
1148 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, faceCount, this->depth);
1152 const bool srgb = this->
isSRGB();
1169 this->version = newVersion;
1211 if (newWidth == 0 || newHeight == 0) {
1221 if (this->width == newWidth && this->height == newHeight) {
1224 auto newMipCount = this->mipCount;
1226 newMipCount = maxMipCount;
1232 this->frameCount = 1;
1234 this->width = newWidth;
1235 this->height = newHeight;
1279 if (this->version >= 5) {
1281 }
else if (this->version == 4) {
1285 if (this->version >= 5) {
1287 }
else if (this->version == 4) {
1321 return this->format;
1332 this->format = newFormat;
1335 const auto oldFormat = this->format;
1337 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);
1339 this->
regenerateImageData(newFormat, this->width, this->height, this->mipCount, this->frameCount, this->
getFaceCount(), this->depth, filter, quality);
1343 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);
1349 return this->mipCount;
1358 newMipCount = maxMipCount;
1377 if (this->mipCount <= 1) {
1383 auto* outputDataPtr = imageResource->data.data();
1386#ifdef SOURCEPP_BUILD_WITH_THREADS
1387 std::vector<std::future<void>> futures;
1388 futures.reserve(this->frameCount * faceCount * this->depth);
1390 for (
int j = 0; j < this->frameCount; j++) {
1391 for (
int k = 0; k < faceCount; k++) {
1392#ifdef SOURCEPP_BUILD_WITH_THREADS
1393 futures.push_back(std::async(std::launch::async, [
this, filter, outputDataPtr, faceCount, j, k] {
1395 for (
int i = 1; i < this->mipCount; i++) {
1398 for (
int l = 0; l < mipDepth; l++) {
1399 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(i - 1, j, k, l), this->format, mipWidthM1, mipWidth, mipHeightM1, mipHeight, this->
isSRGB(), this->
getFlagsExtra() &
FLAG_EXTRA_USING_PREMULTIPLIED_ALPHA_RESIZE, filter);
1400 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) {
1401 std::memcpy(outputDataPtr + offset, mip.data(), length);
1405#ifdef SOURCEPP_BUILD_WITH_THREADS
1407 if (futures.size() >= std::thread::hardware_concurrency()) {
1408 for (
auto& future : futures) {
1416#ifdef SOURCEPP_BUILD_WITH_THREADS
1417 for (
auto& future : futures) {
1424 return this->frameCount;
1446 if (this->platform !=
PLATFORM_PC || this->version >= 6) {
1452 if (this->version >= 1 && this->version <= 4 && expectedLength < image->
data.size()) {
1455 if (expectedLength == image->data.size()) {
1506 static constexpr auto getReflectivityForImage = [](
const VTF& vtf, uint16_t frame, uint8_t face, uint16_t slice) {
1507 static constexpr auto getReflectivityForPixel = [](
const ImagePixel::RGBA8888* pixel) -> math::Vec3f {
1509 math::Vec3f ref{
static_cast<float>(pixel->r()),
static_cast<float>(pixel->g()),
static_cast<float>(pixel->b())};
1511 ref = ref / 255.f * 0.922f;
1520 for (uint64_t i = 0; i < rgba8888Data.size(); i += 4) {
1521 out += getReflectivityForPixel(
reinterpret_cast<ImagePixel::RGBA8888*
>(rgba8888Data.data() + i));
1523 return out / (rgba8888Data.size() /
sizeof(ImagePixel::RGBA8888));
1528#ifdef SOURCEPP_BUILD_WITH_THREADS
1529 if (this->frameCount > 1 || faceCount > 1 || this->depth > 1) {
1530 std::vector<std::future<math::Vec3f>> futures;
1531 futures.reserve(this->frameCount * faceCount * this->depth);
1534 for (
int j = 0; j < this->frameCount; j++) {
1535 for (
int k = 0; k < faceCount; k++) {
1536 for (
int l = 0; l < this->depth; l++) {
1537 futures.push_back(std::async(std::launch::async, [
this, j, k, l] {
1538 return getReflectivityForImage(*
this, j, k, l);
1540 if (futures.size() >= std::thread::hardware_concurrency()) {
1541 for (
auto& future : futures) {
1550 for (
auto& future : futures) {
1553 this->
reflectivity /= this->frameCount * faceCount * this->depth;
1555 this->
reflectivity = getReflectivityForImage(*
this, 0, 0, 0);
1559 for (
int j = 0; j < this->frameCount; j++) {
1560 for (
int k = 0; k < faceCount; k++) {
1561 for (
int l = 0; l < this->depth; l++) {
1562 this->
reflectivity += getReflectivityForImage(*
this, j, k, l);
1566 this->
reflectivity /= this->frameCount * faceCount * this->depth;
1587 return this->thumbnailHeight;
1611 return this->version >= 3;
1615 return this->resources;
1619 for (
const auto& resource : this->resources) {
1620 if (resource.type ==
type) {
1628 for (
auto& resource : this->resources) {
1629 if (resource.type ==
type) {
1637 if (
const auto* resource = this->
getResource(type); resource && resource->
data.size() == data_.size()) {
1638 std::memcpy(resource->data.data(), data_.data(), data_.size());
1643 std::unordered_map<Resource::Type, std::pair<std::vector<std::byte>, uint64_t>> resourceData;
1644 for (
const auto& [type_, flags_, dataSpan] : this->resources) {
1645 resourceData[type_] = {std::vector<std::byte>{dataSpan.begin(), dataSpan.end()}, 0};
1649 if (data_.empty()) {
1650 resourceData.erase(
type);
1652 resourceData[
type] = {{data_.begin(), data_.end()}, 0};
1657 BufferStream writer{this->data};
1659 for (
auto resourceType : resourceData | std::views::keys) {
1660 if (!resourceData.contains(resourceType)) {
1663 auto& [specificResourceData, offset] = resourceData[resourceType];
1664 if (resourceType ==
type) {
1665 if (!this->data.data()) {
1666 this->data.reserve(offset + specificResourceData.size());
1671 {this->data.data() + offset, specificResourceData.size()},
1674 *resourcePtr = newResource;
1676 this->resources.push_back(newResource);
1678 }
else if (!resourceData.contains(resourceType)) {
1681 offset = writer.tell();
1682 writer.write(specificResourceData);
1684 this->data.resize(writer.size());
1686 for (
auto& [type_, flags_, dataSpan] : this->resources) {
1687 if (resourceData.contains(type_)) {
1688 const auto& [specificResourceData, offset] = resourceData[type_];
1689 dataSpan = {this->data.data() + offset, specificResourceData.size()};
1695 std::erase_if(this->resources, [
type](
const Resource& resource) {
return resource.
type ==
type; });
1699 if (!newWidth) newWidth = 1;
1700 if (!newHeight) newHeight = 1;
1701 if (!newMipCount) newMipCount = 1;
1702 if (!newFrameCount) newFrameCount = 1;
1703 if (!newFaceCount) newFaceCount = 1;
1704 if (!newDepth) newDepth = 1;
1706 if (newMipCount > 1) {
1709 if (
ImageFormatDetails::compressed(newFormat) && ((newWidth > 4 && !std::has_single_bit(newWidth)) || (newHeight > 4 && !std::has_single_bit(newHeight)))) {
1719 if (this->format == newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->depth == newDepth) {
1723 std::vector<std::byte> newImageData;
1725 if (this->format != newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->depth == newDepth) {
1729 for (
int i = newMipCount - 1; i >= 0; i--) {
1732 int sourceMipIndex = 0;
1733 for (
int mip = 0, bestScore = std::numeric_limits<int>::max(); mip < this->mipCount; mip++) {
1735 if (mipWidth == newMipWidth && mipHeight == newMipHeight) {
1738 const auto widthDiff =
static_cast<int>(mipWidth) -
static_cast<int>(newMipWidth);
1739 const auto heightDiff =
static_cast<int>(mipHeight) -
static_cast<int>(newMipHeight);
1740 if (widthDiff < 0 || heightDiff < 0) {
1743 if (
const auto score = widthDiff + heightDiff; score < bestScore) {
1745 sourceMipIndex = mip;
1750 for (
int j = 0; j < newFrameCount; j++) {
1751 for (
int k = 0; k < newFaceCount; k++) {
1752 for (
int l = 0; l < newMipDepth; l++) {
1755 std::vector<std::byte> imageBacking;
1756 if (sourceMipWidth != newMipWidth || sourceMipHeight != newMipHeight) {
1758 imageSpan = imageBacking;
1760 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, newMipCount, j, newFrameCount, k, newFaceCount, newWidth, newHeight, l, newDepth) && imageSpan.size() == length) {
1761 std::memcpy(newImageData.data() + offset, imageSpan.data(), length);
1768 if (this->format != newFormat) {
1776 this->format = newFormat;
1777 this->width = newWidth;
1778 this->height = newHeight;
1779 this->mipCount = newMipCount;
1780 this->frameCount = newFrameCount;
1781 if (newFaceCount > 1) {
1786 this->depth = newDepth;
1792 const auto targetSize = 256 *
sizeof(ImagePixel::BGRA8888) * this->frameCount;
1793 if (palette->data.size() != targetSize) {
1794 std::vector<std::byte> paletteData{palette->data.begin(), palette->data.end()};
1795 paletteData.resize(targetSize);
1807 return palette->getDataAsPalette(frame);
1812std::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 {
1821 auto sht = shtResource->getDataAsParticleSheet();
1822 const auto* sequence = sht.getSequenceFromID(shtSequenceID);
1823 if (!sequence || sequence->frames.size() <= shtFrame || shtBounds >= sht.getFrameBoundsCount()) {
1831 const auto& bounds = sequence->frames[shtFrame].bounds[shtBounds];
1832 uint16_t x1 = std::clamp<uint16_t>(std::floor(bounds.x1 *
static_cast<float>(this->getWidth(mip))), 0, this->getWidth(mip));
1833 uint16_t y1 = std::clamp<uint16_t>(std::ceil( bounds.y1 *
static_cast<float>(this->getHeight(mip))), 0, this->getHeight(mip));
1834 uint16_t x2 = std::clamp<uint16_t>(std::ceil( bounds.x2 *
static_cast<float>(this->getWidth(mip))), 0, this->getHeight(mip));
1835 uint16_t y2 = std::clamp<uint16_t>(std::floor(bounds.y2 *
static_cast<float>(this->getHeight(mip))), 0, this->getWidth(mip));
1837 if (x1 > x2) [[unlikely]] {
1840 if (y1 > y2) [[unlikely]] {
1843 spriteWidth = x2 - x1;
1844 spriteWidth = y2 - y1;
1846 const auto out =
ImageConversion::cropImageData(this->
getImageDataRaw(mip, frame, face, slice), this->
getFormat(), this->
getWidth(mip), spriteWidth, x1, this->
getHeight(mip), spriteHeight, y1);
1854std::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 {
1855 return ImageConversion::convertImageDataToFormat(this->
getParticleSheetFrameDataRaw(spriteWidth, spriteHeight, shtSequenceID, shtFrame, shtBounds, mip, frame, face, slice), this->
getFormat(), newFormat, spriteWidth, spriteHeight);
1858std::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 {
1863 std::vector<std::byte> particleSheetData;
1864 BufferStream writer{particleSheetData};
1866 const auto bakedSheet = value.
bake();
1867 writer.write<uint32_t>(bakedSheet.size()).write(bakedSheet);
1868 particleSheetData.resize(writer.size());
1887 BufferStream writer{&lodData,
sizeof(lodData)};
1889 writer << u << v << u360 << v360;
1907 std::vector<std::byte> keyValuesData;
1908 BufferStream writer{keyValuesData};
1910 writer.write<uint32_t>(value.size()).write(value,
false);
1911 keyValuesData.resize(writer.size());
1921 std::vector<std::byte> authorInfo;
1922 BufferStream writer{authorInfo};
1924 writer.write<uint32_t>(value.size()).write(value,
false);
1925 authorInfo.resize(writer.size());
1935 std::vector<std::byte> hotspotData;
1936 BufferStream writer{hotspotData};
1938 const auto bakedHotspotData = value.
bake();
1939 writer.write<uint32_t>(bakedHotspotData.size()).write(bakedHotspotData);
1940 hotspotData.resize(writer.size());
1954 if (newCompressionLevel < 0) {
1981 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)) {
1982 return imageResource->data.subspan(offset, length);
1990 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)) {
1991 return imageResource->data.subspan(offset, length);
1998 const auto rawImageData = this->
getImageDataRaw(mip, frame, face, slice);
1999 if (rawImageData.empty()) {
2017 if (imageData_.empty()) {
2022 uint16_t resizedWidth = width_, resizedHeight = height_;
2029 mip = newMipCount - 1;
2034 this->
regenerateImageData(format_, resizedWidth, resizedHeight, mip + 1, frame + 1, face ? (face < 6 ? 6 : face) : 0, slice + 1);
2043 if (!imageResource) {
2046 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)) {
2047 std::vector<std::byte> image{imageData_.begin(), imageData_.end()};
2049 if (width_ != newWidth || height_ != newHeight) {
2052 if (format_ != this->format) {
2055 std::memcpy(imageResource->data.data() + offset, image.data(), image.size());
2062 int inputWidth, inputHeight, inputFrameCount;
2066 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
2071 auto [requestedResizeWidth, requestedResizeHeight] = resizeBounds.
clamp(inputWidth, inputHeight);
2074 if (inputFrameCount == 1) {
2075 if (requestedResizeWidth != inputWidth || requestedResizeHeight != inputHeight) {
2077 return this->
setImage(imageDataResized, inputFormat, requestedResizeWidth, requestedResizeHeight, filter, mip, frame, face, slice, quality);
2079 return this->
setImage(imageData_, inputFormat, inputWidth, inputHeight, filter, mip, frame, face, slice, quality);
2083 bool allSuccess =
true;
2085 for (
int currentFrame = 0; currentFrame < inputFrameCount; currentFrame++) {
2086 std::span currentFrameData{imageData_.data() + currentFrame * frameSize, imageData_.data() + currentFrame * frameSize + frameSize};
2087 if (requestedResizeWidth != inputWidth || requestedResizeHeight != inputHeight) {
2089 if (!this->
setImage(currentFrameResized, inputFormat, requestedResizeWidth, requestedResizeHeight, filter, mip, frame + currentFrame, face, slice, quality)) {
2092 }
else if (!this->
setImage(currentFrameData, inputFormat, inputWidth, inputHeight, filter, mip, frame + currentFrame, face, slice, quality)) {
2095 if (currentFrame == 0 && this->frameCount < frame + inputFrameCount) {
2108 if (
auto data_ = this->
saveImageToFile(mip, frame, face, slice, fileFormat); !data_.empty()) {
2120 return thumbnailResource->data;
2127 return thumbnailResource->data;
2134 if (rawThumbnailData.empty()) {
2151 this->thumbnailHeight = height_;
2156 int inputWidth, inputHeight, inputFrameCount;
2160 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
2165 if (inputFrameCount == 1) {
2166 this->
setThumbnail(imageData_, inputFormat, inputWidth, inputHeight, quality);
2172 this->
setThumbnail({imageData_.data(), frameSize}, inputFormat, inputWidth, inputHeight, quality);
2184 this->thumbnailHeight = 1;
2185 std::array newThumbnail{
2186 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[0], 0.f, 1.f) * 255.f)),
2187 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[1], 0.f, 1.f) * 255.f)),
2188 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[2], 0.f, 1.f) * 255.f)),
2194 this->thumbnailHeight = 16;
2195 this->
setResourceInternal(
Resource::TYPE_THUMBNAIL_DATA,
ImageConversion::convertImageDataToFormat(
ImageConversion::resizeImageData(this->
getImageDataRaw(), this->format, this->width, this->
thumbnailWidth, this->height, this->thumbnailHeight, this->
isSRGB(), this->
getFlagsExtra() &
FLAG_EXTRA_USING_PREMULTIPLIED_ALPHA_RESIZE, filter), this->format, this->
thumbnailFormat, this->
thumbnailWidth, this->thumbnailHeight, quality));
2202 this->thumbnailHeight = 0;
2223 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->
fallbackMipCount, frame, this->frameCount, face, this->
getFaceCount(), this->
fallbackWidth, this->
fallbackHeight)) {
2224 return fallbackResource->data.subspan(offset, length);
2232 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->
fallbackMipCount, frame, this->frameCount, face, this->
getFaceCount(), this->
fallbackWidth, this->
fallbackHeight)) {
2233 return fallbackResource->data.subspan(offset, length);
2241 if (rawFallbackData.empty()) {
2267 std::vector<std::byte> fallbackData;
2271 for (
int j = 0; j < this->frameCount; j++) {
2272 for (
int k = 0; k < faceCount; k++) {
2273 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(0, j, k, 0), this->format, this->width, mipWidth, this->height, mipHeight, this->
isSRGB(), this->
getFlagsExtra() &
FLAG_EXTRA_USING_PREMULTIPLIED_ALPHA_RESIZE, filter);
2274 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) {
2275 std::memcpy(fallbackData.data() + offset, mip.data(), length);
2296 if (
auto data_ = this->
saveFallbackToFile(mip, frame, face, fileFormat); !data_.empty()) {
2317 uint64_t vtfSize = 0;
2319 switch (this->platform) {
2323 switch (this->version) {
2326 vtfSize +=
sizeof(uint64_t);
2327 vtfSize +=
sizeof(uint32_t) * (2 + this->mipCount * this->frameCount * this->
getFaceCount());
2332 vtfSize += 11 +
sizeof(uint32_t) +
sizeof(uint64_t) * this->
getResources().size();
2333 for (
const auto& [resourceType, resourceFlags, resourceData] : this->
getResources()) {
2335 vtfSize += resourceData.size();
2339 vtfSize +=
sizeof(uint16_t);
2342 vtfSize +=
sizeof(uint32_t) * 9 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 4 +
sizeof(uint8_t) * 3;
2343 if (this->version < 3) {
2348 vtfSize += thumbnailResource->data.size();
2352 vtfSize += imageResource->data.size();
2355 vtfSize +=
static_cast<uint64_t
>(
static_cast<double>(imageResource->data.size()) / 1.75);
2364 vtfSize +=
sizeof(uint32_t) * 6 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 6 +
sizeof(uint8_t) * 6;
2367 vtfSize += thumbnailResource->data.size();
2371 vtfSize += paletteResource->data.size();
2381 if (vtfSize > 512) {
2386 vtfSize +=
sizeof(uint32_t);
2389 vtfSize +=
sizeof(uint32_t) * 7 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 5 +
sizeof(uint8_t) * 6;
2390 vtfSize +=
sizeof(uint64_t) * this->
getResources().size();
2392 for (
const auto& [resourceType, resourceFlags, resourceData] : this->
getResources()) {
2394 vtfSize += resourceData.size();
2399 vtfSize += imageResource->data.size();
2402 vtfSize +=
static_cast<uint64_t
>(
static_cast<double>(imageResource->data.size()) / 1.75);
2411 std::vector<std::byte> out;
2412 BufferStream writer{out};
2414 const auto writeResources = [&writer](uint64_t headerLengthPos,
const std::vector<Resource>& sortedResources) -> uint64_t {
2415 auto resourceHeaderCurPos = writer.tell();
2416 writer.pad<uint64_t>(sortedResources.size());
2417 auto resourceDataCurPos = writer.tell();
2418 writer.seek_u(headerLengthPos).write<uint32_t>(resourceDataCurPos);
2420 uint64_t resourceHeaderImagePos = 0;
2421 for (
const auto& resource : sortedResources) {
2422 writer.seek_u(resourceHeaderCurPos);
2424 uint32_t resourceType = resource.type;
2428 if (writer.is_big_endian()) {
2430 BufferStream::swap_endian(&resourceType);
2432 writer.write<uint32_t>(resourceType);
2435 resourceHeaderImagePos = writer.tell();
2436 writer.write<uint32_t>(0);
2437 resourceHeaderCurPos = writer.tell();
2442 writer.write(resource.data);
2443 resourceHeaderCurPos = writer.tell();
2445 writer.write<uint32_t>(resourceDataCurPos);
2446 resourceHeaderCurPos = writer.tell();
2447 writer.seek_u(resourceDataCurPos).write(resource.data);
2448 resourceDataCurPos = writer.tell();
2451 if (resourceHeaderImagePos) {
2452 writer.seek_u(resourceHeaderImagePos).write<uint32_t>(resourceDataCurPos);
2453 for (
auto& resource : sortedResources) {
2455 writer.seek_u(resourceDataCurPos).write(resource.data);
2459 return resourceDataCurPos;
2465 auto bakeFormat = this->format;
2466 auto bakeFlags = this->
flags;
2474 bakeFormat =
static_cast<ImageFormat>(
static_cast<int32_t
>(this->format) + 3);
2483 switch (this->platform) {
2490 .write(this->version);
2492 const auto headerLengthPos = writer.tell();
2493 writer.write<uint32_t>(0);
2497 .write(this->height)
2499 .write(this->frameCount)
2508 .write(this->mipCount)
2511 .write(this->thumbnailHeight);
2513 if (this->version >= 2) {
2514 writer << this->depth;
2517 if (this->version < 3) {
2519 const auto headerSize = writer.tell();
2520 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(headerSize);
2523 writer.write(thumbnailResource->data);
2526 writer.write(imageResource->data);
2529 std::vector<std::byte> auxCompressionResourceData;
2530 std::vector<std::byte> compressedImageResourceData;
2531 bool hasAuxCompression =
false;
2534 if (hasAuxCompression) {
2536 auxCompressionResourceData.resize((this->mipCount * this->frameCount * faceCount + 2) *
sizeof(uint32_t));
2537 BufferStream auxWriter{auxCompressionResourceData,
false};
2544 .write<uint32_t>(auxCompressionResourceData.size() -
sizeof(uint32_t))
2548 for (
int i = this->mipCount - 1; i >= 0; i--) {
2549 for (
int j = 0; j < this->frameCount; j++) {
2550 for (
int k = 0; k < faceCount; k++) {
2551 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)) {
2553 compressedImageResourceData.insert(compressedImageResourceData.end(), compressedData.begin(), compressedData.end());
2554 auxWriter.write<uint32_t>(compressedData.size());
2562 writer.pad(3).write<uint32_t>(this->
getResources().size() + hasAuxCompression + (this->
flagsExtra != 0)).pad(8);
2564 std::vector<Resource> sortedResources = this->
getResources();
2565 auto flagsExtraBacking = std::bit_cast<std::array<std::byte, 4>>(this->
flagsExtra);
2567 sortedResources.push_back({
2570 .data = flagsExtraBacking,
2573 if (hasAuxCompression) {
2574 for (
auto& resource : sortedResources) {
2576 resource.data = compressedImageResourceData;
2580 sortedResources.push_back({
2583 .data = auxCompressionResourceData,
2586 std::ranges::sort(sortedResources, [](
const Resource& lhs,
const Resource& rhs) {
2589 writeResources(headerLengthPos, sortedResources);
2595 writer.write<uint32_t>(0);
2597 const auto headerSizePos = writer.tell();
2602 .write(this->height)
2604 .write(this->frameCount);
2605 const auto preloadSizePos = writer.tell();
2606 writer.write<uint16_t>(0);
2607 const auto imageOffsetPos = writer.tell();
2614 .write(this->format)
2616 .write(this->thumbnailHeight)
2622 const auto headerSize = writer.tell();
2623 writer.seek_u(headerSizePos).write<uint32_t>(headerSize).seek_u(headerSize);
2626 writer.write(thumbnailResource->data);
2631 writer.write(paletteResource->data);
2635 bool hasFallbackResource =
false;
2637 hasFallbackResource =
true;
2638 std::vector<std::byte> reorderedFallbackData{fallbackResource->data.begin(), fallbackResource->data.end()};
2643 writer.write(reorderedFallbackData);
2649 const auto preloadSize = writer.tell();
2650 writer.seek_u(preloadSizePos).write<uint32_t>(preloadSize).seek_u(preloadSize);
2652 if (hasFallbackResource) {
2655 const auto imageOffset = writer.tell();
2656 writer.seek_u(imageOffsetPos).write<uint16_t>(imageOffset).seek_u(imageOffset);
2659 std::vector<std::byte> reorderedImageData{imageResource->data.begin(), imageResource->data.end()};
2660 ::swapImageDataEndianForConsole<false>(reorderedImageData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, this->platform);
2662 reorderedImageData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
true, reorderedImageData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, ok);
2664 writer.write(reorderedImageData);
2669 if (writer.tell() > 512) {
2679 writer.set_big_endian(
true);
2683 writer.set_big_endian(
true);
2684 writer << this->platform;
2686 writer.write<uint32_t>(8);
2688 const auto headerLengthPos = writer.tell();
2693 .write(this->height)
2695 .write(this->frameCount);
2696 const auto preloadPos = writer.tell();
2700 .write<uint8_t>(this->resources.size())
2706 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[0] * 255)), 0, 255))
2707 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[1] * 255)), 0, 255))
2708 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[2] * 255)), 0, 255))
2709 .write<uint8_t>(255);
2710 const auto compressionPos = writer.tell();
2711 writer.write<uint32_t>(0);
2715 writer.write<uint32_t>(0);
2718 std::vector<std::byte> thumbnailResourceData;
2719 std::vector<std::byte> imageResourceData;
2720 std::vector<Resource> sortedResources = this->
getResources();
2721 for (
auto& resource : sortedResources) {
2723 thumbnailResourceData = {resource.data.begin(), resource.data.end()};
2724 ::swapImageDataEndianForConsole<false>(thumbnailResourceData, this->
thumbnailFormat, 1, 1, 1, this->
thumbnailWidth, this->thumbnailHeight, 1, this->platform);
2725 resource.data = thumbnailResourceData;
2729 imageResourceData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
false, resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
2730 if (!ok || imageResourceData.size() != resource.data.size()) {
2734 imageResourceData = {resource.data.begin(), resource.data.end()};
2736 ::swapImageDataEndianForConsole<false>(imageResourceData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, this->platform);
2746 fixedCompressionLevel = 6;
2749 imageResourceData = std::move(*compressedData);
2750 const auto curPos = writer.tell();
2751 writer.seek_u(compressionPos).write<uint32_t>(imageResourceData.size()).seek_u(curPos);
2755 resource.data = imageResourceData;
2759 std::ranges::sort(sortedResources, [](
const Resource& lhs,
const Resource& rhs) {
2762 const auto resourceDataImagePos = writeResources(headerLengthPos, sortedResources);
2763 writer.seek_u(preloadPos).write(std::max<uint16_t>(resourceDataImagePos, 2048));
2767 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)
CompressionMethod compressionMethod
void computeReflectivity()
uint8_t getThumbnailWidth() const
std::vector< std::byte > saveThumbnailToFile(ImageConversion::FileFormat fileFormat=ImageConversion::FileFormat::DEFAULT) const
bool hasNativeResourceSupport() const
ImageFormat getFormat() const
void setPlatform(Platform newPlatform)
uint16_t getHeight(uint8_t mip=0) const
VTF & operator=(const VTF &other)
void removeFlagsExtra(uint32_t flags_)
uint8_t getPaddedFallbackHeight(uint8_t mip=0) const
uint16_t getCompressionLevel() 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...
void addFlagsExtra(uint32_t flags_)
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
uint16_t compressionLevel
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
uint8_t getFallbackWidth(uint8_t mip=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)
void removeAuthorInfoResource()
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
void setFlagsExtra(uint32_t flags_)
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 setAuthorInfoResource(std::string_view value)
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
uint8_t getFallbackHeight(uint8_t mip=0) const
uint32_t getVersion() const
@ FLAG_EXTRA_USING_PREMULTIPLIED_ALPHA_RESIZE
void setHotspotDataResource(const HOT &value)
void removeFlags(uint32_t flags_)
uint32_t getFlagsExtra() const
void setResourceInternal(Resource::Type type, std::span< const std::byte > data_)
bool setDepth(uint16_t newDepth)
uint64_t estimateBakeSize() const
std::vector< Resource > resources
uint8_t getPaddedFallbackWidth(uint8_t mip=0) const
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)
void toLower(std::string &input)
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 > 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.
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 > resizeImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t height, uint16_t newHeight, bool srgb, bool premultipliedAlpha, 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
@ SOURCEPP_CONSOLE_RGBA16161616_HDR
@ SOURCEPP_RGBA16161616_HDR
std::pair< uint16_t, uint16_t > clamp(uint16_t width, uint16_t height) const
std::string getDataAsAuthorInfo() const
HOT getDataAsHotspotData() const
std::tuple< uint8_t, uint8_t, uint8_t, uint8_t > getDataAsLODControlInfo() const
ConvertedData convertData() const
std::string getDataAsKeyValuesData() const
int16_t getDataAsAuxCompressionLevel() const
CompressionMethod getDataAsAuxCompressionMethod() const
SHT getDataAsParticleSheet() const
std::variant< std::monostate, SHT, uint32_t, std::tuple< uint8_t, uint8_t, uint8_t, uint8_t >, std::string, HOT > ConvertedData
uint32_t getDataAsFlags() const
uint32_t getDataAsCRC() const
uint32_t getDataAsAuxCompressionLength(uint8_t mip, uint8_t mipCount, uint16_t frame, uint16_t frameCount, uint16_t face, uint16_t faceCount) const
@ TYPE_PARTICLE_SHEET_DATA
std::vector< std::byte > getDataAsPalette(uint16_t frame) const
std::span< std::byte > data
uint16_t initialFrameCount
ImageConversion::ResizeBounds resizeBounds
float compressedFormatQuality
bool computeTransparencyFlags
ImageConversion::ResizeFilter filter
ImageConversion::ResizeMethod heightResizeMethod
CompressionMethod compressionMethod
ImageConversion::ResizeMethod widthResizeMethod