Example #1
0
        private static void PadBorders(PNM image, int padding)
        {
            // copy top and bottom
            for (int i = 0; i < padding; i++)
            {
                Buffer.BlockCopy(image.raster, 3 * (padding * image.Width + padding), image.raster, 3 * (i * image.Width + padding), (image.Width - padding * 2) * 3);
                Buffer.BlockCopy(image.raster, 3 * ((image.Height - padding - 1) * image.Width + padding), image.raster, 3 * ((i + image.Height - padding) * image.Width + padding), (image.Width - padding * 2) * 3);
            }
            // pad right and left
            for (int i = 0; i < image.Height - (2 * padding); i++)
            {
                byte r, g, b;

                image.GetPixel(padding, padding + i, out r, out g, out b);
                for (int j = 0; j < padding; j++)
                {
                    image.SetPixel(j, padding + i, r, g, b);
                }

                image.GetPixel(image.Width - padding - 1, padding + i, out r, out g, out b);
                for (int j = 0; j < padding; j++)
                {
                    image.SetPixel(image.Width - j - 1, padding + i, r, g, b);
                }
            }
        }
Example #2
0
        private static PNM ApplyConvolutionMatrixCore(PNM image, float[] matrix, int matrixLength, float weight, float shift)
        {
            PNM newImage  = new PNM(image.Width, image.Height);
            int padding   = matrixLength / 2;
            int maxHeight = newImage.Height - padding;
            int maxWidth  = newImage.Width - padding;
            int width     = newImage.Width;

            Parallel.For(padding, maxHeight, i =>
            {
                for (int j = padding; j < maxWidth; j++)
                {
                    float sumR = 0;
                    float sumG = 0;
                    float sumB = 0;
                    // current index position
                    int position = i * width + j;
                    for (int m = 0; m < matrixLength; m++)
                    {
                        for (int n = 0; n < matrixLength; n++)
                        {
                            byte r, g, b;
                            image.GetPixel(position - ((padding - m) * width) - (padding - n), out r, out g, out b);
                            float coeff = matrix[(m * matrixLength) + n];
                            sumR       += (r * coeff * weight);
                            sumG       += (g * coeff * weight);
                            sumB       += (b * coeff * weight);
                        }
                    }
                    newImage.SetPixel(position, Coerce(sumR + shift), Coerce(sumG + shift), Coerce(sumB + shift));
                }
            });
            return(newImage);
        }
Example #3
0
 internal static void SaveFile(PNM bitmap, FileStream stream)
 {
     bitmap.WriteLongHeader("P5", stream);
     for (int i = 0; i < bitmap.Height * bitmap.Width; i++)
     {
         byte r,g,b;
         bitmap.GetPixel(i, out r, out g, out b);
         byte pixel = RGBToLuminosity(r, g, b);
         stream.WriteByte(pixel);
     }
 }
Example #4
0
 internal static void SaveFile(PNM bitmap, FileStream stream)
 {
     bitmap.WriteLongHeader("P5", stream);
     for (int i = 0; i < bitmap.Height * bitmap.Width; i++)
     {
         byte r, g, b;
         bitmap.GetPixel(i, out r, out g, out b);
         byte pixel = RGBToLuminosity(r, g, b);
         stream.WriteByte(pixel);
     }
 }
Example #5
0
        private static void PadCorners(PNM image, int padding)
        {
            byte r, g, b;

            // pad top left
            image.GetPixel(padding, padding, out r, out g, out b);
            for (int i = 0; i < padding; i++)
            {
                for (int j = 0; j < padding; j++)
                {
                    image.SetPixel(i, j, r, g, b);
                }
            }
            // pad top right
            image.GetPixel(image.Width - padding - 1, padding, out r, out g, out b);
            for (int i = image.Width - padding; i < image.Width; i++)
            {
                for (int j = 0; j < padding; j++)
                {
                    image.SetPixel(i, j, r, g, b);
                }
            }
            // pad bottom left
            image.GetPixel(padding, image.Height - padding - 1, out r, out g, out b);
            for (int i = 0; i < padding; i++)
            {
                for (int j = image.Height - padding; j < image.Height; j++)
                {
                    image.SetPixel(i, j, r, g, b);
                }
            }
            // pad bottom right
            image.GetPixel(image.Width - padding - 1, image.Height - padding - 1, out r, out g, out b);
            for (int i = image.Width - padding; i < image.Width; i++)
            {
                for (int j = image.Height - padding; j < image.Height; j++)
                {
                    image.SetPixel(i, j, r, g, b);
                }
            }
        }
Example #6
0
 internal static void SaveFile(PNM bitmap, FileStream stream)
 {
     bitmap.WriteLongHeader("P6", stream);
     for (int i = 0; i < bitmap.Height * bitmap.Width; i++)
     {
         byte r, g, b;
         bitmap.GetPixel(i, out r, out g, out b);
         stream.WriteByte(r);
         stream.WriteByte(g);
         stream.WriteByte(b);
     }
 }
Example #7
0
 internal static byte PackBytes(PNM bitmap, int start, int amount)
 {
     int result = 0;
     byte r,g,b;
     for(int i = 0; i< amount; i++)
     {
         bitmap.GetPixel(start + i, out r,out g,out b);
         if (ColorToBlack(r, g, b))
             result |= (128 >> i);
     }
     return (byte)result;
 }
Example #8
0
        internal static void SaveFile(PNM bitmap, FileStream stream)
        {

            bitmap.WriteLongHeader("P6", stream);
            for (int i = 0; i < bitmap.Height * bitmap.Width; i++)
            {
                byte r, g, b;
                bitmap.GetPixel(i, out r, out g, out b);
                stream.WriteByte(r);
                stream.WriteByte(g);
                stream.WriteByte(b);
            }
        }
Example #9
0
        public static PNM ApplyPointProcessing(this PNM oldImage, Func <byte, byte, byte, Pixel> filter)
        {
            PNM  newImage = new PNM(oldImage.Width, oldImage.Height);
            byte r, g, b;
            int  size = oldImage.Width * oldImage.Height;

            for (int i = 0; i < size; i++)
            {
                oldImage.GetPixel(i, out r, out g, out b);
                Pixel pixel = filter(r, g, b);
                newImage.SetPixel(i, pixel.Red, pixel.Green, pixel.Blue);
            }
            ;
            return(newImage);
        }
Example #10
0
        internal static byte PackBytes(PNM bitmap, int start, int amount)
        {
            int  result = 0;
            byte r, g, b;

            for (int i = 0; i < amount; i++)
            {
                bitmap.GetPixel(start + i, out r, out g, out b);
                if (ColorToBlack(r, g, b))
                {
                    result |= (128 >> i);
                }
            }
            return((byte)result);
        }
Example #11
0
        public static PNM ApplyHeightMapFunction(this PNM image, int matrixLength, Func <int, int, float[], int, Pixel> func)
        {
            PNM newImage = PNM.Copy(image);
            int padding  = matrixLength / 2;

            Pad(newImage, padding);
            int newImageSize = newImage.Width * newImage.Height;

            float[] heightmap = new float[newImageSize];
            byte    r, g, b;

            for (int i = 0; i < newImageSize; i++)
            {
                newImage.GetPixel(i, out r, out g, out b);
                heightmap[i] = PNM.RGBToLuminosity(r, g, b) / 255f;
            }
            newImage = ApplyHeightMapFunctionCore(newImage.Width, newImage.Height, heightmap, matrixLength, func);
            Trim(newImage, padding);
            return(newImage);
        }
Example #12
0
        internal static Tuple <float[], float[], float[]> ApplyConvolutionUnbound(PNM image, float[] matrix, int matrixLength)
        {
            int padding   = matrixLength / 2;
            int oldHeight = image.Height - (padding * 2);
            int oldWidth  = image.Width - (padding * 2);
            Tuple <float[], float[], float[]> rasters = Tuple.Create(new float[oldHeight * oldWidth],
                                                                     new float[oldHeight * oldWidth],
                                                                     new float[oldHeight * oldWidth]);
            int maxHeight = image.Height - padding;
            int maxWidth  = image.Width - padding;

            Parallel.For(padding, maxHeight, i =>
            {
                int index = (i - padding) * oldWidth;
                for (int j = padding; j < maxWidth; j++)
                {
                    float sumR = 0;
                    float sumG = 0;
                    float sumB = 0;
                    // current index position
                    int position = i * image.Width + j;
                    for (int m = 0; m < matrixLength; m++)
                    {
                        for (int n = 0; n < matrixLength; n++)
                        {
                            byte r, g, b;
                            image.GetPixel(position - ((padding - m) * image.Width) - (padding - n), out r, out g, out b);
                            float coeff = matrix[(m * matrixLength) + n];
                            sumR       += r * coeff;
                            sumG       += g * coeff;
                            sumB       += b * coeff;
                        }
                    }
                    rasters.Item1[index] = sumR;
                    rasters.Item2[index] = sumG;
                    rasters.Item3[index] = sumB;
                    index++;
                }
            });
            return(rasters);
        }