예제 #1
0
        public static IPNMReader GetIPNMReader(PNMType ptype)
        {
            IPNMReader imReader = null;

            switch (ptype)
            {
            case PNMType.PBM:
                imReader = new PBMReader();
                break;

            case PNMType.PGM:
                imReader = new PGMReader();
                break;

            case PNMType.PPM:
                imReader = new PPMReader();
                break;
            }

            return(imReader);
        }
예제 #2
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));
        }