Example #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BitmapGridEnumerator"/> class based on the specified parentGrid.
 /// This automatically ignores any bytes in the range past the "width".
 /// </summary>
 /// <param name="parentGrid">The parent grid to cycle through the values of</param>
 public BitmapGridEnumerator(BitmapGrid parentGrid)
 {
     _values = parentGrid.Values;
     _stride = parentGrid.Stride;
     _height = parentGrid.Height;
     _width  = parentGrid.Width;
 }
Example #2
0
        /// <summary>
        /// Compares the bytes of this grid to the bytes of another grid. If the measurements
        /// of the other grid don't match this, then this returns false.
        /// </summary>
        /// <param name="otherGrid">The other BitmapGrid to test against</param>
        /// <returns>Boolean, true if the bytes are the same in each case.</returns>
        public bool Matches(BitmapGrid otherGrid)
        {
            if (otherGrid.Width != Width)
            {
                return(false);
            }
            if (otherGrid.Height != Height)
            {
                return(false);
            }
            if (otherGrid.Stride != Stride)
            {
                return(false);
            }
            byte[] otherValues = otherGrid.Values;
            for (int i = 0; i < otherValues.Length; i++)
            {
                if (Values[i] != otherValues[i])
                {
                    return(false);
                }
            }

            return(true);
        }
Example #3
0
        /// <summary>
        /// Creates a disconnected duplicate of this BitmapGrid.
        /// </summary>
        /// <returns>A copy of this BitmapGrid.</returns>
        public BitmapGrid Copy()
        {
            // Since we are starting with the image, we need to ensure that the values are up to date
            Bitmap     newBmp = BitmapImage.Clone() as Bitmap;
            BitmapGrid result = new BitmapGrid(newBmp);

            return(result);
        }
Example #4
0
        private BitmapGrid DoDifference(BitmapGrid compare, bool ignoreAlpha)
        {
            int h1 = Height;
            int w1 = Width;
            int h2 = compare.Height;
            int w2 = compare.Width;

            int        minHeight = Math.Min(h1, h2);
            int        maxHeight = Math.Max(h1, h2);
            int        minWidth  = Math.Min(w1, w2);
            int        maxWidth  = Math.Max(w1, w2);
            BitmapGrid result    = new BitmapGrid(maxWidth, maxHeight);

            byte[] compareValues = compare.Values;
            byte[] resultValues  = result.Values;
            int    stride1       = Stride;
            int    stride2       = compare.Stride;
            int    resStride     = result.Stride;

            for (int row = 0; row < maxHeight; row++)
            {
                for (int col = 0; col < maxWidth; col++)
                {
                    if (row < minHeight && col < minWidth)
                    {
                        // Both have values
                        byte a;
                        if (ignoreAlpha == false)
                        {
                            a = (byte)Math.Abs(compareValues[row * stride2 + col * 4 + 3] - Values[row * stride1 + col * 4 + 3]);
                        }
                        else
                        {
                            a = 255;
                        }

                        resultValues[row * resStride + col * 4]     = (byte)Math.Abs(compareValues[row * stride2 + col * 4] - Values[row * stride1 + col * 4]);
                        resultValues[row * resStride + col * 4 + 1] = (byte)Math.Abs(compareValues[row * stride2 + col * 4 + 1] - Values[row * stride1 + col * 4 + 1]);
                        resultValues[row * resStride + col * 4 + 2] = (byte)Math.Abs(compareValues[row * stride2 + col * 4 + 2] - Values[row * stride1 + col * 4 + 2]);
                        resultValues[row * resStride + col * 4 + 3] = a;
                    }
                    else if ((row >= h1 || col >= w1) && (row >= h2 || col >= w2))
                    {
                        // neither image has values, but the difference here is zero.
                        resultValues[row * resStride + col * 4]     = 0;
                        resultValues[row * resStride + col * 4 + 1] = 0;
                        resultValues[row * resStride + col * 4 + 2] = 0;
                        resultValues[row * resStride + col * 4 + 3] = 0;
                    }
                    else if (row < h1 && col < w1)
                    {
                        // This image has values here but the compare grid doesn't
                        resultValues[row * resStride + col * 4]     = Values[row * stride1 + col * 4];
                        resultValues[row * resStride + col * 4 + 1] = Values[row * stride1 + col * 4 + 1];
                        resultValues[row * resStride + col * 4 + 2] = Values[row * stride1 + col * 4 + 2];
                        resultValues[row * resStride + col * 4 + 3] = Values[row * stride1 + col * 4 + 3];
                    }
                    else if (row < h2 && col < w2)
                    {
                        // The compare grid has values here but this grid doesn't
                        resultValues[row * resStride + col * 4]     = compareValues[row * stride2 + col * 4];
                        resultValues[row * resStride + col * 4 + 1] = compareValues[row * stride2 + col * 4 + 1];
                        resultValues[row * resStride + col * 4 + 2] = compareValues[row * stride2 + col * 4 + 2];
                        resultValues[row * resStride + col * 4 + 3] = compareValues[row * stride2 + col * 4 + 3];
                    }
                }
            }

            return(result);
        }
Example #5
0
 /// <summary>
 /// By default, all the bands have a difference comparison done. This may not have the desired effect
 /// because the alpha channel for most colors is actually just 255. The mathematical difference would
 /// be zero, resulting in an output image that, while being the actual difference, is essentially
 /// entirely invisibile (except where the images don't overlap.)
 /// </summary>
 /// <param name="compare">The BitmapGrid this BitmapGrid should be compared to.</param>
 /// <param name="ignoreAlpha">Indicate whether the alpha value should be ignored.</param>
 /// <returns>A BitmapGrid with byte values calculated by taking the difference between the two grids.</returns>
 public BitmapGrid Difference(BitmapGrid compare, bool ignoreAlpha)
 {
     return(DoDifference(compare, ignoreAlpha));
 }
Example #6
0
 /// <summary>
 /// This will calculate the difference. The width and height of the new grid will be the larger
 /// of the rows and columns between the compare grid and this grid. Values outside the range
 /// of one of the grids will simply be filled in as the value stored in the other grid. Because
 /// byte values can't be negative, the difference will be the absolute value.
 /// </summary>
 /// <param name="compare">The BitmapGrid this BitmapGrid should be compared to.</param>
 /// <returns>A BitmapGrid with byte values calculated by taking the difference between the two grids.</returns>
 public BitmapGrid Difference(BitmapGrid compare)
 {
     return(DoDifference(compare, false));
 }