Mercurial > hg > orthanc
comparison OrthancFramework/UnitTestsSources/ImageProcessingTests.cpp @ 4875:5dae41084ab7
fix filling polygons
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 18 Jan 2022 15:04:04 +0100 |
parents | b1556cefa5c6 |
children | 664e383b68ad |
comparison
equal
deleted
inserted
replaced
4874:bee6da5155dc | 4875:5dae41084ab7 |
---|---|
194 } | 194 } |
195 } | 195 } |
196 } | 196 } |
197 | 197 |
198 | 198 |
199 #include "../Sources/Images/PngWriter.h" | |
200 | |
201 TYPED_TEST(TestIntegerImageTraits, FillPolygon) | 199 TYPED_TEST(TestIntegerImageTraits, FillPolygon) |
202 { | 200 { |
203 ImageAccessor& image = this->GetImage(); | 201 ImageAccessor& image = this->GetImage(); |
204 | 202 |
205 ImageProcessing::Set(image, 128); | 203 ImageProcessing::Set(image, 128); |
210 points.push_back(ImageProcessing::ImagePoint(1,5)); | 208 points.push_back(ImageProcessing::ImagePoint(1,5)); |
211 points.push_back(ImageProcessing::ImagePoint(5,5)); | 209 points.push_back(ImageProcessing::ImagePoint(5,5)); |
212 | 210 |
213 ImageProcessing::FillPolygon(image, points, 255); | 211 ImageProcessing::FillPolygon(image, points, 255); |
214 | 212 |
215 Orthanc::PngWriter writer; | |
216 Orthanc::IImageWriter::WriteToFile(writer, "tutu.png", image); | |
217 | |
218 // outside polygon | 213 // outside polygon |
219 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 0, 0)); | 214 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 0, 0)); |
220 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 0, 6)); | 215 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 0, 6)); |
221 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 6, 6)); | 216 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 6, 6)); |
222 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 6, 0)); | 217 ASSERT_FLOAT_EQ(128, TestFixture::ImageTraits::GetFloatPixel(image, 6, 0)); |
309 if (p != value) printf("%d %d\n", p, value); | 304 if (p != value) printf("%d %d\n", p, value); |
310 return p == value; | 305 return p == value; |
311 } | 306 } |
312 | 307 |
313 static void SetSignedGrayscale16Pixel(ImageAccessor& image, | 308 static void SetSignedGrayscale16Pixel(ImageAccessor& image, |
314 unsigned int x, | 309 unsigned int x, |
315 unsigned int y, | 310 unsigned int y, |
316 int16_t value) | 311 int16_t value) |
317 { | 312 { |
318 ImageTraits<PixelFormat_SignedGrayscale16>::SetPixel(image, value, x, y); | 313 ImageTraits<PixelFormat_SignedGrayscale16>::SetPixel(image, value, x, y); |
319 } | 314 } |
320 | 315 |
321 static bool TestSignedGrayscale16Pixel(const ImageAccessor& image, | 316 static bool TestSignedGrayscale16Pixel(const ImageAccessor& image, |
1094 ASSERT_FLOAT_EQ((*a) * (10.0f + (*b)), | 1089 ASSERT_FLOAT_EQ((*a) * (10.0f + (*b)), |
1095 ImageTraits<PixelFormat_Float32>::GetFloatPixel(target, 0, 0)); | 1090 ImageTraits<PixelFormat_Float32>::GetFloatPixel(target, 0, 0)); |
1096 } | 1091 } |
1097 } | 1092 } |
1098 } | 1093 } |
1094 | |
1095 | |
1096 namespace | |
1097 { | |
1098 class PolygonSegments : public ImageProcessing::IPolygonFiller | |
1099 { | |
1100 private: | |
1101 std::vector<int> y_, x1_, x2_; | |
1102 | |
1103 public: | |
1104 virtual void Fill(int y, | |
1105 int x1, | |
1106 int x2) ORTHANC_OVERRIDE | |
1107 { | |
1108 assert(x1 <= x2); | |
1109 y_.push_back(y); | |
1110 x1_.push_back(x1); | |
1111 x2_.push_back(x2); | |
1112 } | |
1113 | |
1114 size_t GetSize() const | |
1115 { | |
1116 return y_.size(); | |
1117 } | |
1118 | |
1119 int GetY(size_t i) const | |
1120 { | |
1121 return y_[i]; | |
1122 } | |
1123 | |
1124 int GetX1(size_t i) const | |
1125 { | |
1126 return x1_[i]; | |
1127 } | |
1128 | |
1129 int GetX2(size_t i) const | |
1130 { | |
1131 return x2_[i]; | |
1132 } | |
1133 }; | |
1134 } | |
1135 | |
1136 | |
1137 TEST(ImageProcessing, FillPolygon) | |
1138 { | |
1139 { | |
1140 std::vector<Orthanc::ImageProcessing::ImagePoint> polygon; | |
1141 | |
1142 PolygonSegments segments; | |
1143 Orthanc::ImageProcessing::FillPolygon(segments, polygon); | |
1144 ASSERT_EQ(0u, segments.GetSize()); | |
1145 } | |
1146 | |
1147 { | |
1148 std::vector<Orthanc::ImageProcessing::ImagePoint> polygon; | |
1149 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(288, 208)); | |
1150 | |
1151 PolygonSegments segments; | |
1152 Orthanc::ImageProcessing::FillPolygon(segments, polygon); | |
1153 ASSERT_EQ(0u, segments.GetSize()); | |
1154 } | |
1155 | |
1156 { | |
1157 std::vector<Orthanc::ImageProcessing::ImagePoint> polygon; | |
1158 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(10, 100)); | |
1159 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(50, 100)); | |
1160 | |
1161 PolygonSegments segments; | |
1162 Orthanc::ImageProcessing::FillPolygon(segments, polygon); | |
1163 ASSERT_EQ(1u, segments.GetSize()); | |
1164 ASSERT_EQ(100, segments.GetY(0)); | |
1165 ASSERT_EQ(10, segments.GetX1(0)); | |
1166 ASSERT_EQ(50, segments.GetX2(0)); | |
1167 } | |
1168 | |
1169 { | |
1170 std::vector<Orthanc::ImageProcessing::ImagePoint> polygon; | |
1171 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(10, 100)); | |
1172 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(10, 101)); | |
1173 | |
1174 PolygonSegments segments; | |
1175 Orthanc::ImageProcessing::FillPolygon(segments, polygon); | |
1176 ASSERT_EQ(2u, segments.GetSize()); | |
1177 ASSERT_EQ(100, segments.GetY(0)); | |
1178 ASSERT_EQ(10, segments.GetX1(0)); | |
1179 ASSERT_EQ(10, segments.GetX2(0)); | |
1180 ASSERT_EQ(101, segments.GetY(1)); | |
1181 ASSERT_EQ(10, segments.GetX1(1)); | |
1182 ASSERT_EQ(10, segments.GetX2(1)); | |
1183 } | |
1184 | |
1185 { | |
1186 std::vector<Orthanc::ImageProcessing::ImagePoint> polygon; | |
1187 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(10, 100)); | |
1188 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(11, 101)); | |
1189 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(13, 103)); | |
1190 | |
1191 PolygonSegments segments; | |
1192 Orthanc::ImageProcessing::FillPolygon(segments, polygon); | |
1193 ASSERT_EQ(4u, segments.GetSize()); | |
1194 ASSERT_EQ(100, segments.GetY(0)); | |
1195 ASSERT_EQ(10, segments.GetX1(0)); | |
1196 ASSERT_EQ(10, segments.GetX2(0)); | |
1197 ASSERT_EQ(101, segments.GetY(1)); | |
1198 ASSERT_EQ(11, segments.GetX1(1)); | |
1199 ASSERT_EQ(11, segments.GetX2(1)); | |
1200 ASSERT_EQ(102, segments.GetY(2)); | |
1201 ASSERT_EQ(12, segments.GetX1(2)); | |
1202 ASSERT_EQ(12, segments.GetX2(2)); | |
1203 ASSERT_EQ(103, segments.GetY(3)); | |
1204 ASSERT_EQ(13, segments.GetX1(3)); | |
1205 ASSERT_EQ(13, segments.GetX2(3)); | |
1206 } | |
1207 | |
1208 { | |
1209 std::vector<Orthanc::ImageProcessing::ImagePoint> polygon; | |
1210 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(5, 5)); | |
1211 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(7, 7)); | |
1212 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(9, 5)); | |
1213 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(9, 8)); | |
1214 polygon.push_back(Orthanc::ImageProcessing::ImagePoint(5, 8)); | |
1215 | |
1216 PolygonSegments segments; | |
1217 Orthanc::ImageProcessing::FillPolygon(segments, polygon); | |
1218 ASSERT_EQ(6u, segments.GetSize()); | |
1219 ASSERT_EQ(5, segments.GetY(0)); ASSERT_EQ(5, segments.GetX1(0)); ASSERT_EQ(5, segments.GetX2(0)); | |
1220 ASSERT_EQ(5, segments.GetY(1)); ASSERT_EQ(9, segments.GetX1(1)); ASSERT_EQ(9, segments.GetX2(1)); | |
1221 ASSERT_EQ(6, segments.GetY(2)); ASSERT_EQ(5, segments.GetX1(2)); ASSERT_EQ(6, segments.GetX2(2)); | |
1222 ASSERT_EQ(6, segments.GetY(3)); ASSERT_EQ(8, segments.GetX1(3)); ASSERT_EQ(9, segments.GetX2(3)); | |
1223 ASSERT_EQ(7, segments.GetY(4)); ASSERT_EQ(5, segments.GetX1(4)); ASSERT_EQ(9, segments.GetX2(4)); | |
1224 ASSERT_EQ(8, segments.GetY(5)); ASSERT_EQ(5, segments.GetX1(5)); ASSERT_EQ(9, segments.GetX2(5)); | |
1225 } | |
1226 } |