From 6755ac801d4f7cc0000d3352331b5225a02bf4d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20De=20Lillo?= Date: Thu, 20 Feb 2020 17:05:01 +0100 Subject: [PATCH] [image] Fix bug with input RGBA Images Alpha channel was erased on image reading --- src/aliceVision/image/io.cpp | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/aliceVision/image/io.cpp b/src/aliceVision/image/io.cpp index dc30c448e0..87a9298dc4 100644 --- a/src/aliceVision/image/io.cpp +++ b/src/aliceVision/image/io.cpp @@ -278,23 +278,33 @@ void readImage(const std::string& path, oiio::ImageBuf grayscaleBuf; oiio::ImageBufAlgo::channel_sum(grayscaleBuf, inBuf, weights, convertionROI); inBuf.copy(grayscaleBuf); + + // TODO: if inSpec.nchannels == 4: premult? } - // add missing channels - if(nchannels > inSpec.nchannels) + // duplicate first channel for RGB + if (nchannels >= 3 && inSpec.nchannels == 1) { - oiio::ImageSpec requestedSpec(inSpec.width, inSpec.height, nchannels, format); + oiio::ImageSpec requestedSpec(inSpec.width, inSpec.height, 3, format); oiio::ImageBuf requestedBuf(requestedSpec); + int channelOrder[] = { 0, 0, 0 }; + float channelValues[] = { 0 /*ignore*/, 0 /*ignore*/, 0 /*ignore*/ }; + oiio::ImageBufAlgo::channels(requestedBuf, inBuf, 3, channelOrder, channelValues); + inBuf.swap(requestedBuf); + } - // duplicate first channel for RGB - if(requestedSpec.nchannels >= 3 && inSpec.nchannels < 3) - { - oiio::ImageBufAlgo::paste(requestedBuf, 0, 0, 0, 0, inBuf); - oiio::ImageBufAlgo::paste(requestedBuf, 0, 0, 0, 1, inBuf); - oiio::ImageBufAlgo::paste(requestedBuf, 0, 0, 0, 2, inBuf); - } - - inBuf.copy(requestedBuf); + // Add an alpha channel if needed + if (nchannels == 4 && inBuf.spec().nchannels == 3) + { + oiio::ImageSpec requestedSpec(inSpec.width, inSpec.height, 3, format); + oiio::ImageBuf requestedBuf(requestedSpec); + int channelOrder[] = { 0, 1, 2, -1 /*constant value*/ }; + float channelValues[] = { 0 /*ignore*/, 0 /*ignore*/, 0 /*ignore*/, 1.0 }; + oiio::ImageBufAlgo::channels(requestedBuf, inBuf, + 4, // create an image with 4 channels + channelOrder, + channelValues); // only the 4th value is used + inBuf.swap(requestedBuf); } // copy pixels from oiio to eigen