changeset 3365:9345710bbf12

check limits in fillpolygon
author Alain Mazy <alain@mazy.be>
date Mon, 13 May 2019 14:50:24 +0200
parents ea299aca479b
children 7a4d586caf2d
files Core/Images/ImageProcessing.cpp UnitTestsSources/ImageProcessingTests.cpp
diffstat 2 files changed, 43 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Core/Images/ImageProcessing.cpp	Tue May 07 13:21:41 2019 +0200
+++ b/Core/Images/ImageProcessing.cpp	Mon May 13 14:50:24 2019 +0200
@@ -1433,6 +1433,11 @@
     size_t cpSize = points.size();
     for (size_t i = 0; i < points.size(); i++)
     {
+      if (points[i].GetX() < 0 || points[i].GetX() >= imageWidth
+          || points[i].GetY() < 0 || points[i].GetY() >= imageHeight)
+      {
+        throw Orthanc::OrthancException(ErrorCode_ParameterOutOfRange);
+      }
       cpx.push_back((double)points[i].GetX());
       cpy.push_back((double)points[i].GetY());
     }
--- a/UnitTestsSources/ImageProcessingTests.cpp	Tue May 07 13:21:41 2019 +0200
+++ b/UnitTestsSources/ImageProcessingTests.cpp	Mon May 13 14:50:24 2019 +0200
@@ -223,3 +223,41 @@
   ASSERT_FLOAT_EQ(255, TestFixture::ImageTraits::GetFloatPixel(image, 1, 2));
   ASSERT_FLOAT_EQ(255, TestFixture::ImageTraits::GetFloatPixel(image, 2, 4));
 }
+
+TYPED_TEST(TestIntegerImageTraits, FillPolygonLargerThandImage)
+{
+  ImageAccessor& image = this->GetImage();
+
+  ImageProcessing::Set(image, 0);
+
+  std::vector<ImageProcessing::ImagePoint> points;
+  points.push_back(ImageProcessing::ImagePoint(0, 0));
+  points.push_back(ImageProcessing::ImagePoint(image.GetWidth(),0));
+  points.push_back(ImageProcessing::ImagePoint(image.GetWidth(),image.GetHeight()));
+  points.push_back(ImageProcessing::ImagePoint(0,image.GetHeight()));
+
+  ASSERT_THROW(Orthanc::ImageProcessing::FillPolygon(image, points, 255), Orthanc::OrthancException);
+
+  // inside polygon (note: we don't test too close from the edges since the current algo is taking some margin from the edges and might be improved in that sense)
+  ASSERT_FLOAT_EQ(255, TestFixture::ImageTraits::GetFloatPixel(image, 0, 0));
+  ASSERT_FLOAT_EQ(255, TestFixture::ImageTraits::GetFloatPixel(image, image.GetWidth()-1, 0));
+}
+
+TYPED_TEST(TestIntegerImageTraits, FillPolygonFullImage)
+{
+  ImageAccessor& image = this->GetImage();
+
+  ImageProcessing::Set(image, 0);
+
+  std::vector<ImageProcessing::ImagePoint> points;
+  points.push_back(ImageProcessing::ImagePoint(0, 0));
+  points.push_back(ImageProcessing::ImagePoint(image.GetWidth() - 1,0));
+  points.push_back(ImageProcessing::ImagePoint(image.GetWidth() - 1,image.GetHeight() - 1));
+  points.push_back(ImageProcessing::ImagePoint(0,image.GetHeight() - 1));
+
+  Orthanc::ImageProcessing::FillPolygon(image, points, 255);
+
+  // inside polygon (note: we don't test too close from the edges since the current algo is taking some margin from the edges and might be improved in that sense)
+  ASSERT_FLOAT_EQ(255, TestFixture::ImageTraits::GetFloatPixel(image, 1, 1));
+  ASSERT_FLOAT_EQ(255, TestFixture::ImageTraits::GetFloatPixel(image, image.GetWidth() - 2, image.GetHeight() - 2));
+}