public void WriteRGBToImage(string path, RGB[,] matrix)
 {
     var width = matrix.GetLength(0);
     var height = matrix.GetLength(1);
     using (var image = new Bitmap(width, height, PixelFormat.Format24bppRgb))
     {
         var data = image.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly,
                                   PixelFormat.Format24bppRgb);
         unsafe
         {
             var pointer = (byte*)data.Scan0;
             for (int i = 0; i < width; i++)
             {
                 for (int j = 0; j < height; j++)
                 {
                     pointer[0] = (byte)matrix[i, j].R;
                     pointer[1] = (byte)matrix[i, j].G;
                     pointer[2] = (byte)matrix[i, j].B;
                     pointer += 3;
                 }
             }
         }
         image.UnlockBits(data);
         image.Save(path);
     }
 }
        public RGB[,] ReadRGBFromImage(Stream stream)
        {
            using (var image = new Bitmap(stream))
            {
                int width = (int)(8 * Math.Ceiling(image.Width / 8.0));
                int height = (int)(8 * Math.Ceiling(image.Height / 8.0));
                var superBlock = new RGB[width, height];
                BitmapData blockData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);

                unsafe
                {
                    var pointer = (byte*)blockData.Scan0;

                    for (int i = 0; i < blockData.Width; i++)
                    {
                        for (int j = 0; j < blockData.Height; j++)
                        {
                            superBlock[i, j].R = pointer[0];
                            superBlock[i, j].G = pointer[1];
                            superBlock[i, j].B = pointer[2];
                            pointer += 3;
                        }
                        pointer += blockData.Stride - (blockData.Width * 3);
                    }
                }
                image.UnlockBits(blockData);

                return superBlock;
            }
        }
 public static YCrCb Parse(RGB rgb)
 {
     return new YCrCb
                {
                    Y = calculateY(rgb),
                    Cr = calculateCr(rgb),
                    Cb = calculateCb(rgb),
                };
 }
 public static void PrintMatrix(string name, RGB [,] matrix)
 {
     Console.WriteLine(name);
     for (int i = 0; i < matrix.GetLength(0); i++)
     {
         for (int j = 0; j < matrix.GetLength(1); j++)
         {
             Console.Write((matrix[i, j].B).ToString().PadLeft(10, ' '));
         }
         Console.WriteLine();
     }
     Console.WriteLine();
 }
        public static YCrCb[,] Parse(RGB[,] rgb)
        {
            int m = rgb.GetLength(0);
            int n = rgb.GetLength(1);
            var matrix = new YCrCb[m, n];

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = Parse(rgb[i, j]);
                }
            }
            return matrix;
        }
Beispiel #6
0
        public static RGB[,] Parse(YCrCb[,] yCrCb)
        {
            int m = yCrCb.GetLength(0);
            int n = yCrCb.GetLength(1);
            var matrix = new RGB[m, n];

            for (int i = 0; i < m; i++)
            {
                for (int j = 0; j < n; j++)
                {
                    matrix[i, j] = Parse(yCrCb[i, j]);
                }
            }

            return matrix;
        }
 private static double calculateY(RGB rgb)
 {
     return Math.Round(rgb.R * 0.299 + rgb.G * 0.587 + rgb.B * 0.114);
 }
 private static double calculateCr(RGB rgb)
 {
     return Math.Round(128 + (0.5 * rgb.R - 0.419 * rgb.G - 0.081 * rgb.B));
 }
 private static double calculateCb(RGB rgb)
 {
     return Math.Round(128 + (-0.169 * rgb.R - 0.331 * rgb.G + 0.5 * rgb.B));
 }