コード例 #1
0
        public void GetColoursTest()
        {
            ReportStart();
            _bitmap = RandomBitmap.Create(new Size(100, 100), 20,
                                          PixelFormat.Format32bppArgb);

            Collection <Color> expectedColours = new Collection <Color>();

            for (int y = 0; y < _bitmap.Height; y++)
            {
                for (int x = 0; x < _bitmap.Width; x++)
                {
                    expectedColours.Add(_bitmap.GetPixel(x, y));
                }
            }

            Color[] actualColours = ImageTools.GetColours(_bitmap);

            Assert.AreEqual(expectedColours.Count, actualColours.Length);

            for (int i = 0; i < expectedColours.Count; i++)
            {
                ColourAssert.AreEqual(expectedColours[i],
                                      actualColours[i],
                                      "Index " + i);
            }
            ReportEnd();
        }
コード例 #2
0
        public void GetRgbCollectionTest()
        {
            ReportStart();
            _bitmap = RandomBitmap.Create(new Size(50, 50), 10,
                                          PixelFormat.Format32bppArgb);

            Color[] colours = ImageTools.GetColours(_bitmap);

            int len = colours.Length;

            byte[] exected = new byte[len * 3];
            int    rgbIndex;

            for (int colourIndex = 0; colourIndex < len; colourIndex++)
            {
                rgbIndex              = colourIndex * 3;
                exected[rgbIndex]     = (byte)colours[colourIndex].R;
                exected[rgbIndex + 1] = (byte)colours[colourIndex].G;
                exected[rgbIndex + 2] = (byte)colours[colourIndex].B;
            }

            Collection <byte> actual = ImageTools.GetRgbCollection(colours);

            Assert.AreEqual(exected.Length, actual.Count);

            for (int i = 0; i < exected.Length; i++)
            {
                Assert.AreEqual(actual[i], exected[i], "Index " + i);
            }
            ReportEnd();
        }
コード例 #3
0
        public void GetDistinctColoursTest()
        {
            ReportStart();
            _bitmap = RandomBitmap.Create(new Size(50, 50), 10,
                                          PixelFormat.Format32bppArgb);

            Collection <Color> expectedDistinctColours = new Collection <Color>();
            Color c;

            for (int y = 0; y < _bitmap.Height; y++)
            {
                for (int x = 0; x < _bitmap.Width; x++)
                {
                    c = _bitmap.GetPixel(x, y);
                    if (expectedDistinctColours.Contains(c) == false)
                    {
                        expectedDistinctColours.Add(c);
                    }
                }
            }

            Color[]            actualColours = ImageTools.GetColours(_bitmap);
            Collection <Color> actualDistinctColours
                = ImageTools.GetDistinctColours(actualColours);

            Assert.AreEqual(expectedDistinctColours.Count,
                            actualDistinctColours.Count);

            // NB we can't compare expected[i] to actual[i] as the two collections
            // are in different orders due to the use of Hashtable in the
            // GetDistinctColours method
            foreach (Color thisColour in expectedDistinctColours)
            {
                CollectionAssert.Contains(actualDistinctColours, thisColour,
                                          thisColour.ToString());
            }
            ReportEnd();
        }
コード例 #4
0
        public void GetRgbSpeedComparisonTest()
        {
            ReportStart();
            _bitmap = RandomBitmap.Create(new Size(1000, 1000), 100,
                                          PixelFormat.Format32bppArgb);
            _bitmap.Save("GetRgbSpeedComparisonTest.bmp");
            Color[] colours = ImageTools.GetColours(_bitmap);

            DateTime startTime;
            DateTime endTime;

            startTime = DateTime.Now;
            Collection <byte> rgbCollection
                = ImageTools.GetRgbCollection(colours);

            endTime = DateTime.Now;
            TimeSpan collectionRunTime = endTime - startTime;

            WriteMessage("GetRgbCollection took " + collectionRunTime);

            startTime = DateTime.Now;
            byte[] rgbArray = ImageTools.GetRgbArray(colours);
            endTime = DateTime.Now;
            TimeSpan arrayRunTime = endTime - startTime;

            WriteMessage("GetRgbArray took " + arrayRunTime);

            // GetRgbArray should be quicker than GetRgbCollection. If not then
            // there's probably something wrong with GetRgbArray
            Assert.IsTrue(arrayRunTime < collectionRunTime);

            Assert.AreEqual(rgbCollection.Count, rgbArray.Length);
            for (int i = 0; i < rgbCollection.Count; i++)
            {
                string colourName;
                int    remainder;
                int    quotient = Math.DivRem(i, 3, out remainder);
                switch (remainder)
                {
                case 0:
                    colourName = "red";
                    break;

                case 1:
                    colourName = "green";
                    break;

                case 2:
                    colourName = "blue";
                    break;

                default:
                    throw new InvalidOperationException("unexpected remainder: " + remainder);
                }
                string message
                    = "Index: " + i + ", pixel " + quotient
                      + " (" + colourName + ")";
                Assert.AreEqual(rgbCollection[i], rgbArray[i], message);
            }
            ReportEnd();
        }