Skip to content

Commit 9dfd3fd

Browse files
committed
Fix handling of bad transfer functions for --normalize.
1 parent f3f6b3b commit 9dfd3fd

File tree

2 files changed

+30
-16
lines changed

2 files changed

+30
-16
lines changed

tools/ktx/command_create.cpp

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ struct OptionsCreate {
778778
}
779779
}
780780

781+
if (isFormatSRGB(vkFormat) && normalize)
782+
report.fatal_usage("Option --{} cannot be used with sRGB formats.", kNormalize);
783+
781784
if (isFormatNotSRGBButHasSRGBVariant(vkFormat)) {
782785
const auto error_message = "Invalid value \"{}\" to --{} for format \"{}\". Transfer function must not be sRGB for a non-sRGB VkFormat with sRGB variant.";
783786
if (!convertTF.has_value() && assignTF.has_value() && assignTF == KHR_DF_TRANSFER_SRGB) {
@@ -1766,22 +1769,33 @@ void CommandCreate::executeCreate() {
17661769

17671770
if (options.normalize) {
17681771
if (target.format().transfer() != KHR_DF_TRANSFER_UNSPECIFIED && target.format().transfer() != KHR_DF_TRANSFER_LINEAR) {
1769-
const auto input_error_message = "Input file \"{}\" The transfer function to be applied to the created texture is neither linear nor none. Normalize is only available for these transfer functions.";
1770-
const auto assign_error_message = "Input file \"{}\" Use \"{}\" to assign the linear transfer function to the input image, if required.";
1771-
const auto convert_error_message = "Input file \"{}\" Modify \"{}\" settings to convert the input image to linear transfer function, if required.";
1772-
const auto inputTransfer = inputImageFile->spec().format().transfer();
1773-
bool is_file_error = (inputTransfer != KHR_DF_TRANSFER_UNSPECIFIED && inputTransfer != KHR_DF_TRANSFER_LINEAR);
1774-
bool is_assign_error = !options.assignTF.has_value();
1775-
bool is_convert_error = !options.convertTF.has_value();
1776-
if (is_assign_error)
1777-
fatal(rc::INVALID_FILE, assign_error_message, fmtInFile(inputFilepath), OptionsCreate::kAssignOetf);
1778-
else if (is_convert_error)
1779-
fatal(rc::INVALID_FILE, convert_error_message, fmtInFile(inputFilepath), OptionsCreate::kConvertOetf);
1780-
else {
1781-
assert(is_file_error && "In this branch it must be the input file that has the transfer function issue"); (void)is_file_error;
1782-
fatal(rc::INVALID_FILE, input_error_message, fmtInFile(inputFilepath));
1783-
}
1772+
// Report source of problematic TF.
1773+
//
1774+
// If --format is an SRGB format
1775+
// - a fatal usage error will already have been thrown so nothing to do.
1776+
// If --format is non-SRGB format
1777+
// - absent TF options, an implicit conversion to LINEAR takes place if the file
1778+
// TF is not LINEAR or UNSPECIFIED. If it can't be converted a fatal
1779+
// unsupported conversion error will already have been thrown. Therefore
1780+
// nothing to do. But if `create` is changed to set the TF for non-SRGB
1781+
// formats from the file's TF then this error handling will need updating.
1782+
// - --assign-tf has many other possible values so that is a possible source.
1783+
// - --convert-tf can only be linear or srgb. If it's srgb and the format does
1784+
// not have an equivalent SRGB format, that is another possible source.
1785+
1786+
//const auto input_error_message = "Input file \"{}\" The transfer function to be applied to the created texture is neither linear nor none. Normalize is only available for these transfer functions.";
1787+
//const auto inputTransfer = inputImageFile->spec().format().transfer();
1788+
//bool is_file_error = (inputTransfer != KHR_DF_TRANSFER_UNSPECIFIED && inputTransfer != KHR_DF_TRANSFER_LINEAR);
1789+
const auto option_error_message = "--{} value is {}. Normalize can only be used if the transfer function is linear or none.";
1790+
if (options.convertTF.has_value()) {
1791+
fatal_usage(option_error_message, OptionsCreate::kConvertTf,
1792+
toString(options.convertTF.value()));
1793+
} else if (options.assignTF.has_value()) {
1794+
fatal_usage(option_error_message, OptionsCreate::kAssignTf,
1795+
toString(options.assignTF.value()));
17841796
}
1797+
assert(false && "target.format().transfer() is not suitable for --normalize though --assign-tf and --conver-tf were not used.");
1798+
}
17851799
image->normalize();
17861800
}
17871801

0 commit comments

Comments
 (0)