コード例 #1
0
        public static Bitmap ImageToBitmap(GrayscaleFloatImageFormat 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] < 0.0f ? (byte)0 : image[i, j] > 255.0f ? (byte)255 : (byte)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);
        }
コード例 #2
0
 public static void ImageToFile(GrayscaleFloatImageFormat image, string filename)
 {
     using (Bitmap b = ImageToBitmap(image))
         b.Save(filename);
 }
コード例 #3
0
        static GrayscaleFloatImageFormat ReadPGM(string filename)
        {
            FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);

            try
            {
                while (true)
                {
                    string str = ReadString(fs).Trim();
                    if (str == null)
                    {
                        return(null);
                    }
                    else if (str == "" || str.StartsWith("#"))
                    {
                        continue;
                    }
                    else if (str != "P5")
                    {
                        return(null);
                    }
                    else
                    {
                        break;
                    }
                }

                int Width = -1, Height = -1, MaxL = -1;

                while (true)
                {
                    string str = ReadString(fs).Trim();
                    if (str == null)
                    {
                        return(null);
                    }
                    else if (str == "" || str.StartsWith("#"))
                    {
                        continue;
                    }

                    string[] arr = str.Split(' ', '\t');
                    Width  = int.Parse(arr[0]);
                    Height = int.Parse(arr[1]);

                    break;
                }

                while (true)
                {
                    string str = ReadString(fs).Trim();
                    if (str == null)
                    {
                        return(null);
                    }
                    else if (str == "" || str.StartsWith("#"))
                    {
                        continue;
                    }

                    MaxL = int.Parse(str);

                    break;
                }

                GrayscaleFloatImageFormat res = new GrayscaleFloatImageFormat(Width, Height);

                if (MaxL <= 255)
                {
                    byte[] raw = new byte[Width * Height];
                    fs.Read(raw, 0, raw.Length);
                    for (int j = 0; j < Height; j++)
                    {
                        for (int i = 0; i < Width; i++)
                        {
                            res[i, j] = raw[j * Width + i];
                        }
                    }
                }
                else
                {
                    byte[] raw = new byte[Width * Height * 2];

                    fs.Read(raw, 0, raw.Length * 2);

                    for (int j = 0; j < Height; j++)
                    {
                        for (int i = 0; i < Width; i++)
                        {
                            res[i, j] = raw[(j * Width + i) * 2] + raw[(j * Width + i) * 2 + 1] * 255;
                        }
                    }
                }

                return(res);
            }
            finally
            {
                fs.Close();
            }
        }