5#include <unordered_map>
8#ifdef SOURCEPP_BUILD_WITH_TBB
12#ifdef SOURCEPP_BUILD_WITH_THREADS
17#include <BufferStream.h>
29[[nodiscard]] std::vector<std::byte> compressData(std::span<const std::byte> data, int16_t level,
CompressionMethod method) {
33 mz_ulong compressedSize = mz_compressBound(data.size());
34 std::vector<std::byte> out(compressedSize);
37 while ((status = mz_compress2(
reinterpret_cast<unsigned char*
>(out.data()), &compressedSize,
reinterpret_cast<const unsigned char*
>(data.data()), data.size(), level)) == MZ_BUF_ERROR) {
39 out.resize(compressedSize);
42 if (status != MZ_OK) {
45 out.resize(compressedSize);
53 const auto expectedSize = ZSTD_compressBound(data.size());
54 std::vector<std::byte> out(expectedSize);
56 const auto compressedSize = ZSTD_compress(out.data(), expectedSize, data.data(), data.size(), level);
57 if (ZSTD_isError(compressedSize)) {
61 out.resize(compressedSize);
74template<
bool ExistingDataIsSwizzled>
75constexpr void swizzleImageDataForPS3(std::span<std::byte> inputData, std::span<std::byte> outputData,
ImageFormat format, uint16_t width, uint16_t height, uint16_t slice) {
81 ](uint32_t x, uint32_t y, uint32_t z) {
82 auto widthL2m = widthL2;
83 auto heightL2m = heightL2;
84 auto sliceL2m = sliceL2;
86 uint32_t shiftCount = 0;
89 offset |= (z & 1) << shiftCount++;
92 if (heightL2m --> 0) {
93 offset |= (y & 1) << shiftCount++;
97 offset |= (x & 1) << shiftCount++;
104 const auto* inputPtr =
reinterpret_cast<const uint32_t*
>(inputData.data());
105 auto* outputPtr =
reinterpret_cast<uint32_t*
>(outputData.data());
106 for (uint16_t x = 0; x < width; x++) {
107 for (uint16_t y = 0; y < height; y++) {
108 for (uint16_t z = 0; z < slice; z++) {
109 if constexpr (ExistingDataIsSwizzled) {
110 *outputPtr++ =
reinterpret_cast<const uint32_t*
>(inputData.data())[zIndex(x, y, z)];
112 reinterpret_cast<uint32_t*
>(outputData.data())[zIndex(x, y, z)] = *inputPtr++;
119template<
bool ConvertingFromSource>
120void 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 sliceCount,
VTF::Platform platform) {
121 if (imageData.empty() || format == ImageFormat::EMPTY || platform ==
VTF::PLATFORM_PC) {
133 std::ranges::copy(newData, imageData.begin());
141 std::span dxtData{
reinterpret_cast<uint16_t*
>(imageData.data()), imageData.size() /
sizeof(uint16_t)};
143#ifdef SOURCEPP_BUILD_WITH_TBB
144 std::execution::par_unseq,
146 dxtData.begin(), dxtData.end(), [](uint16_t& value) {
147 BufferStream::swap_endian(&value);
157 std::vector<std::byte> out(imageData.size());
158 for(
int mip = mipCount - 1; mip >= 0; mip--) {
161 for (
int frame = 0; frame < frameCount; frame++) {
162 for (
int face = 0; face < faceCount; face++) {
163 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, format, mip, mipCount, frame, frameCount, face, faceCount, width, height)) {
164 std::span imageDataSpan{imageData.data() + offset, length * sliceCount};
165 std::span outSpan{out.data() + offset, length * sliceCount};
166 ::swizzleImageDataForPS3<ConvertingFromSource>(imageDataSpan, outSpan, format, mipWidth, mipHeight, sliceCount);
171 std::memcpy(imageData.data(), out.data(), out.size());
178 switch (this->
type) {
180 if (this->data.size() <=
sizeof(uint32_t)) {
183 return SHT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
186 if (this->data.size() !=
sizeof(uint32_t)) {
189 return *
reinterpret_cast<const uint32_t*
>(this->data.data());
191 if (this->data.size() !=
sizeof(uint32_t)) {
194 return std::make_tuple(
195 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 0),
196 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 1),
197 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 2),
198 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 3));
200 if (this->data.size() <=
sizeof(uint32_t)) {
203 return std::string(
reinterpret_cast<const char*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data()));
205 if (this->data.size() <=
sizeof(uint32_t)) {
208 return HOT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
210 if (this->data.size() <=
sizeof(uint32_t) || this->data.size() %
sizeof(uint32_t) != 0) {
213 return std::span{
reinterpret_cast<uint32_t*
>(this->data.data()), this->data.size() / 4};
231 this->frameCount = 0;
232 this->sliceCount = 0;
237VTF::VTF(std::vector<std::byte>&& vtfData,
bool parseHeaderOnly)
238 : data(std::move(vtfData)) {
239 BufferStreamReadOnly stream{this->data};
241 if (
const auto signature = stream.read<uint32_t>(); signature ==
VTF_SIGNATURE) {
247 if (this->version > 6) {
251 stream.set_big_endian(
true);
257 if (this->version != 8) {
271 const auto headerSize = stream.read<uint32_t>();
273 const auto readResources = [
this, &stream](uint32_t resourceCount) {
276 for (
int i = 0; i < resourceCount; i++) {
277 auto& [type, flags_, data_] = this->
resources.emplace_back();
279 auto typeAndFlags = stream.read<uint32_t>();
280 if (stream.is_big_endian()) {
282 BufferStream::swap_endian(&typeAndFlags);
286 data_ = stream.read_span<std::byte>(4);
289 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(data_.data()));
298 return lhs.type < rhs.type;
306 return *
reinterpret_cast<uint32_t*
>(lhs.
data.data()) < *
reinterpret_cast<uint32_t*
>(rhs.
data.data());
314 const auto lastOffset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
315 const auto currentOffset = *
reinterpret_cast<uint32_t*
>(resource.data.data());
316 const auto curPos = stream.tell();
317 stream.seek(lastOffset);
318 lastResource->
data = stream.read_span<std::byte>(currentOffset - lastOffset);
319 stream.seek(
static_cast<int64_t
>(curPos));
321 lastResource = &resource;
325 auto offset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
326 auto curPos = stream.tell();
328 lastResource->data = stream.read_span<std::byte>(stream.size() - offset);
329 stream.seek(
static_cast<int64_t
>(curPos));
334 const auto postReadTransform = [
this] {
348 switch (this->platform) {
356 .read(this->frameCount)
365 .read(this->mipCount);
379 this->sliceCount = 1;
381 stream.read(this->sliceCount);
384 if (parseHeaderOnly) {
391 auto resourceCount = stream.read<uint32_t>();
393 readResources(resourceCount);
395 this->
opened = stream.tell() == headerSize;
400 if (auxResource && imageResource) {
401 if (auxResource->getDataAsAuxCompressionLevel() != 0) {
403 std::vector<std::byte> decompressedImageData(
ImageFormatDetails::getDataLength(this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->sliceCount));
404 uint32_t oldOffset = 0;
405 for (
int i = this->mipCount - 1; i >= 0; i--) {
407 for (
int k = 0; k < faceCount; k++) {
408 uint32_t oldLength = auxResource->getDataAsAuxCompressionLength(i, this->mipCount, j, this->frameCount, k, faceCount);
409 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->
getSliceCount())) {
411 mz_ulong decompressedImageDataSize = newLength * this->
sliceCount;
412 switch (auxResource->getDataAsAuxCompressionMethod()) {
415 if (mz_uncompress(
reinterpret_cast<unsigned char*
>(decompressedImageData.data() + newOffset), &decompressedImageDataSize,
reinterpret_cast<const unsigned char*
>(imageResource->data.data() + oldOffset), oldLength) != MZ_OK) {
421 if (
auto decompressedSize = ZSTD_decompress(
reinterpret_cast<unsigned char*
>(decompressedImageData.data() + newOffset), decompressedImageDataSize,
reinterpret_cast<const unsigned char*
>(imageResource->data.data() + oldOffset), oldLength); ZSTD_isError(decompressedSize) || decompressedSize != decompressedImageDataSize) {
432 oldOffset += oldLength;
442 this->
opened = stream.tell() == headerSize;
457 .data = stream.read_span<std::byte>(stream.size() - stream.tell()),
472 uint8_t resourceCount;
477 .read(this->sliceCount)
478 .read(this->frameCount)
487 .skip<math::Vec4ui8>()
494 stream.skip<uint32_t>();
499 if (parseHeaderOnly) {
505 readResources(resourceCount);
507 this->
opened = stream.tell() == headerSize;
510 for (
const auto& resource : this->
resources) {
524 ::swapImageDataEndianForConsole<true>(resource.data, this->thumbnailFormat, 1, 1, 1, this->thumbnailWidth, this->thumbnailHeight, 1, this->platform);
526 ::swapImageDataEndianForConsole<true>(resource.data, this->format, this->mipCount, this->frameCount, this->getFaceCount(), this->width, this->height, this->sliceCount, this->platform);
528 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resource.data.data()));
536VTF::VTF(std::span<const std::byte> vtfData,
bool parseHeaderOnly)
537 :
VTF(std::vector<std::byte>{vtfData.begin(), vtfData.end()}, parseHeaderOnly) {}
539VTF::VTF(
const std::string& vtfPath,
bool parseHeaderOnly)
540 :
VTF(fs::readFileBuffer(vtfPath), parseHeaderOnly) {}
548 this->data = other.
data;
550 this->width = other.
width;
551 this->height = other.
height;
557 this->format = other.
format;
565 for (
const auto& [otherType, otherFlags, otherData] : other.
resources) {
566 auto& [type, flags_, data_] = this->
resources.emplace_back();
569 data_ = {this->data.data() + (otherData.data() - other.
data.data()), otherData.size()};
581VTF::operator bool()
const {
588 for (
int i = 1; i < writer.
mipCount; i++) {
592 if (options.
invertGreenChannel && !writer.
setImage(
ImageConversion::invertGreenChannelForImageData(writer.
getImageDataRaw(i, j, k, l), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i)), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i),
ImageConversion::ResizeFilter::DEFAULT, i, j, k, l)) {
595 if (options.
gammaCorrection != 1.f && !writer.
setImage(
ImageConversion::gammaCorrectImageData(writer.
getImageDataRaw(i, j, k, l), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i), options.
gammaCorrection), writer.
getFormat(), writer.
getWidth(i), writer.
getHeight(i),
ImageConversion::ResizeFilter::DEFAULT, i, j, k, l)) {
650 return writer.
bake(vtfPath);
654 std::vector<std::byte> imageData;
670 std::vector<std::byte> imageData;
686 return writer.
bake(vtfPath);
704 if (this->platform == newPlatform) {
709 const auto oldPlatform = this->
platform;
711 switch (newPlatform) {
723 this->platform = newPlatform;
728 if (this->mipCount != recommendedCount) {
733 if (this->mipCount > recommendedMipCount) {
749 if (faceCount == 7 && (newVersion < 1 || newVersion > 4)) {
752 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, faceCount, this->sliceCount);
756 bool srgb = this->
isSRGB();
757 if ((this->version < 2 && newVersion >= 2) || (this->
version >= 2 && newVersion < 2)) {
760 if ((this->version < 3 && newVersion >= 3) || (this->
version >= 3 && newVersion < 3)) {
763 if ((this->version < 4 && newVersion >= 4) || (this->
version >= 4 && newVersion < 4)) {
767 if ((this->version < 5 && newVersion >= 5) || (this->
version >= 5 && newVersion < 5)) {
806 if (newWidth == 0 || newHeight == 0) {
816 if (this->width == newWidth && this->height == newHeight) {
822 newMipCount = recommendedCount;
831 this->frameCount = 1;
832 this->
flags &= ~FLAG_ENVMAP;
833 this->width = newWidth;
834 this->height = newHeight;
835 this->sliceCount = 1;
849 this->
flags |= flags_ & ~FLAGS_MASK_INTERNAL;
864 }
else if (this->
version >= 4) {
870 }
else if (this->
version >= 4) {
916 this->format = newFormat;
921 newMipCount = recommendedCount;
924 this->
regenerateImageData(newFormat, this->width +
math::paddingForAlignment(4, this->width), this->height +
math::paddingForAlignment(4, this->height), newMipCount, this->frameCount, this->
getFaceCount(), this->sliceCount, filter, quality);
926 this->
regenerateImageData(newFormat, this->width, this->height, newMipCount, this->frameCount, this->
getFaceCount(), this->sliceCount, filter, quality);
938 if (this->platform !=
PLATFORM_PC && newMipCount > 1) {
941 newMipCount = recommended;
942 if (newMipCount == 1) {
963 if (this->mipCount <= 1) {
969 auto* outputDataPtr = imageResource->data.data();
972#ifdef SOURCEPP_BUILD_WITH_THREADS
973 std::vector<std::future<void>> futures;
974 futures.reserve(this->frameCount * faceCount * this->sliceCount);
977 for (
int k = 0; k < faceCount; k++) {
979#ifdef SOURCEPP_BUILD_WITH_THREADS
980 futures.push_back(std::async(std::launch::async, [
this, filter, outputDataPtr, faceCount, j, k, l] {
982 for (
int i = 1; i < this->
mipCount; i++) {
983 auto mip =
ImageConversion::resizeImageData(this->
getImageDataRaw(i - 1, j, k, l), this->format,
ImageDimensions::getMipDim(i - 1, this->width),
ImageDimensions::getMipDim(i, this->width),
ImageDimensions::getMipDim(i - 1, this->height),
ImageDimensions::getMipDim(i, this->height), this->
isSRGB(), filter);
984 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->sliceCount) && mip.size() == length) {
985 std::memcpy(outputDataPtr + offset, mip.data(), length);
988#ifdef SOURCEPP_BUILD_WITH_THREADS
990 if (std::thread::hardware_concurrency() > 0 && futures.size() >= std::thread::hardware_concurrency()) {
991 for (
auto& future : futures) {
1000#ifdef SOURCEPP_BUILD_WITH_THREADS
1001 for (
auto& future : futures) {
1036 if (this->
version >= 1 && this->
version <= 4 && expectedLength < image->
data.size()) {
1039 if (expectedLength == image->data.size()) {
1049 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, this->frameCount, isCubemap ? ((this->
version >= 1 && this->
version <= 4) ? 7 : 6) : 1, this->sliceCount);
1069 this->
regenerateImageData(this->format, this->width, this->height, this->mipCount, newFrameCount, isCubemap ? ((this->
version >= 1 && this->
version <= 4) ? 7 : 6) : 1, newSliceCount);
1090 static constexpr auto getReflectivityForImage = [](
const VTF& vtf, uint16_t frame, uint8_t face, uint16_t slice) {
1091 static constexpr auto getReflectivityForPixel = [](
const ImagePixel::RGBA8888* pixel) -> math::Vec3f {
1093 math::Vec3f ref{
static_cast<float>(pixel->r),
static_cast<float>(pixel->g),
static_cast<float>(pixel->b)};
1094 ref /= 255.f * 0.9f;
1103 for (uint64_t i = 0; i < rgba8888Data.size(); i += 4) {
1104 out += getReflectivityForPixel(
reinterpret_cast<ImagePixel::RGBA8888*
>(rgba8888Data.data() + i));
1111#ifdef SOURCEPP_BUILD_WITH_THREADS
1112 if (this->frameCount > 1 || faceCount > 1 || this->sliceCount > 1) {
1113 std::vector<std::future<math::Vec3f>> futures;
1114 futures.reserve(this->frameCount * faceCount * this->sliceCount);
1118 for (
int k = 0; k < faceCount; k++) {
1120 futures.push_back(std::async(std::launch::async, [
this, j, k, l] {
1121 return getReflectivityForImage(*
this, j, k, l);
1123 if (std::thread::hardware_concurrency() > 0 && futures.size() >= std::thread::hardware_concurrency()) {
1124 for (
auto& future : futures) {
1133 for (
auto& future : futures) {
1138 this->
reflectivity = getReflectivityForImage(*
this, 0, 0, 0);
1143 for (
int k = 0; k < faceCount; k++) {
1145 this->
reflectivity += getReflectivityForImage(*
this, j, k, l);
1178 for (
const auto& resource : this->
resources) {
1179 if (resource.type == type) {
1187 for (
auto& resource : this->
resources) {
1188 if (resource.type == type) {
1196 if (
const auto* resource = this->
getResource(type); resource && resource->
data.size() == data_.size()) {
1197 std::memcpy(resource->data.data(), data_.data(), data_.size());
1202 std::unordered_map<Resource::Type, std::pair<std::vector<std::byte>, uint64_t>> resourceData;
1203 for (
const auto& [type_, flags_, dataSpan] : this->
resources) {
1204 resourceData[type_] = {std::vector<std::byte>{dataSpan.begin(), dataSpan.end()}, 0};
1208 if (data_.empty()) {
1209 resourceData.erase(type);
1211 resourceData[type] = {{data_.begin(), data_.end()}, 0};
1216 BufferStream writer{this->data};
1219 if (!resourceData.contains(resourceType)) {
1222 auto& [specificResourceData, offset] = resourceData[resourceType];
1223 if (resourceType == type) {
1227 {this->data.data() + offset, specificResourceData.size()},
1230 *resourcePtr = newResource;
1232 this->resources.push_back(newResource);
1234 }
else if (!resourceData.contains(resourceType)) {
1237 offset = writer.tell();
1238 writer.write(specificResourceData);
1240 this->data.resize(writer.size());
1242 for (
auto& [type_, flags_, dataSpan] : this->resources) {
1243 if (resourceData.contains(type_)) {
1244 const auto& [specificResourceData, offset] = resourceData[type_];
1245 dataSpan = {this->data.data() + offset, specificResourceData.size()};
1255 if (!newWidth) { newWidth = 1; }
1256 if (!newHeight) { newHeight = 1; }
1257 if (!newMipCount) { newMipCount = 1; }
1258 if (!newFrameCount) { newFrameCount = 1; }
1259 if (!newFaceCount) { newFaceCount = 1; }
1260 if (!newSliceCount) { newSliceCount = 1; }
1263 if (this->format == newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->sliceCount == newSliceCount) {
1267 if (newMipCount > 1) {
1273 std::vector<std::byte> newImageData;
1275 if (this->format != newFormat && this->width == newWidth && this->height == newHeight && this->mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->sliceCount == newSliceCount) {
1279 for (
int i = newMipCount - 1; i >= 0; i--) {
1280 for (
int j = 0; j < newFrameCount; j++) {
1281 for (
int k = 0; k < newFaceCount; k++) {
1282 for (
int l = 0; l < newSliceCount; l++) {
1285 std::vector<std::byte> image{imageSpan.begin(), imageSpan.end()};
1286 if (this->width != newWidth || this->height != newHeight) {
1287 image =
ImageConversion::resizeImageData(image, this->format,
ImageDimensions::getMipDim(i, this->width),
ImageDimensions::getMipDim(i, newWidth),
ImageDimensions::getMipDim(i, this->height),
ImageDimensions::getMipDim(i, newHeight), this->
isSRGB(), filter);
1289 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, newMipCount, j, newFrameCount, k, newFaceCount, newWidth, newHeight, l, newSliceCount) && image.size() == length) {
1290 std::memcpy(newImageData.data() + offset, image.data(), length);
1297 if (this->format != newFormat) {
1305 this->format = newFormat;
1306 this->width = newWidth;
1307 this->height = newHeight;
1308 this->mipCount = newMipCount;
1309 this->frameCount = newFrameCount;
1310 if (newFaceCount > 1) {
1313 this->
flags &= ~FLAG_ENVMAP;
1315 this->sliceCount = newSliceCount;
1320std::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 {
1329 auto sht = shtResource->getDataAsParticleSheet();
1330 const auto* sequence = sht.getSequenceFromID(shtSequenceID);
1331 if (!sequence || sequence->frames.size() <= shtFrame || shtBounds >= sht.getFrameBoundsCount()) {
1339 const auto& bounds = sequence->frames[shtFrame].bounds[shtBounds];
1340 uint16_t x1 = std::clamp<uint16_t>(std::floor(bounds.x1 *
static_cast<float>(this->getWidth(mip))), 0, this->getWidth(mip));
1341 uint16_t y1 = std::clamp<uint16_t>(std::ceil( bounds.y1 *
static_cast<float>(this->getHeight(mip))), 0, this->getHeight(mip));
1342 uint16_t x2 = std::clamp<uint16_t>(std::ceil( bounds.x2 *
static_cast<float>(this->getWidth(mip))), 0, this->getHeight(mip));
1343 uint16_t y2 = std::clamp<uint16_t>(std::floor(bounds.y2 *
static_cast<float>(this->getHeight(mip))), 0, this->getWidth(mip));
1345 if (x1 > x2) [[unlikely]] {
1348 if (y1 > y2) [[unlikely]] {
1351 spriteWidth = x2 - x1;
1352 spriteWidth = y2 - y1;
1354 const auto out =
ImageConversion::cropImageData(this->
getImageDataRaw(mip, frame, face, slice), this->
getFormat(), this->
getWidth(mip), spriteWidth, x1, this->
getHeight(mip), spriteHeight, y1);
1362std::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 {
1363 return ImageConversion::convertImageDataToFormat(this->
getParticleSheetFrameDataRaw(spriteWidth, spriteHeight, shtSequenceID, shtFrame, shtBounds, mip, frame, face, slice), this->
getFormat(), newFormat, spriteWidth, spriteHeight);
1366std::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 {
1371 std::vector<std::byte> particleSheetData;
1372 BufferStream writer{particleSheetData};
1374 const auto bakedSheet = value.
bake();
1375 writer.write<uint32_t>(bakedSheet.size()).write(bakedSheet);
1376 particleSheetData.resize(writer.size());
1395 BufferStream writer{&lodData,
sizeof(lodData)};
1397 writer << u << v << u360 << v360;
1415 std::vector<std::byte> keyValuesData;
1416 BufferStream writer{keyValuesData};
1418 writer.write<uint32_t>(value.size()).write(value,
false);
1419 keyValuesData.resize(writer.size());
1429 std::vector<std::byte> hotspotData;
1430 BufferStream writer{hotspotData};
1432 const auto bakedHotspotData = value.
bake();
1433 writer.write<uint32_t>(bakedHotspotData.size()).write(bakedHotspotData);
1434 hotspotData.resize(writer.size());
1471 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->sliceCount)) {
1472 return imageResource->data.subspan(offset, length);
1479 const auto rawImageData = this->
getImageDataRaw(mip, frame, face, slice);
1480 if (rawImageData.empty()) {
1491 if (imageData_.empty()) {
1496 uint16_t resizedWidth = width_, resizedHeight = height_;
1503 mip = newMipCount - 1;
1505 if (face > 6 || (face == 6 && (this->version < 1 || this->
version > 4))) {
1508 this->
regenerateImageData(format_, resizedWidth, resizedHeight, mip + 1, frame + 1, face ? (face < 5 ? 5 : face) : 0, slice + 1);
1512 if (this->mipCount <= mip || this->
frameCount <= frame || faceCount <= face || this->
sliceCount <= slice) {
1517 if (!imageResource) {
1520 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->sliceCount)) {
1521 std::vector<std::byte> image{imageData_.begin(), imageData_.end()};
1524 if (width_ != newWidth || height_ != newHeight) {
1527 if (format_ != this->format) {
1530 std::memcpy(imageResource->data.data() + offset, image.data(), image.size());
1537 int inputWidth, inputHeight, inputFrameCount;
1541 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
1546 if (inputFrameCount == 1) {
1547 return this->
setImage(imageData_, inputFormat, inputWidth, inputHeight, filter, mip, frame, face, slice, quality);
1551 bool allSuccess =
true;
1553 for (
int currentFrame = 0; currentFrame < inputFrameCount; currentFrame++) {
1554 if (!this->
setImage({imageData_.data() + currentFrame * frameSize, imageData_.data() + currentFrame * frameSize + frameSize}, inputFormat, inputWidth, inputHeight, filter, mip, frame + currentFrame, face, slice, quality)) {
1557 if (currentFrame == 0 && this->frameCount < frame + inputFrameCount) {
1570 if (
auto data_ = this->
saveImageToFile(mip, frame, face, slice, fileFormat); !data_.empty()) {
1582 return thumbnailResource->data;
1589 if (rawThumbnailData.empty()) {
1616 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));
1638 std::vector<std::byte> out;
1639 BufferStream writer{out};
1643 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(&type));
1645 writer_.write<uint32_t>(type);
1646 const auto resourceOffsetPos = writer_.tell();
1647 writer_.seek(0, std::ios::end);
1648 const auto resourceOffsetValue = writer_.tell();
1649 writer_.write(
data);
1650 writer_.seek_u(resourceOffsetPos).write<uint32_t>(resourceOffsetValue);
1654 auto bakeFormat = this->
format;
1655 auto bakeFlags = this->
flags;
1665 switch (this->platform) {
1674 const auto headerLengthPos = writer.tell();
1675 writer.write<uint32_t>(0);
1679 .write(this->height)
1681 .write(this->frameCount)
1688 .write(this->mipCount)
1699 for (uint16_t i = 0; i < headerAlignment; i++) {
1700 writer.write<std::byte>({});
1702 const auto headerSize = writer.tell();
1703 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(headerSize);
1706 writer.write(thumbnailResource->data);
1709 writer.write(imageResource->data);
1712 std::vector<std::byte> auxCompressionResourceData;
1713 std::vector<std::byte> compressedImageResourceData;
1714 bool hasAuxCompression =
false;
1717 if (hasAuxCompression) {
1719 auxCompressionResourceData.resize((this->mipCount * this->frameCount * faceCount + 2) *
sizeof(uint32_t));
1720 BufferStream auxWriter{auxCompressionResourceData,
false};
1727 .write<uint32_t>(auxCompressionResourceData.size() -
sizeof(uint32_t))
1731 for (
int i = this->mipCount - 1; i >= 0; i--) {
1733 for (
int k = 0; k < faceCount; k++) {
1734 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->sliceCount)) {
1736 compressedImageResourceData.insert(compressedImageResourceData.end(), compressedData.begin(), compressedData.end());
1737 auxWriter.write<uint32_t>(compressedData.size());
1749 .write<uint32_t>(this->
getResources().size() + hasAuxCompression)
1750 .write<uint64_t>(0);
1752 const auto resourceStart = writer.tell();
1753 const auto headerSize = resourceStart + ((this->
getResources().size() + hasAuxCompression) *
sizeof(uint64_t));
1754 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(resourceStart);
1755 while (writer.tell() < headerSize) {
1756 writer.write<uint64_t>(0);
1758 writer.seek_u(resourceStart);
1762 writeNonLocalResource(writer, resourceType, auxCompressionResourceData, this->platform);
1764 writeNonLocalResource(writer, resourceType, compressedImageResourceData, this->platform);
1765 }
else if (
const auto* resource = this->
getResource(resourceType)) {
1768 writer.write(resource->data);
1770 writeNonLocalResource(writer, resource->type, resource->data, this->platform);
1776 out.resize(writer.size());
1784 writer.set_big_endian(
true);
1788 writer.set_big_endian(
true);
1793 uint8_t mipSkip = 0;
1794 for (
int mip = 0; mip < this->
mipCount; mip++, mipSkip++) {
1800 writer.write<uint32_t>(8);
1801 const auto headerLengthPos = writer.tell();
1806 .write(this->height)
1807 .write(this->sliceCount)
1808 .write(this->frameCount);
1809 const auto preloadPos = writer.tell();
1819 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[0] * 255)), 0, 255))
1820 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[1] * 255)), 0, 255))
1821 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[2] * 255)), 0, 255))
1822 .write<uint8_t>(255);
1823 const auto compressionPos = writer.tell();
1824 writer.write<uint32_t>(0);
1828 writer.write<uint32_t>(0);
1833 std::vector<std::byte> imageResourceData;
1834 bool hasCompression =
false;
1836 imageResourceData.assign(imageResource->data.begin(), imageResource->data.end());
1837 ::swapImageDataEndianForConsole<false>(imageResourceData, this->format, this->mipCount, this->frameCount, this->
getFaceCount(), this->width, this->height, this->sliceCount, this->platform);
1841 if (hasCompression) {
1847 fixedCompressionLevel = 6;
1850 imageResourceData.assign(compressedData->begin(), compressedData->end());
1852 hasCompression =
false;
1858 const auto resourceStart = writer.tell();
1859 const auto headerSize = resourceStart + (this->
getResources().size() *
sizeof(uint64_t));
1860 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(resourceStart);
1861 while (writer.tell() < headerSize) {
1862 writer.write<uint64_t>(0);
1864 writer.seek_u(resourceStart);
1868 auto curPos = writer.tell();
1869 const auto imagePos = writer.seek(0, std::ios::end).tell();
1870 writer.seek_u(preloadPos).write(std::max<uint16_t>(imagePos, 2048)).seek_u(curPos);
1872 writeNonLocalResource(writer, resourceType, imageResourceData, this->platform);
1874 if (hasCompression) {
1875 curPos = writer.tell();
1876 writer.seek_u(compressionPos).write<uint32_t>(imageResourceData.size()).seek_u(curPos);
1878 }
else if (
const auto* resource = this->
getResource(resourceType)) {
1879 std::vector<std::byte> resData{resource->data.begin(), resource->data.end()};
1884 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resData.data()));
1888 writer.set_big_endian(
false);
1890 writer.set_big_endian(
true);
1891 writer.write(resData);
1893 writeNonLocalResource(writer, resource->type, resData, this->platform);
1898 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
static constexpr uint32_t FLAGS_MASK_V5
static constexpr uint32_t FLAGS_MASK_V2
void setImageHeightResizeMethod(ImageConversion::ResizeMethod imageHeightResizeMethod_)
void removeKeyValuesDataResource()
CompressionMethod compressionMethod
void computeReflectivity()
uint8_t getThumbnailWidth() const
std::vector< std::byte > saveThumbnailToFile(ImageConversion::FileFormat fileFormat=ImageConversion::FileFormat::DEFAULT) const
void regenerateImageData(ImageFormat newFormat, uint16_t newWidth, uint16_t newHeight, uint8_t newMipCount, uint16_t newFrameCount, uint8_t newFaceCount, uint16_t newSliceCount, ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
ImageFormat getFormat() const
void setPlatform(Platform newPlatform)
uint16_t getHeight(uint8_t mip=0) const
VTF & operator=(const VTF &other)
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)
static constexpr uint32_t FLAGS_MASK_INTERNAL
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 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...
static constexpr uint32_t FLAGS_MASK_V3
float getBumpMapScale() const
void computeTransparencyFlags()
static constexpr uint32_t FLAGS_MASK_V4_TF2
std::vector< std::byte > data
bool setFrameFaceAndSliceCount(uint16_t newFrameCount, bool isCubeMap, uint16_t newSliceCount=1)
Platform getPlatform() 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 getSliceCount() const
void removeParticleSheetResource()
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)
bool setFrameCount(uint16_t newFrameCount)
ImageConversion::ResizeMethod getImageWidthResizeMethod() const
std::vector< std::byte > getImageDataAs(ImageFormat newFormat, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
static ImageFormat getDefaultCompressedFormat(ImageFormat inputFormat, uint32_t version, bool isCubeMap)
void setStartFrame(uint16_t newStartFrame)
std::vector< std::byte > getThumbnailDataAs(ImageFormat newFormat) const
static constexpr uint32_t FLAGS_MASK_V5_CSGO
bool setSliceCount(uint16_t newSliceCount)
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
std::vector< std::byte > getThumbnailDataAsRGBA8888() 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
void setCRCResource(uint32_t value)
void setCompressionMethod(CompressionMethod newCompressionMethod)
bool setFaceCount(bool isCubeMap)
uint8_t getThumbnailHeight() const
const Resource * getResource(Resource::Type type) const
static constexpr uint32_t FLAGS_MASK_V4
uint32_t getFlags() const
void setParticleSheetResource(const SHT &value)
void setKeyValuesDataResource(const std::string &value)
uint32_t getVersion() const
int16_t getCompressionLevel() const
void setHotspotDataResource(const HOT &value)
void removeFlags(uint32_t flags_)
void setResourceInternal(Resource::Type type, std::span< const std::byte > data_)
std::vector< Resource > resources
bool setImage(std::span< const std::byte > imageData_, ImageFormat format_, uint16_t width_, uint16_t height_, ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0, float quality=ImageConversion::DEFAULT_COMPRESSED_QUALITY)
std::vector< std::byte > getParticleSheetFrameDataRaw(uint16_t &spriteWidth, uint16_t &spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds=0, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
This is a convenience function. You're best off uploading the bounds to the GPU and scaling the UV th...
void removeExtendedFlagsResource()
static bool create(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, const std::string &vtfPath, const CreationOptions &options)
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)
std::vector< std::byte > readFileBuffer(const std::string &filepath, std::size_t startOffset=0)
bool writeFileBuffer(const std::string &filepath, std::span< const std::byte > buffer)
constexpr uint16_t paddingForAlignment(uint16_t alignment, uint64_t n)
constexpr uint32_t log2ceil(uint32_t value)
std::vector< std::byte > convertFileToImageData(std::span< const std::byte > fileData, ImageFormat &format, int &width, int &height, int &frameCount)
std::vector< std::byte > convertImageDataToFile(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, FileFormat fileFormat=FileFormat::DEFAULT)
Converts image data to the given file format (PNG or EXR by default).
void setResizedDims(uint16_t &width, ResizeMethod widthResize, uint16_t &height, ResizeMethod heightResize)
Set the new image dimensions given a resize method.
std::vector< std::byte > convertImageDataToFormat(std::span< const std::byte > imageData, ImageFormat oldFormat, ImageFormat newFormat, uint16_t width, uint16_t height, float quality=DEFAULT_COMPRESSED_QUALITY)
Converts an image from one format to another.
std::vector< std::byte > gammaCorrectImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, float gamma)
Perform gamma correction on the given image data. Will not perform gamma correction if the input imag...
std::vector< std::byte > 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 sliceCount, float quality=DEFAULT_COMPRESSED_QUALITY)
Converts several images from one format to another.
std::vector< std::byte > cropImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t xOffset, uint16_t height, uint16_t newHeight, uint16_t yOffset)
Crops the given image to the new dimensions. If the image format is compressed it will be converted t...
std::vector< std::byte > invertGreenChannelForImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height)
Invert the green channel. Meant for converting normal maps between OpenGL and DirectX formats.
std::vector< std::byte > resizeImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t height, uint16_t newHeight, bool srgb, ResizeFilter filter, ResizeEdge edge=ResizeEdge::CLAMP)
Resize given image data to the new dimensions.
constexpr uint32_t getMipDim(uint8_t mip, uint16_t dim)
constexpr uint8_t getActualMipCountForDimsOnConsole(uint16_t width, uint16_t height)
constexpr uint8_t getRecommendedMipCountForDims(ImageFormat format, uint16_t width, uint16_t height)
constexpr uint32_t VTF_SIGNATURE
constexpr uint32_t VTFX_SIGNATURE
constexpr uint32_t VTF3_SIGNATURE
static consteval std::array< Type, 9 > getOrder()
ConvertedData convertData() const
std::variant< std::monostate, SHT, uint32_t, std::tuple< uint8_t, uint8_t, uint8_t, uint8_t >, std::string, HOT, std::span< uint32_t > > ConvertedData
@ TYPE_PARTICLE_SHEET_DATA
std::span< std::byte > data
uint16_t initialFrameCount
uint16_t initialSliceCount
float compressedFormatQuality
bool computeTransparencyFlags
ImageConversion::ResizeFilter filter
ImageConversion::ResizeMethod heightResizeMethod
CompressionMethod compressionMethod
ImageConversion::ResizeMethod widthResizeMethod