PGM格式图像文件读写

        private int mWidth;
        private int mLength;
        private int mColor;
        private string mType;
        private string mComments;
        private byte[] mData;

        public void ReadPGM(string filePath)
        {
            FileStream InputStream = File.OpenRead(filePath);
            BinaryReader PGMReader = new BinaryReader(InputStream);
            char[] Seperators = { ' ', '\n' };
            byte NewLineAsciiCode = 10;  //另起一行标记
            byte DiezAsciiCode = 35;     //35='#', Comments开始标记
            byte SpaceAsciiCode = 32;    //空格标记
            byte[] TempArray = new byte[1000];
            int i = 0;
            string TempS;
            byte TempByte;
            //read PGM Type P2, P5
            TempArray[0] = PGMReader.ReadByte();
            TempArray[1] = PGMReader.ReadByte();
            this.mType = System.Text.ASCIIEncoding.Default.GetString(TempArray, 0, 2);
            //read until new line
            while (PGMReader.ReadByte() != NewLineAsciiCode) { ;}
            //read comments if exists. Only one comment line supported!!
            i = 0;
            TempArray[i] = PGMReader.ReadByte();
            TempByte = TempArray[i];
            if (TempArray[i] == DiezAsciiCode)//若有标记"#"的Comments
            {
                TempByte = PGMReader.ReadByte();
                while (TempByte != NewLineAsciiCode)
                {
                    TempArray[i++] = TempByte;
                    TempByte = PGMReader.ReadByte();
                }
                this.mComments = System.Text.ASCIIEncoding.Default.GetString(TempArray, 0, i);
                TempByte = PGMReader.ReadByte();
            }
            //read width
            i = 0;
            while (TempByte != SpaceAsciiCode)
            {
                TempArray[i++] = TempByte;
                TempByte = PGMReader.ReadByte();
            }
            TempS = System.Text.ASCIIEncoding.Default.GetString(TempArray, 0, i);
            mWidth = Convert.ToInt32(TempS);
            //read length
            i = 0;
            TempByte = PGMReader.ReadByte();
            while (TempByte != NewLineAsciiCode)
            {
                TempArray[i++] = TempByte;
                TempByte = PGMReader.ReadByte();
            }
            TempS = System.Text.ASCIIEncoding.Default.GetString(TempArray, 0, i);
            mLength = Convert.ToInt32(TempS);
            //read color
            i = 0;
            TempByte = PGMReader.ReadByte();
            while (TempByte != NewLineAsciiCode)
            {
                TempArray[i++] = TempByte;
                TempByte = PGMReader.ReadByte();
            }
            TempS = System.Text.ASCIIEncoding.Default.GetString(TempArray, 0, i);
            this.mColor = Convert.ToInt32(TempS);
            //read image data
            byte[] PGMDataBuffer = new byte[this.mWidth * this.mLength];
            int k = 0;
            if (this.mType == "P5")
            {
                //If file is binary, read every byte
                byte[] ReadedByte = PGMReader.ReadBytes(PGMDataBuffer.Length);
                Array.Copy(ReadedByte, PGMDataBuffer, ReadedByte.Length);
            }
            else if (this.mType == "P2")
            {
                //If file is text based every pixel is distinguished 
                //by "space" and it has up to 3 chars(255)
                try
                {
                    TempByte = PGMReader.ReadByte();
                    bool flag = true;
                    do
                    {
                        i = 0;
                        while (TempByte != NewLineAsciiCode && TempByte != SpaceAsciiCode)
                        {
                            TempArray[i++] = TempByte;
                            TempByte = PGMReader.ReadByte();
                        }
                        TempS = System.Text.ASCIIEncoding.Default.GetString(TempArray, 0, i);
                        i = 0;
                        //TempS contains, string representation of every pixel
                        PGMDataBuffer[k++] = Convert.ToByte(TempS);
                        TempByte = PGMReader.ReadByte();
                        if (TempByte == NewLineAsciiCode || TempByte == SpaceAsciiCode)
                        {
                            TempByte = PGMReader.ReadByte();
                            flag = true;
                        }
                        else flag = false;
                    } while (flag);
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.InnerException);
                }
            }
            mData = PGMDataBuffer;
            PGMReader.Close();
            InputStream.Close();
        }


        public void WritePGM(Bitmap bm, int iw, int ih, string filePath)
        {
            mColor = 256;
            string PGMInfo = "P5" + Convert.ToChar(10) + '#' +
                           " " + Convert.ToChar(10) +
                           iw.ToString() + " " +
                           ih.ToString() + Convert.ToChar(10) +
                           mColor.ToString() + Convert.ToChar(10);
            FileStream OutputStream = File.Create(filePath);
            BinaryWriter PGMWriter = new BinaryWriter(OutputStream);
            byte[] PGMInfoBuffer = System.Text.ASCIIEncoding.Default.GetBytes(PGMInfo);
            PGMWriter.Write(PGMInfoBuffer);
            byte[] data = new byte[iw * ih];
            for (int j = 0; j < ih; j++)
                for (int i = 0; i < iw; i++)
                    data[i + j * iw] = (byte)bm.GetPixel(i, j).R;
            PGMWriter.Write(data);
            PGMWriter.Close();
        }

        public int getWidth()
        {
            return mWidth;
        }

        public int getHeight()
        {
            return mLength;
        }

        public string getType()
        {
            return mType;
        }

        public byte[] getData()
        {
            return mData;
        }

        public int getColor()
        {
            return mColor;
        }
    }

猜你喜欢

转载自blog.csdn.net/pengfeixiong/article/details/18378401