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())}};
305 if (this->data.size() !=
sizeof(uint32_t)) {
308 return *
reinterpret_cast<const uint32_t*
>(this->data.data());
310 if (this->data.size() !=
sizeof(uint32_t)) {
313 return std::make_tuple(
314 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 0),
315 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 1),
316 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 2),
317 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 3));
320 if (this->data.size() <=
sizeof(uint32_t)) {
323 return std::string(
reinterpret_cast<const char*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data()));
325 if (this->data.size() <=
sizeof(uint32_t)) {
328 return HOT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
336 static constexpr auto PALETTE_FRAME_SIZE = 256 *
sizeof(ImagePixel::BGRA8888);
337 if (this->data.size() % PALETTE_FRAME_SIZE != 0 || PALETTE_FRAME_SIZE * frame > this->data.size()) {
340 return {this->data.data() + PALETTE_FRAME_SIZE * frame, this->data.data() + PALETTE_FRAME_SIZE * (frame + 1)};
356 return std::get<std::tuple<uint8_t, uint8_t, uint8_t, uint8_t>>(this->
convertData());
372 if (this->data.size() <
sizeof(uint32_t) * 2) {
375 return static_cast<int16_t
>(BufferStream{this->data}.skip<uint32_t>().read<uint32_t>() & 0xffff);
379 if (this->data.size() <
sizeof(uint32_t) * 2) {
382 const auto method =
static_cast<int16_t
>((BufferStream{this->data}.skip<uint32_t>().read<uint32_t>() & 0xffff0000) >> 16);
390 if (this->data.size() < ((mipCount - 1 - mip) * frameCount * faceCount + frame * faceCount + face + 2) *
sizeof(uint32_t)) {
393 return BufferStream{this->data}.skip<uint32_t>((mipCount - 1 - mip) * frameCount * faceCount + frame * faceCount + face + 2).read<uint32_t>();
400VTF::VTF(std::vector<std::byte>&& vtfData,
bool parseHeaderOnly,
bool hdr)
401 :
data(std::move(vtfData)) {
402 BufferStreamReadOnly stream{this->data};
404 if (
const auto signature = stream.read<uint32_t>(); signature ==
VTF_SIGNATURE) {
405 stream >> this->platform;
406 if (this->platform != PLATFORM_PC) {
409 stream >> this->version;
414 stream.set_big_endian(
true);
415 stream >> this->platform;
416 if (this->platform != PLATFORM_X360 && this->platform != PLATFORM_PS3_ORANGEBOX && this->platform != PLATFORM_PS3_PORTAL2) {
419 stream >> this->version;
420 if (this->version != 8) {
425 this->platform = PLATFORM_PS3_PORTAL2;
431 stream >> this->platform;
432 if (this->platform != PLATFORM_XBOX) {
435 stream >> this->version;
436 if (this->version != 0) {
445 const auto headerSize = stream.read<uint32_t>();
447 const auto readResources = [
this, &stream](uint32_t resourceCount) {
449 this->resources.reserve(resourceCount);
450 for (
int i = 0; i < resourceCount; i++) {
451 auto& [type, flags_, data_] = this->resources.emplace_back();
453 auto typeAndFlags = stream.read<uint32_t>();
454 if (stream.is_big_endian()) {
456 BufferStream::swap_endian(&typeAndFlags);
460 data_ = stream.read_span<std::byte>(4);
463 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(data_.data()));
472 std::ranges::sort(this->resources, [](
const Resource& lhs,
const Resource& rhs) {
482 return *
reinterpret_cast<uint32_t*
>(lhs.
data.data()) < *
reinterpret_cast<uint32_t*
>(rhs.
data.data());
487 for (
auto& resource : this->resources) {
490 const auto lastOffset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
491 const auto currentOffset = *
reinterpret_cast<uint32_t*
>(resource.data.data());
492 const auto curPos = stream.tell();
493 stream.seek(lastOffset);
494 lastResource->data = stream.read_span<std::byte>(currentOffset - lastOffset);
495 stream.seek(
static_cast<int64_t
>(curPos));
497 lastResource = &resource;
501 const auto offset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
502 const auto curPos = stream.tell();
504 lastResource->data = stream.read_span<std::byte>(stream.size() - offset);
505 stream.seek(
static_cast<int64_t
>(curPos));
510 const auto postHeaderReadTransform = [
this, hdr] {
514 if (this->format ==
ImageFormat::DXT1 && this->flags & (FLAG_V0_ONE_BIT_ALPHA | FLAG_V0_MULTI_BIT_ALPHA)) {
520 this->format =
static_cast<ImageFormat>(
static_cast<int32_t
>(this->format) - 3);
527 if (this->platform == PLATFORM_PC) {
536 if (this->flags & FLAG_V0_NO_MIP && this->mipCount > 1) {
537 this->removeFlags(FLAG_V0_NO_MIP);
541 switch (this->platform) {
542 case PLATFORM_UNKNOWN:
549 .read(this->frameCount)
550 .read(this->startFrame)
552 .read(this->reflectivity[0])
553 .read(this->reflectivity[1])
554 .read(this->reflectivity[2])
556 .read(this->bumpMapScale)
558 .read(this->mipCount);
560 postHeaderReadTransform();
564 stream >> this->thumbnailWidth >> this->thumbnailHeight;
565 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
571 if (this->version < 2) {
574 stream.read(this->depth);
577 if (parseHeaderOnly) {
582 if (this->version >= 3) {
584 auto resourceCount = stream.read<uint32_t>();
586 readResources(resourceCount);
588 this->opened = stream.tell() == headerSize;
590 if (this->opened && this->version >= 6) {
593 if (auxResource && imageResource) {
594 if (auxResource->getDataAsAuxCompressionLevel() != 0) {
595 const auto faceCount = this->getFaceCount();
596 std::vector<std::byte> decompressedImageData(
ImageFormatDetails::getDataLength(this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth));
597 uint32_t oldOffset = 0;
598 for (
int i = this->mipCount - 1; i >= 0; i--) {
599 for (
int j = 0; j < this->frameCount; j++) {
600 for (
int k = 0; k < faceCount; k++) {
601 uint32_t oldLength = auxResource->getDataAsAuxCompressionLength(i, this->mipCount, j, this->frameCount, k, faceCount);
602 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())) {
604 mz_ulong decompressedImageDataSize = newLength * this->depth;
605 switch (auxResource->getDataAsAuxCompressionMethod()) {
608 if (mz_uncompress(
reinterpret_cast<unsigned char*
>(decompressedImageData.data() + newOffset), &decompressedImageDataSize,
reinterpret_cast<const unsigned char*
>(imageResource->data.data() + oldOffset), oldLength) != MZ_OK) {
609 this->opened =
false;
614 if (
const auto decompressedSize = ZSTD_decompress(decompressedImageData.data() + newOffset, decompressedImageDataSize, imageResource->data.data() + oldOffset, oldLength); ZSTD_isError(decompressedSize) || decompressedSize != decompressedImageDataSize) {
615 this->opened =
false;
625 oldOffset += oldLength;
635 this->opened = stream.tell() == headerSize;
637 this->resources.reserve(2);
639 if (this->hasThumbnailData()) {
640 this->resources.push_back({
646 if (this->hasImageData()) {
647 this->resources.push_back({
650 .data = stream.read_span<std::byte>(stream.size() - stream.tell()),
656 this->compressionLevel = resource->getDataAsAuxCompressionLevel();
657 this->compressionMethod = resource->getDataAsAuxCompressionMethod();
662 case PLATFORM_XBOX: {
663 if (this->platform == PLATFORM_XBOX) {
664 uint16_t preloadSize = 0, imageOffset = 0;
670 .read(this->frameCount)
673 .read(this->reflectivity[0])
674 .read(this->reflectivity[1])
675 .read(this->reflectivity[2])
676 .read(this->bumpMapScale)
678 .read(this->thumbnailWidth)
679 .read(this->thumbnailHeight)
680 .read(this->fallbackWidth)
681 .read(this->fallbackHeight)
682 .read(this->consoleMipScale)
685 const bool headerSizeIsAccurate = stream.tell() == headerSize;
690 postHeaderReadTransform();
693 const auto faceCount = (this->flags & FLAG_V0_ENVMAP) ? 6 : 1;
695 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
700 if (!parseHeaderOnly) {
701 this->resources.push_back({
704 .data = stream.read_span<std::byte>(thumbnailSize),
707 stream.skip(thumbnailSize);
712 const auto paletteSize = 256 *
sizeof(ImagePixel::BGRA8888) * this->frameCount;
713 if (!parseHeaderOnly) {
714 this->resources.push_back({
717 .data = stream.read_span<std::byte>(paletteSize),
720 stream.skip_u(paletteSize);
726 std::vector<std::byte> reorderedFallbackData;
727 if (this->hasFallbackData()) {
728 if (stream.tell() + fallbackSize != preloadSize) {
731 if (stream.tell() + fallbackSize != preloadSize) {
732 this->opened =
false;
735 this->fallbackMipCount = 1;
737 this->flags |= FLAG_V0_NO_MIP;
739 reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
false, stream.read_span<std::byte>(fallbackSize), this->format, this->fallbackMipCount, this->frameCount, faceCount, this->fallbackWidth, this->fallbackHeight, 1, ok);
741 this->opened =
false;
744 ::swapImageDataEndianForConsole<true>(reorderedFallbackData, this->format, this->fallbackMipCount, this->frameCount, faceCount, this->fallbackWidth, this->fallbackHeight, 1, this->platform);
747 this->opened = headerSizeIsAccurate;
748 if (parseHeaderOnly) {
753 std::vector<std::byte> reorderedImageData;
754 if (this->hasImageData()) {
755 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);
757 this->opened =
false;
760 ::swapImageDataEndianForConsole<true>(reorderedImageData, this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->depth, this->platform);
764 if (this->hasFallbackData()) {
767 if (this->hasImageData()) {
774 case PLATFORM_PS3_ORANGEBOX:
775 case PLATFORM_PS3_PORTAL2: {
776 uint8_t resourceCount;
782 .read(this->frameCount)
784 .read(this->consoleMipScale)
786 .read(this->reflectivity[0])
787 .read(this->reflectivity[1])
788 .read(this->reflectivity[2])
789 .read(this->bumpMapScale)
791 .skip<math::Vec4ui8>()
794 postHeaderReadTransform();
797 if (this->platform == PLATFORM_PS3_PORTAL2) {
798 stream.skip<uint32_t>();
803 if (parseHeaderOnly) {
808 this->resources.reserve(resourceCount);
809 readResources(resourceCount);
811 this->opened = stream.tell() == headerSize;
814 for (
const auto& resource : this->resources) {
818 this->setResourceInternal(resource.type, *decompressedData);
828 ::swapImageDataEndianForConsole<true>(resource.data, this->thumbnailFormat, 1, 1, 1, this->thumbnailWidth, this->thumbnailHeight, 1, this->platform);
830 if (this->platform == PLATFORM_PS3_ORANGEBOX) {
832 const auto reorderedFallbackData = ::convertBetweenDDSAndVTFMipOrderForXBOX<true>(
false, resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
833 if (!ok || reorderedFallbackData.size() != resource.data.size()) {
834 this->opened =
false;
837 std::memcpy(resource.data.data(), reorderedFallbackData.data(), resource.data.size());
839 ::swapImageDataEndianForConsole<true>(resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, this->platform);
841 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resource.data.data()));
849VTF::VTF(std::span<const std::byte> vtfData,
bool parseHeaderOnly,
bool hdr)
850 :
VTF(std::vector<std::byte>{vtfData.begin(), vtfData.end()}, parseHeaderOnly, hdr) {}
852VTF::VTF(
const std::filesystem::path& vtfPath,
bool parseHeaderOnly)
853 :
VTF(
fs::readFileBuffer(vtfPath), parseHeaderOnly, [](std::
string path) {
855 return path.ends_with(
".hdr.vtf") || path.ends_with(
".hdr.360.vtf") || path.ends_with(
".hdr.ps3.vtf");
856 }(vtfPath.filename().
string())) {}
864 this->data = other.
data;
866 this->width = other.
width;
867 this->height = other.
height;
873 this->format = other.
format;
882 this->depth = other.
depth;
884 this->resources.clear();
885 for (
const auto& [otherType, otherFlags, otherData] : other.
resources) {
886 auto& [
type, flags_, data_] = this->resources.emplace_back();
889 data_ = {this->data.data() + (otherData.data() - other.
data.data()), otherData.size()};
901VTF::operator bool()
const {
908 for (
int i = 0; i < writer.
mipCount; i++) {
911 for (
int l = 0; l < writer.
depth; l++) {
969 if (requestedResizeWidth !=
width || requestedResizeHeight !=
height) {
971 if (!writer.
setImage(imageDataResized,
format, requestedResizeWidth, requestedResizeHeight, options.
filter)) {
980 return writer.
bake(vtfPath);
984 std::vector<std::byte> imageData;
987 return create(imageData,
format, requestedResizeWidth, requestedResizeHeight, vtfPath, options);
996 if (requestedResizeWidth !=
width || requestedResizeHeight !=
height) {
998 if (!writer.
setImage(imageDataResized,
format, requestedResizeWidth, requestedResizeHeight, options.
filter)) {
1013 std::vector<std::byte> imageData;
1016 return create(imageData,
format, requestedResizeWidth, requestedResizeHeight, options);
1030 return writer.
bake(vtfPath);
1049 return this->platform;
1053 if (this->platform == newPlatform) {
1058 const auto oldPlatform = this->platform;
1060 switch (newPlatform) {
1064 switch (oldPlatform) {
1091 this->platform = newPlatform;
1095 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, 6, this->depth);
1110 this->thumbnailHeight = 0;
1125 if (this->mipCount != maxMipCount) {
1132 return this->version;
1140 const auto faceCount = (this->
flags &
FLAG_V0_ENVMAP) ? (newVersion < 1 || newVersion > 4 ? 6 : 7) : 1;
1141 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, faceCount, this->depth);
1145 const bool srgb = this->
isSRGB();
1162 this->version = newVersion;
1204 if (newWidth == 0 || newHeight == 0) {
1214 if (this->width == newWidth && this->height == newHeight) {
1217 auto newMipCount = this->mipCount;
1219 newMipCount = maxMipCount;
1225 this->frameCount = 1;
1227 this->width = newWidth;
1228 this->height = newHeight;
1256 if (this->version >= 5) {
1258 }
else if (this->version == 4) {
1262 if (this->version >= 5) {
1264 }
else if (this->version == 4) {
1298 return this->format;
1309 this->format = newFormat;
1312 const auto oldFormat = this->format;
1314 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);
1316 this->
regenerateImageData(newFormat, this->width, this->height, this->mipCount, this->frameCount, this->
getFaceCount(), this->depth, filter, quality);
1320 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);
1326 return this->mipCount;
1335 newMipCount = maxMipCount;
1354 if (this->mipCount <= 1) {
1360 auto* outputDataPtr = imageResource->data.data();
1363#ifdef SOURCEPP_BUILD_WITH_THREADS
1364 std::vector<std::future<void>> futures;
1365 futures.reserve(this->frameCount * faceCount * this->depth);
1367 for (
int j = 0; j < this->frameCount; j++) {
1368 for (
int k = 0; k < faceCount; k++) {
1369#ifdef SOURCEPP_BUILD_WITH_THREADS
1370 futures.push_back(std::async(std::launch::async, [
this, filter, outputDataPtr, faceCount, j, k] {
1372 for (
int i = 1; i < this->mipCount; i++) {
1375 for (
int l = 0; l < mipDepth; l++) {
1376 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(i - 1, j, k, l), this->format, mipWidthM1, mipWidth, mipHeightM1, mipHeight, this->
isSRGB(), filter);
1377 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) {
1378 std::memcpy(outputDataPtr + offset, mip.data(), length);
1382#ifdef SOURCEPP_BUILD_WITH_THREADS
1384 if (futures.size() >= std::thread::hardware_concurrency()) {
1385 for (
auto& future : futures) {
1393#ifdef SOURCEPP_BUILD_WITH_THREADS
1394 for (
auto& future : futures) {
1401 return this->frameCount;
1423 if (this->platform !=
PLATFORM_PC || this->version >= 6) {
1429 if (this->version >= 1 && this->version <= 4 && expectedLength < image->
data.size()) {
1432 if (expectedLength == image->data.size()) {
1483 static constexpr auto getReflectivityForImage = [](
const VTF& vtf, uint16_t frame, uint8_t face, uint16_t slice) {
1484 static constexpr auto getReflectivityForPixel = [](
const ImagePixel::RGBA8888* pixel) -> math::Vec3f {
1486 math::Vec3f ref{
static_cast<float>(pixel->r()),
static_cast<float>(pixel->g()),
static_cast<float>(pixel->b())};
1488 ref = ref / 255.f * 0.922f;
1497 for (uint64_t i = 0; i < rgba8888Data.size(); i += 4) {
1498 out += getReflectivityForPixel(
reinterpret_cast<ImagePixel::RGBA8888*
>(rgba8888Data.data() + i));
1500 return out / (rgba8888Data.size() /
sizeof(ImagePixel::RGBA8888));
1505#ifdef SOURCEPP_BUILD_WITH_THREADS
1506 if (this->frameCount > 1 || faceCount > 1 || this->depth > 1) {
1507 std::vector<std::future<math::Vec3f>> futures;
1508 futures.reserve(this->frameCount * faceCount * this->depth);
1511 for (
int j = 0; j < this->frameCount; j++) {
1512 for (
int k = 0; k < faceCount; k++) {
1513 for (
int l = 0; l < this->depth; l++) {
1514 futures.push_back(std::async(std::launch::async, [
this, j, k, l] {
1515 return getReflectivityForImage(*
this, j, k, l);
1517 if (futures.size() >= std::thread::hardware_concurrency()) {
1518 for (
auto& future : futures) {
1527 for (
auto& future : futures) {
1530 this->
reflectivity /= this->frameCount * faceCount * this->depth;
1532 this->
reflectivity = getReflectivityForImage(*
this, 0, 0, 0);
1536 for (
int j = 0; j < this->frameCount; j++) {
1537 for (
int k = 0; k < faceCount; k++) {
1538 for (
int l = 0; l < this->depth; l++) {
1539 this->
reflectivity += getReflectivityForImage(*
this, j, k, l);
1543 this->
reflectivity /= this->frameCount * faceCount * this->depth;
1564 return this->thumbnailHeight;
1588 return this->version >= 3;
1592 return this->resources;
1596 for (
const auto& resource : this->resources) {
1597 if (resource.type ==
type) {
1605 for (
auto& resource : this->resources) {
1606 if (resource.type ==
type) {
1614 if (
const auto* resource = this->
getResource(type); resource && resource->
data.size() == data_.size()) {
1615 std::memcpy(resource->data.data(), data_.data(), data_.size());
1620 std::unordered_map<Resource::Type, std::pair<std::vector<std::byte>, uint64_t>> resourceData;
1621 for (
const auto& [type_, flags_, dataSpan] : this->resources) {
1622 resourceData[type_] = {std::vector<std::byte>{dataSpan.begin(), dataSpan.end()}, 0};
1626 if (data_.empty()) {
1627 resourceData.erase(
type);
1629 resourceData[
type] = {{data_.begin(), data_.end()}, 0};
1634 BufferStream writer{this->data};
1636 for (
auto resourceType : resourceData | std::views::keys) {
1637 if (!resourceData.contains(resourceType)) {
1640 auto& [specificResourceData, offset] = resourceData[resourceType];
1641 if (resourceType ==
type) {
1645 {this->data.data() + offset, specificResourceData.size()},
1648 *resourcePtr = newResource;
1650 this->resources.push_back(newResource);
1652 }
else if (!resourceData.contains(resourceType)) {
1655 offset = writer.tell();
1656 writer.write(specificResourceData);
1658 this->data.resize(writer.size());
1660 for (
auto& [type_, flags_, dataSpan] : this->resources) {
1661 if (resourceData.contains(type_)) {
1662 const auto& [specificResourceData, offset] = resourceData[type_];
1663 dataSpan = {this->data.data() + offset, specificResourceData.size()};
1669 std::erase_if(this->resources, [
type](
const Resource& resource) {
return resource.
type ==
type; });
1673 if (!newWidth) newWidth = 1;
1674 if (!newHeight) newHeight = 1;
1675 if (!newMipCount) newMipCount = 1;
1676 if (!newFrameCount) newFrameCount = 1;
1677 if (!newFaceCount) newFaceCount = 1;
1678 if (!newDepth) newDepth = 1;
1680 if (newMipCount > 1) {
1683 if (
ImageFormatDetails::compressed(newFormat) && ((newWidth > 4 && !std::has_single_bit(newWidth)) || (newHeight > 4 && !std::has_single_bit(newHeight)))) {
1693 if (this->format == newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->depth == newDepth) {
1697 std::vector<std::byte> newImageData;
1699 if (this->format != newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->depth == newDepth) {
1703 for (
int i = newMipCount - 1; i >= 0; i--) {
1706 int sourceMipIndex = 0;
1707 for (
int mip = 0, bestScore = std::numeric_limits<int>::max(); mip < this->mipCount; mip++) {
1709 if (mipWidth == newMipWidth && mipHeight == newMipHeight) {
1712 const auto widthDiff =
static_cast<int>(mipWidth) -
static_cast<int>(newMipWidth);
1713 const auto heightDiff =
static_cast<int>(mipHeight) -
static_cast<int>(newMipHeight);
1714 if (widthDiff < 0 || heightDiff < 0) {
1717 if (
const auto score = widthDiff + heightDiff; score < bestScore) {
1719 sourceMipIndex = mip;
1724 for (
int j = 0; j < newFrameCount; j++) {
1725 for (
int k = 0; k < newFaceCount; k++) {
1726 for (
int l = 0; l < newMipDepth; l++) {
1729 std::vector<std::byte> imageBacking;
1730 if (sourceMipWidth != newMipWidth || sourceMipHeight != newMipHeight) {
1732 imageSpan = imageBacking;
1734 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, newMipCount, j, newFrameCount, k, newFaceCount, newWidth, newHeight, l, newDepth) && imageSpan.size() == length) {
1735 std::memcpy(newImageData.data() + offset, imageSpan.data(), length);
1742 if (this->format != newFormat) {
1750 this->format = newFormat;
1751 this->width = newWidth;
1752 this->height = newHeight;
1753 this->mipCount = newMipCount;
1754 this->frameCount = newFrameCount;
1755 if (newFaceCount > 1) {
1760 this->depth = newDepth;
1766 const auto targetSize = 256 *
sizeof(ImagePixel::BGRA8888) * this->frameCount;
1767 if (palette->data.size() != targetSize) {
1768 std::vector<std::byte> paletteData{palette->data.begin(), palette->data.end()};
1769 paletteData.resize(targetSize);
1781 return palette->getDataAsPalette(frame);
1786std::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 {
1795 auto sht = shtResource->getDataAsParticleSheet();
1796 const auto* sequence = sht.getSequenceFromID(shtSequenceID);
1797 if (!sequence || sequence->frames.size() <= shtFrame || shtBounds >= sht.getFrameBoundsCount()) {
1805 const auto& bounds = sequence->frames[shtFrame].bounds[shtBounds];
1806 uint16_t x1 = std::clamp<uint16_t>(std::floor(bounds.x1 *
static_cast<float>(this->getWidth(mip))), 0, this->getWidth(mip));
1807 uint16_t y1 = std::clamp<uint16_t>(std::ceil( bounds.y1 *
static_cast<float>(this->getHeight(mip))), 0, this->getHeight(mip));
1808 uint16_t x2 = std::clamp<uint16_t>(std::ceil( bounds.x2 *
static_cast<float>(this->getWidth(mip))), 0, this->getHeight(mip));
1809 uint16_t y2 = std::clamp<uint16_t>(std::floor(bounds.y2 *
static_cast<float>(this->getHeight(mip))), 0, this->getWidth(mip));
1811 if (x1 > x2) [[unlikely]] {
1814 if (y1 > y2) [[unlikely]] {
1817 spriteWidth = x2 - x1;
1818 spriteWidth = y2 - y1;
1820 const auto out =
ImageConversion::cropImageData(this->
getImageDataRaw(mip, frame, face, slice), this->
getFormat(), this->
getWidth(mip), spriteWidth, x1, this->
getHeight(mip), spriteHeight, y1);
1828std::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 {
1829 return ImageConversion::convertImageDataToFormat(this->
getParticleSheetFrameDataRaw(spriteWidth, spriteHeight, shtSequenceID, shtFrame, shtBounds, mip, frame, face, slice), this->
getFormat(), newFormat, spriteWidth, spriteHeight);
1832std::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 {
1837 std::vector<std::byte> particleSheetData;
1838 BufferStream writer{particleSheetData};
1840 const auto bakedSheet = value.
bake();
1841 writer.write<uint32_t>(bakedSheet.size()).write(bakedSheet);
1842 particleSheetData.resize(writer.size());
1861 BufferStream writer{&lodData,
sizeof(lodData)};
1863 writer << u << v << u360 << v360;
1881 std::vector<std::byte> keyValuesData;
1882 BufferStream writer{keyValuesData};
1884 writer.write<uint32_t>(value.size()).write(value,
false);
1885 keyValuesData.resize(writer.size());
1895 std::vector<std::byte> authorInfo;
1896 BufferStream writer{authorInfo};
1898 writer.write<uint32_t>(value.size()).write(value,
false);
1899 authorInfo.resize(writer.size());
1909 std::vector<std::byte> hotspotData;
1910 BufferStream writer{hotspotData};
1912 const auto bakedHotspotData = value.
bake();
1913 writer.write<uint32_t>(bakedHotspotData.size()).write(bakedHotspotData);
1914 hotspotData.resize(writer.size());
1928 if (newCompressionLevel < 0) {
1955 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)) {
1956 return imageResource->data.subspan(offset, length);
1964 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)) {
1965 return imageResource->data.subspan(offset, length);
1972 const auto rawImageData = this->
getImageDataRaw(mip, frame, face, slice);
1973 if (rawImageData.empty()) {
1991 if (imageData_.empty()) {
1996 uint16_t resizedWidth = width_, resizedHeight = height_;
2003 mip = newMipCount - 1;
2008 this->
regenerateImageData(format_, resizedWidth, resizedHeight, mip + 1, frame + 1, face ? (face < 6 ? 6 : face) : 0, slice + 1);
2017 if (!imageResource) {
2020 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)) {
2021 std::vector<std::byte> image{imageData_.begin(), imageData_.end()};
2023 if (width_ != newWidth || height_ != newHeight) {
2026 if (format_ != this->format) {
2029 std::memcpy(imageResource->data.data() + offset, image.data(), image.size());
2036 int inputWidth, inputHeight, inputFrameCount;
2040 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
2045 auto [requestedResizeWidth, requestedResizeHeight] = resizeBounds.
clamp(inputWidth, inputHeight);
2048 if (inputFrameCount == 1) {
2049 if (requestedResizeWidth != inputWidth || requestedResizeHeight != inputHeight) {
2051 return this->
setImage(imageDataResized, inputFormat, requestedResizeWidth, requestedResizeHeight, filter, mip, frame, face, slice, quality);
2053 return this->
setImage(imageData_, inputFormat, inputWidth, inputHeight, filter, mip, frame, face, slice, quality);
2057 bool allSuccess =
true;
2059 for (
int currentFrame = 0; currentFrame < inputFrameCount; currentFrame++) {
2060 std::span currentFrameData{imageData_.data() + currentFrame * frameSize, imageData_.data() + currentFrame * frameSize + frameSize};
2061 if (requestedResizeWidth != inputWidth || requestedResizeHeight != inputHeight) {
2063 if (!this->
setImage(currentFrameResized, inputFormat, requestedResizeWidth, requestedResizeHeight, filter, mip, frame + currentFrame, face, slice, quality)) {
2066 }
else if (!this->
setImage(currentFrameData, inputFormat, inputWidth, inputHeight, filter, mip, frame + currentFrame, face, slice, quality)) {
2069 if (currentFrame == 0 && this->frameCount < frame + inputFrameCount) {
2082 if (
auto data_ = this->
saveImageToFile(mip, frame, face, slice, fileFormat); !data_.empty()) {
2094 return thumbnailResource->data;
2101 return thumbnailResource->data;
2108 if (rawThumbnailData.empty()) {
2125 this->thumbnailHeight = height_;
2130 int inputWidth, inputHeight, inputFrameCount;
2134 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
2139 if (inputFrameCount == 1) {
2140 this->
setThumbnail(imageData_, inputFormat, inputWidth, inputHeight, quality);
2146 this->
setThumbnail({imageData_.data(), frameSize}, inputFormat, inputWidth, inputHeight, quality);
2158 this->thumbnailHeight = 1;
2159 std::array newThumbnail{
2160 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[0], 0.f, 1.f) * 255.f)),
2161 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[1], 0.f, 1.f) * 255.f)),
2162 static_cast<std::byte
>(
static_cast<uint8_t
>(std::clamp(this->
reflectivity[2], 0.f, 1.f) * 255.f)),
2168 this->thumbnailHeight = 16;
2169 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));
2176 this->thumbnailHeight = 0;
2197 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->
fallbackMipCount, frame, this->frameCount, face, this->
getFaceCount(), this->
fallbackWidth, this->
fallbackHeight)) {
2198 return fallbackResource->data.subspan(offset, length);
2206 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, mip, this->
fallbackMipCount, frame, this->frameCount, face, this->
getFaceCount(), this->
fallbackWidth, this->
fallbackHeight)) {
2207 return fallbackResource->data.subspan(offset, length);
2215 if (rawFallbackData.empty()) {
2241 std::vector<std::byte> fallbackData;
2245 for (
int j = 0; j < this->frameCount; j++) {
2246 for (
int k = 0; k < faceCount; k++) {
2247 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(0, j, k, 0), this->format, this->width, mipWidth, this->height, mipHeight, this->
isSRGB(), filter);
2248 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) {
2249 std::memcpy(fallbackData.data() + offset, mip.data(), length);
2270 if (
auto data_ = this->
saveFallbackToFile(mip, frame, face, fileFormat); !data_.empty()) {
2291 uint64_t vtfSize = 0;
2293 switch (this->platform) {
2297 switch (this->version) {
2300 vtfSize +=
sizeof(uint64_t);
2301 vtfSize +=
sizeof(uint32_t) * (2 + this->mipCount * this->frameCount * this->
getFaceCount());
2306 vtfSize += 11 +
sizeof(uint32_t) +
sizeof(uint64_t) * this->
getResources().size();
2307 for (
const auto& [resourceType, resourceFlags, resourceData] : this->
getResources()) {
2309 vtfSize += resourceData.size();
2313 vtfSize +=
sizeof(uint16_t);
2316 vtfSize +=
sizeof(uint32_t) * 9 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 4 +
sizeof(uint8_t) * 3;
2317 if (this->version < 3) {
2322 vtfSize += thumbnailResource->data.size();
2326 vtfSize += imageResource->data.size();
2329 vtfSize +=
static_cast<uint64_t
>(
static_cast<double>(imageResource->data.size()) / 1.75);
2338 vtfSize +=
sizeof(uint32_t) * 6 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 6 +
sizeof(uint8_t) * 6;
2341 vtfSize += thumbnailResource->data.size();
2345 vtfSize += paletteResource->data.size();
2355 if (vtfSize > 512) {
2360 vtfSize +=
sizeof(uint32_t);
2363 vtfSize +=
sizeof(uint32_t) * 7 +
sizeof(
float) * 4 +
sizeof(uint16_t) * 5 +
sizeof(uint8_t) * 6;
2364 vtfSize +=
sizeof(uint64_t) * this->
getResources().size();
2366 for (
const auto& [resourceType, resourceFlags, resourceData] : this->
getResources()) {
2368 vtfSize += resourceData.size();
2373 vtfSize += imageResource->data.size();
2376 vtfSize +=
static_cast<uint64_t
>(
static_cast<double>(imageResource->data.size()) / 1.75);
2385 std::vector<std::byte> out;
2386 BufferStream writer{out};
2388 const auto writeResources = [&writer](uint64_t headerLengthPos,
const std::vector<Resource>& sortedResources) -> uint64_t {
2389 auto resourceHeaderCurPos = writer.tell();
2390 writer.pad<uint64_t>(sortedResources.size());
2391 auto resourceDataCurPos = writer.tell();
2392 writer.seek_u(headerLengthPos).write<uint32_t>(resourceDataCurPos);
2394 uint64_t resourceHeaderImagePos = 0;
2395 for (
const auto& resource : sortedResources) {
2396 writer.seek_u(resourceHeaderCurPos);
2398 uint32_t resourceType = resource.type;
2402 if (writer.is_big_endian()) {
2404 BufferStream::swap_endian(&resourceType);
2406 writer.write<uint32_t>(resourceType);
2409 resourceHeaderImagePos = writer.tell();
2410 writer.write<uint32_t>(0);
2411 resourceHeaderCurPos = writer.tell();
2416 writer.write(resource.data);
2417 resourceHeaderCurPos = writer.tell();
2419 writer.write<uint32_t>(resourceDataCurPos);
2420 resourceHeaderCurPos = writer.tell();
2421 writer.seek_u(resourceDataCurPos).write(resource.data);
2422 resourceDataCurPos = writer.tell();
2425 if (resourceHeaderImagePos) {
2426 writer.seek_u(resourceHeaderImagePos).write<uint32_t>(resourceDataCurPos);
2427 for (
auto& resource : sortedResources) {
2429 writer.seek_u(resourceDataCurPos).write(resource.data);
2433 return resourceDataCurPos;
2439 auto bakeFormat = this->format;
2440 auto bakeFlags = this->
flags;
2448 bakeFormat =
static_cast<ImageFormat>(
static_cast<int32_t
>(this->format) + 3);
2457 switch (this->platform) {
2464 .write(this->version);
2466 const auto headerLengthPos = writer.tell();
2467 writer.write<uint32_t>(0);
2471 .write(this->height)
2473 .write(this->frameCount)
2482 .write(this->mipCount)
2485 .write(this->thumbnailHeight);
2487 if (this->version >= 2) {
2488 writer << this->depth;
2491 if (this->version < 3) {
2493 const auto headerSize = writer.tell();
2494 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(headerSize);
2497 writer.write(thumbnailResource->data);
2500 writer.write(imageResource->data);
2503 std::vector<std::byte> auxCompressionResourceData;
2504 std::vector<std::byte> compressedImageResourceData;
2505 bool hasAuxCompression =
false;
2508 if (hasAuxCompression) {
2510 auxCompressionResourceData.resize((this->mipCount * this->frameCount * faceCount + 2) *
sizeof(uint32_t));
2511 BufferStream auxWriter{auxCompressionResourceData,
false};
2518 .write<uint32_t>(auxCompressionResourceData.size() -
sizeof(uint32_t))
2522 for (
int i = this->mipCount - 1; i >= 0; i--) {
2523 for (
int j = 0; j < this->frameCount; j++) {
2524 for (
int k = 0; k < faceCount; k++) {
2525 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)) {
2527 compressedImageResourceData.insert(compressedImageResourceData.end(), compressedData.begin(), compressedData.end());
2528 auxWriter.write<uint32_t>(compressedData.size());
2536 writer.pad(3).write<uint32_t>(this->
getResources().size() + hasAuxCompression).pad(8);
2538 std::vector<Resource> sortedResources = this->
getResources();
2539 if (hasAuxCompression) {
2540 for (
auto& resource : sortedResources) {
2542 resource.data = compressedImageResourceData;
2546 sortedResources.push_back({
2549 .data = auxCompressionResourceData,
2552 std::ranges::sort(sortedResources, [](
const Resource& lhs,
const Resource& rhs) {
2555 writeResources(headerLengthPos, sortedResources);
2561 writer.write<uint32_t>(0);
2563 const auto headerSizePos = writer.tell();
2568 .write(this->height)
2570 .write(this->frameCount);
2571 const auto preloadSizePos = writer.tell();
2572 writer.write<uint16_t>(0);
2573 const auto imageOffsetPos = writer.tell();
2580 .write(this->format)
2582 .write(this->thumbnailHeight)
2588 const auto headerSize = writer.tell();
2589 writer.seek_u(headerSizePos).write<uint32_t>(headerSize).seek_u(headerSize);
2592 writer.write(thumbnailResource->data);
2597 writer.write(paletteResource->data);
2601 bool hasFallbackResource =
false;
2603 hasFallbackResource =
true;
2604 std::vector<std::byte> reorderedFallbackData{fallbackResource->data.begin(), fallbackResource->data.end()};
2609 writer.write(reorderedFallbackData);
2615 const auto preloadSize = writer.tell();
2616 writer.seek_u(preloadSizePos).write<uint32_t>(preloadSize).seek_u(preloadSize);
2618 if (hasFallbackResource) {
2621 const auto imageOffset = writer.tell();
2622 writer.seek_u(imageOffsetPos).write<uint16_t>(imageOffset).seek_u(imageOffset);
2625 std::vector<std::byte> reorderedImageData{imageResource->data.begin(), imageResource->data.end()};
2626 ::swapImageDataEndianForConsole<false>(reorderedImageData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, this->platform);
2628 reorderedImageData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
true, reorderedImageData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, ok);
2630 writer.write(reorderedImageData);
2635 if (writer.tell() > 512) {
2645 writer.set_big_endian(
true);
2649 writer.set_big_endian(
true);
2650 writer << this->platform;
2652 writer.write<uint32_t>(8);
2654 const auto headerLengthPos = writer.tell();
2659 .write(this->height)
2661 .write(this->frameCount);
2662 const auto preloadPos = writer.tell();
2666 .write<uint8_t>(this->resources.size())
2672 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[0] * 255)), 0, 255))
2673 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[1] * 255)), 0, 255))
2674 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[2] * 255)), 0, 255))
2675 .write<uint8_t>(255);
2676 const auto compressionPos = writer.tell();
2677 writer.write<uint32_t>(0);
2681 writer.write<uint32_t>(0);
2684 std::vector<std::byte> thumbnailResourceData;
2685 std::vector<std::byte> imageResourceData;
2686 std::vector<Resource> sortedResources = this->
getResources();
2687 for (
auto& resource : sortedResources) {
2689 thumbnailResourceData = {resource.data.begin(), resource.data.end()};
2690 ::swapImageDataEndianForConsole<false>(thumbnailResourceData, this->
thumbnailFormat, 1, 1, 1, this->
thumbnailWidth, this->thumbnailHeight, 1, this->platform);
2691 resource.data = thumbnailResourceData;
2695 imageResourceData = ::convertBetweenDDSAndVTFMipOrderForXBOX<false>(
false, resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->depth, ok);
2696 if (!ok || imageResourceData.size() != resource.data.size()) {
2700 imageResourceData = {resource.data.begin(), resource.data.end()};
2702 ::swapImageDataEndianForConsole<false>(imageResourceData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->depth, this->platform);
2712 fixedCompressionLevel = 6;
2715 imageResourceData = std::move(*compressedData);
2716 const auto curPos = writer.tell();
2717 writer.seek_u(compressionPos).write<uint32_t>(imageResourceData.size()).seek_u(curPos);
2721 resource.data = imageResourceData;
2725 std::ranges::sort(sortedResources, [](
const Resource& lhs,
const Resource& rhs) {
2728 const auto resourceDataImagePos = writeResources(headerLengthPos, sortedResources);
2729 writer.seek_u(preloadPos).write(std::max<uint16_t>(resourceDataImagePos, 2048));
2733 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)
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...
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
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
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
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, 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 getDataAsExtendedFlags() 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