comparison UnitTestsSources/ImageProcessingTests.cpp @ 3682:5f64c866108a

merging implementations of ImageProcessing::ShiftScale() and ApplyWindowing()
author Sebastien Jodogne <s.jodogne@gmail.com>
date Mon, 24 Feb 2020 16:26:59 +0100
parents 46cb00e4adbb
children 12253ddefe5a
comparison
equal deleted inserted replaced
3680:453c0ece560a 3682:5f64c866108a
283 } 283 }
284 284
285 static void SetGrayscale16Pixel(ImageAccessor& image, 285 static void SetGrayscale16Pixel(ImageAccessor& image,
286 unsigned int x, 286 unsigned int x,
287 unsigned int y, 287 unsigned int y,
288 uint8_t value) 288 uint16_t value)
289 { 289 {
290 ImageTraits<PixelFormat_Grayscale16>::SetPixel(image, value, x, y); 290 ImageTraits<PixelFormat_Grayscale16>::SetPixel(image, value, x, y);
291 } 291 }
292 292
293 static bool TestGrayscale16Pixel(const ImageAccessor& image, 293 static bool TestGrayscale16Pixel(const ImageAccessor& image,
295 unsigned int y, 295 unsigned int y,
296 uint16_t value) 296 uint16_t value)
297 { 297 {
298 PixelTraits<PixelFormat_Grayscale16>::PixelType p; 298 PixelTraits<PixelFormat_Grayscale16>::PixelType p;
299 ImageTraits<PixelFormat_Grayscale16>::GetPixel(p, image, x, y); 299 ImageTraits<PixelFormat_Grayscale16>::GetPixel(p, image, x, y);
300 if (p != value) printf("%d %d\n", p, value);
301 return p == value;
302 }
303
304 static void SetSignedGrayscale16Pixel(ImageAccessor& image,
305 unsigned int x,
306 unsigned int y,
307 int16_t value)
308 {
309 ImageTraits<PixelFormat_SignedGrayscale16>::SetPixel(image, value, x, y);
310 }
311
312 static bool TestSignedGrayscale16Pixel(const ImageAccessor& image,
313 unsigned int x,
314 unsigned int y,
315 int16_t value)
316 {
317 PixelTraits<PixelFormat_SignedGrayscale16>::PixelType p;
318 ImageTraits<PixelFormat_SignedGrayscale16>::GetPixel(p, image, x, y);
300 if (p != value) printf("%d %d\n", p, value); 319 if (p != value) printf("%d %d\n", p, value);
301 return p == value; 320 return p == value;
302 } 321 }
303 322
304 static void SetRGB24Pixel(ImageAccessor& image, 323 static void SetRGB24Pixel(ImageAccessor& image,
829 ASSERT_TRUE(TestGrayscale8Pixel(target, 5, 0, 255 - 255*2/10)); 848 ASSERT_TRUE(TestGrayscale8Pixel(target, 5, 0, 255 - 255*2/10));
830 } 849 }
831 850
832 { 851 {
833 Image target(PixelFormat_Grayscale8, 6, 1, false); 852 Image target(PixelFormat_Grayscale8, 6, 1, false);
834 ImageProcessing::ApplyWindowing(target, image, 5000.0f, 10000.0f, 1000.0f, 0.0f, false); 853 ImageProcessing::ApplyWindowing(target, image, 5000.0f, 10000.01f, 1000.0f, 0.0f, false);
835 854
836 ASSERT_TRUE(TestGrayscale8Pixel(target, 0, 0, 0)); 855 ASSERT_TRUE(TestGrayscale8Pixel(target, 0, 0, 0));
837 ASSERT_TRUE(TestGrayscale8Pixel(target, 1, 0, 0)); 856 ASSERT_TRUE(TestGrayscale8Pixel(target, 1, 0, 0));
838 ASSERT_TRUE(TestGrayscale8Pixel(target, 2, 0, 128)); 857 ASSERT_TRUE(TestGrayscale8Pixel(target, 2, 0, 128));
839 ASSERT_TRUE(TestGrayscale8Pixel(target, 3, 0, 255)); 858 ASSERT_TRUE(TestGrayscale8Pixel(target, 3, 0, 255));
841 ASSERT_TRUE(TestGrayscale8Pixel(target, 5, 0, 255*2/10)); 860 ASSERT_TRUE(TestGrayscale8Pixel(target, 5, 0, 255*2/10));
842 } 861 }
843 862
844 { 863 {
845 Image target(PixelFormat_Grayscale8, 6, 1, false); 864 Image target(PixelFormat_Grayscale8, 6, 1, false);
846 ImageProcessing::ApplyWindowing(target, image, 5000.0f, 10000.0f, 1000.0f, 0.0f, true); 865 ImageProcessing::ApplyWindowing(target, image, 5000.0f, 10000.01f, 1000.0f, 0.0f, true);
847 866
848 ASSERT_TRUE(TestGrayscale8Pixel(target, 0, 0, 255)); 867 ASSERT_TRUE(TestGrayscale8Pixel(target, 0, 0, 255));
849 ASSERT_TRUE(TestGrayscale8Pixel(target, 1, 0, 255)); 868 ASSERT_TRUE(TestGrayscale8Pixel(target, 1, 0, 255));
850 ASSERT_TRUE(TestGrayscale8Pixel(target, 2, 0, 127)); 869 ASSERT_TRUE(TestGrayscale8Pixel(target, 2, 0, 127));
851 ASSERT_TRUE(TestGrayscale8Pixel(target, 3, 0, 0)); 870 ASSERT_TRUE(TestGrayscale8Pixel(target, 3, 0, 0));
853 ASSERT_TRUE(TestGrayscale8Pixel(target, 5, 0, 255 - 256*2/10)); 872 ASSERT_TRUE(TestGrayscale8Pixel(target, 5, 0, 255 - 256*2/10));
854 } 873 }
855 874
856 { 875 {
857 Image target(PixelFormat_Grayscale8, 6, 1, false); 876 Image target(PixelFormat_Grayscale8, 6, 1, false);
858 ImageProcessing::ApplyWindowing(target, image, 50.0f, 100.0f, 10.0f, 30.0f, false); 877 ImageProcessing::ApplyWindowing(target, image, 50.0f, 100.1f, 10.0f, 30.0f, false);
859 878
860 ASSERT_TRUE(TestGrayscale8Pixel(target, 0, 0, 0)); // (-5 * 10) + 30 => pixel value = -20 => 0 879 ASSERT_TRUE(TestGrayscale8Pixel(target, 0, 0, 0)); // (-5 * 10) + 30 => pixel value = -20 => 0
861 ASSERT_TRUE(TestGrayscale8Pixel(target, 1, 0, 256*30/100)); // ((0 * 10) + 30 => pixel value = 30 => 30% 880 ASSERT_TRUE(TestGrayscale8Pixel(target, 1, 0, 256*30/100)); // ((0 * 10) + 30 => pixel value = 30 => 30%
862 ASSERT_TRUE(TestGrayscale8Pixel(target, 2, 0, 256*80/100)); // ((5 * 10) + 30 => pixel value = 80 => 80% 881 ASSERT_TRUE(TestGrayscale8Pixel(target, 2, 0, 256*80/100)); // ((5 * 10) + 30 => pixel value = 80 => 80%
863 ASSERT_TRUE(TestGrayscale8Pixel(target, 3, 0, 255)); // ((10 * 10) + 30 => pixel value = 130 => 100% 882 ASSERT_TRUE(TestGrayscale8Pixel(target, 3, 0, 255)); // ((10 * 10) + 30 => pixel value = 130 => 100%
936 ASSERT_TRUE(TestGrayscale16Pixel(target, 3, 0, 65535)); 955 ASSERT_TRUE(TestGrayscale16Pixel(target, 3, 0, 65535));
937 ASSERT_TRUE(TestGrayscale16Pixel(target, 4, 0, 65535)); 956 ASSERT_TRUE(TestGrayscale16Pixel(target, 4, 0, 65535));
938 } 957 }
939 } 958 }
940 } 959 }
960
961
962 TEST(ImageProcessing, ShiftScaleGrayscale8)
963 {
964 Image image(PixelFormat_Grayscale8, 5, 1, false);
965 SetGrayscale8Pixel(image, 0, 0, 0);
966 SetGrayscale8Pixel(image, 1, 0, 2);
967 SetGrayscale8Pixel(image, 2, 0, 5);
968 SetGrayscale8Pixel(image, 3, 0, 10);
969 SetGrayscale8Pixel(image, 4, 0, 255);
970
971 ImageProcessing::ShiftScale(image, -1.1, 1.5, true);
972 ASSERT_TRUE(TestGrayscale8Pixel(image, 0, 0, 0));
973 ASSERT_TRUE(TestGrayscale8Pixel(image, 1, 0, 1));
974 ASSERT_TRUE(TestGrayscale8Pixel(image, 2, 0, 6));
975 ASSERT_TRUE(TestGrayscale8Pixel(image, 3, 0, 13));
976 ASSERT_TRUE(TestGrayscale8Pixel(image, 4, 0, 255));
977 }
978
979
980 TEST(ImageProcessing, ShiftScaleGrayscale16)
981 {
982 Image image(PixelFormat_Grayscale16, 5, 1, false);
983 SetGrayscale16Pixel(image, 0, 0, 0);
984 SetGrayscale16Pixel(image, 1, 0, 2);
985 SetGrayscale16Pixel(image, 2, 0, 5);
986 SetGrayscale16Pixel(image, 3, 0, 10);
987 SetGrayscale16Pixel(image, 4, 0, 255);
988
989 ImageProcessing::ShiftScale(image, -1.1, 1.5, true);
990 ASSERT_TRUE(TestGrayscale16Pixel(image, 0, 0, 0));
991 ASSERT_TRUE(TestGrayscale16Pixel(image, 1, 0, 1));
992 ASSERT_TRUE(TestGrayscale16Pixel(image, 2, 0, 6));
993 ASSERT_TRUE(TestGrayscale16Pixel(image, 3, 0, 13));
994 ASSERT_TRUE(TestGrayscale16Pixel(image, 4, 0, 381));
995 }
996
997
998 TEST(ImageProcessing, ShiftScaleSignedGrayscale16)
999 {
1000 Image image(PixelFormat_SignedGrayscale16, 5, 1, false);
1001 SetSignedGrayscale16Pixel(image, 0, 0, 0);
1002 SetSignedGrayscale16Pixel(image, 1, 0, 2);
1003 SetSignedGrayscale16Pixel(image, 2, 0, 5);
1004 SetSignedGrayscale16Pixel(image, 3, 0, 10);
1005 SetSignedGrayscale16Pixel(image, 4, 0, 255);
1006
1007 ImageProcessing::ShiftScale(image, -17.1, 11.5, true);
1008 ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 0, 0, -197));
1009 ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 1, 0, -174));
1010 ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 2, 0, -139));
1011 ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 3, 0, -82));
1012 ASSERT_TRUE(TestSignedGrayscale16Pixel(image, 4, 0, 2736));
1013 }