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 {
570 return writer.
bake(vtfPath);
574 std::vector<std::byte> imageData;
590 std::vector<std::byte> imageData;
604 return writer.
bake(vtfPath);
630 if (this->
mipCount != recommendedCount) {
634 this->platform = newPlatform;
664 if (faceCount == 7 && (newMinorVersion < 1 || newMinorVersion > 4)) {
702 if (newWidth == 0 || newHeight == 0) {
712 if (this->width == newWidth && this->height == newHeight) {
718 newMipCount = recommendedCount;
727 this->frameCount = 1;
728 this->
flags &= ~FLAG_ENVMAP;
729 this->width = newWidth;
730 this->height = newHeight;
745 this->
flags |= flags_ & ~FLAG_MASK_INTERNAL;
791 this->format = newFormat;
796 newMipCount = recommendedCount;
814 newMipCount = recommended;
815 if (newMipCount == 1) {
819 if (newMipCount > 1) {
847 auto* outputDataPtr = imageResource->data.data();
850#ifdef SOURCEPP_BUILD_WITH_THREADS
851 std::vector<std::future<void>> futures;
852 futures.reserve((this->
mipCount - 1) * this->frameCount * faceCount * this->
sliceCount);
855 for (
int k = 0; k < faceCount; k++) {
857#ifdef SOURCEPP_BUILD_WITH_THREADS
858 futures.push_back(std::async(std::launch::async, [
this, filter, outputDataPtr, faceCount, j, k, l] {
860 for (
int i = 1; i < this->
mipCount; i++) {
861 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);
862 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) {
863 std::memcpy(outputDataPtr + offset, mip.data(), length);
866#ifdef SOURCEPP_BUILD_WITH_THREADS
868 if (std::thread::hardware_concurrency() > 0 && futures.size() >= std::thread::hardware_concurrency()) {
869 for (
auto& future : futures) {
878#ifdef SOURCEPP_BUILD_WITH_THREADS
879 for (
auto& future : futures) {
912 if (expectedLength == image->data.size()) {
963 static constexpr auto getReflectivityForImage = [](
const VTF& vtf, uint16_t frame, uint8_t face, uint16_t slice) {
964 static constexpr auto getReflectivityForPixel = [](
const ImagePixel::RGBA8888* pixel) -> math::Vec3f {
966 math::Vec3f ref{
static_cast<float>(pixel->r),
static_cast<float>(pixel->g),
static_cast<float>(pixel->b)};
976 for (uint64_t i = 0; i < rgba8888Data.size(); i += 4) {
977 out += getReflectivityForPixel(
reinterpret_cast<ImagePixel::RGBA8888*
>(rgba8888Data.data() + i));
984#ifdef SOURCEPP_BUILD_WITH_THREADS
985 if (this->frameCount > 1 || faceCount > 1 || this->
sliceCount > 1) {
986 std::vector<std::future<math::Vec3f>> futures;
987 futures.reserve(this->frameCount * faceCount * this->
sliceCount);
991 for (
int k = 0; k < faceCount; k++) {
993 futures.push_back(std::async(std::launch::async, [
this, j, k, l] {
994 return getReflectivityForImage(*
this, j, k, l);
996 if (std::thread::hardware_concurrency() > 0 && futures.size() >= std::thread::hardware_concurrency()) {
997 for (
auto& future : futures) {
1006 for (
auto& future : futures) {
1011 this->
reflectivity = getReflectivityForImage(*
this, 0, 0, 0);
1016 for (
int k = 0; k < faceCount; k++) {
1018 this->
reflectivity += getReflectivityForImage(*
this, j, k, l);
1051 for (
const auto& resource : this->resources) {
1052 if (resource.type == type) {
1060 for (
auto& resource : this->resources) {
1061 if (resource.type == type) {
1069 if (
const auto* resource = this->
getResource(type); resource && resource->
data.size() == data_.size()) {
1070 std::memcpy(resource->data.data(), data_.data(), data_.size());
1075 std::unordered_map<Resource::Type, std::pair<std::vector<std::byte>, uint64_t>> resourceData;
1076 for (
const auto& [type_, flags_, dataSpan] : this->resources) {
1077 resourceData[type_] = {std::vector<std::byte>{dataSpan.begin(), dataSpan.end()}, 0};
1081 if (data_.empty()) {
1082 resourceData.erase(type);
1084 resourceData[type] = {{data_.begin(), data_.end()}, 0};
1089 BufferStream writer{this->data};
1092 if (!resourceData.contains(resourceType)) {
1095 auto& [specificResourceData, offset] = resourceData[resourceType];
1096 if (resourceType == type) {
1100 {this->data.data() + offset, specificResourceData.size()},
1103 *resourcePtr = newResource;
1105 this->resources.push_back(newResource);
1107 }
else if (!resourceData.contains(resourceType)) {
1110 offset = writer.tell();
1111 writer.write(specificResourceData);
1113 this->data.resize(writer.size());
1115 for (
auto& [type_, flags_, dataSpan] : this->resources) {
1116 if (resourceData.contains(type_)) {
1117 const auto& [specificResourceData, offset] = resourceData[type_];
1118 dataSpan = {this->data.data() + offset, specificResourceData.size()};
1124 std::erase_if(this->resources, [type](
const Resource& resource) {
return resource.
type == type; });
1128 if (!newWidth) { newWidth = 1; }
1129 if (!newHeight) { newHeight = 1; }
1130 if (!newMipCount) { newMipCount = 1; }
1131 if (!newFrameCount) { newFrameCount = 1; }
1132 if (!newFaceCount) { newFaceCount = 1; }
1133 if (!newSliceCount) { newSliceCount = 1; }
1136 if (this->format == newFormat && this->width == newWidth && this->height == newHeight && this->
mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->
sliceCount == newSliceCount) {
1140 std::vector<std::byte> newImageData;
1142 if (this->format != newFormat && this->width == newWidth && this->height == newHeight && this->
mipCount == newMipCount && this->frameCount == newFrameCount && faceCount == newFaceCount && this->
sliceCount == newSliceCount) {
1146 for (
int i = newMipCount - 1; i >= 0; i--) {
1147 for (
int j = 0; j < newFrameCount; j++) {
1148 for (
int k = 0; k < newFaceCount; k++) {
1149 for (
int l = 0; l < newSliceCount; l++) {
1152 std::vector<std::byte> image{imageSpan.begin(), imageSpan.end()};
1153 if (this->width != newWidth || this->height != newHeight) {
1154 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);
1156 if (uint32_t offset, length;
ImageFormatDetails::getDataPosition(offset, length, this->format, i, newMipCount, j, newFrameCount, k, newFaceCount, newWidth, newHeight, l, newSliceCount) && image.size() == length) {
1157 std::memcpy(newImageData.data() + offset, image.data(), length);
1164 if (this->format != newFormat) {
1172 this->format = newFormat;
1173 this->width = newWidth;
1174 this->height = newHeight;
1176 this->frameCount = newFrameCount;
1177 if (newFaceCount > 1) {
1180 this->
flags &= ~FLAG_ENVMAP;
1187std::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 {
1196 auto sht = shtResource->getDataAsParticleSheet();
1197 const auto* sequence = sht.getSequenceFromID(shtSequenceID);
1198 if (!sequence || sequence->frames.size() <= shtFrame || shtBounds >= sht.getFrameBoundsCount()) {
1206 const auto& bounds = sequence->frames[shtFrame].bounds[shtBounds];
1207 uint16_t x1 = std::clamp<uint16_t>(std::floor(bounds.x1 *
static_cast<float>(this->getWidth(mip))), 0, this->getWidth(mip));
1208 uint16_t y1 = std::clamp<uint16_t>(std::ceil( bounds.y1 *
static_cast<float>(this->getHeight(mip))), 0, this->getHeight(mip));
1209 uint16_t x2 = std::clamp<uint16_t>(std::ceil( bounds.x2 *
static_cast<float>(this->getWidth(mip))), 0, this->getHeight(mip));
1210 uint16_t y2 = std::clamp<uint16_t>(std::floor(bounds.y2 *
static_cast<float>(this->getHeight(mip))), 0, this->getWidth(mip));
1212 if (x1 > x2) [[unlikely]] {
1215 if (y1 > y2) [[unlikely]] {
1218 spriteWidth = x2 - x1;
1219 spriteWidth = y2 - y1;
1221 const auto out =
ImageConversion::cropImageData(this->
getImageDataRaw(mip, frame, face, slice), this->
getFormat(), this->
getWidth(mip), spriteWidth, x1, this->
getHeight(mip), spriteHeight, y1);
1229std::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 {
1230 return ImageConversion::convertImageDataToFormat(this->
getParticleSheetFrameDataRaw(spriteWidth, spriteHeight, shtSequenceID, shtFrame, shtBounds, mip, frame, face, slice), this->
getFormat(), newFormat, spriteWidth, spriteHeight);
1233std::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 {
1238 std::vector<std::byte> particleSheetData;
1239 BufferStream writer{particleSheetData};
1241 auto bakedSheet = value.
bake();
1242 writer.write<uint32_t>(bakedSheet.size());
1243 writer.write(bakedSheet);
1244 particleSheetData.resize(writer.size());
1263 BufferStream writer{&lodData,
sizeof(lodData)};
1265 writer << u << v << u360 << v360;
1283 std::vector<std::byte> keyValuesData;
1284 BufferStream writer{keyValuesData};
1286 writer.write<uint32_t>(value.size());
1287 writer.write(value,
false);
1288 keyValuesData.resize(writer.size());
1329 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)) {
1330 return imageResource->data.subspan(offset, length);
1337 const auto rawImageData = this->
getImageDataRaw(mip, frame, face, slice);
1338 if (rawImageData.empty()) {
1349 if (imageData_.empty()) {
1354 uint16_t resizedWidth = width_, resizedHeight = height_;
1361 mip = newMipCount - 1;
1363 if (face > 6 || (face == 6 && (this->minorVersion < 1 || this->
minorVersion > 4))) {
1366 this->
regenerateImageData(format_, resizedWidth, resizedHeight, mip + 1, frame + 1, face ? (face < 5 ? 5 : face) : 0, slice + 1);
1370 if (this->mipCount <= mip || this->
frameCount <= frame || faceCount <= face || this->
sliceCount <= slice) {
1375 if (!imageResource) {
1378 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)) {
1379 std::vector<std::byte> image{imageData_.begin(), imageData_.end()};
1382 if (width_ != newWidth || height_ != newHeight) {
1385 if (format_ != this->format) {
1388 std::memcpy(imageResource->data.data() + offset, image.data(), image.size());
1395 int inputWidth, inputHeight, inputFrameCount;
1399 if (imageData_.empty() || inputFormat ==
ImageFormat::EMPTY || !inputWidth || !inputHeight || !inputFrameCount) {
1404 if (inputFrameCount == 1) {
1405 return this->
setImage(imageData_, inputFormat, inputWidth, inputHeight, filter, mip, frame, face, slice);
1409 bool allSuccess =
true;
1411 for (
int currentFrame = 0; currentFrame < inputFrameCount; currentFrame++) {
1412 if (!this->
setImage({imageData_.data() + currentFrame * frameSize, imageData_.data() + currentFrame * frameSize + frameSize}, inputFormat, inputWidth, inputHeight, filter, mip, frame + currentFrame, face, slice)) {
1415 if (currentFrame == 0 && this->frameCount < frame + inputFrameCount) {
1428 if (
auto data_ = this->
saveImageToFile(mip, frame, face, slice, fileFormat); !data_.empty()) {
1440 return thumbnailResource->data;
1447 if (rawThumbnailData.empty()) {
1464 this->thumbnailHeight = height_;
1473 this->thumbnailHeight = 16;
1474 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));
1480 this->thumbnailHeight = 0;
1496 std::vector<std::byte> out;
1497 BufferStream writer{out};
1501 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(&type));
1503 writer_.write<uint32_t>(type);
1504 const auto resourceOffsetPos = writer_.tell();
1505 writer_.seek(0, std::ios::end);
1506 const auto resourceOffsetValue = writer_.tell();
1507 writer_.write(
data);
1508 writer_.seek_u(resourceOffsetPos).write<uint32_t>(resourceOffsetValue);
1513 writer.set_big_endian(
true);
1520 writer.write<uint32_t>(8);
1521 const auto headerLengthPos = writer.tell();
1526 .write(this->height)
1528 .write(this->frameCount);
1529 const auto preloadPos = writer.tell();
1533 .write<uint8_t>(this->resources.size())
1538 .write(this->format)
1539 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[0] * 255)), 0, 255))
1540 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[1] * 255)), 0, 255))
1541 .write<uint8_t>(std::clamp(
static_cast<int>(std::roundf(this->
reflectivity[2] * 255)), 0, 255))
1542 .write<uint8_t>(255);
1543 const auto compressionPos = writer.tell();
1544 writer.write<uint32_t>(0);
1548 writer.write<uint32_t>(0);
1551 std::vector<std::byte> imageResourceData;
1552 bool hasCompression =
false;
1554 imageResourceData.assign(imageResource->data.begin(), imageResource->data.end());
1555 ::swapImageDataEndianForConsole(imageResourceData, this->format, this->width, this->height, this->platform);
1565 fixedCompressionLevel = 6;
1568 if (compressedData) {
1569 imageResourceData.assign(compressedData->begin(), compressedData->end());
1571 hasCompression =
false;
1576 const auto resourceStart = writer.tell();
1577 const auto headerSize = resourceStart + (this->
getResources().size() *
sizeof(uint64_t));
1578 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(resourceStart);
1579 while (writer.tell() < headerSize) {
1580 writer.write<uint64_t>(0);
1582 writer.seek_u(resourceStart);
1586 auto curPos = writer.tell();
1587 const auto imagePos = writer.seek(0, std::ios::end).tell();
1588 writer.seek_u(preloadPos).write<uint16_t>(imagePos).seek_u(curPos);
1590 writeNonLocalResource(writer, resourceType, imageResourceData, this->platform);
1592 if (hasCompression) {
1593 curPos = writer.tell();
1594 writer.seek_u(compressionPos).write<uint32_t>(imageResourceData.size()).seek_u(curPos);
1596 }
else if (
const auto* resource = this->
getResource(resourceType)) {
1597 std::vector<std::byte> resData{resource->data.begin(), resource->data.end()};
1600 switch (resource->type) {
1613 if (resData.size() >=
sizeof(uint32_t)) {
1614 BufferStream::swap_endian(
reinterpret_cast<uint32_t*
>(resData.data()));
1620 writer.set_big_endian(
false);
1622 writer.set_big_endian(
true);
1623 writer.write(resource->data);
1625 writeNonLocalResource(writer, resource->type, resource->data, this->platform);
1630 out.resize(writer.size());
1635 const auto headerLengthPos = writer.tell();
1636 writer.write<uint32_t>(0);
1640 .write(this->height)
1642 .write(this->frameCount)
1648 .write(this->format)
1652 .write(this->thumbnailHeight);
1654 if (this->minorVersion >= 2) {
1658 if (this->minorVersion < 3) {
1660 for (uint16_t i = 0; i < headerAlignment; i++) {
1661 writer.write<std::byte>({});
1663 const auto headerSize = writer.tell();
1664 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(headerSize);
1667 writer.write(thumbnailResource->data);
1670 writer.write(imageResource->data);
1673 std::vector<std::byte> auxCompressionResourceData;
1674 std::vector<std::byte> compressedImageResourceData;
1675 bool hasAuxCompression =
false;
1677 hasAuxCompression = this->minorVersion >= 6 && this->
compressionLevel != 0;
1678 if (hasAuxCompression) {
1680 auxCompressionResourceData.resize((this->
mipCount * this->frameCount * faceCount + 2) *
sizeof(uint32_t));
1681 BufferStream auxWriter{auxCompressionResourceData,
false};
1688 .write<uint32_t>(auxCompressionResourceData.size() -
sizeof(uint32_t))
1692 for (
int i = this->
mipCount - 1; i >= 0; i--) {
1694 for (
int k = 0; k < faceCount; k++) {
1695 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)) {
1697 compressedImageResourceData.insert(compressedImageResourceData.end(), compressedData.begin(), compressedData.end());
1698 auxWriter.write<uint32_t>(compressedData.size());
1710 .write<uint32_t>(this->
getResources().size() + hasAuxCompression)
1711 .write<uint64_t>(0);
1713 const auto resourceStart = writer.tell();
1714 const auto headerSize = resourceStart + ((this->
getResources().size() + hasAuxCompression) *
sizeof(uint64_t));
1715 writer.seek_u(headerLengthPos).write<uint32_t>(headerSize).seek_u(resourceStart);
1716 while (writer.tell() < headerSize) {
1717 writer.write<uint64_t>(0);
1719 writer.seek_u(resourceStart);
1723 writeNonLocalResource(writer, resourceType, auxCompressionResourceData, this->platform);
1725 writeNonLocalResource(writer, resourceType, compressedImageResourceData, this->platform);
1726 }
else if (
const auto* resource = this->
getResource(resourceType)) {
1729 writer.write(resource->data);
1731 writeNonLocalResource(writer, resource->type, resource->data, this->platform);
1737 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
static ImageFormat getDefaultCompressedFormat(ImageFormat inputFormat, uint32_t majorVersion, uint32_t minorVersion)
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()
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()
bool setFaceCount(bool isCubemap, bool hasSphereMap=false)
uint32_t getMinorVersion() const
void removeFlags(Flags flags_)
std::vector< std::byte > data
Platform getPlatform() const
uint8_t getFaceCount() const
ImageFormat thumbnailFormat
void setSize(uint16_t newWidth, uint16_t newHeight, ImageConversion::ResizeFilter filter)
bool setFrameFaceAndSliceCount(uint16_t newFrameCount, bool isCubemap, bool hasSphereMap=false, uint16_t newSliceCount=1)
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
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)
static void createInternal(VTF &writer, CreationOptions options)
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)
uint8_t getThumbnailHeight() const
const Resource * getResource(Resource::Type type) const
void setParticleSheetResource(const SHT &value)
void setKeyValuesDataResource(const std::string &value)
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 > 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 > 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