changeset 3549:fab5777f4dd4

ImageProcessing::FitSize()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 25 Oct 2019 18:09:27 +0200
parents dabe17e23e23
children 0f5f9a5eed25
files Core/Images/ImageProcessing.cpp Core/Images/ImageProcessing.h
diffstat 2 files changed, 60 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Images/ImageProcessing.cpp	Tue Oct 22 17:30:43 2019 +0200
+++ b/Core/Images/ImageProcessing.cpp	Fri Oct 25 18:09:27 2019 +0200
@@ -2221,4 +2221,57 @@
 
     SeparableConvolution(image, kernel, 2, kernel, 2);
   }
+
+
+  void ImageProcessing::FitSize(ImageAccessor& target,
+                                const ImageAccessor& source)
+  {
+    if (target.GetWidth() == 0 ||
+        target.GetHeight() == 0)
+    {
+      return;
+    }
+
+    if (source.GetWidth() == target.GetWidth() &&
+        source.GetHeight() == target.GetHeight())
+    {
+      Copy(target, source);
+      return;
+    }
+
+    Set(target, 0);
+
+    // Preserve the aspect ratio
+    float cw = static_cast<float>(source.GetWidth());
+    float ch = static_cast<float>(source.GetHeight());
+    float r = std::min(
+      static_cast<float>(target.GetWidth()) / cw,
+      static_cast<float>(target.GetHeight()) / ch);
+
+    unsigned int sw = std::min(static_cast<unsigned int>(boost::math::iround(cw * r)), target.GetWidth());  
+    unsigned int sh = std::min(static_cast<unsigned int>(boost::math::iround(ch * r)), target.GetHeight());
+    Image resized(target.GetFormat(), sw, sh, false);
+  
+    //ImageProcessing::SmoothGaussian5x5(source);
+    ImageProcessing::Resize(resized, source);
+
+    assert(target.GetWidth() >= resized.GetWidth() &&
+           target.GetHeight() >= resized.GetHeight());
+    unsigned int offsetX = (target.GetWidth() - resized.GetWidth()) / 2;
+    unsigned int offsetY = (target.GetHeight() - resized.GetHeight()) / 2;
+
+    ImageAccessor region;
+    target.GetRegion(region, offsetX, offsetY, resized.GetWidth(), resized.GetHeight());
+    ImageProcessing::Copy(region, resized);
+  }
+
+
+  ImageAccessor* ImageProcessing::FitSize(const ImageAccessor& source,
+                                          unsigned int width,
+                                          unsigned int height)
+  {
+    std::auto_ptr<ImageAccessor> target(new Image(source.GetFormat(), width, height, false));
+    FitSize(*target, source);
+    return target.release();
+  }
 }
--- a/Core/Images/ImageProcessing.h	Tue Oct 22 17:30:43 2019 +0200
+++ b/Core/Images/ImageProcessing.h	Fri Oct 25 18:09:27 2019 +0200
@@ -165,5 +165,12 @@
                               size_t verticalAnchor);
 
     void SmoothGaussian5x5(ImageAccessor& image);
+
+    void FitSize(ImageAccessor& target,
+                 const ImageAccessor& source);
+    
+    ImageAccessor* FitSize(const ImageAccessor& source,
+                           unsigned int width,
+                           unsigned int height);
   }
 }