5#include <unordered_map>
8#ifdef SOURCEPP_BUILD_WITH_TBB
12#ifdef SOURCEPP_BUILD_WITH_THREADS
17#include <BufferStream.h>
29std::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 auto expectedSize = ZSTD_compressBound(data.size());
54 std::vector<std::byte> out(expectedSize);
56 auto compressedSize = ZSTD_compress(out.data(), expectedSize, data.data(), data.size(), level);
57 if (ZSTD_isError(compressedSize)) {
61 out.resize(compressedSize);
75void swapImageDataEndianForConsole(std::span<std::byte> imageData,
ImageFormat format, uint16_t width, uint16_t height,
VTF::Platform platform) {
76 if (imageData.empty() || format == ImageFormat::EMPTY || platform ==
VTF::PLATFORM_PC) {
87 std::copy(newData.begin(), newData.end(), imageData.begin());
98 std::span<uint16_t> dxtData{
reinterpret_cast<uint16_t*
>(imageData.data()), imageData.size() /
sizeof(uint16_t)};
100#ifdef SOURCEPP_BUILD_WITH_TBB
101 std::execution::par_unseq,
103 dxtData.begin(), dxtData.end(), [](uint16_t& value) {
104 BufferStream::swap_endian(&value);
117 static constinit std::array<Type, 8> typeArray{
127 static bool unsorted =
true;
129 std::sort(typeArray.begin(), typeArray.end());
136 switch (this->
type) {
138 if (this->data.size() <=
sizeof(uint32_t)) {
141 return SHT{{
reinterpret_cast<const std::byte*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data())}};
144 if (this->data.size() !=
sizeof(uint32_t)) {
147 return *
reinterpret_cast<const uint32_t*
>(this->data.data());
149 if (this->data.size() !=
sizeof(uint32_t)) {
152 return std::make_tuple(
153 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 0),
154 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 1),
155 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 2),
156 *(
reinterpret_cast<const uint8_t*
>(this->data.data()) + 3));
158 if (this->data.size() <=
sizeof(uint32_t)) {
161 return std::string(
reinterpret_cast<const char*
>(this->data.data()) +
sizeof(uint32_t), *
reinterpret_cast<const uint32_t*
>(this->data.data()));
163 if (this->data.size() <=
sizeof(uint32_t) || this->data.size() %
sizeof(uint32_t) != 0) {
166 return std::span{
reinterpret_cast<uint32_t*
>(this->data.data()), this->data.size() / 4};
191VTF::VTF(std::vector<std::byte>&& vtfData,
bool parseHeaderOnly)
192 : data(std::move(vtfData)) {
193 BufferStreamReadOnly stream{this->data};
195 if (
auto signature = stream.read<uint32_t>(); signature ==
VTF_SIGNATURE) {
198 if (this->
majorVersion != 7 || this->minorVersion > 6) {
202 stream.set_big_endian(
true);
203 uint32_t minorConsoleVersion = 0;
204 stream >> this->platform >> minorConsoleVersion;
205 if (minorConsoleVersion != 8) {
212 switch (this->platform) {
228 const auto headerSize = stream.read<uint32_t>();
230 const auto readResources = [
this, &stream](uint32_t resourceCount) {
233 for (
int i = 0; i < resourceCount; i++) {
234 auto& [type, flags_, data_] = this->
resources.emplace_back();
236 auto typeAndFlags = stream.read<uint32_t>();
237 if (stream.is_big_endian()) {
239 BufferStream::swap_endian(&typeAndFlags);
243 data_ = stream.read_span<std::byte>(4);
246 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(data_.data()));
254 if ((lhs.flags & Resource::FLAG_LOCAL_DATA) && (rhs.flags & Resource::FLAG_LOCAL_DATA)) {
255 return lhs.type < rhs.type;
263 return *
reinterpret_cast<uint32_t*
>(lhs.
data.data()) < *
reinterpret_cast<uint32_t*
>(rhs.
data.data());
271 auto lastOffset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
272 auto currentOffset = *
reinterpret_cast<uint32_t*
>(resource.data.data());
273 auto curPos = stream.tell();
274 stream.seek(lastOffset);
275 lastResource->
data = stream.read_span<std::byte>(currentOffset - lastOffset);
276 stream.seek(
static_cast<int64_t
>(curPos));
278 lastResource = &resource;
282 auto offset = *
reinterpret_cast<uint32_t*
>(lastResource->data.data());
283 auto curPos = stream.tell();
285 lastResource->data = stream.read_span<std::byte>(stream.size() - offset);
286 stream.seek(
static_cast<int64_t
>(curPos));
290 if (this->platform != PLATFORM_PC) {
291 uint8_t resourceCount;
296 .read(this->sliceCount)
297 .read(this->frameCount)
301 .read(this->reflectivity[0])
302 .read(this->reflectivity[1])
303 .read(this->reflectivity[2])
304 .read(this->bumpMapScale)
306 .skip<math::Vec4ui8>()
309 if (this->platform == PLATFORM_PS3_PORTAL2) {
310 stream.skip<uint32_t>();
315 if (parseHeaderOnly) {
320 this->resources.reserve(resourceCount);
321 readResources(resourceCount);
323 this->opened = stream.tell() == headerSize;
326 for (
const auto& resource : this->resources) {
330 this->setResourceInternal(resource.type, *decompressedData);
341 switch (resource.type) {
349 ::swapImageDataEndianForConsole(resource.data, this->thumbnailFormat, this->thumbnailWidth, this->thumbnailHeight, this->platform);
352 ::swapImageDataEndianForConsole(resource.data, this->format, this->width, this->height, this->platform);
357 if (resource.data.size() >=
sizeof(uint32_t)) {
358 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resource.data.data()));
370 .read(this->frameCount)
371 .read(this->startFrame)
373 .read(this->reflectivity[0])
374 .read(this->reflectivity[1])
375 .read(this->reflectivity[2])
377 .read(this->bumpMapScale)
379 .read(this->mipCount);
383 stream >> this->thumbnailWidth >> this->thumbnailHeight;
384 if (this->thumbnailWidth == 0 || this->thumbnailHeight == 0) {
390 if (this->minorVersion < 2) {
391 this->sliceCount = 1;
393 stream.read(this->sliceCount);
396 if (parseHeaderOnly) {
401 if (this->minorVersion >= 3) {
403 auto resourceCount = stream.read<uint32_t>();
405 readResources(resourceCount);
407 this->opened = stream.tell() == headerSize;
409 if (this->opened && this->minorVersion >= 6) {
412 if (auxResource && imageResource) {
413 if (auxResource->getDataAsAuxCompressionLevel() != 0) {
414 const auto faceCount = this->getFaceCount();
415 std::vector<std::byte> decompressedImageData(
ImageFormatDetails::getDataLength(this->format, this->mipCount, this->frameCount, faceCount, this->width, this->height, this->sliceCount));
416 uint32_t oldOffset = 0;
417 for (
int i = this->mipCount - 1; i >= 0; i--) {
418 for (
int j = 0; j < this->frameCount; j++) {
419 for (
int k = 0; k < faceCount; k++) {
420 uint32_t oldLength = auxResource->getDataAsAuxCompressionLength(i, this->mipCount, j, this->frameCount, k, faceCount);
421 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())) {
423 mz_ulong decompressedImageDataSize = newLength * this->sliceCount;
424 switch (auxResource->getDataAsAuxCompressionMethod()) {
427 if (mz_uncompress(
reinterpret_cast<unsigned char*
>(decompressedImageData.data() + newOffset), &decompressedImageDataSize,
reinterpret_cast<const unsigned char*
>(imageResource->data.data() + oldOffset), oldLength) != MZ_OK) {
428 this->opened =
false;
433 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) {
434 this->opened =
false;
444 oldOffset += oldLength;
454 this->opened = stream.tell() == headerSize;
456 this->resources.reserve(2);
458 if (this->hasThumbnailData()) {
459 this->resources.push_back({
465 if (this->hasImageData()) {
466 this->resources.push_back({
469 .data = stream.read_span<std::byte>(stream.size() - stream.tell()),
475 this->compressionLevel = resource->getDataAsAuxCompressionLevel();
476 this->compressionMethod = resource->getDataAsAuxCompressionMethod();
481VTF::VTF(std::span<const std::byte> vtfData,
bool parseHeaderOnly)
482 :
VTF(std::vector<std::byte>{vtfData.begin(), vtfData.end()}, parseHeaderOnly) {}
484VTF::VTF(
const std::string& vtfPath,
bool parseHeaderOnly)
485 :
VTF(fs::readFileBuffer(vtfPath), parseHeaderOnly) {}
493 this->data = other.
data;
496 this->width = other.
width;
497 this->height = other.
height;
503 this->format = other.
format;
510 this->resources.clear();
511 for (
const auto& [otherType, otherFlags, otherData] : other.
resources) {
512 auto& [type, flags_, data_] = this->resources.emplace_back();
515 data_ = {this->data.data() + (otherData.data() - other.
data.data()), otherData.size()};
527VTF::operator bool()
const {
534 for (
int i = 1; i < writer.
mipCount; i++) {
538 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)) {
541 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)) {
594 return writer.
bake(vtfPath);
598 std::vector<std::byte> imageData;
614 std::vector<std::byte> imageData;
630 return writer.
bake(vtfPath);
656 if (this->
mipCount != recommendedCount) {
660 this->platform = newPlatform;
690 if (faceCount == 7 && (newMinorVersion < 1 || newMinorVersion > 4)) {
695 if (this->minorVersion <= 3 && newMinorVersion > 3) {
701 }
else if (this->
minorVersion > 3 && newMinorVersion <= 3) {
740 if (newWidth == 0 || newHeight == 0) {
750 if (this->width == newWidth && this->height == newHeight) {
756 newMipCount = recommendedCount;
765 this->frameCount = 1;
766 this->
flags &= ~FLAG_ENVMAP;
767 this->width = newWidth;
768 this->height = newHeight;
783 this->
flags |= flags_ & ~FLAG_MASK_INTERNAL;
832 this->format = newFormat;
837 newMipCount = recommendedCount;
855 newMipCount = recommended;
856 if (newMipCount == 1) {
860 if (newMipCount > 1) {
888 auto* outputDataPtr = imageResource->data.data();
891#ifdef SOURCEPP_BUILD_WITH_THREADS
892 std::vector<std::future<void>> futures;
893 futures.reserve((this->
mipCount - 1) * this->frameCount * faceCount * this->
sliceCount);
896 for (
int k = 0; k < faceCount; k++) {
898#ifdef SOURCEPP_BUILD_WITH_THREADS
899 futures.push_back(std::async(std::launch::async, [
this, filter, outputDataPtr, faceCount, j, k, l] {
901 for (
int i = 1; i < this->
mipCount; i++) {
902 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->
imageDataIsSRGB(), filter);
903 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) {
904 std::memcpy(outputDataPtr + offset, mip.data(), length);
907#ifdef SOURCEPP_BUILD_WITH_THREADS
909 if (std::thread::hardware_concurrency() > 0 && futures.size() >= std::thread::hardware_concurrency()) {
910 for (
auto& future : futures) {
919#ifdef SOURCEPP_BUILD_WITH_THREADS
920 for (
auto& future : futures) {
958 if (expectedLength == image->data.size()) {
1009 static constexpr auto getReflectivityForImage = [](
const VTF& vtf, uint16_t frame, uint8_t face, uint16_t slice) {
1010 static constexpr auto getReflectivityForPixel = [](
const ImagePixel::RGBA8888* pixel) -> math::Vec3f {
1012 math::Vec3f ref{
static_cast<float>(pixel->r),
static_cast<float>(pixel->g),
static_cast<float>(pixel->b)};
1013 ref /= 255.f * 0.9f;
1022 for (uint64_t i = 0; i < rgba8888Data.size(); i += 4) {
1023 out += getReflectivityForPixel(
reinterpret_cast<ImagePixel::RGBA8888*
>(rgba8888Data.data() + i));
1030#ifdef SOURCEPP_BUILD_WITH_THREADS
1031 if (this->frameCount > 1 || faceCount > 1 || this->
sliceCount > 1) {
1032 std::vector<std::future<math::Vec3f>> futures;
1033 futures.reserve(this->frameCount * faceCount * this->
sliceCount);
1037 for (
int k = 0; k < faceCount; k++) {
1039 futures.push_back(std::async(std::launch::async, [
this, j, k, l] {
1040 return getReflectivityForImage(*
this, j, k, l);
1042 if (std::thread::hardware_concurrency() > 0 && futures.size() >= std::thread::hardware_concurrency()) {
1043 for (
auto& future : futures) {
1052 for (
auto& future : futures) {
1057 this->
reflectivity = getReflectivityForImage(*
this, 0, 0, 0);
1062 for (
int k = 0; k < faceCount; k++) {
1064 this->
reflectivity += getReflectivityForImage(*
this, j, k, l);
1097 for (
const auto& resource : this->resources) {
1098 if (resource.type == type) {
1106 for (
auto& resource : this->resources) {
1107 if (resource.type == type) {
1115 if (
const auto* resource = this->
getResource(type); resource && resource->
data.size() == data_.size()) {
1116 std::memcpy(resource->data.data(), data_.data(), data_.size());
1121 std::unordered_map<Resource::Type, std::pair<std::vector<std::byte>, uint64_t>> resourceData;
1122 for (
const auto& [type_, flags_, dataSpan] : this->resources) {
1123 resourceData[type_] = {std::vector<std::byte>{dataSpan.begin(), dataSpan.end()}, 0};
1127 if (data_.empty()) {
1128 resourceData.erase(type);
1130 resourceData[type] = {{data_.begin(), data_.end()}, 0};
1135 BufferStream writer{this->data};
1138 if (!resourceData.contains(resourceType)) {
1141 auto& [specificResourceData, offset] = resourceData[resourceType];
1142 if (resourceType == type) {
1146 {this->data.data() + offset, specificResourceData.size()},
1149 *resourcePtr = newResource;
1151 this->resources.push_back(newResource);
1153 }
else if (!resourceData.contains(resourceType)) {
1156 offset = writer.tell();
1157 writer.write(specificResourceData);
1159 this->data.resize(writer.size());
1161 for (
auto& [type_, flags_, dataSpan] : this->resources) {
1162 if (resourceData.contains(type_)) {
1163 const auto& [specificResourceData, offset] = resourceData[type_];
1164 dataSpan = {this->data.data() + offset, specificResourceData.size()};
1170 std::erase_if(this->resources, [type](
const Resource& resource) {
return resource.
type == type; });
1174 if (!newWidth) { newWidth = 1; }
1175 if (!newHeight) { newHeight = 1; }
1176 if (!newMipCount) { newMipCount = 1; }
1177 if (!newFrameCount) { newFrameCount = 1; }
1178 if (!newFaceCount) { newFaceCount = 1; }
1179 if (!newSliceCount) { newSliceCount = 1; }
1182 if (this->format == newFormat && this->width == newWidth && this->height == newHeight && this->
mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->
sliceCount == newSliceCount) {
1186 std::vector<std::byte> newImageData;
1188 if (this->format != newFormat && this->width == newWidth && this->height == newHeight && this->
mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->
sliceCount == newSliceCount) {
1192 for (
int i = newMipCount - 1; i >= 0; i--) {
1193 for (
int j = 0; j < newFrameCount; j++) {
1194 for (
int k = 0; k < newFaceCount; k++) {
1195 for (
int l = 0; l < newSliceCount; l++) {
1198 std::vector<std::byte> image{imageSpan.begin(), imageSpan.end()};
1199 if (this->width != newWidth || this->height != newHeight) {
1200 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->
imageDataIsSRGB(), filter);
1202 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, newMipCount, j, newFrameCount, k, newFaceCount, newWidth, newHeight, l, newSliceCount) && image.size() == length) {
1203 std::memcpy(newImageData.data() + offset, image.data(), length);
1210 if (this->format != newFormat) {
1218 this->format = newFormat;
1219 this->width = newWidth;
1220 this->height = newHeight;
1222 this->frameCount = newFrameCount;
1223 if (newFaceCount > 1) {
1226 this->
flags &= ~FLAG_ENVMAP;
1233std::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 {
1242 auto sht = shtResource->getDataAsParticleSheet();
1243 const auto* sequence = sht.getSequenceFromID(shtSequenceID);
1244 if (!sequence || sequence->frames.size() <= shtFrame || shtBounds >= sht.getFrameBoundsCount()) {
1252 const auto& bounds = sequence->frames[shtFrame].bounds[shtBounds];
1253 uint16_t x1 = std::clamp<uint16_t>(std::floor(bounds.x1 *
static_cast<float>(this->getWidth(mip))), 0, this->getWidth(mip));
1254 uint16_t y1 = std::clamp<uint16_t>(std::ceil( bounds.y1 *
static_cast<float>(this->getHeight(mip))), 0, this->getHeight(mip));
1255 uint16_t x2 = std::clamp<uint16_t>(std::ceil( bounds.x2 *
static_cast<float>(this->getWidth(mip))), 0, this->getHeight(mip));
1256 uint16_t y2 = std::clamp<uint16_t>(std::floor(bounds.y2 *
static_cast<float>(this->getHeight(mip))), 0, this->getWidth(mip));
1258 if (x1 > x2) [[unlikely]] {
1261 if (y1 > y2) [[unlikely]] {
1264 spriteWidth = x2 - x1;
1265 spriteWidth = y2 - y1;
1267 const auto out =
ImageConversion::cropImageData(this->
getImageDataRaw(mip, frame, face, slice), this->
getFormat(), this->
getWidth(mip), spriteWidth, x1, this->
getHeight(mip), spriteHeight, y1);
1275std::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 {
1276 return ImageConversion::convertImageDataToFormat(this->
getParticleSheetFrameDataRaw(spriteWidth, spriteHeight, shtSequenceID, shtFrame, shtBounds, mip, frame, face, slice), this->
getFormat(), newFormat, spriteWidth, spriteHeight);
1279std::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 {
1284 std::vector<std::byte> particleSheetData;
1285 BufferStream writer{particleSheetData};
1287 auto bakedSheet = value.
bake();
1288 writer.write<uint32_t>(bakedSheet.size());
1289 writer.write(bakedSheet);
1290 particleSheetData.resize(writer.size());
1309 BufferStream writer{&lodData,
sizeof(lodData)};
1311 writer << u << v << u360 << v360;
1329 std::vector<std::byte> keyValuesData;
1330 BufferStream writer{keyValuesData};
1332 writer.write<uint32_t>(value.size());
1333 writer.write(value,
false);
1334 keyValuesData.resize(writer.size());
1375 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)) {
1376 return imageResource->data.subspan(offset, length);
1383 const auto rawImageData = this->
getImageDataRaw(mip, frame, face, slice);
1384 if (rawImageData.empty()) {
1395 if (imageData_.empty()) {
1400 uint16_t resizedWidth = width_, resizedHeight = height_;
1407 mip = newMipCount - 1;
1409 if (face > 6 || (face == 6 && (this->minorVersion < 1 || this->
minorVersion > 4))) {
1412 this->
regenerateImageData(format_, resizedWidth, resizedHeight, mip + 1, frame + 1, face ? (face < 5 ? 5 : face) : 0, slice + 1);
1416 if (this->mipCount <= mip || this->
frameCount <= frame || faceCount <= face || this->
sliceCount <= slice) {
1421 if (!imageResource) {
1424 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)) {
1425 std::vector<std::byte> image{imageData_.begin(), imageData_.end()};
1428 if (width_ != newWidth || height_ != newHeight) {
1431 if (format_ != this->format) {
1434 std::memcpy(imageResource->data.data() + offset, image.data(), image.size());
1441 int inputWidth, inputHeight, inputFrameCount;
1445 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
1450 if (inputFrameCount == 1) {
1451 return this->
setImage(imageData_, inputFormat, inputWidth, inputHeight, filter, mip, frame, face, slice);
1455 bool allSuccess =
true;
1457 for (
int currentFrame = 0; currentFrame < inputFrameCount; currentFrame++) {
1458 if (!this->
setImage({imageData_.data() + currentFrame * frameSize, imageData_.data() + currentFrame * frameSize + frameSize}, inputFormat, inputWidth, inputHeight, filter, mip, frame + currentFrame, face, slice)) {
1461 if (currentFrame == 0 && this->frameCount < frame + inputFrameCount) {
1474 if (
auto data_ = this->
saveImageToFile(mip, frame, face, slice, fileFormat); !data_.empty()) {
1486 return thumbnailResource->data;
1493 if (rawThumbnailData.empty()) {
1510 this->thumbnailHeight = height_;
1519 this->thumbnailHeight = 16;
1520 this->
setResourceInternal(
Resource::TYPE_THUMBNAIL_DATA,
ImageConversion::convertImageDataToFormat(
ImageConversion::resizeImageData(this->
getImageDataRaw(), this->format, this->width, this->
thumbnailWidth, this->height, this->thumbnailHeight, this->
flags &
FLAG_SRGB, filter), this->format, this->
thumbnailFormat, this->
thumbnailWidth, this->thumbnailHeight));
1526 this->thumbnailHeight = 0;
1542 std::vector<std::byte> out;
1543 BufferStream writer{out};
1547 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(&type));
1549 writer_.write<uint32_t>(type);
1550 const auto resourceOffsetPos = writer_.tell();
1551 writer_.seek(0, std::ios::end);
1552 const auto resourceOffsetValue = writer_.tell();
1553 writer_.write(
data);
1554 writer_.seek_u(resourceOffsetPos).write<uint32_t>(resourceOffsetValue);
1559 writer.set_big_endian(
true);
1566 writer.write<uint32_t>(8);
1567 const auto headerLengthPos = writer.tell();
1572 .write(this->height)
1574 .write(this->frameCount);
1575 const auto preloadPos = writer.tell();
1579 .write<uint8_t>(this->resources.size())
1584 .write(this->format)
1585 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[0] * 255)), 0, 255))
1586 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[1] * 255)), 0, 255))
1587 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[2] * 255)), 0, 255))
1588 .write<uint8_t>(255);
1589 const auto compressionPos = writer.tell();
1590 writer.write<uint32_t>(0);
1594 writer.write<uint32_t>(0);
1597 std::vector<std::byte> imageResourceData;
1598 bool hasCompression =
false;
1600 imageResourceData.assign(imageResource->data.begin(), imageResource->data.end());
1601 ::swapImageDataEndianForConsole(imageResourceData, this->format, this->width, this->height, this->platform);
1611 fixedCompressionLevel = 6;
1614 if (compressedData) {
1615 imageResourceData.assign(compressedData->begin(), compressedData->end());
1617 hasCompression =
false;
1622 const auto resourceStart = writer.tell();
1623 const auto headerSize = resourceStart + (this->
getResources().size() *
sizeof(uint64_t));
1624 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(resourceStart);
1625 while (writer.tell() < headerSize) {
1626 writer.write<uint64_t>(0);
1628 writer.seek_u(resourceStart);
1632 auto curPos = writer.tell();
1633 const auto imagePos = writer.seek(0, std::ios::end).tell();
1634 writer.seek_u(preloadPos).write<uint16_t>(imagePos).seek_u(curPos);
1636 writeNonLocalResource(writer, resourceType, imageResourceData, this->platform);
1638 if (hasCompression) {
1639 curPos = writer.tell();
1640 writer.seek_u(compressionPos).write<uint32_t>(imageResourceData.size()).seek_u(curPos);
1642 }
else if (
const auto* resource = this->
getResource(resourceType)) {
1643 std::vector<std::byte> resData{resource->data.begin(), resource->data.end()};
1646 switch (resource->type) {
1659 if (resData.size() >=
sizeof(uint32_t)) {
1660 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resData.data()));
1666 writer.set_big_endian(
false);
1668 writer.set_big_endian(
true);
1669 writer.write(resource->data);
1671 writeNonLocalResource(writer, resource->type, resource->data, this->platform);
1676 out.resize(writer.size());
1681 const auto headerLengthPos = writer.tell();
1682 writer.write<uint32_t>(0);
1686 .write(this->height)
1688 .write(this->frameCount)
1694 .write(this->format)
1698 .write(this->thumbnailHeight);
1700 if (this->minorVersion >= 2) {
1704 if (this->minorVersion < 3) {
1706 for (uint16_t i = 0; i < headerAlignment; i++) {
1707 writer.write<std::byte>({});
1709 const auto headerSize = writer.tell();
1710 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(headerSize);
1713 writer.write(thumbnailResource->data);
1716 writer.write(imageResource->data);
1719 std::vector<std::byte> auxCompressionResourceData;
1720 std::vector<std::byte> compressedImageResourceData;
1721 bool hasAuxCompression =
false;
1723 hasAuxCompression = this->minorVersion >= 6 && this->
compressionLevel != 0;
1724 if (hasAuxCompression) {
1726 auxCompressionResourceData.resize((this->
mipCount * this->frameCount * faceCount + 2) *
sizeof(uint32_t));
1727 BufferStream auxWriter{auxCompressionResourceData,
false};
1734 .write<uint32_t>(auxCompressionResourceData.size() -
sizeof(uint32_t))
1738 for (
int i = this->
mipCount - 1; i >= 0; i--) {
1740 for (
int k = 0; k < faceCount; k++) {
1741 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)) {
1743 compressedImageResourceData.insert(compressedImageResourceData.end(), compressedData.begin(), compressedData.end());
1744 auxWriter.write<uint32_t>(compressedData.size());
1756 .write<uint32_t>(this->
getResources().size() + hasAuxCompression)
1757 .write<uint64_t>(0);
1759 const auto resourceStart = writer.tell();
1760 const auto headerSize = resourceStart + ((this->
getResources().size() + hasAuxCompression) *
sizeof(uint64_t));
1761 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(resourceStart);
1762 while (writer.tell() < headerSize) {
1763 writer.write<uint64_t>(0);
1765 writer.seek_u(resourceStart);
1769 writeNonLocalResource(writer, resourceType, auxCompressionResourceData, this->platform);
1771 writeNonLocalResource(writer, resourceType, compressedImageResourceData, this->platform);
1772 }
else if (
const auto* resource = this->
getResource(resourceType)) {
1775 writer.write(resource->data);
1777 writeNonLocalResource(writer, resource->type, resource->data, this->platform);
1783 out.resize(writer.size());
#define SOURCEPP_DEBUG_BREAK
Create a breakpoint in debug.
std::vector< std::byte > bake() const
void setImageHeightResizeMethod(ImageConversion::ResizeMethod imageHeightResizeMethod_)
void removeKeyValuesDataResource()
void setMinorVersion(uint32_t newMinorVersion)
CompressionMethod compressionMethod
void computeReflectivity()
uint8_t getThumbnailWidth() const
std::vector< std::byte > saveThumbnailToFile(ImageConversion::FileFormat fileFormat=ImageConversion::FileFormat::DEFAULT) const
ImageFormat getFormat() const
void setPlatform(Platform newPlatform)
uint16_t getHeight(uint8_t mip=0) const
VTF & operator=(const VTF &other)
sourcepp::math::Vec3f reflectivity
uint16_t getWidth(uint8_t mip=0) const
void setVersion(uint32_t newMajorVersion, uint32_t newMinorVersion)
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 setImageResizeMethods(ImageConversion::ResizeMethod imageWidthResizeMethod_, ImageConversion::ResizeMethod imageHeightResizeMethod_)
std::vector< std::byte > getParticleSheetFrameDataAs(ImageFormat newFormat, uint16_t &spriteWidth, uint16_t &spriteHeight, uint32_t shtSequenceID, uint32_t shtFrame, uint8_t shtBounds=0, uint8_t mip=0, uint16_t frame=0, uint8_t face=0, uint16_t slice=0) const
This is a convenience function. You're best off uploading the bounds to the GPU and scaling the UV th...
void setMajorVersion(uint32_t newMajorVersion)
float getBumpMapScale() const
void computeTransparencyFlags()
uint32_t getMinorVersion() const
void removeFlags(Flags flags_)
std::vector< std::byte > data
bool setFrameFaceAndSliceCount(uint16_t newFrameCount, bool isCubeMap, uint16_t newSliceCount=1)
Platform getPlatform() const
uint8_t getFaceCount() const
ImageFormat thumbnailFormat
void setSize(uint16_t newWidth, uint16_t newHeight, ImageConversion::ResizeFilter filter)
ImageConversion::ResizeMethod getImageHeightResizeMethod() const
void addFlags(Flags flags_)
void setBumpMapScale(float newBumpMapScale)
ImageFormat getThumbnailFormat() const
uint16_t getStartFrame() const
static constexpr std::underlying_type_t< Flags > FLAG_MASK_INTERNAL
void setThumbnail(std::span< const std::byte > imageData_, ImageFormat format_, uint16_t width_, uint16_t height_)
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
bool imageDataIsSRGB() const
static constexpr std::underlying_type_t< Flags > FLAG_MASK_AFTER_V7_3
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)
std::vector< std::byte > bake() 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)
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 constexpr std::underlying_type_t< Flags > FLAG_MASK_SRGB
void setFormat(ImageFormat newFormat, ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT)
void setStartFrame(uint16_t newStartFrame)
std::vector< std::byte > getThumbnailDataAs(ImageFormat newFormat) const
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 setFlags(Flags flags_)
void setCRCResource(uint32_t value)
void setCompressionMethod(CompressionMethod newCompressionMethod)
void computeThumbnail(ImageConversion::ResizeFilter filter=ImageConversion::ResizeFilter::DEFAULT)
bool setFaceCount(bool isCubeMap)
uint8_t getThumbnailHeight() const
const Resource * getResource(Resource::Type type) const
void setParticleSheetResource(const SHT &value)
void setKeyValuesDataResource(const std::string &value)
static ImageFormat getDefaultCompressedFormat(ImageFormat inputFormat, uint32_t majorVersion, uint32_t minorVersion, bool isCubeMap)
static bool create(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, const std::string &vtfPath, CreationOptions options)
int16_t getCompressionLevel() const
void setResourceInternal(Resource::Type type, std::span< const std::byte > data_)
std::vector< Resource > resources
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()
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)
uint32_t getMajorVersion() const
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)
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 > gammaCorrectImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t height, float gamma)
Perform gamma correction on the given image data. Will not perform gamma correction if the input imag...
std::vector< std::byte > cropImageData(std::span< const std::byte > imageData, ImageFormat format, uint16_t width, uint16_t newWidth, uint16_t xOffset, uint16_t height, uint16_t newHeight, uint16_t yOffset)
Crops the given image to the new dimensions. If the image format is compressed it will be converted t...
std::vector< std::byte > 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)
Converts several images from one format to another.
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 > convertImageDataToFormat(std::span< const std::byte > imageData, ImageFormat oldFormat, ImageFormat newFormat, uint16_t width, uint16_t height)
Converts an image from one format to another.
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
std::variant< std::monostate, SHT, uint32_t, std::tuple< uint8_t, uint8_t, uint8_t, uint8_t >, std::string, std::span< uint32_t > > ConvertedData
ConvertedData convertData() const
@ TYPE_PARTICLE_SHEET_DATA
static const std::array< Type, 8 > & getOrder()
std::span< std::byte > data
uint16_t initialFrameCount
uint16_t initialSliceCount
bool computeTransparencyFlags
ImageConversion::ResizeFilter filter
ImageConversion::ResizeMethod heightResizeMethod
CompressionMethod compressionMethod
ImageConversion::ResizeMethod widthResizeMethod