public static RGBImage FitToBlockSize(RGBImage image, int blockSize) { int oldHeight = image.Height; int oldWidth = image.Width; int newHeight = (int)Math.Ceiling((float)image.Height / blockSize); newHeight *= blockSize; int newWidth = (int)Math.Ceiling((float)image.Width / blockSize); newWidth *= blockSize; if (oldHeight == newHeight && oldWidth == newWidth) { return(image); } var oldPixels = image.Pixels; var newPixels = new RGBPixel[newWidth, newHeight]; var stubPixel = new RGBPixel(0, 0, 0); for (var i = 0; i < newWidth; i++) { for (var j = 0; j < newHeight; j++) { if (i < oldWidth && j < oldHeight) { newPixels[i, j] = oldPixels[i, j]; } else { newPixels[i, j] = stubPixel; } } } return(new RGBImage(newPixels)); }
public static RGBPixel[,] toRGBMatrix(Image image) { int height = image.Height; int width = image.Width; RGBPixel[,] result = new RGBPixel[width, height]; Rectangle rect = new Rectangle(0, 0, width, height); BitmapData bitmapData = ((Bitmap)image).LockBits(rect, ImageLockMode.ReadOnly, image.PixelFormat); IntPtr ptr = bitmapData.Scan0; byte[] bytes = new byte[Math.Abs(bitmapData.Stride) * height]; Marshal.Copy(ptr, bytes, 0, bytes.Length); ((Bitmap)image).UnlockBits(bitmapData); var pixelSize = Image.GetPixelFormatSize(image.PixelFormat) / 8; var pointer = 0; var k = 0; for (int j = 0; j < height; j++) { pointer = bitmapData.Stride * j; for (int i = 0; i < width; i++) { result[i, j] = new RGBPixel(bytes[pointer + k + 2], bytes[pointer + k + 1], bytes[pointer + k]); k += pixelSize; } k = 0; } return(result); }
public static RGBImage FromMatrix(RGBImage[,] matrix) { var w = matrix.GetLength(0); var h = matrix.GetLength(1); var pixels = new RGBPixel[w * 16, h * 16]; for (int i = 0; i < w; i++) { for (int j = 0; j < h; j++) { for (int k = 0; k < 16; k++) { for (int t = 0; t < 16; t++) { pixels[i * 16 + k, j * 16 + t] = matrix[i, j].Pixels[k, t]; } } } } return(new RGBImage(pixels)); }