Пример #1
0
        /// <summary>Places the source array into target array.
        /// Its like copy a bitmap into a bitmap at a specific location.</summary>
        /// <param name="sourceArray">The source array.</param>
        /// <param name="targetArray">The target array.</param>
        /// <param name="placeAtX">The place at x.</param>
        /// <param name="placeAtY">The place at y.</param>
        private static void PlaceSourceColorMapIntoTargetColorMap(ColorMap sourceArray, ref ColorMap targetArray, int placeAtX, int placeAtY)
        {
            // get the source pixel
            for (var x = 0; x < sourceArray.Width; x++)
            {
                for (var y = 0; y < sourceArray.Height; y++)
                {
                    var sourcePixel = sourceArray.Get(x, y);

                    targetArray.TrySet(placeAtX + x, placeAtY + y, sourcePixel);
                }
            }
        }
Пример #2
0
        /// <summary>Create a new color-map with a vertical flip of given color-map.</summary>
        /// <param name="source">The source.</param>
        /// <returns>A vertical flipped color-map.</returns>
        private static ColorMap FlipColorMapHVertical(ColorMap source)
        {
            var target = new ColorMap(source.Width, source.Height);

            for (int x = 0; x < source.Width; x++)
            {
                for (int y = 0; y < source.Height; y++)
                {
                    var sourcePixel = source.Get(x, y);
                    target.Set(x, (source.Height - 1) - y, sourcePixel);
                }
            }

            return(target);
        }
Пример #3
0
        /// <summary>Create a new color-map with a vertical flip of given color-map.</summary>
        /// <param name="source">The source.</param>
        /// <returns>A vertical flipped color-map.</returns>
        private static ColorMap FlipColorMapHVertical(ColorMap source)
        {
            var target = new ColorMap(source.Width, source.Height);
            for (int x = 0; x < source.Width; x++)
            {
                for (int y = 0; y < source.Height; y++)
                {
                    var sourcePixel = source.Get(x, y);
                    target.Set(x, (source.Height - 1) - y, sourcePixel);
                }
            }

            return target;
        }
Пример #4
0
        /// <summary>Places the source array into target array.
        /// Its like copy a bitmap into a bitmap at a specific location.</summary>
        /// <param name="sourceArray">The source array.</param>
        /// <param name="targetArray">The target array.</param>
        /// <param name="placeAtX">The place at x.</param>
        /// <param name="placeAtY">The place at y.</param>
        private static void PlaceSourceColorMapIntoTargetColorMap(ColorMap sourceArray, ref ColorMap targetArray, int placeAtX, int placeAtY)
        {
            // get the source pixel
            for (var x = 0; x < sourceArray.Width; x++)
            {
                for (var y = 0; y < sourceArray.Height; y++)
                {
                    var sourcePixel = sourceArray.Get(x, y);

                    targetArray.TrySet(placeAtX + x, placeAtY + y, sourcePixel);
                }
            }
        }
Пример #5
0
        /// <summary>Resizes the color-map to the given target size.</summary>
        /// <param name="sourceArray">The source array.</param>
        /// <param name="targetWidth">Width of the target.</param>
        /// <param name="targetHeight">Height of the target.</param>
        /// <returns>A color-map with given target size.</returns>
        private static ColorMap ResizeImage(ColorMap sourceArray, int targetWidth, int targetHeight)
        {
            var targetArray = new ColorMap(targetWidth, targetHeight);

            // walk trough every pixel on the target
            // find out the area that the a pixel on target is covering on the source
            // average the source area into one pixel to be used on the target
            //
            // the way i am looking at it is that you have a target (emitting) rectangle standing on a table
            // and somewhere further in the distance you have a larger source (receiving) rectangle standing on a table
            // we are shooting beams trough the target (emitting) rectangle
            // those hit the source (receiving) rectangle at a given position
            // we use those hit-points to calculate what the color will be in the target rectangle
            for (int targetX = 0; targetX < targetWidth; targetX++)
            {
                for (int targetY = 0; targetY < targetHeight; targetY++)
                {
                    // we walk trough every point in the target rectangle

                    // find out, how big the source rectangle is
                    var sourceAreaWidth = (float)sourceArray.Width / targetWidth;
                    var sourceAreaHeight = (float)sourceArray.Height / targetHeight;

                    // find the center of the source rectangle (receiver) on the table
                    var percentageTargetX = (float)targetX / targetWidth;
                    var percentageTargetY = (float)targetY / targetHeight;
                    var sourceCentreX = Lerp(0, sourceArray.Width, percentageTargetX);
                    var sourceCentreY = Lerp(0, sourceArray.Height, percentageTargetY);

                    // get the array of pixels on the source rectangle , that we will use for averaging my location on the target rectangle
                    var sourceLeft = Math.Floor(sourceCentreX - (sourceAreaWidth * 0.5f));
                    var sourceRight = Math.Ceiling(sourceCentreX + (sourceAreaWidth * 0.5f));
                    var sourceTop = Math.Floor(sourceCentreY - (sourceAreaHeight * 0.5f));
                    var sourceBottom = Math.Ceiling(sourceCentreY + (sourceAreaHeight * 0.5f));

                    // get all the colors in the source rectangle that are targeted
                    var colorToAverage = new List<GUIColor>();
                    for (var parseX = (int)sourceLeft; parseX < sourceRight; parseX++)
                    {
                        for (var parseY = (int)sourceTop; parseY < sourceBottom; parseY++)
                        {
                            if (parseX < 0 || parseX > sourceArray.Width || parseY < 0 || parseY > sourceArray.Height)
                            {
                                continue;
                            }

                            var sourceColor = sourceArray.Get(parseX, parseY);
                            colorToAverage.Add(sourceColor);
                        }
                    }

                    // average those colors into the color that will be used in the target rectangle
                    float r = 0;
                    float g = 0;
                    float b = 0;
                    float a = 0;
                    for (var index = 0; index < colorToAverage.Count; index++)
                    {
                        var clr = colorToAverage[index];
                        r = r + clr.R;
                        g = g + clr.G;
                        b = b + clr.B;
                        a = a + clr.A;
                    }

                    r = r / colorToAverage.Count;
                    g = g / colorToAverage.Count;
                    b = b / colorToAverage.Count;
                    a = a / colorToAverage.Count;

                    targetArray.Set(targetX, targetY, new GUIColor((byte)r, (byte)g, (byte)b, (byte)a));
                }
            }

            return targetArray;
        }
Пример #6
0
        /// <summary>Resizes the color-map to the given target size.</summary>
        /// <param name="sourceArray">The source array.</param>
        /// <param name="targetWidth">Width of the target.</param>
        /// <param name="targetHeight">Height of the target.</param>
        /// <returns>A color-map with given target size.</returns>
        private static ColorMap ResizeImage(ColorMap sourceArray, int targetWidth, int targetHeight)
        {
            var targetArray = new ColorMap(targetWidth, targetHeight);

            // walk trough every pixel on the target
            // find out the area that the a pixel on target is covering on the source
            // average the source area into one pixel to be used on the target
            //
            // the way i am looking at it is that you have a target (emitting) rectangle standing on a table
            // and somewhere further in the distance you have a larger source (receiving) rectangle standing on a table
            // we are shooting beams trough the target (emitting) rectangle
            // those hit the source (receiving) rectangle at a given position
            // we use those hit-points to calculate what the color will be in the target rectangle
            for (int targetX = 0; targetX < targetWidth; targetX++)
            {
                for (int targetY = 0; targetY < targetHeight; targetY++)
                {
                    // we walk trough every point in the target rectangle

                    // find out, how big the source rectangle is
                    var sourceAreaWidth  = (float)sourceArray.Width / targetWidth;
                    var sourceAreaHeight = (float)sourceArray.Height / targetHeight;

                    // find the center of the source rectangle (receiver) on the table
                    var percentageTargetX = (float)targetX / targetWidth;
                    var percentageTargetY = (float)targetY / targetHeight;
                    var sourceCentreX     = Lerp(0, sourceArray.Width, percentageTargetX);
                    var sourceCentreY     = Lerp(0, sourceArray.Height, percentageTargetY);

                    // get the array of pixels on the source rectangle , that we will use for averaging my location on the target rectangle
                    var sourceLeft   = Math.Floor(sourceCentreX - (sourceAreaWidth * 0.5f));
                    var sourceRight  = Math.Ceiling(sourceCentreX + (sourceAreaWidth * 0.5f));
                    var sourceTop    = Math.Floor(sourceCentreY - (sourceAreaHeight * 0.5f));
                    var sourceBottom = Math.Ceiling(sourceCentreY + (sourceAreaHeight * 0.5f));

                    // get all the colors in the source rectangle that are targeted
                    var colorToAverage = new List <GUIColor>();
                    for (var parseX = (int)sourceLeft; parseX < sourceRight; parseX++)
                    {
                        for (var parseY = (int)sourceTop; parseY < sourceBottom; parseY++)
                        {
                            if (parseX < 0 || parseX > sourceArray.Width || parseY < 0 || parseY > sourceArray.Height)
                            {
                                continue;
                            }

                            var sourceColor = sourceArray.Get(parseX, parseY);
                            colorToAverage.Add(sourceColor);
                        }
                    }

                    // average those colors into the color that will be used in the target rectangle
                    float r = 0;
                    float g = 0;
                    float b = 0;
                    float a = 0;
                    for (var index = 0; index < colorToAverage.Count; index++)
                    {
                        var clr = colorToAverage[index];
                        r = r + clr.R;
                        g = g + clr.G;
                        b = b + clr.B;
                        a = a + clr.A;
                    }

                    r = r / colorToAverage.Count;
                    g = g / colorToAverage.Count;
                    b = b / colorToAverage.Count;
                    a = a / colorToAverage.Count;

                    targetArray.Set(targetX, targetY, new GUIColor((byte)r, (byte)g, (byte)b, (byte)a));
                }
            }

            return(targetArray);
        }