Skip to content

Commit

Permalink
Fix incorrect index calculations in image conversions (#735)
Browse files Browse the repository at this point in the history
Fixes #733. Fixes #736.
  • Loading branch information
Császár Mátyás authored Jul 13, 2023
1 parent 80a61bc commit 682f456
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion tests/cts
Submodule cts updated 57 files
+ clitests/golden/create/basic_exr/A2R10G10B10_SINT_PACK32_RGBA_HALF_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/A2R10G10B10_SINT_PACK32_RGBA_HALF_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/A2R10G10B10_UINT_PACK32_RGBA_HALF_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/A2R10G10B10_UINT_PACK32_RGBA_HALF_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/B10G11R11_UFLOAT_PACK32_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/B10G11R11_UFLOAT_PACK32_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/E5B9G9R9_UFLOAT_PACK32_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/E5B9G9R9_UFLOAT_PACK32_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16G16B16A16_SFLOAT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16G16B16A16_SFLOAT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16G16B16A16_SINT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16G16B16A16_SINT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16G16B16_SFLOAT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16G16B16_SFLOAT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16G16B16_SINT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16G16B16_SINT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16G16_SFLOAT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16G16_SFLOAT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16G16_SINT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16G16_SINT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16_SFLOAT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16_SFLOAT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R16_SINT_RGBA_FLOAT_HDR_1000x50.ktx2
+ clitests/golden/create/basic_exr/R16_SINT_RGBA_FLOAT_HDR_50x1000.ktx2
+ clitests/golden/create/basic_exr/R32G32B32A32_UINT_RGBA_UINT_1000x50.ktx2
+ clitests/golden/create/basic_exr/R32G32B32A32_UINT_RGBA_UINT_50x1000.ktx2
+ clitests/golden/create/basic_exr/R32G32B32_UINT_RGBA_UINT_1000x50.ktx2
+ clitests/golden/create/basic_exr/R32G32B32_UINT_RGBA_UINT_50x1000.ktx2
+ clitests/golden/create/basic_exr/R32G32_UINT_RGBA_UINT_1000x50.ktx2
+ clitests/golden/create/basic_exr/R32G32_UINT_RGBA_UINT_50x1000.ktx2
+ clitests/golden/create/basic_exr/R32_UINT_RGBA_UINT_1000x50.ktx2
+ clitests/golden/create/basic_exr/R32_UINT_RGBA_UINT_50x1000.ktx2
+ clitests/golden/create/basic_png/ASTC_4x4_UNORM_BLOCK_RGBA8_1000x50.ktx2
+ clitests/golden/create/basic_png/ASTC_4x4_UNORM_BLOCK_RGBA8_50x1000.ktx2
+ clitests/golden/create/basic_png/R5G5B5A1_UNORM_PACK16_RGBA8_1000x50.ktx2
+ clitests/golden/create/basic_png/R5G5B5A1_UNORM_PACK16_RGBA8_50x1000.ktx2
+ clitests/golden/create/basic_png/R5G6B5_UNORM_PACK16_RGB8_1000x50.ktx2
+ clitests/golden/create/basic_png/R8G8B8A8_UNORM_RGBA8_1000x50.ktx2
+ clitests/golden/create/basic_png/R8G8B8A8_UNORM_RGBA8_50x1000.ktx2
+ clitests/golden/create/basic_png/R8G8B8_UNORM_RGB8_1000x50.ktx2
+ clitests/golden/create/basic_png/R8G8B8_UNORM_RGB8_50x1000.ktx2
+ clitests/golden/create/basic_png/R8G8_UNORM_RGB8_1000x50.ktx2
+ clitests/golden/create/basic_png/R8G8_UNORM_RGB8_50x1000.ktx2
+ clitests/golden/create/basic_png/R8_UNORM_RGB8_1000x50.ktx2
+ clitests/golden/create/basic_png/R8_UNORM_RGB8_50x1000.ktx2
+ clitests/input/exr/basic_RGBA_FLOAT_HDR_1000x50.exr
+ clitests/input/exr/basic_RGBA_FLOAT_HDR_50x1000.exr
+ clitests/input/exr/basic_RGBA_HALF_HDR_1000x50.exr
+ clitests/input/exr/basic_RGBA_HALF_HDR_50x1000.exr
+ clitests/input/exr/basic_RGBA_UINT_1000x50.exr
+ clitests/input/exr/basic_RGBA_UINT_50x1000.exr
+ clitests/input/png/basic_RGB8_1000x50.png
+ clitests/input/png/basic_RGB8_50x1000.png
+ clitests/input/png/basic_RGBA8_1000x50.png
+ clitests/input/png/basic_RGBA8_50x1000.png
+34 −1 clitests/tests/create/basic_exr.json
+16 −1 clitests/tests/create/basic_png.json
4 changes: 2 additions & 2 deletions tools/imageio/exr.imageio/exrinput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,12 @@ void ExrInput::readImage(void* outputBuffer, size_t bufferByteCount,
// Copy the data
const auto copyData = [&](unsigned char* ptr, uint32_t dataSize) {
const auto sourcePtr = [&](uint32_t channel, uint32_t x, uint32_t y) {
return reinterpret_cast<const unsigned char*>(image.images[channel] + (y * height + x) * dataSize);
return reinterpret_cast<const unsigned char*>(image.images[channel] + (y * width + x) * dataSize);
};

for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
auto* targetPixel = ptr + (y * height * numTargetChannels + x * numTargetChannels) * dataSize;
auto* targetPixel = ptr + (y * width * numTargetChannels + x * numTargetChannels) * dataSize;
for (uint32_t c = 0; c < numTargetChannels; ++c)
std::memcpy(targetPixel + c * dataSize, sourcePtr(*channels[c], x, y), dataSize);
}
Expand Down
18 changes: 9 additions & 9 deletions tools/ktx/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -808,7 +808,7 @@ class ImageT : public Image {
for (uint32_t c = 0; c < numChannels; ++c) {
const auto sourceValue = c < componentCount ? pixels[y * width + x][c] : (c != 3 ? componentType{0} : Color::one());
const auto value = ktx::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, targetBits);
auto* target = data.data() + (y * height * numChannels + x * numChannels + c) * targetBytes;
auto* target = data.data() + (y * width * numChannels + x * numChannels + c) * targetBytes;

if (targetBytes == 1) {
const auto outValue = static_cast<uint8_t>(value);
Expand Down Expand Up @@ -850,7 +850,7 @@ class ImageT : public Image {
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
const auto& pixel = pixels[y * width + x];
auto* target = data.data() + (y * height + x) * targetPackBytes;
auto* target = data.data() + (y * width + x) * targetPackBytes;

const auto copy = [&](auto& pack) {
using PackType = std::remove_reference_t<decltype(pack)>;
Expand Down Expand Up @@ -911,7 +911,7 @@ class ImageT : public Image {
for (uint32_t x = 0; x < width; ++x) {
for (uint32_t c = 0; c < numChannels; ++c) {
const auto value = c < componentCount ? pixels[y * width + x][c] : (c != 3 ? componentType{0} : componentType{1});
auto* target = data.data() + (y * height * numChannels + x * numChannels + c) * targetBytes;
auto* target = data.data() + (y * width * numChannels + x * numChannels + c) * targetBytes;

if (sizeof(componentType) == targetBytes) {
*reinterpret_cast<componentType*>(target) = value;
Expand All @@ -937,7 +937,7 @@ class ImageT : public Image {

for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
auto* target = data.data() + (y * height + x) * 4;
auto* target = data.data() + (y * width + x) * 4;

const auto pixel = (*this)(x, y);
const auto r = pixel[0];
Expand All @@ -959,7 +959,7 @@ class ImageT : public Image {

for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
auto* target = data.data() + (y * height + x) * 4;
auto* target = data.data() + (y * width + x) * 4;

const auto pixel = (*this)(x, y);
const auto r = pixel[0];
Expand All @@ -984,7 +984,7 @@ class ImageT : public Image {
for (uint32_t x = 0; x < width; ++x) {
for (uint32_t c = 0; c < numChannels; ++c) {
const auto value = c < componentCount ? pixels[y * width + x][c] : c != 3 ? 0 : componentType{1};
auto* target = data.data() + (y * height * numChannels + x * numChannels + c) * targetBytes;
auto* target = data.data() + (y * width * numChannels + x * numChannels + c) * targetBytes;

if (targetBytes == 1) {
const auto outValue = static_cast<uint8_t>(value);
Expand Down Expand Up @@ -1017,7 +1017,7 @@ class ImageT : public Image {
for (uint32_t x = 0; x < width; ++x) {
for (uint32_t c = 0; c < numChannels; ++c) {
const auto value = c < componentCount ? pixels[y * width + x][c] : c != 3 ? 0 : componentType{1};
auto* target = data.data() + (y * height * numChannels + x * numChannels + c) * targetBytes;
auto* target = data.data() + (y * width * numChannels + x * numChannels + c) * targetBytes;

if (targetBytes == 1) {
const auto outValue = static_cast<int8_t>(value);
Expand Down Expand Up @@ -1048,7 +1048,7 @@ class ImageT : public Image {
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
const auto& pixel = pixels[y * width + x];
auto* target = data.data() + (y * height + x) * sizeof(uint32_t);
auto* target = data.data() + (y * width + x) * sizeof(uint32_t);

uint32_t pack = 0;
pack |= ktx::convertUINT(static_cast<uint32_t>(pixel[0]), sizeof(uint32_t) * 8, c0) << (c1 + c2 + c3);
Expand All @@ -1072,7 +1072,7 @@ class ImageT : public Image {
for (uint32_t y = 0; y < height; ++y) {
for (uint32_t x = 0; x < width; ++x) {
const auto& pixel = pixels[y * width + x];
auto* target = data.data() + (y * height + x) * sizeof(uint32_t);
auto* target = data.data() + (y * width + x) * sizeof(uint32_t);

uint32_t pack = 0;
pack |= ktx::convertSINT(ktx::bit_cast<uint32_t>(static_cast<int32_t>(pixel[0])), sizeof(uint32_t) * 8, c0) << (c1 + c2 + c3);
Expand Down

0 comments on commit 682f456

Please sign in to comment.