Ejemplo n.º 1
0
        public void PixelMatrix_skips()
        {
            try
            {
                List <int> dimensions = ReturnPixelJumps();

                Stopwatch sw = new Stopwatch();
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"f:\temp\runtime\pixelmatrixanalysis_jumps.txt", true))
                {
                    for (int i = 0; i < dimensions.Count; i++)
                    {
                        for (int n = 0; n < 200; n++)
                        {
                            BitmapWrapper bm1 = new BitmapWrapper(@"F:\temp\analysis\640x480\test_0.jpg");
                            BitmapWrapper bm2 = new BitmapWrapper(@"F:\temp\analysis\640x480\test_1.jpg");

                            sw.Restart();
                            PixelMatrix matrix = new PixelMatrix();
                            matrix.WidthSearchOffset = dimensions[i];
                            matrix.Populate(bm1, bm2);

                            sw.Stop();
                            file.WriteLine(i + " - " + n + " - " + " grid - " + sw.Elapsed.TotalMilliseconds);
                        }
                    }
                }
                Assert.IsTrue(true);
            }
            catch
            {
                Assert.IsTrue(false);
            }
        }
Ejemplo n.º 2
0
        public void Motion2aMultipleCompare()
        {
            try
            {
                //images in memory when sent to the
                ByteWrapper image1 = ImageConvert.ReturnByteWrapper(@"F:\temp\analysis\640x480\test_0.jpg");
                ByteWrapper image2 = ImageConvert.ReturnByteWrapper(@"F:\temp\analysis\640x480\test_1.jpg");

                PixelMatrix dummy = new PixelMatrix();
                dummy.LinkCompare = true;
                dummy.Populate(new BitmapWrapper(ImageConvert.ReturnBitmap(image1.bytes)), new BitmapWrapper(ImageConvert.ReturnBitmap(image2.bytes)));

                MotionSensor_2a motion = new MotionSensor_2a();
                motion.ThresholdSet         = true;
                motion.settings.linkCompare = true;
                motion.Comparison           = dummy.Comparision;

                motion.ImageCreated(image1, EventArgs.Empty);
                motion.ImageCreated(image2, EventArgs.Empty);

                motion.ImageCreated(image1, EventArgs.Empty);
                motion.ImageCreated(image2, EventArgs.Empty);


                Assert.IsTrue(true);
            }
            catch
            {
                Assert.IsTrue(false);
            }
        }
Ejemplo n.º 3
0
    //Draws a circle on the bitmap using Bresenham
    public static PixelMatrix BresenhamCircle(PixelMatrix bitmap, int centerX, int centerY, int radius, Color color)
    {
        int d = (5 - radius * 4) / 4;
        int x = 0;
        int y = radius;

        do
        {
            bitmap.SetPixel(centerX + x, centerY + y, color);
            bitmap.SetPixel(centerX + x, centerY - y, color);
            bitmap.SetPixel(centerX - x, centerY + y, color);
            bitmap.SetPixel(centerX - x, centerY - y, color);
            bitmap.SetPixel(centerX + y, centerY + x, color);
            bitmap.SetPixel(centerX + y, centerY - x, color);
            bitmap.SetPixel(centerX - y, centerY + x, color);
            bitmap.SetPixel(centerX - y, centerY - x, color);
            if (d < 0)
            {
                d += 2 * x + 1;
            }
            else
            {
                d += 2 * (x - y) + 1;
                y--;
            }
            x++;
        } while (x <= y);

        return(bitmap);
    }
Ejemplo n.º 4
0
    //Rotate an image by increments of 90 degrees
    public static PixelMatrix RotateSq(PixelMatrix original, int rotates)
    {
        //No more rotates
        if (rotates == 0)
        {
            return(original);
        }

        //Setup new rotated
        int         side    = Mathf.Max(original.width, original.height);
        PixelMatrix rotated = new PixelMatrix(side, side, Color.clear);

        //Rotate 90º
        for (int i = 0; i < original.width; i++)
        {
            int finalI = original.width - i - 1;
            for (int j = 0; j < original.height; j++)
            {
                rotated.SetPixelSafe(j, finalI, original.GetPixelSafe(i, j));
            }
        }

        //Call new rotation
        rotated = RotateSq(rotated, rotates - 1);

        return(rotated);
    }
Ejemplo n.º 5
0
    //Draws a line on the bitmap using Bresenham
    public static PixelMatrix BresenhamLine(PixelMatrix bitmap, int x0, int y0, int x1, int y1, Color color)
    {
        int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
        int dy = Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
        int err = (dx > dy ? dx : -dy) / 2, e2;

        do
        {
            bitmap.SetPixel(x0, y0, color);
            if (x0 == x1 && y0 == y1)
            {
                break;
            }
            e2 = err;
            if (e2 > -dx)
            {
                err -= dy;
                x0  += sx;
            }
            if (e2 < dy)
            {
                err += dx;
                y0  += sy;
            }
        } while (true);

        return(bitmap);
    }
Ejemplo n.º 6
0
    //Draws a line on the bitmap using Bresenham and thickness
    public static PixelMatrix BresenhamLineThick(PixelMatrix bitmap, int x0, int y0, int x1, int y1, Color color, int thickness)
    {
        //Prepare filled circle
        PixelMatrix decal = FilledCircle(thickness, color);

        int dx = Mathf.Abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
        int dy = Mathf.Abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
        int err = (dx > dy ? dx : -dy) / 2, e2;

        do
        {
            bitmap = Decal(bitmap, decal, x0, y0);
            if (x0 == x1 && y0 == y1)
            {
                break;
            }
            e2 = err;
            if (e2 > -dx)
            {
                err -= dy; x0 += sx;
            }
            if (e2 < dy)
            {
                err += dx; y0 += sy;
            }
        } while (true);

        return(bitmap);
    }
Ejemplo n.º 7
0
        /// <summary>
        /// Calculates the best-fitting character based on Mean squared error. See <see cref="RenderOption.CalculateCharForMatrix(PixelMatrix)"/> and <see cref="https://en.wikipedia.org/wiki/Mean_squared_error"/> for more information.
        /// </summary>
        /// <param name="tile">The part of the image to be represented.</param>
        /// <returns>The best-fitting character and its corresponding PixelMatrix.</returns>
        public override KeyValuePair <char, PixelMatrix> CalculateCharForMatrix(PixelMatrix tile)
        {
            double minScore = double.MaxValue;
            KeyValuePair <char, PixelMatrix> bestFit = Font.Chars.FirstOrDefault();

            foreach (var item in Font.Chars)
            {
                double score = 0;

                for (int y = 0; y < Font.CharSize.Height; y++)
                {
                    for (int x = 0; x < Font.CharSize.Width; x += Utils.BytesPerPixel)
                    {
                        byte grayChar   = Utils.ColorToGray(item.Value.GetPixel(x, y));
                        byte grayMatrix = Utils.ColorToGray(tile.GetPixel(x, y));

                        score += (grayMatrix - grayChar) * (grayMatrix - grayChar);
                    }
                }

                if (score < minScore)
                {
                    bestFit  = item;
                    minScore = score;
                }
            }

            return(bestFit);
        }
Ejemplo n.º 8
0
    //Scan-line flood fill, much faster
    //https://lodev.org/cgtutor/floodfill.html#Scanline_Floodfill_Algorithm_With_Stack
    public static PixelMatrix FloodFillLine(PixelMatrix bmp, int x, int y, Color replacementColor)
    {
        Point pt = new Point(x, y);

        Color targetColor = bmp.GetPixel(pt.x, pt.y);

        if (targetColor == replacementColor)
        {
            return(bmp);
        }

        Stack <Point> pixels = new Stack <Point>();

        pixels.Push(pt);
        while (pixels.Count != 0)
        {
            Point temp = pixels.Pop();
            int   y1   = temp.y;

            while (y1 >= 0 && bmp.GetPixel(temp.x, y1) == targetColor)
            {
                y1--;
            }
            y1++;
            bool spanLeft  = false;
            bool spanRight = false;

            while (y1 < bmp.height && bmp.GetPixel(temp.x, y1) == targetColor)
            {
                bmp.SetPixelSafe(temp.x, y1, replacementColor);

                Color clm1 = bmp.GetPixel(temp.x - 1, y1);
                Color clp1 = bmp.GetPixel(temp.x + 1, y1);

                if (!spanLeft && temp.x > 0 && clm1 == targetColor)
                {
                    pixels.Push(new Point(temp.x - 1, y1));
                    spanLeft = true;
                }
                else if (spanLeft && temp.x - 1 >= 0 && clm1 != targetColor)
                {
                    spanLeft = false;
                }
                if (!spanRight && temp.x < bmp.width - 1 && clp1 == targetColor)
                {
                    pixels.Push(new Point(temp.x + 1, y1));
                    spanRight = true;
                }
                else if (spanRight && temp.x < bmp.width - 1 && clp1 != targetColor)
                {
                    spanRight = false;
                }
                y1++;
            }
        }

        return(bmp);
    }
Ejemplo n.º 9
0
        }                                                  //the thresholds, per grid

        public override void Compare(ByteWrapper image1, ByteWrapper image2)
        {
            var bm1 = new BitmapWrapper(ImageConvert.ReturnBitmap(image1.bytes));
            var bm2 = new BitmapWrapper(ImageConvert.ReturnBitmap(image2.bytes));

            PixelMatrix matrix = new PixelMatrix();

            if (settings.searchHeight > 0)
            {
                matrix.SearchHeight = settings.searchHeight;
            }
            if (settings.searchWidth > 0)
            {
                matrix.SearchWidth = settings.searchWidth;
            }
            if (settings.linkCompare)
            {
                matrix.LinkCompare = true;
            }
            matrix.GridSystemOn = true;
            matrix.Populate(bm1, bm2);

            double sumChangedPixels = matrix.SumChangedPixels;

            //keep adding for threshold calculation, set the threshold, or monitor
            if (ThresholdSet)
            {
                //do the motion detection
                for (int i = 0; i < ThresholdImage.Columns.Count; i++)
                {
                    for (int n = 0; n < ThresholdImage.Columns[i].grids.Count; n++)
                    {
                        if (matrix.imageGrid.Columns[i].grids[n].change > ThresholdImage.Columns[i].grids[n].threshold)
                        {
                            OnMotion(image1, image2, ThresholdImage.GridNumber(i, n));
                            return;
                        }
                    }
                }
            }
            else if (!ThresholdSet && gridImages.Count < ControlImageNumber)
            {
                gridImages.Add(matrix.imageGrid); //keep adding for the later threshold calculation
            }
            else
            {
                SetThreshold(); //enough images received to set the threshold and start monitoring
            }

            Comparison = matrix.Comparator;

            //clean up the memory
            matrix.Dispose();
            bm1.bitmap.Dispose();
            bm2.bitmap.Dispose();
            bm1 = null;
            bm2 = null;
        }//Compare
Ejemplo n.º 10
0
    //Draw a filled box
    public static PixelMatrix FilledBox(int width, int height, Color color)
    {
        //New Decal
        PixelMatrix decal = new PixelMatrix(width, height, new Color(0, 0, 0, 0));

        //Box perimeter
        decal = Border(decal, color);

        //Fill box
        decal = FloodFillLine(decal, width / 2, height / 2, color);

        return(decal);
    }
Ejemplo n.º 11
0
        public void Grid()
        {
            PixelMatrix matrix = new PixelMatrix();

            matrix.GridSystemOn = true;
            matrix.Populate(@"d:\temp\MotionSensor\2.2\test_101.jpg", @"d:\temp\MotionSensor\2.2\test_128.jpg");

            Assert.IsNotNull(matrix.imageGrid.Columns);
            Assert.IsTrue(matrix.imageGrid.Columns.Count == 4);
            Assert.IsTrue(matrix.imageGrid.Columns[0].grids.Count == 4);
            Assert.IsTrue(matrix.imageGrid.Columns[1].grids.Count == 4);
            Assert.IsTrue(matrix.imageGrid.Columns[2].grids.Count == 4);
            Assert.IsTrue(matrix.imageGrid.Columns[3].grids.Count == 4);
        }
Ejemplo n.º 12
0
 //Draws a border on the bitmap
 public static PixelMatrix Border(PixelMatrix bitmap, Color color)
 {
     for (int i = 0; i < bitmap.height; i++)
     {
         bitmap.SetPixelSafe(0, i, color);
         bitmap.SetPixelSafe(bitmap.width - 1, i, color);
     }
     for (int i = 0; i < bitmap.width; i++)
     {
         bitmap.SetPixelSafe(i, 0, color);
         bitmap.SetPixelSafe(i, bitmap.height - 1, color);
     }
     return(bitmap);
 }
Ejemplo n.º 13
0
    //Draw a filled circle
    public static PixelMatrix FilledCircle(int radius, Color color)
    {
        //New Decal
        int         size  = radius * 2 + 1;
        PixelMatrix decal = new PixelMatrix(size, size, new Color(0, 0, 0, 0));

        //Circle perimeter
        decal = BresenhamCircle(decal, radius, radius, radius, color);

        //Fill circle
        decal = FloodFillLine(decal, radius, radius, color);

        return(decal);
    }
Ejemplo n.º 14
0
        public void Motion2aSubequent()
        {
            try
            {
                List <int> dimensions = ReturnDimensions();
                Stopwatch  sw         = new Stopwatch();
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"f:\temp\runtime\motion_analysis.txt", true))
                {
                    for (int i = 0; i < dimensions.Count; i++)
                    {
                        for (int n = 0; n < 200; n++)
                        {
                            //images in memory when sent to the
                            ByteWrapper image1 = ImageConvert.ReturnByteWrapper(@"F:\temp\analysis\640x480\test_0.jpg");
                            image1.sequenceNumber = i;
                            ByteWrapper image2 = ImageConvert.ReturnByteWrapper(@"F:\temp\analysis\640x480\test_1.jpg");
                            image2.sequenceNumber = i;


                            PixelMatrix dummy = new PixelMatrix();
                            dummy.LinkCompare = true;
                            dummy.SearchWidth = dimensions[i];
                            dummy.Populate(new BitmapWrapper(ImageConvert.ReturnBitmap(image1.bytes)), new BitmapWrapper(ImageConvert.ReturnBitmap(image2.bytes)));


                            sw.Restart();
                            MotionSensor_2a motion = new MotionSensor_2a();
                            motion.ThresholdSet         = true;
                            motion.settings.linkCompare = true;
                            motion.settings.searchWidth = dimensions[i];
                            motion.Comparison           = dummy.Comparision;

                            motion.ImageCreated(image1, EventArgs.Empty);
                            motion.ImageCreated(image2, EventArgs.Empty);

                            motion.ImageCreated(image1, EventArgs.Empty);
                            motion.ImageCreated(image2, EventArgs.Empty);
                            sw.Stop();
                            file.WriteLine(i + " - " + n + " - " + " no grid - " + sw.Elapsed.TotalMilliseconds);
                        }
                    }
                }
                Assert.IsTrue(true);
            }
            catch
            {
                Assert.IsTrue(false);
            }
        }
Ejemplo n.º 15
0
        public void GridPopulation()
        {
            PixelMatrix matrix = new PixelMatrix();

            matrix.GridSystemOn = true;
            matrix.Populate(@"d:\temp\MotionSensor\2.2\test_101.jpg", @"d:\temp\MotionSensor\2.2\test_128.jpg");

            for (int i = 0; i < matrix.imageGrid.Columns.Count; i++)
            {
                for (int n = 0; n < matrix.imageGrid.Columns[i].grids.Count; n++)
                {
                    Assert.IsTrue(matrix.imageGrid.Columns[i].grids[n].change != 0);
                }
            }
        }//GridPopulation
Ejemplo n.º 16
0
        /// <summary>
        /// Calculates the best-fitting character based on the brightness. See <see cref="RenderOption.CalculateCharForMatrix(PixelMatrix)"/> for more information.
        /// </summary>
        /// <param name="tile">The part of the image to be represented.</param>
        /// <returns>The best-fitting character and its corresponding PixelMatrix.</returns>
        public override KeyValuePair <char, PixelMatrix> CalculateCharForMatrix(PixelMatrix tile)
        {
            KeyValuePair <char, PixelMatrix> bestFit = Font.Chars.FirstOrDefault();

            foreach (var item in Font.Chars)
            {
                if (RenderingMode == RenderingMode.Default && item.Value.CompareBrightness(tile) < bestFit.Value.CompareBrightness(tile))
                {
                    bestFit = item;
                }
                else if (RenderingMode == RenderingMode.Inverted && item.Value.CompareBrightness(tile) > bestFit.Value.CompareBrightness(tile))
                {
                    bestFit = item;
                }
            }

            return(bestFit);
        }
Ejemplo n.º 17
0
        internal T Convert(BitmapSource source, IProgress <double> progress)
        {
            if (source.Format != Utils.PixelFormat)
            {
                source = new FormatConvertedBitmap(source, Utils.PixelFormat, null, 0);
            }

            List <PixelMatrix> tiles = PixelMatrix.CreateTiles(source, new Size(RenderOptions.TileSize.Width, RenderOptions.TileSize.Height));
            int width  = source.PixelWidth - (source.PixelWidth % RenderOptions.TileSize.Width);
            int height = source.PixelHeight - (source.PixelHeight % RenderOptions.TileSize.Height);

            InitializeOutput(new Size(width, height));

            int tempCount = 0;

            if (_parallelConversion)
            {
                Parallel.For(0, tiles.Count, i =>
                {
                    AddToOutput(RenderOptions.CalculateCharForMatrix(tiles[i]), new Point(i % (width / RenderOptions.TileSize.Width) * RenderOptions.TileSize.Width, i / (width / RenderOptions.TileSize.Width) * RenderOptions.TileSize.Height), tiles[i]);

                    if (progress != null)
                    {
                        progress.Report((double)tempCount / tiles.Count);
                        tempCount++;
                    }
                });
            }
            else
            {
                for (int i = 0; i < tiles.Count; i++)
                {
                    AddToOutput(RenderOptions.CalculateCharForMatrix(tiles[i]), new Point(i % (width / RenderOptions.TileSize.Width) * RenderOptions.TileSize.Width, i / (width / RenderOptions.TileSize.Width) * RenderOptions.TileSize.Height), tiles[i]);

                    if (progress != null)
                    {
                        progress.Report((double)tempCount / tiles.Count);
                        tempCount++;
                    }
                }
            }

            return(FinalizeOutput());
        }
Ejemplo n.º 18
0
        /// <summary>
        /// Calculates the best-fitting character based on Structural similarity index. See <see cref="RenderOption.CalculateCharForMatrix(PixelMatrix)"/> and <see cref="https://en.wikipedia.org/wiki/Structural_similarity"/> for more information.
        /// </summary>
        /// <param name="tile">The part of the image to be represented.</param>
        /// <returns>The best-fitting character and its corresponding PixelMatrix.</returns>
        public override KeyValuePair <char, PixelMatrix> CalculateCharForMatrix(PixelMatrix tile)
        {
            double maxScore = RenderingMode == RenderingMode.Default ? double.MinValue : double.MaxValue;
            KeyValuePair <char, PixelMatrix> bestFit = Font.Chars.FirstOrDefault();

            foreach (var item in Font.Chars)
            {
                double score = 0;

                double covariance = 0;
                for (int y = 0; y < Font.CharSize.Height; y++)
                {
                    for (int x = 0; x < Font.CharSize.Width; x += Utils.BytesPerPixel)
                    {
                        byte grayChar   = Utils.ColorToGray(item.Value.GetPixel(x, y));
                        byte grayMatrix = Utils.ColorToGray(tile.GetPixel(x, y));

                        covariance += (grayChar - item.Value.Mean) * (grayMatrix - tile.Mean);
                    }
                }

                covariance /= Font.CharSize.Height * Font.CharSize.Width;

                score  = (2 * Math.Sqrt(item.Value.Variance) * Math.Sqrt(tile.Variance) + C1) * (2 * covariance + C2);
                score /= (item.Value.Mean * item.Value.Mean + tile.Mean * tile.Mean + C1) * (covariance * covariance + C2);

                if (RenderingMode == RenderingMode.Default && score > maxScore)
                {
                    bestFit  = item;
                    maxScore = score;
                }
                else if (RenderingMode == RenderingMode.Inverted && score < maxScore)
                {
                    bestFit  = item;
                    maxScore = score;
                }
            }

            return(bestFit);
        }
Ejemplo n.º 19
0
    //Add a decal to an image
    public static PixelMatrix Decal(PixelMatrix original, PixelMatrix decal, int x, int y)
    {
        //Offsets to apply
        int widthOffset  = -decal.width / 2 + x;
        int heightOffset = -decal.height / 2 + y;

        for (int i = 0; i < decal.width; i++)
        {
            //X offset
            int finalX = i + widthOffset;

            //Skip column if out of original
            if (finalX < 0 || finalX > original.width - 1)
            {
                continue;
            }

            for (int j = 0; j < decal.height; j++)
            {
                //Get color
                Color cl = decal.GetPixelSafe(i, j);

                //Skip transparent pixels
                //TODO Blend with transparency?
                if (cl.a < 0.7)
                {
                    continue;
                }

                //Y offset
                int finalY = j + heightOffset;

                //Apply color
                original.SetPixel(finalX, finalY, cl);
            }
        }

        return(original);
    }
        /// <summary>
        /// Calculates the best-fitting character based on the Euclidean distance between the character's and tile's histograms. See <see cref="RenderOption.CalculateCharForMatrix(PixelMatrix)"/> and <see cref="Histogram.EuclideanDistance(Histogram)"/> for more information.
        /// </summary>
        /// <param name="tile">The part of the image to be represented.</param>
        /// <returns>The best-fitting character and its corresponding PixelMatrix.</returns>
        public override KeyValuePair <char, PixelMatrix> CalculateCharForMatrix(PixelMatrix tile)
        {
            double maxScore = RenderingMode == RenderingMode.Default ? double.MinValue : double.MaxValue;
            KeyValuePair <char, PixelMatrix> bestFit = Font.Chars.FirstOrDefault();

            foreach (var item in Font.Chars)
            {
                double score = tile.Histogram.EuclideanDistance(item.Value.Histogram);

                if (RenderingMode == RenderingMode.Default && score > maxScore)
                {
                    bestFit  = item;
                    maxScore = score;
                }
                else if (RenderingMode == RenderingMode.Inverted && score < maxScore)
                {
                    bestFit  = item;
                    maxScore = score;
                }
            }

            return(bestFit);
        }
        public override void Compare(ByteWrapper image1, ByteWrapper image2)
        {
            var bm1 = new BitmapWrapper(ImageConvert.ReturnBitmap(image1.bytes));

            bm1.sequenceNumber = logging.imagesReceived - 2;
            var bm2 = new BitmapWrapper(ImageConvert.ReturnBitmap(image2.bytes));

            bm2.sequenceNumber = logging.imagesReceived - 1;

            PixelMatrix matrix = new PixelMatrix();

            matrix.LinkCompare = settings.linkCompare;
            if (settings.searchHeight > 0)
            {
                matrix.SearchHeight = settings.searchHeight;
            }
            if (settings.searchWidth > 0)
            {
                matrix.SearchWidth = settings.searchWidth;
            }
            if (settings.horizontalPixelsToSkip > 0)
            {
                matrix.WidthSearchOffset = settings.horizontalPixelsToSkip + 1;
            }
            if (settings.verticalPixelsToSkip > 0)
            {
                matrix.WidthSearchOffset = settings.verticalPixelsToSkip + 1;
            }

            if (settings.linkCompare && Comparison != null)
            {
                matrix.Populate(Comparison, bm2);
            }
            else
            {
                matrix.Populate(bm1, bm2);
            }

            double sumChangedPixels = matrix.SumChangedPixelsPositive;

            //keep adding for threshold calculation, set the threshold, or monitor
            if (ThresholdSet)
            {
                //now scanning, compare the two images and see what the difference is
                if (sumChangedPixels > pixelChangeThreshold)
                {
                    OnMotionAsync(image1, image2);
                }
            }
            else if (!ThresholdSet && pixelChange.Count < ControlImageNumber)
            {
                pixelChange.Add(sumChangedPixels);
            }
            else
            {
                SetThreshold(); //enough images received to set the threshold and start monitoring
            }

            Comparison = matrix.Comparator;

            if (logging.LoggingOn && logging.matrices != null)
            {
                logging.matrices.Add(matrix);
            }

            //clean up the memory
            matrix.Dispose();
            bm1.bitmap.Dispose();
            bm2.bitmap.Dispose();
            bm1 = null;
            bm2 = null;
        }//Compare
Ejemplo n.º 22
0
 /// <summary>
 /// Copies the pixel values to the output image. See <see cref="Converter{T}.AddToOutput(KeyValuePair{char, PixelMatrix}, Point, PixelMatrix)"/> for more information.
 /// </summary>
 /// <param name="character">The character to be drawn.</param>
 /// <param name="point">A point that represents the coordinates of the upper left corner to be drawn.</param>
 /// <param name="tile">The PixelMatrix representing a tile of the source image.</param>
 protected override void AddToOutput(KeyValuePair <char, PixelMatrix> character, Point point, PixelMatrix tile)
 {
     for (int y = point.Y; y < point.Y + RenderOptions.TileSize.Height; y++)
     {
         for (int x = point.X * Utils.BytesPerPixel; x < (point.X + RenderOptions.TileSize.Width) * Utils.BytesPerPixel; x += Utils.BytesPerPixel)
         {
             int index = y * _size.Width * Utils.BytesPerPixel;
             System.Drawing.Color c = DrawingMode.CalculateColor(character.Value.GetPixel(x - Utils.BytesPerPixel * point.X, y - point.Y), tile.GetPixel(x - Utils.BytesPerPixel * point.X, y - point.Y));
             _pixels[index + x]     = c.B; // b
             _pixels[index + x + 1] = c.G; // g
             _pixels[index + x + 2] = c.R; // r
             _pixels[index + x + 3] = c.A; // a
         }
     }
 }
Ejemplo n.º 23
0
        public void LinqTesting()
        {
            try
            {
                List <int> dimensions = ReturnDimensions();
                Stopwatch  sw         = new Stopwatch();
                using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"f:\temp\runtime\linqanalysis.txt", true))
                {
                    for (int m = 0; m < dimensions.Count; m++)
                    {
                        for (int i = 0; i < 30; i++)
                        {
                            //do the minimum
                            BitmapWrapper bm1    = new BitmapWrapper(@"F:\temp\analysis\640x480\test_0.jpg");
                            BitmapWrapper bm2    = new BitmapWrapper(@"F:\temp\analysis\640x480\test_1.jpg");
                            PixelMatrix   matrix = new PixelMatrix();
                            matrix.SearchWidth = dimensions[m];
                            matrix.Populate(bm1, bm2);

                            //LINQ
                            sw.Restart();
                            double min = matrix.MinChanged;
                            sw.Stop();
                            file.WriteLine("LINQ - min - " + m + " - " + sw.Elapsed.TotalMilliseconds);

                            sw.Restart();
                            double max = matrix.MaxChanged;
                            sw.Stop();
                            file.WriteLine("LINQ - max - " + m + " - " + sw.Elapsed.TotalMilliseconds);

                            sw.Restart();
                            double sum = matrix.SumChangedPixels;
                            sw.Stop();
                            file.WriteLine("LINQ - sum - " + m + " - " + sw.Elapsed.TotalMilliseconds);

                            //ITERATION
                            sw.Restart();
                            double min2 = 0;
                            for (int n = 0; n < matrix.Columns.Count; n++)
                            {
                                for (int k = 0; k < matrix.Columns[n].Cells.Count; k++)
                                {
                                    if (matrix.Columns[n].Cells[k].change < min2)
                                    {
                                        min2 = matrix.Columns[n].Cells[k].change;
                                    }
                                }
                            }
                            sw.Stop();
                            file.WriteLine("ITERATION - min - " + m + " - " + sw.Elapsed.TotalMilliseconds);


                            sw.Restart();
                            double max2 = 0;
                            for (int n = 0; n < matrix.Columns.Count; n++)
                            {
                                for (int k = 0; k < matrix.Columns[n].Cells.Count; k++)
                                {
                                    if (matrix.Columns[n].Cells[k].change > max2)
                                    {
                                        max2 = matrix.Columns[n].Cells[k].change;
                                    }
                                }
                            }
                            sw.Stop();
                            file.WriteLine("ITERATION - max - " + m + " - " + sw.Elapsed.TotalMilliseconds);

                            sw.Restart();
                            double sum2 = 0;
                            for (int n = 0; n < matrix.Columns.Count; n++)
                            {
                                for (int k = 0; k < matrix.Columns[n].Cells.Count; k++)
                                {
                                    sum2 += matrix.Columns[n].Cells[k].change;
                                }
                            }
                            sw.Stop();
                            file.WriteLine("ITERATION - sum - " + m + " - " + sw.Elapsed.TotalMilliseconds);
                        }
                    }
                }
                Assert.IsTrue(true);
            }
            catch
            {
                Assert.IsTrue(false);
            }
        }
Ejemplo n.º 24
0
 /// <summary>
 /// Calculates the best-fitting character for a part of the image.
 /// </summary>
 /// <param name="tile">The part of the image to be represented.</param>
 /// <returns>The best-fitting character and its corresponding PixelMatrix.</returns>
 public abstract KeyValuePair <char, PixelMatrix> CalculateCharForMatrix(PixelMatrix tile);
Ejemplo n.º 25
0
        /// <summary>
        /// Adds the character to the output string. See <see cref="Converter{T}.AddToOutput(KeyValuePair{char, PixelMatrix}, Point, PixelMatrix)"/> for more information.
        /// </summary>
        /// <param name="character">The character to be drawn.</param>
        /// <param name="point">A point that represents the coordinates of the upper left corner to be drawn.</param>
        /// <param name="tile">The PixelMatrix representing a tile of the source image.</param>
        protected override void AddToOutput(KeyValuePair <char, PixelMatrix> character, Point point, PixelMatrix tile)
        {
            if (point.X == 0 && point.Y != 0)
            {
                _result.Append("\r\n");
            }

            _result.Append(character.Key);
        }
Ejemplo n.º 26
0
        /// <summary>
        /// Adds the character to the output string. See <see cref="Converter{T}.AddToOutput(KeyValuePair{char, PixelMatrix}, Point, PixelMatrix)"/> for more information.
        /// </summary>
        /// <param name="character">The character to be drawn.</param>
        /// <param name="point">A point that represents the coordinates of the upper left corner to be drawn.</param>
        /// <param name="tile">The PixelMatrix representing a tile of the source image.</param>
        protected override void AddToOutput(KeyValuePair <char, PixelMatrix> character, Point point, PixelMatrix tile)
        {
            if (point.X == 0 && point.Y != 0)
            {
                _result.Append("</p>");
            }

            Color c = DrawingMode.CalculateColor(Color.FromArgb(0), tile.AverageColor);

            _result.Append(string.Format("<font color=\"{0:X2}{1:X2}{2:X2}\">{3}</font>", c.R, c.G, c.B, character.Key));
        }
Ejemplo n.º 27
0
 /// <summary>
 /// Adds the character at a specified position to the output.
 /// </summary>
 /// <param name="character">The character to be drawn.</param>
 /// <param name="point">A point that represents the coordinates of the upper left corner to be drawn.</param>
 /// <param name="tile">The PixelMatrix representing a tile of the source image.</param>
 protected abstract void AddToOutput(KeyValuePair <char, PixelMatrix> character, Point point, PixelMatrix tile);
Ejemplo n.º 28
0
 public StandardImage(ColorModel.RGB[,] data)
 {
     // Set the pixel matrix
     this.data = new PixelMatrix(data);
 }
Ejemplo n.º 29
0
        public PixelMatrix Test(PixelMatrix image)
        {
            var cut = new RotatePixelMatrix90degrees();

            return(RunTest(image, cut));
        }
Ejemplo n.º 30
0
 public StandardImage(Bitmap image)
 {
     this.image = image;
     // Read a the pixel data
     this.data = new PixelMatrix(image.GetRGBPixelArray());
 }