Skip to content

Commit

Permalink
[image] add dedicated functions for rescaling in-place
Browse files Browse the repository at this point in the history
  • Loading branch information
mugulmd authored and fabiencastan committed Feb 15, 2023
1 parent e8027db commit 384096e
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 23 deletions.
5 changes: 1 addition & 4 deletions src/aliceVision/image/caching.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -414,10 +414,7 @@ void ImageCache::load(const CacheKey& key)

// apply downscale
int downscale = 1 << key.halfSampleLevel;
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, *img, *img);
}
imageAlgo::resizeImage(downscale, *img);

_info.nbLoadFromDisk++;
}
Expand Down
114 changes: 114 additions & 0 deletions src/aliceVision/image/imageAlgo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@ void resizeImage(int downscale, const image::Image<unsigned char>& inImage,
inImage.data(), outImage.data(), filter, filterSize);
}

void resizeImage(int downscale, image::Image<unsigned char>& inoutImage,
const std::string& filter, float filterSize)
{
if (downscale <= 1)
{
return;
}

const int outWidth = inoutImage.Width() / downscale;
const int outHeight = inoutImage.Height() / downscale;

image::Image<unsigned char> rescaled(outWidth, outHeight);

resizeImage(oiio::TypeDesc::UINT8, inoutImage.Width(), inoutImage.Height(), outWidth, outHeight, 1,
inoutImage.data(), rescaled.data(), filter, filterSize);

inoutImage.swap(rescaled);
}

void resizeImage(int downscale, const image::Image<float>& inImage,
image::Image<float>& outImage, const std::string& filter, float filterSize)
{
Expand All @@ -265,6 +284,25 @@ void resizeImage(int downscale, const image::Image<float>& inImage,
inImage.data(), outImage.data(), filter, filterSize);
}

void resizeImage(int downscale, image::Image<float>& inoutImage,
const std::string& filter, float filterSize)
{
if (downscale <= 1)
{
return;
}

const int outWidth = inoutImage.Width() / downscale;
const int outHeight = inoutImage.Height() / downscale;

image::Image<float> rescaled(outWidth, outHeight);

resizeImage(oiio::TypeDesc::FLOAT, inoutImage.Width(), inoutImage.Height(), outWidth, outHeight, 1,
inoutImage.data(), rescaled.data(), filter, filterSize);

inoutImage.swap(rescaled);
}

void resizeImage(int downscale, const image::Image<image::RGBColor> &inImage,
image::Image<image::RGBColor> &outImage, const std::string &filter,
float filterSize)
Expand All @@ -276,6 +314,25 @@ void resizeImage(int downscale, const image::Image<image::RGBColor> &inImage,
inImage.data(), outImage.data(), filter, filterSize);
}

void resizeImage(int downscale, image::Image<image::RGBColor>& inoutImage,
const std::string& filter, float filterSize)
{
if (downscale <= 1)
{
return;
}

const int outWidth = inoutImage.Width() / downscale;
const int outHeight = inoutImage.Height() / downscale;

image::Image<image::RGBColor> rescaled(outWidth, outHeight);

resizeImage(oiio::TypeDesc::UINT8, inoutImage.Width(), inoutImage.Height(), outWidth, outHeight, 3,
inoutImage.data(), rescaled.data(), filter, filterSize);

inoutImage.swap(rescaled);
}

void resizeImage(int downscale, const image::Image<image::RGBfColor> &inImage,
image::Image<image::RGBfColor> &outImage, const std::string &filter,
float filterSize)
Expand All @@ -287,6 +344,25 @@ void resizeImage(int downscale, const image::Image<image::RGBfColor> &inImage,
inImage.data(), outImage.data(), filter, filterSize);
}

void resizeImage(int downscale, image::Image<image::RGBfColor>& inoutImage,
const std::string& filter, float filterSize)
{
if (downscale <= 1)
{
return;
}

const int outWidth = inoutImage.Width() / downscale;
const int outHeight = inoutImage.Height() / downscale;

image::Image<image::RGBfColor> rescaled(outWidth, outHeight);

resizeImage(oiio::TypeDesc::FLOAT, inoutImage.Width(), inoutImage.Height(), outWidth, outHeight, 3,
inoutImage.data(), rescaled.data(), filter, filterSize);

inoutImage.swap(rescaled);
}

void resizeImage(int downscale, const image::Image<image::RGBAColor> &inImage,
image::Image<image::RGBAColor> &outImage, const std::string &filter,
float filterSize)
Expand All @@ -298,6 +374,25 @@ void resizeImage(int downscale, const image::Image<image::RGBAColor> &inImage,
inImage.data(), outImage.data(), filter, filterSize);
}

void resizeImage(int downscale, image::Image<image::RGBAColor>& inoutImage,
const std::string& filter, float filterSize)
{
if (downscale <= 1)
{
return;
}

const int outWidth = inoutImage.Width() / downscale;
const int outHeight = inoutImage.Height() / downscale;

image::Image<image::RGBAColor> rescaled(outWidth, outHeight);

resizeImage(oiio::TypeDesc::UINT8, inoutImage.Width(), inoutImage.Height(), outWidth, outHeight, 4,
inoutImage.data(), rescaled.data(), filter, filterSize);

inoutImage.swap(rescaled);
}

void resizeImage(int downscale, const image::Image<image::RGBAfColor> &inImage,
image::Image<image::RGBAfColor> &outImage, const std::string &filter,
float filterSize)
Expand All @@ -309,6 +404,25 @@ void resizeImage(int downscale, const image::Image<image::RGBAfColor> &inImage,
inImage.data(), outImage.data(), filter, filterSize);
}

void resizeImage(int downscale, image::Image<image::RGBAfColor>& inoutImage,
const std::string& filter, float filterSize)
{
if (downscale <= 1)
{
return;
}

const int outWidth = inoutImage.Width() / downscale;
const int outHeight = inoutImage.Height() / downscale;

image::Image<image::RGBAfColor> rescaled(outWidth, outHeight);

resizeImage(oiio::TypeDesc::FLOAT, inoutImage.Width(), inoutImage.Height(), outWidth, outHeight, 4,
inoutImage.data(), rescaled.data(), filter, filterSize);

inoutImage.swap(rescaled);
}

template<typename T>
void convolveImage(oiio::TypeDesc typeDesc,
int inWidth,
Expand Down
28 changes: 25 additions & 3 deletions src/aliceVision/image/imageAlgo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ void colorconvert(oiio::ImageBuf& dst, const oiio::ImageBuf& src,
image::EImageColorSpace fromColorSpace, image::EImageColorSpace toColorSpace);

/**
* @brief resize a given image buffer
* @brief Resize a given image buffer.
* @param[in] downscale The resize downscale
* @param[in] inBuffer The input image buffer
* @param[out] outBuffer The output image buffer
* @param[in] inImage The input image buffer
* @param[out] outImage The output image buffer
* @param[in] filter The name of a high-quality filter to use when resampling
* Default is bilinear resampling
* See openImageIO documentation "ImageBufAlgo filtername"
Expand All @@ -75,6 +75,28 @@ void resizeImage(int downscale, const image::Image<image::RGBAfColor>& inImage,
image::Image<image::RGBAfColor>& outImage,
const std::string& filter = "", float filterSize = 0);

/**
* @brief Resize a given image buffer in place.
* @param[in] downscale The resize downscale
* @param[in,out] inoutImage The input/output image buffer
* @param[in] filter The name of a high-quality filter to use when resampling
* Default is bilinear resampling
* See openImageIO documentation "ImageBufAlgo filtername"
* @param[in] filterSize The resize filter size
*/
void resizeImage(int downscale, image::Image<unsigned char>& inoutImage,
const std::string& filter = "", float filterSize = 0);
void resizeImage(int downscale, image::Image<float>& inoutImage,
const std::string& filter = "", float filterSize = 0);
void resizeImage(int downscale, image::Image<image::RGBColor>& inoutImage,
const std::string& filter = "", float filterSize = 0);
void resizeImage(int downscale, image::Image<image::RGBfColor>& inoutImage,
const std::string& filter = "", float filterSize = 0);
void resizeImage(int downscale, image::Image<image::RGBAColor>& inoutImage,
const std::string& filter = "", float filterSize = 0);
void resizeImage(int downscale, image::Image<image::RGBAfColor>& inoutImage,
const std::string& filter = "", float filterSize = 0);

/**
* @brief convolve a given image buffer
* @param[in] inBuffer The input image buffer
Expand Down
20 changes: 4 additions & 16 deletions src/software/pipeline/main_panoramaSeams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,7 @@ bool computeWTALabels(image::Image<IndexT> & labels, const std::vector<std::shar
ALICEVISION_LOG_TRACE("Load mask with path " << maskPath);
image::Image<unsigned char> mask;
image::readImageDirect(maskPath, mask);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, mask, mask);
}
imageAlgo::resizeImage(downscale, mask);

// Get offset
oiio::ParamValueList metadata = image::readImageMetadata(maskPath);
Expand All @@ -72,10 +69,7 @@ bool computeWTALabels(image::Image<IndexT> & labels, const std::vector<std::shar
ALICEVISION_LOG_TRACE("Load weights with path " << weightsPath);
image::Image<float> weights;
image::readImage(weightsPath, weights, image::EImageColorSpace::NO_CONVERSION);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, weights, weights);
}
imageAlgo::resizeImage(downscale, weights);

if (!seams.appendWithLoop(mask, weights, viewId, offsetX, offsetY))
{
Expand Down Expand Up @@ -113,20 +107,14 @@ bool computeGCLabels(image::Image<IndexT>& labels, const std::vector<std::shared
ALICEVISION_LOG_TRACE("Load mask with path " << maskPath);
image::Image<unsigned char> mask;
image::readImageDirect(maskPath, mask);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, mask, mask);
}
imageAlgo::resizeImage(downscale, mask);

// Load Color
const std::string colorsPath = (fs::path(inputPath) / (std::to_string(viewId) + ".exr")).string();
ALICEVISION_LOG_TRACE("Load colors with path " << colorsPath);
image::Image<image::RGBfColor> colors;
image::readImage(colorsPath, colors, image::EImageColorSpace::NO_CONVERSION);
if (downscale > 1)
{
imageAlgo::resizeImage(downscale, colors, colors);
}
imageAlgo::resizeImage(downscale, colors);

// Get offset
oiio::ParamValueList metadata = image::readImageMetadata(maskPath);
Expand Down

0 comments on commit 384096e

Please sign in to comment.