Skip to content

Commit

Permalink
wip(vtfpp): fix rgba8888 -> rgb565/bgr565 conversions, fix accessing …
Browse files Browse the repository at this point in the history
…mip data always with mip 0 width/height
  • Loading branch information
craftablescience committed May 29, 2024
1 parent a78fdeb commit a86db5f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/vtfpp/ImageConversion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ namespace {
case RGB565:
for (int i = 0; i < imageData.size(); i += 2) {
auto pixel = *reinterpret_cast<const uint16_t*>(&imageData[i]);
newData.push_back(static_cast<std::byte>(((((pixel >> 11) & 0x1f) * 527) + 23) >> 6));
newData.push_back(static_cast<std::byte>(((((pixel >> 5) & 0x3f) * 259) + 33) >> 6));
newData.push_back(static_cast<std::byte>(((( pixel & 0x1f) * 527) + 23) >> 6));
newData.push_back(static_cast<std::byte>(((((pixel >> 5) & 0x3f) * 259) + 33) >> 6));
newData.push_back(static_cast<std::byte>(((((pixel >> 11) & 0x1f) * 527) + 23) >> 6));
newData.push_back(std::byte{0xff});
}
break;
Expand Down Expand Up @@ -156,9 +156,9 @@ namespace {
case BGR565:
for (int i = 0; i < imageData.size(); i += 2) {
auto pixel = *reinterpret_cast<const uint16_t*>(&imageData[i]);
newData.push_back(static_cast<std::byte>(((( pixel & 0x1f) * 527) + 23) >> 6));
newData.push_back(static_cast<std::byte>(((((pixel >> 5) & 0x3f) * 259) + 33) >> 6));
newData.push_back(static_cast<std::byte>(((((pixel >> 11) & 0x1f) * 527) + 23) >> 6));
newData.push_back(static_cast<std::byte>(((((pixel >> 5) & 0x3f) * 259) + 33) >> 6));
newData.push_back(static_cast<std::byte>(((( pixel & 0x1f) * 527) + 23) >> 6));
newData.push_back(std::byte{0xff});
}
break;
Expand Down Expand Up @@ -376,7 +376,7 @@ namespace {

CMP_CompressOptions options{};
options.dwSize = sizeof(options);
options.bDXT1UseAlpha = false;
options.bDXT1UseAlpha = format == ImageFormat::DXT1_ONE_BIT_ALPHA;
options.dwnumThreads = 0;

if (CMP_ConvertTexture(&srcTexture, &destTexture, &options, nullptr) != CMP_OK) {
Expand Down Expand Up @@ -410,7 +410,7 @@ namespace {

CMP_CompressOptions options{};
options.dwSize = sizeof(options);
options.bDXT1UseAlpha = false;
options.bDXT1UseAlpha = format == ImageFormat::DXT1_ONE_BIT_ALPHA;
options.dwnumThreads = 0;

if (CMP_ConvertTexture(&srcTexture, &destTexture, &options, nullptr) != CMP_OK) {
Expand Down
4 changes: 2 additions & 2 deletions src/vtfpp/vtfpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ std::vector<std::byte> VTF::getImageDataAs(ImageFormat newFormat, uint8_t mip, u
if (rawImageData.empty()) {
return {};
}
return ImageConversion::convertImageDataToFormat(rawImageData, this->format, newFormat, this->width, this->height);
return ImageConversion::convertImageDataToFormat(rawImageData, this->format, newFormat, ImageDimensions::getMipDim(mip, this->width), ImageDimensions::getMipDim(mip, this->height));
}

std::vector<std::byte> VTF::getImageDataAsRGBA8888(uint8_t mip, uint16_t frame, uint16_t face, uint16_t slice) const {
Expand All @@ -260,7 +260,7 @@ std::vector<std::byte> VTF::convertImageDataToFile(uint8_t mip, uint16_t frame,
if (rawImageData.empty()) {
return {};
}
return ImageConversion::convertImageDataToFile(rawImageData, this->format, this->width, this->height);
return ImageConversion::convertImageDataToFile(rawImageData, this->format, ImageDimensions::getMipDim(mip, this->width), ImageDimensions::getMipDim(mip, this->height));
}

std::span<const std::byte> VTF::getThumbnailData() const {
Expand Down
2 changes: 0 additions & 2 deletions test/vtfpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ TEST(vtfpp, read_fmt_rgb565) {
ASSERT_TRUE(image);
EXPECT_EQ(image->data.size(), ImageFormatDetails::getDataLength(vtf.getFormat(), vtf.getMipCount(), vtf.getFrameCount(), vtf.getFaceCount(), vtf.getWidth(), vtf.getHeight(), vtf.getSliceCount()));

// todo: fix
// Convert
::createFile("fmt_rgb565.png", vtf.convertImageDataToFile());
}
Expand Down Expand Up @@ -340,7 +339,6 @@ TEST(vtfpp, read_fmt_bgr565) {
ASSERT_TRUE(image);
EXPECT_EQ(image->data.size(), ImageFormatDetails::getDataLength(vtf.getFormat(), vtf.getMipCount(), vtf.getFrameCount(), vtf.getFaceCount(), vtf.getWidth(), vtf.getHeight(), vtf.getSliceCount()));

// todo: fix
// Convert
::createFile("fmt_bgr565.png", vtf.convertImageDataToFile());
}
Expand Down

0 comments on commit a86db5f

Please sign in to comment.