public void AddCTest() { foreach (int bitsPerPixel in new[] { 1, 2, 4, 8, 16, 24, 32 }) { foreach (int scaleFactor in new[] { 0, 1, -2 }) { Image src = new Image((32 * 2) + 23, 43, bitsPerPixel, 200, 200); src.Randomize(); Image copy = src.Clone(true); switch (bitsPerPixel) { case 8: { Image dst1 = src.AddC(null, 23, scaleFactor); Image dst2 = src.AddC(src, 23, scaleFactor); Assert.IsTrue(src == dst2); for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { uint expected = (uint)Math.Min(255, (int)Math.Round(((int)copy.GetPixel(x, y) + 23) * Math.Pow(2, -scaleFactor), MidpointRounding.ToEven)); Assert.AreEqual(expected, dst1.GetPixel(x, y)); Assert.AreEqual(expected, dst2.GetPixel(x, y)); } } } break; case 24: { Color addcolor = Color.FromArgb(0, 12, 34, 56); Image dst1 = src.AddC(null, addcolor.Argb, scaleFactor); Image dst2 = src.AddC(src, addcolor.Argb, scaleFactor); Assert.IsTrue(src == dst2); for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { Color source = Color.FromArgb(copy.GetPixel(x, y)); Color expected = Color.Add(source, addcolor, Math.Pow(2, -scaleFactor), MidpointRounding.ToEven); Assert.AreEqual(expected.Argb, dst1.GetPixel(x, y)); Assert.AreEqual(expected.Argb, dst2.GetPixel(x, y)); } } } break; case 32: { Color addcolor = Color.FromArgb(23, 12, 34, 56); Image dst1 = src.AddC(null, addcolor.Argb, scaleFactor); Image dst2 = src.AddC(src, addcolor.Argb, scaleFactor); Assert.IsTrue(src == dst2); for (int y = 0; y < src.Height; y++) { for (int x = 0; x < src.Width; x++) { Color source = Color.FromArgb(copy.GetPixel(x, y)); Color expected = Color.Add(source, addcolor, Math.Pow(2, -scaleFactor), MidpointRounding.ToEven); Assert.AreEqual(expected.Argb, dst1.GetPixel(x, y)); Assert.AreEqual(expected.Argb, dst2.GetPixel(x, y)); } } } break; default: try { src.AddC(null, 23, scaleFactor); Assert.Fail(); } catch (NotSupportedException) { } break; } } } }