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); } } }
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); }
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); } }
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); } }
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); } } }
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); } }
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; }
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); }
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); }
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); }
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); }