Example #1
0
        public System.Drawing.Image ReadImageData(IPNMDataReader dr, int width, int height)
        {
            try
            {
                int    x, y;
                byte   val;
                Bitmap im;

                im = new Bitmap(width, height);

                for (y = 0; y < height; y++)
                {
                    for (x = 0; x < width; x++)
                    {
                        //writing 2D matrix of pixles
                        val = dr.ReadByte();
                        im.SetPixel(x, y, Color.FromArgb(val, val, val));
                    }
                }

                return(im);
            }
            catch
            {
                throw;
            }
            finally
            {
                dr.Close();
            }
        }
Example #2
0
        public PGM(string path)
        {
            if (!File.Exists(path))
            {
                return;
            }
            string       line;
            int          lineNumber = 0;
            int          byteCount  = 0;
            StreamReader sr         = new StreamReader(path);

            while ((line = sr.ReadLine().Trim()) != null)
            {
                if (line.StartsWith("#"))
                {
                    continue;
                }
                lineNumber++;
                switch (lineNumber)
                {
                case 1:
                    binary = line.Equals(P5);
                    if (binary)
                    {
                        reader = new BinaryDataReader(new FileStream(path, FileMode.Open));
                    }
                    else
                    {
                        reader = new ASCIIDataReader(new FileStream(path, FileMode.Open));
                    }
                    break;

                case 2:
                    string[] aux = line.Split(' ');
                    width  = int.Parse(aux[0]);
                    height = int.Parse(aux[1]);
                    break;

                case 3:
                    white = int.Parse(line);
                    break;

                default:
                    return;
                }
            }
        }
Example #3
0
        public static IPNMDataReader GetIPNMDataReader
            (FileStream fs, PNMEncoding encoding)
        {
            IPNMDataReader dr = null;

            switch (encoding)
            {
            case PNMEncoding.ASCIIEncoding:
                dr = new ASCIIDataReader(fs);
                break;

            case PNMEncoding.BinaryEncoding:
                dr = new BinaryDataReader(fs);
                break;
            }

            return(dr);
        }
Example #4
0
        public static System.Drawing.Image ReadPNM(string FilePath)
        {
            char   fchar;
            int    max, width, height;
            string line, ftype;

            FileStream fs = new FileStream
                                (FilePath, FileMode.Open, FileAccess.Read, FileShare.Read);

            /* Read first line, and test if it is propoer PNM type */
            line  = ((char)fs.ReadByte()).ToString() + ((char)fs.ReadByte()).ToString();
            ftype = line;
            //read off the endline char
            fs.ReadByte();

            PNMEncoding    encoding = PNMFactory.GetPNMEncoding(ftype);
            IPNMDataReader dr       = PNMFactory.GetIPNMDataReader(fs, encoding);

            if (dr == null)
            {
                throw new Exception
                          ("Currently only Binary and ASCII encoding is supported");
            }

            IPNMReader imReader = PNMFactory.GetIPNMReader(PNMFactory.GetPNMType(ftype));

            if (imReader == null)
            {
                throw new Exception
                          ("Currently only PBM, PGM and PNM Image Types are supported");
            }

            /* Read lines, ignoring those starting with Comment Character, until the
             *      Image Dimensions are read. */
            do
            {
                //read first char to determine if its a comment
                line = dr.ReadLine();
                if (line.Length == 0)
                {
                    fchar = '#';
                }
                else
                {
                    fchar = line.Substring(0, 1).ToCharArray(0, 1)[0];
                }
            }while(fchar == '#');

            string[] toks = line.Split(new char[] { ' ' });
            //read height and width
            width  = int.Parse(toks[0]);
            height = int.Parse(toks[1]);

            if (ftype != "P1")
            {
                /* Read lines, ignoring those starting with Comment Character, until the
                 *      maximum pixel value is read. */
                do
                {
                    //read first char to determine if its a comment
                    line = dr.ReadLine();
                    if (line.Length == 0)
                    {
                        fchar = '#';
                    }
                    else
                    {
                        fchar = line.Substring(0, 1).ToCharArray(0, 1)[0];
                    }
                }while(fchar == '#');

                max = int.Parse(line);

                if (!(max == 255))
                {
                    Console.WriteLine
                        ("Warning, max value for pixels in this image is not 255");
                }
            }

            return(imReader.ReadImageData(dr, width, height));
        }