Skip to content

Commit

Permalink
Do target_type changes only in toktx (#757)
Browse files Browse the repository at this point in the history
They were being done in both `toktx` and the `imageinput` plugin.

Fixes #727.

* Move image-related utilities and image.hpp from `ktxtools` to `imageio` target for sharing
   between new and legacy tools.
* Change `toktx` to use the updated version of image.hpp.
* Make `other_Includes` a system include for `imageio` and `unittests`.
* Make `toktx`  use `imageio`'s INTERFACE includes instead of directly referencing the directories.
* Fix mismatched `new[]` and `delete` in `glloadtests`.
  • Loading branch information
MarkCallow authored Aug 27, 2023
1 parent c3a0695 commit 2cf053c
Show file tree
Hide file tree
Showing 42 changed files with 896 additions and 1,457 deletions.
11 changes: 8 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,18 @@ macro(common_libktx_settings target enable_write library_type)
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/lib/basisu/zstd>
$<INSTALL_INTERFACE:lib/basisu/zstd>

$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/other_include>
$<INSTALL_INTERFACE:other_include>

$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/utils>
$<INSTALL_INTERFACE:utils>
)

target_include_directories(
${target}
SYSTEM
PRIVATE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/other_include>
$<INSTALL_INTERFACE:other_include>
)

if( ${library_type} STREQUAL STATIC )
target_compile_definitions(${target} PUBLIC KHRONOS_STATIC)
endif()
Expand Down
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_10x5_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_12x10_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_12x12_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_4x4_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x5_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x6_kodim17_fast.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x6_kodim17_fastest.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x6_kodim17_medium.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x6_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x6_posy.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_6x6_posz.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_8x6_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_8x8_posx.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/astc_mipmap_ldr_cubemap_6x6.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/testimages/cimg5293_uastc.ktx2
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/testimages/cimg5293_uastc_zstd.ktx2
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/testimages/cubemap_goldengate_uastc_rdo4_zstd5_rd.ktx2
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/testimages/cubemap_yokohama_basis_rd.ktx2
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/testimages/ktx_document_basis.ktx2
Git LFS file not shown
4 changes: 2 additions & 2 deletions tests/testimages/ktx_document_uastc_rdo4_zstd5.ktx2
Git LFS file not shown
8 changes: 8 additions & 0 deletions tests/tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@ PRIVATE
$<TARGET_PROPERTY:ktx,INCLUDE_DIRECTORIES>
${PROJECT_SOURCE_DIR}/lib
${PROJECT_SOURCE_DIR}/tools
${PROJECT_SOURCE_DIR}/tools/imageio
loadtests/common
)

target_include_directories(
unittests
SYSTEM
PRIVATE
${PROJECT_SOURCE_DIR}/other_include
)

target_link_libraries(
unittests
gtest
Expand Down
10 changes: 5 additions & 5 deletions tests/toktx-tests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -241,20 +241,20 @@ if (NOT ${CPU_ARCHITECTURE} STREQUAL "arm64" )
gencmpktx( astc_mipmap_ldr_6x6_posx astc_mipmap_ldr_6x6_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 6x6 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_6x6_posz astc_mipmap_ldr_6x6_posz.ktx2 ../srcimages/Yokohama3/posz.jpg "--test --encode astc --astc_blk_d 6x6 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_6x6_posy astc_mipmap_ldr_6x6_posy.ktx2 ../srcimages/Yokohama3/posy.jpg "--test --encode astc --astc_blk_d 6x6 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_6x6_kodim17_fastest astc_mipmap_ldr_6x6_kodim17_fastest.ktx2 ../srcimages/kodim17.png "--test --encode astc --astc_blk_d 6x6 --genmipmap --astc_quality fastest " "" "" )
gencmpktx( astc_mipmap_ldr_6x6_kodim17_fast astc_mipmap_ldr_6x6_kodim17_fast.ktx2 ../srcimages/kodim17.png "--test --encode astc --astc_blk_d 6x6 --genmipmap --astc_quality fast " "" "" )
endif()
gencmpktx( astc_mipmap_ldr_6x6_kodim17_fastest astc_mipmap_ldr_6x6_kodim17_fastest.ktx2 ../srcimages/kodim17.png "--test --encode astc --astc_blk_d 6x6 --genmipmap --astc_quality fastest " "" "" )
gencmpktx( astc_mipmap_ldr_6x6_kodim17_fast astc_mipmap_ldr_6x6_kodim17_fast.ktx2 ../srcimages/kodim17.png "--test --encode astc --astc_blk_d 6x6 --genmipmap --astc_quality fast " "" "" )
gencmpktx( astc_mipmap_ldr_6x6_kodim17_medium astc_mipmap_ldr_6x6_kodim17_medium.ktx2 ../srcimages/kodim17.png "--test --encode astc --astc_blk_d 6x6 --genmipmap --astc_quality medium " "" "" )

if (NOT ${CPU_ARCHITECTURE} STREQUAL "arm64" )
gencmpktx( astc_mipmap_ldr_4x4_posx astc_mipmap_ldr_4x4_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 4x4 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_6x5_posx astc_mipmap_ldr_6x5_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 6x5 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_8x6_posx astc_mipmap_ldr_8x6_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 8x6 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_10x5_posx astc_mipmap_ldr_10x5_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 10x5 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_8x8_posx astc_mipmap_ldr_8x8_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 8x8 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_12x10_posx astc_mipmap_ldr_12x10_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 12x10 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_12x12_posx astc_mipmap_ldr_12x12_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 12x12 --genmipmap" "" "" )
endif()
gencmpktx( astc_mipmap_ldr_8x8_posx astc_mipmap_ldr_8x8_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 8x8 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_12x10_posx astc_mipmap_ldr_12x10_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 12x10 --genmipmap" "" "" )
gencmpktx( astc_mipmap_ldr_12x12_posx astc_mipmap_ldr_12x12_posx.ktx2 ../srcimages/Yokohama3/posx.jpg "--test --encode astc --astc_blk_d 12x12 --genmipmap" "" "" )

gencmpktx( astc_ldr_4x4_FlightHelmet_baseColor astc_ldr_4x4_FlightHelmet_baseColor.ktx2 ../srcimages/FlightHelmet_baseColor.png "--test --encode astc --astc_blk_d 4x4" "" "")
gencmpktx( astc_ldr_6x5_FlightHelmet_baseColor astc_ldr_6x5_FlightHelmet_baseColor.ktx2 ../srcimages/FlightHelmet_baseColor.png "--test --encode astc --astc_blk_d 6x5" "" "")
Expand Down
2 changes: 1 addition & 1 deletion tests/unittests/image_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#endif

#include "gtest/gtest.h"
#include "toktx/image.hpp"
#include "image.hpp"

namespace {

Expand Down
4 changes: 2 additions & 2 deletions tests/webgl/libktx-webgl/ktx_app_basis.ktx2
Git LFS file not shown
2 changes: 1 addition & 1 deletion tests/webgl/libktx-webgl/ktx_document_uastc_rdo5.ktx2
Git LFS file not shown
13 changes: 11 additions & 2 deletions tools/imageio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ set( PLUGIN_HEADERS

add_library( imageio STATIC
formatdesc.h
image.hpp
imageinput.cc
imageio.cc
imageio.h
imageio_utility.h
imageoutput.cc
${PROJECT_SOURCE_DIR}/lib/astc-encoder/Source/tinyexr.h
${PROJECT_SOURCE_DIR}/lib/basisu/encoder/jpgd.cpp
Expand All @@ -43,11 +45,18 @@ target_include_directories(
PUBLIC
.
PRIVATE
${PROJECT_SOURCE_DIR}/lib/astc-encoder/Source
$<TARGET_PROPERTY:objUtil,INTERFACE_INCLUDE_DIRECTORIES>
)

target_include_directories(
imageio
SYSTEM
PUBLIC
${PROJECT_SOURCE_DIR}/other_include
PRIVATE
${PROJECT_SOURCE_DIR}/lib/astc-encoder/Source
${PROJECT_SOURCE_DIR}/lib/basisu
${PROJECT_SOURCE_DIR}/lib/dfdutils
${PROJECT_SOURCE_DIR}/other_include
)

target_compile_definitions(
Expand Down
4 changes: 4 additions & 0 deletions tools/imageio/exr.imageio/exrinput.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ void ExrInput::open(ImageSpec& newspec) {
newspec = spec();
}

/// @brief Read an entire image into contiguous memory performing conversions
/// to @a requestFormat.
///
/// Supported conversions are half->[half,float,uint], float->float, and uint->uint.
void ExrInput::readImage(void* outputBuffer, size_t bufferByteCount,
uint32_t subimage, uint32_t miplevel,
const FormatDescriptor& requestFormat) {
Expand Down
12 changes: 12 additions & 0 deletions tools/imageio/formatdesc.h
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,18 @@ struct FormatDescriptor {
}
return maxBitLength;
}
bool anyChannelBitLengthNotEqual(uint32_t bitLength) const {
for (uint32_t i = 0; i < 16; ++i) {
uint32_t channelBitLength = 0;
for (const auto& sample : samples)
if (sample.channelType == i)
channelBitLength += sample.bitLength + 1;

if (bitLength != channelBitLength)
return true;
}
return false;
}
khr_df_sample_datatype_qualifiers_e
channelDataType(khr_df_model_channels_e c) const {
// TODO: Fix for shared exponent case...
Expand Down
41 changes: 24 additions & 17 deletions tools/ktx/image.hpp → tools/imageio/image.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,24 @@
#ifndef IMAGE_HPP
#define IMAGE_HPP

#include <cmath>
#include <algorithm>
#include <array>
#include <cmath>
#include <sstream>
#include <stdexcept>
#include <string_view>
#include <vector>
#include <KHR/khr_df.h>
#include <fmt/format.h>
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4201)
#endif
#include <glm/gtc/packing.hpp>

#include "utility.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#include "imageio_utility.h"
#include "unused.h"
#include "encoder/basisu_resampler.h"
#include "encoder/basisu_resampler_filters.h"
Expand Down Expand Up @@ -807,7 +814,7 @@ class ImageT : public Image {
for (uint32_t x = 0; x < width; ++x) {
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);
const auto value = imageio::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, targetBits);
auto* target = data.data() + (y * width * numChannels + x * numChannels + c) * targetBytes;

if (targetBytes == 1) {
Expand Down Expand Up @@ -857,22 +864,22 @@ class ImageT : public Image {

if (packC0) {
const auto sourceValue = hasC0 ? pixel[0] : componentType{0};
const auto value = ktx::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c0);
const auto value = imageio::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c0);
pack |= static_cast<PackType>(value) << (c0Pad + c1 + c1Pad + c2 + c2Pad + c3 + c3Pad);
}
if (packC1) {
const auto sourceValue = hasC1 ? pixel[1] : componentType{0};
const auto value = ktx::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c1);
const auto value = imageio::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c1);
pack |= static_cast<PackType>(value) << (c1Pad + c2 + c2Pad + c3 + c3Pad);
}
if (packC2) {
const auto sourceValue = hasC2 ? pixel[2] : componentType{0};
const auto value = ktx::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c2);
const auto value = imageio::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c2);
pack |= static_cast<PackType>(value) << (c2Pad + c3 + c3Pad);
}
if (packC3) {
const auto sourceValue = hasC3 ? pixel[3] : Color::one();
const auto value = ktx::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c3);
const auto value = imageio::convertUNORM(static_cast<uint32_t>(sourceValue), sourceBits, c3);
pack |= static_cast<PackType>(value) << (c3Pad);
}
};
Expand Down Expand Up @@ -916,7 +923,7 @@ class ImageT : public Image {
if (sizeof(componentType) == targetBytes) {
*reinterpret_cast<componentType*>(target) = value;
} else if (targetBytes == 2) {
const auto outValue = ktx::float_to_half(static_cast<float>(value));
const auto outValue = imageio::float_to_half(static_cast<float>(value));
std::memcpy(target, &outValue, targetBytes);
} else if (targetBytes == 4) {
const auto outValue = static_cast<float>(value);
Expand Down Expand Up @@ -1051,10 +1058,10 @@ class ImageT : public Image {
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);
pack |= ktx::convertUINT(static_cast<uint32_t>(pixel[1]), sizeof(uint32_t) * 8, c1) << (c2 + c3);
pack |= ktx::convertUINT(static_cast<uint32_t>(pixel[2]), sizeof(uint32_t) * 8, c2) << c3;
pack |= ktx::convertUINT(static_cast<uint32_t>(pixel[3]), sizeof(uint32_t) * 8, c3);
pack |= imageio::convertUINT(static_cast<uint32_t>(pixel[0]), sizeof(uint32_t) * 8, c0) << (c1 + c2 + c3);
pack |= imageio::convertUINT(static_cast<uint32_t>(pixel[1]), sizeof(uint32_t) * 8, c1) << (c2 + c3);
pack |= imageio::convertUINT(static_cast<uint32_t>(pixel[2]), sizeof(uint32_t) * 8, c2) << c3;
pack |= imageio::convertUINT(static_cast<uint32_t>(pixel[3]), sizeof(uint32_t) * 8, c3);

std::memcpy(target, &pack, sizeof(pack));
}
Expand All @@ -1075,10 +1082,10 @@ class ImageT : public Image {
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);
pack |= ktx::convertSINT(ktx::bit_cast<uint32_t>(static_cast<int32_t>(pixel[1])), sizeof(uint32_t) * 8, c1) << (c2 + c3);
pack |= ktx::convertSINT(ktx::bit_cast<uint32_t>(static_cast<int32_t>(pixel[2])), sizeof(uint32_t) * 8, c2) << c3;
pack |= ktx::convertSINT(ktx::bit_cast<uint32_t>(static_cast<int32_t>(pixel[3])), sizeof(uint32_t) * 8, c3);
pack |= imageio::convertSINT(imageio::bit_cast<uint32_t>(static_cast<int32_t>(pixel[0])), sizeof(uint32_t) * 8, c0) << (c1 + c2 + c3);
pack |= imageio::convertSINT(imageio::bit_cast<uint32_t>(static_cast<int32_t>(pixel[1])), sizeof(uint32_t) * 8, c1) << (c2 + c3);
pack |= imageio::convertSINT(imageio::bit_cast<uint32_t>(static_cast<int32_t>(pixel[2])), sizeof(uint32_t) * 8, c2) << c3;
pack |= imageio::convertSINT(imageio::bit_cast<uint32_t>(static_cast<int32_t>(pixel[3])), sizeof(uint32_t) * 8, c3);

std::memcpy(target, &pack, sizeof(pack));
}
Expand Down
Loading

0 comments on commit 2cf053c

Please sign in to comment.