Ejemplo n.º 1
0
        /// <summary>
        /// Transform this image to a greyscale version out-of-place
        /// </summary>
        /// <param name="source">The source image</param>
        /// <param name="output">The image to write to</param>
        /// <returns>The output writable lockbit image</returns>
        public static WritableLockBitImage Transform(WritableLockBitImage source, WritableLockBitImage output)
        {
            if (source.Locked || output.Locked)
            {
                throw new ArgumentException("Lockbit image is locked.");
            }

            for (int y = 0; y < source.Height; y++)
            {
                for (int x = 0; x < source.Width; x++)
                {
                    Color sourcePixel = source.GetPixel(x, y);

                    int greyColor =
                        (int)Math.Floor(sourcePixel.R * 0.299) +
                        (int)Math.Floor(sourcePixel.G * 0.587) +
                        (int)Math.Floor(sourcePixel.B * 0.114);

                    Color greyScale = Color.FromArgb(
                        greyColor,
                        greyColor,
                        greyColor
                        );
                    output.SetPixel(x, y, greyScale);
                }
            }

            return(output);
        }
Ejemplo n.º 2
0
 private static void CopyPixels(WritableLockBitImage sourceImage, WritableLockBitImage destImage)
 {
     for (int row = 0; row < sourceImage.Height; row++)
     {
         for (int col = 0; col < sourceImage.Width; col++)
         {
             destImage.SetPixel(col, row, sourceImage.GetPixel(col, row));
         }
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Quantisize a photo by ceiling or flooring each value depending one whether it's above or below the median value
        /// </summary>
        /// <param name="sourceImage">The source image</param>
        /// <param name="outputImage">The image to write to</param>
        /// <param name="maxValue">The value to quantize the maximum value to</param>
        /// <returns>The output image, but returned for convenience</returns>
        public static WritableLockBitImage Transform(WritableLockBitImage sourceImage, WritableLockBitImage outputImage, byte maxValue = byte.MaxValue)
        {
            Color medianColor = GetMedianColorValue(sourceImage);

            for (int row = 0; row < sourceImage.Height; row++)
            {
                for (int col = 0; col < sourceImage.Width; col++)
                {
                    // Quantisize the individual channels
                    Color currentColor = sourceImage.GetPixel(col, row);
                    byte  outputRed, outputGreen, outputBlue;
                    if (currentColor.R < medianColor.R)
                    {
                        outputRed = 0;
                    }
                    else
                    {
                        outputRed = maxValue;
                    }

                    if (currentColor.G < medianColor.G)
                    {
                        outputGreen = 0;
                    }
                    else
                    {
                        outputGreen = maxValue;
                    }

                    if (currentColor.B < medianColor.B)
                    {
                        outputBlue = 0;
                    }
                    else
                    {
                        outputBlue = maxValue;
                    }

                    outputImage.SetPixel(col, row, Color.FromArgb(outputRed, outputGreen, outputBlue));
                }
            }

            return(outputImage);
        }
Ejemplo n.º 4
0
        private static WritableLockBitImage CalculateContour(int[] horizontalMatrix, int[] verticalMatrix, int width, int height)
        {
            WritableLockBitImage outputImage = new WritableLockBitImage(width, height);

            for (int row = 0; row < height; row++)
            {
                for (int col = 0; col < width; col++)
                {
                    int  horizontalValue = horizontalMatrix[row * width + col];
                    int  verticalValue   = verticalMatrix[row * width + col];
                    byte pixelValue      = (byte)Math.Round(Math.Sqrt(
                                                                (horizontalValue * horizontalValue) +
                                                                (verticalValue * verticalValue)
                                                                ));

                    outputImage.SetPixel(col, row, Color.FromArgb(pixelValue, pixelValue, pixelValue));
                }
            }
            return(outputImage);
        }
Ejemplo n.º 5
0
        private static void PerformHorizontalBoxBlurAcc(WritableLockBitImage sourceImage, WritableLockBitImage outputImage, int radius)
        {
            double iarr = 1 / ((double)radius + radius + 1);

            for (int row = 0; row < sourceImage.Height; row++)
            {
                Color firstPixel    = sourceImage.GetPixel(0, row);
                Color lastPixel     = sourceImage.GetPixel(sourceImage.Width - 1, row);
                int   cumRedValue   = (radius + 1) * firstPixel.R;
                int   cumGreenValue = (radius + 1) * firstPixel.G;
                int   cumBlueValue  = (radius + 1) * firstPixel.B;

                int currentLastColIndex   = 0;      // li
                int currentRadiusColIndex = radius; // ri

                for (int col = 0; col < radius; col++)
                {
                    Color chosenPixel = sourceImage.GetPixel(col, row);
                    cumRedValue   += chosenPixel.R;
                    cumGreenValue += chosenPixel.G;
                    cumBlueValue  += chosenPixel.B;
                }

                for (int col = 0; col <= radius; col++)
                {
                    Color chosenPixel = sourceImage.GetPixel(currentRadiusColIndex, row);
                    cumRedValue   += chosenPixel.R - firstPixel.R;
                    cumGreenValue += chosenPixel.G - firstPixel.G;
                    cumBlueValue  += chosenPixel.B - firstPixel.B;
                    currentRadiusColIndex++;

                    outputImage.SetPixel(
                        col,
                        row,
                        Color.FromArgb(
                            (int)Math.Round(cumRedValue * iarr),
                            (int)Math.Round(cumGreenValue * iarr),
                            (int)Math.Round(cumBlueValue * iarr)
                            )
                        );
                }

                for (int col = radius + 1; col < sourceImage.Width - radius; col++)
                {
                    Color chosenRadiusPixel = sourceImage.GetPixel(currentRadiusColIndex, row);
                    Color chosenLastPixel   = sourceImage.GetPixel(currentLastColIndex, row);
                    cumRedValue   += chosenRadiusPixel.R - chosenLastPixel.R;
                    cumGreenValue += chosenRadiusPixel.G - chosenLastPixel.G;
                    cumBlueValue  += chosenRadiusPixel.B - chosenLastPixel.B;
                    currentRadiusColIndex++;
                    currentLastColIndex++;

                    outputImage.SetPixel(
                        col,
                        row,
                        Color.FromArgb(
                            (int)Math.Round(cumRedValue * iarr),
                            (int)Math.Round(cumGreenValue * iarr),
                            (int)Math.Round(cumBlueValue * iarr)
                            )
                        );
                }

                for (int col = sourceImage.Width - radius; col < sourceImage.Width; col++)
                {
                    Color chosenLastPixel = sourceImage.GetPixel(currentLastColIndex, row);
                    cumRedValue   += lastPixel.R - chosenLastPixel.R;
                    cumGreenValue += lastPixel.G - chosenLastPixel.G;
                    cumBlueValue  += lastPixel.B - chosenLastPixel.B;
                    currentLastColIndex++;

                    outputImage.SetPixel(
                        col,
                        row,
                        Color.FromArgb(
                            (int)Math.Round(cumRedValue * iarr),
                            (int)Math.Round(cumGreenValue * iarr),
                            (int)Math.Round(cumBlueValue * iarr)
                            )
                        );
                }
            }
        }
Ejemplo n.º 6
0
        private static void PerformTotalBoxBlurAcc(WritableLockBitImage sourceImage, WritableLockBitImage outputImage, int radius)
        {
            double iarr = 1 / ((double)radius + radius + 1);

            for (int col = 0; col < sourceImage.Width; col++)
            {
                Color topPixel    = sourceImage.GetPixel(col, 0);
                Color bottomPixel = sourceImage.GetPixel(col, sourceImage.Height - 1);

                int cumRedValue   = (radius + 1) * topPixel.R;
                int cumGreenValue = (radius + 1) * topPixel.G;
                int cumBlueValue  = (radius + 1) * topPixel.B;

                for (int row = 0; row < radius; row++)
                {
                    Color chosenPixel = sourceImage.GetPixel(col, row);
                    cumRedValue   += chosenPixel.R;
                    cumGreenValue += chosenPixel.G;
                    cumBlueValue  += chosenPixel.B;
                }

                for (int row = 0; row <= radius; row++)
                {
                    Color chosenPixel = sourceImage.GetPixel(col, row + radius);
                    cumRedValue   += chosenPixel.R - topPixel.R;
                    cumGreenValue += chosenPixel.G - topPixel.G;
                    cumBlueValue  += chosenPixel.B - topPixel.B;

                    outputImage.SetPixel(
                        col,
                        row,
                        Color.FromArgb(
                            (int)Math.Round(cumRedValue * iarr),
                            (int)Math.Round(cumGreenValue * iarr),
                            (int)Math.Round(cumBlueValue * iarr)
                            )
                        );
                }

                for (int row = radius + 1; row < sourceImage.Height - radius; row++)
                {
                    Color radiusPixel  = sourceImage.GetPixel(col, radius + row);
                    Color laggingPixel = sourceImage.GetPixel(col, row - radius - 1);
                    cumRedValue   += radiusPixel.R - laggingPixel.R;
                    cumGreenValue += radiusPixel.G - laggingPixel.G;
                    cumBlueValue  += radiusPixel.B - laggingPixel.B;

                    outputImage.SetPixel(
                        col,
                        row,
                        Color.FromArgb(
                            (int)Math.Round(cumRedValue * iarr),
                            (int)Math.Round(cumGreenValue * iarr),
                            (int)Math.Round(cumBlueValue * iarr)
                            )
                        );
                }

                for (int row = sourceImage.Height - radius; row < sourceImage.Height; row++)
                {
                    Color laggingPixel = sourceImage.GetPixel(col, row - radius);
                    cumRedValue   += bottomPixel.R - laggingPixel.R;
                    cumGreenValue += bottomPixel.G - laggingPixel.G;
                    cumBlueValue  += bottomPixel.B - laggingPixel.B;

                    outputImage.SetPixel(
                        col,
                        row,
                        Color.FromArgb(
                            (int)Math.Round(cumRedValue * iarr),
                            (int)Math.Round(cumGreenValue * iarr),
                            (int)Math.Round(cumBlueValue * iarr)
                            )
                        );
                }
            }
        }