Skip to content

Commit 36f7c15

Browse files
authored
oiiotool: -i:native=1, fix --native behavior, fix convert datatype (#4708)
A constellation of closely related fixes and additions related to oiiotool `-i` behavior: * When using `-i:now=1` to force for immediate read rather than using an ImageCache, it would "forget" any prior setting of `--native`. This is fixed now to correctly combine desire for bypassing the cache with hinting to maintain native data type. * When restricting channel subset with `-i:ch=...`, use of the `--native` flag was incorrect in some cases -- it would use the overall best single data format of all channels, rather than restricting that to the selected channel range. So, for example, if you said `-i:ch=R,G,B,A` and those channels were all `half`, but channel 17 (not selected) was `float`, you would get a conversion to float rather than keeping the selected channels at their native half. The solution here is that the type chosen for `--native` behavior should only consider the selected channel subset, not all channels. * Add a new `-i` modifier: `:native=1`, allowing you to select the "native" behavior on a per-input-file basis (the existing `--native` command sets the default). Signed-off-by: Larry Gritz <[email protected]>
1 parent 0e71adb commit 36f7c15

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

src/doc/oiiotool.rst

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1180,8 +1180,13 @@ Reading images
11801180

11811181
Optional appended modifiers include:
11821182

1183+
`:native=` *int*
1184+
If nonzero, read the image in as close as possible to its "native"
1185+
format, versus oiiotool's default of converting all images to float
1186+
internally. This also turns on "now". (Added in release 3.0.6.0.)
11831187
`:now=` *int*
1184-
If 1, read the image now, before proceeding to the next command.
1188+
If nonzero, read the image now, before proceeding to the next command
1189+
(bypassing the ImageCache, even for big images).
11851190
`:autocc=` *int*
11861191
Enable or disable `--autocc` for this input image (the default is to use
11871192
the global setting).

src/oiiotool/imagerec.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,16 @@ ImageRec::read(ReadPolicy readpolicy, string_view channel_set)
335335
if (m_input_dataformat != ib->nativespec().format)
336336
m_subimages[s].m_was_direct_read = false;
337337
forceread = true;
338-
} else if (readpolicy & ReadNative)
339-
convert = ib->nativespec().format;
338+
} else if (readpolicy & ReadNative) {
339+
if (channel_set.size()) {
340+
convert = TypeUnknown;
341+
for (int c = chbegin; c < chend; ++c)
342+
convert = TypeDesc::basetype_merge(
343+
convert, ib->nativespec().channelformat(c));
344+
} else {
345+
convert = ib->nativespec().format;
346+
}
347+
}
340348
if (!forceread && convert != TypeDesc::UINT8
341349
&& convert != TypeDesc::UINT16 && convert != TypeDesc::HALF
342350
&& convert != TypeDesc::FLOAT) {

src/oiiotool/oiiotool.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5176,6 +5176,7 @@ input_file(Oiiotool& ot, cspan<const char*> argv)
51765176
auto fileoptions = ot.extract_options(command);
51775177
int printinfo = fileoptions.get_int("info", ot.printinfo);
51785178
bool readnow = fileoptions.get_int("now", 0);
5179+
bool native = fileoptions.get_int("native", int(ot.nativeread));
51795180
bool autocc = fileoptions.get_int("autocc", ot.autocc);
51805181
bool autoccunpremult = fileoptions.get_int("unpremult", ot.autoccunpremult);
51815182
std::string infoformat = fileoptions.get_string("infoformat",
@@ -5261,6 +5262,7 @@ input_file(Oiiotool& ot, cspan<const char*> argv)
52615262
ot.input_channel_set = channel_set;
52625263
readnow = true;
52635264
}
5265+
readnow |= native;
52645266

52655267
if (substitute) {
52665268
ot.push(ImageRecRef(new ImageRec(substitute)));
@@ -5273,9 +5275,10 @@ input_file(Oiiotool& ot, cspan<const char*> argv)
52735275
if (ot.input_config_set)
52745276
ot.curimg->configspec(ot.input_config);
52755277
ot.curimg->input_dataformat(input_dataformat);
5276-
if (readnow)
5277-
ot.read(ReadNoCache, channel_set);
5278-
else
5278+
if (readnow) {
5279+
ReadPolicy policy = native ? ReadNativeNoCache : ReadNoCache;
5280+
ot.read(policy, channel_set);
5281+
} else
52795282
ot.read_nativespec();
52805283
if (ot.first_input_dimensions.format == TypeUnknown) {
52815284
ot.first_input_dimensions.copy_dimensions(
@@ -6561,7 +6564,7 @@ Oiiotool::getargs(int argc, char* argv[])
65616564

65626565
ap.separator("Commands that read images:");
65636566
ap.arg("-i %s:FILENAME")
6564-
.help("Input file (options: autocc=, ch=, info=, infoformat=, now=, type=, unpremult=)")
6567+
.help("Input file (options: autocc=, ch=, info=, infoformat=, native=, now=, type=, unpremult=)")
65656568
.OTACTION(input_file);
65666569
ap.arg("--iconfig %s:NAME %s:VALUE")
65676570
.help("Sets input config attribute (options: type=...)")

0 commit comments

Comments
 (0)