Skip to content

Commit

Permalink
oiiotool: Fix resize and resample to honor -a and handle subimages
Browse files Browse the repository at this point in the history
  • Loading branch information
lgritz committed Apr 28, 2019
1 parent a6a9d38 commit a35a767
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 38 deletions.
98 changes: 60 additions & 38 deletions src/oiiotool/oiiotool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3483,33 +3483,44 @@ class OpResample : public OiiotoolOp {
: OiiotoolOp(ot, opname, argc, argv, 1)
{
}
virtual int compute_subimages() { return 1; } // just the first one
virtual void option_defaults() { options["interp"] = "1"; }
virtual bool setup()
{
// The size argument will be the resulting display (full) window.
const ImageSpec& Aspec(*ir[1]->spec(0, 0));
ImageSpec newspec = Aspec;
ot.adjust_geometry(args[0], newspec.full_width, newspec.full_height,
newspec.full_x, newspec.full_y,
args[1].c_str() /*size*/, true);
if (newspec.full_width == Aspec.full_width
&& newspec.full_height == Aspec.full_height) {
int subimages = compute_subimages();
bool nochange = true;
std::vector<ImageSpec> newspecs(subimages);
for (int s = 0; s < subimages; ++s) {
// The size argument will be the resulting display (full) window.
const ImageSpec& Aspec(*ir[1]->spec(s));
ImageSpec& newspec(newspecs[s]);
newspec = Aspec;
ot.adjust_geometry(args[0], newspec.full_width, newspec.full_height,
newspec.full_x, newspec.full_y,
args[1].c_str() /*size*/, true);
if (newspec.full_width == Aspec.full_width
&& newspec.full_height == Aspec.full_height) {
continue;
}
nochange = false;
// Compute corresponding data window.
float wratio = float(newspec.full_width) / float(Aspec.full_width);
float hratio = float(newspec.full_height)
/ float(Aspec.full_height);
newspec.x = newspec.full_x
+ int(floorf((Aspec.x - Aspec.full_x) * wratio));
newspec.y = newspec.full_y
+ int(floorf((Aspec.y - Aspec.full_y) * hratio));
newspec.width = int(ceilf(Aspec.width * wratio));
newspec.height = int(ceilf(Aspec.height * hratio));
}
if (nochange) {
// No change -- pop the temp result and restore the original
ot.pop();
ot.push(ir[1]);
return false; // nothing more to do
}
// Compute corresponding data window.
float wratio = float(newspec.full_width) / float(Aspec.full_width);
float hratio = float(newspec.full_height) / float(Aspec.full_height);
newspec.x = newspec.full_x
+ int(floorf((Aspec.x - Aspec.full_x) * wratio));
newspec.y = newspec.full_y
+ int(floorf((Aspec.y - Aspec.full_y) * hratio));
newspec.width = int(ceilf(Aspec.width * wratio));
newspec.height = int(ceilf(Aspec.height * hratio));
(*ir[0])(0, 0).reset(newspec);
for (int s = 0; s < subimages; ++s)
(*ir[0])(s).reset(newspecs[s]);
return true;
}
virtual int impl(ImageBuf** img)
Expand All @@ -3529,32 +3540,43 @@ class OpResize : public OiiotoolOp {
: OiiotoolOp(ot, opname, argc, argv, 1)
{
}
virtual int compute_subimages() { return 1; } // just the first one
virtual bool setup()
{
// The size argument will be the resulting display (full) window.
const ImageSpec& Aspec(*ir[1]->spec(0, 0));
ImageSpec newspec = Aspec;
ot.adjust_geometry(args[0], newspec.full_width, newspec.full_height,
newspec.full_x, newspec.full_y,
args[1].c_str() /*size*/, true);
if (newspec.full_width == Aspec.full_width
&& newspec.full_height == Aspec.full_height) {
int subimages = compute_subimages();
bool nochange = true;
std::vector<ImageSpec> newspecs(subimages);
for (int s = 0; s < subimages; ++s) {
// The size argument will be the resulting display (full) window.
const ImageSpec& Aspec(*ir[1]->spec(s));
ImageSpec& newspec(newspecs[s]);
newspec = Aspec;
ot.adjust_geometry(args[0], newspec.full_width, newspec.full_height,
newspec.full_x, newspec.full_y,
args[1].c_str() /*size*/, true);
if (newspec.full_width == Aspec.full_width
&& newspec.full_height == Aspec.full_height) {
continue;
}
nochange = false;
// Compute corresponding data window.
float wratio = float(newspec.full_width) / float(Aspec.full_width);
float hratio = float(newspec.full_height)
/ float(Aspec.full_height);
newspec.x = newspec.full_x
+ int(floorf((Aspec.x - Aspec.full_x) * wratio));
newspec.y = newspec.full_y
+ int(floorf((Aspec.y - Aspec.full_y) * hratio));
newspec.width = int(ceilf(Aspec.width * wratio));
newspec.height = int(ceilf(Aspec.height * hratio));
}
if (nochange) {
// No change -- pop the temp result and restore the original
ot.pop();
ot.push(ir[1]);
return false; // nothing more to do
}
// Compute corresponding data window.
float wratio = float(newspec.full_width) / float(Aspec.full_width);
float hratio = float(newspec.full_height) / float(Aspec.full_height);
newspec.x = newspec.full_x
+ int(floorf((Aspec.x - Aspec.full_x) * wratio));
newspec.y = newspec.full_y
+ int(floorf((Aspec.y - Aspec.full_y) * hratio));
newspec.width = int(ceilf(Aspec.width * wratio));
newspec.height = int(ceilf(Aspec.height * hratio));
(*ir[0])(0, 0).reset(newspec);
for (int s = 0; s < subimages; ++s)
(*ir[0])(s).reset(newspecs[s]);
return true;
}
virtual int impl(ImageBuf** img)
Expand Down
1 change: 1 addition & 0 deletions src/oiiotool/oiiotool.h
Original file line number Diff line number Diff line change
Expand Up @@ -661,6 +661,7 @@ class OiiotoolOp {
// Read the inputs
for (int i = 1; i < nimages(); ++i)
ot.read(ir[i]);
subimages = compute_subimages();
// Initialize the output image
ir[0].reset(new ImageRec(opname(), subimages));
ot.push(ir[0]);
Expand Down

0 comments on commit a35a767

Please sign in to comment.