Exemplo n.º 1
0
        public static Bitmap ImageToBitmap(GrayscaleByteImage image)
        {
            Bitmap B = new Bitmap(image.Width, image.Height, PixelFormat.Format24bppRgb);

            LockBitmapInfo lbi = LockBitmap(B);

            try
            {
                for (int j = 0; j < image.Height; j++)
                {
                    for (int i = 0; i < image.Width; i++)
                    {
                        byte c = image[i, j];
                        lbi.data[lbi.linewidth * j + i * 4]     = c;
                        lbi.data[lbi.linewidth * j + i * 4 + 1] = c;
                        lbi.data[lbi.linewidth * j + i * 4 + 2] = c;
                    }
                }
            }
            finally
            {
                UnlockBitmap(lbi);
            }

            return(B);
        }
Exemplo n.º 2
0
        public static GrayscaleByteImage FileToGrayscaleByteImage(string filename)
        {
            if (CheckPGM(filename))
            {
                return(ReadPGM(filename).ToGrayscaleByteImage());
            }

            Bitmap             B   = new Bitmap(filename);
            GrayscaleByteImage res = BitmapToGrayscaleByteImage(B);

            B.Dispose();
            return(res);
        }
Exemplo n.º 3
0
        public GrayscaleByteImage getImage()
        {
            ulong maxvalue = counts.Max();
            var   img      = new GrayscaleByteImage(256, (int)(maxvalue / 255 + 5));

            for (int i = 0; i < 256; i++)
            {
                var tmp         = counts[i];
                int countPixels = (int)(tmp / 255);
                for (int j = 0; j < countPixels; j++)
                {
                    img[i, j] = 255;
                }
                if (tmp % 255 != 0)
                {
                    img[i, countPixels + 1] = (byte)(tmp % 255);
                }
            }
            return(img);
        }
Exemplo n.º 4
0
        public byte[] Load_Byte_Image(string path)
        {
            GrayscaleByteImage image = ImageIO.FileToGrayscaleByteImage(path);

            return(image.rawdata);
        }
Exemplo n.º 5
0
 public static void ImageToFile(GrayscaleByteImage image, string filename)
 {
     using (Bitmap B = ImageToBitmap(image))
         B.Save(filename);
 }
Exemplo n.º 6
0
        public static GrayscaleByteImage BitmapToGrayscaleByteImage(Bitmap B)
        {
            int W = B.Width, H = B.Height;
            GrayscaleByteImage res = new GrayscaleByteImage(W, H);

            if (B.PixelFormat == PixelFormat.Format8bppIndexed)
            {
                Color[] pi  = B.Palette.Entries;
                byte[]  pal = new byte[1024];
                for (int i = 0; i < pi.Length; i++)
                {
                    Color C = pi[i];
                    pal[i * 4]     = C.B;
                    pal[i * 4 + 1] = C.G;
                    pal[i * 4 + 2] = C.R;
                    pal[i * 4 + 3] = C.A;
                }

                LockBitmapInfo lbi = LockBitmap(B, PixelFormat.Format8bppIndexed, 1);
                try
                {
                    for (int j = 0; j < H; j++)
                    {
                        for (int i = 0; i < W; i++)
                        {
                            int c = lbi.data[lbi.linewidth * j + i];
                            int b = pal[c * 4];
                            int g = pal[c * 4 + 1];
                            int r = pal[c * 4 + 2];
                            res[i, j] = (byte)(0.114f * b + 0.587f * g + 0.299f * r);
                        }
                    }
                }
                finally
                {
                    UnlockBitmap(lbi);
                }
            }
            else
            {
                LockBitmapInfo lbi = LockBitmap(B);
                try
                {
                    for (int j = 0; j < H; j++)
                    {
                        for (int i = 0; i < W; i++)
                        {
                            int b = lbi.data[lbi.linewidth * j + i * 4];
                            int g = lbi.data[lbi.linewidth * j + i * 4 + 1];
                            int r = lbi.data[lbi.linewidth * j + i * 4 + 2];
                            res[i, j] = (byte)(0.114f * b + 0.587f * g + 0.299f * r);
                        }
                    }
                }
                finally
                {
                    UnlockBitmap(lbi);
                }
            }

            return(res);
        }