Skip to content

Commit 3f6e605

Browse files
committed
oiiotool: new --layersplit action for splitting channel-name-based layers on the stack
Signed-off-by: Loïc Vital <[email protected]>
1 parent 85ece69 commit 3f6e605

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/oiiotool/oiiotool.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,50 @@ action_subimage_split(Oiiotool& ot, cspan<const char*> argv)
28802880

28812881

28822882

2883+
// --layersplit
2884+
static void
2885+
action_layer_split(Oiiotool& ot, cspan<const char*> argv)
2886+
{
2887+
if (ot.postpone_callback(1, action_layer_split, argv))
2888+
return;
2889+
string_view command = ot.express(argv[0]);
2890+
OTScopedTimer timer(ot, command);
2891+
2892+
ImageRecRef A = ot.pop();
2893+
ot.read(A);
2894+
2895+
// Push the individual channel-name-based layers onto the stack
2896+
ImageSpec* spec = A->spec();
2897+
int chbegin = 0;
2898+
for (size_t i = 0; i < spec->channelnames.size(); ++i) {
2899+
const std::string & chname = spec->channelnames[i];
2900+
const std::string::size_type dotpos = chname.find(".");
2901+
const std::string layername = (dotpos == std::string::npos) ? "" : chname.substr(0, dotpos);
2902+
2903+
bool pushlayer = false;
2904+
if (i < spec->channelnames.size() - 1) {
2905+
const std::string & nextchname = spec->channelnames[i + 1];
2906+
const std::string::size_type nextdotpos = nextchname.find(".");
2907+
const std::string nextlayername = (nextdotpos == std::string::npos) ? "" : nextchname.substr(0, nextdotpos);
2908+
pushlayer = (nextlayername != layername);
2909+
} else {
2910+
pushlayer = true;
2911+
}
2912+
2913+
if (pushlayer) {
2914+
const int chend = i + 1;
2915+
ImageBufRef img = std::make_shared<ImageBuf>();
2916+
std::vector<int> channelorder(chend - chbegin);
2917+
std::iota(channelorder.begin(), channelorder.end(), chbegin);
2918+
ImageBufAlgo::channels(*img, (*A)(), chend - chbegin, cspan<int>(channelorder));
2919+
ot.push(new ImageRec(img, true));
2920+
chbegin = chend;
2921+
}
2922+
}
2923+
}
2924+
2925+
2926+
28832927
static void
28842928
action_subimage_append_n(Oiiotool& ot, int n, string_view command)
28852929
{
@@ -6898,6 +6942,9 @@ Oiiotool::getargs(int argc, char* argv[])
68986942
ap.arg("--siappendall")
68996943
.help("Append all images on the stack into a single multi-subimage image")
69006944
.OTACTION(action_subimage_append_all);
6945+
ap.arg("--layersplit")
6946+
.help("Split the top image's channel-name-based layers into separate images on the stack")
6947+
.OTACTION(action_layer_split);
69016948
ap.arg("--deepen")
69026949
.help("Deepen normal 2D image to deep")
69036950
.OTACTION(action_deepen);

0 commit comments

Comments
 (0)