public Bitmap PgmToBitmap(PgmImage pgmImage)
        {
            if (pgmImage != null && pgmImage.MagicNumber == "P2")
            {
                int    width  = pgmImage.Width;
                int    height = pgmImage.Height;
                Bitmap result = new Bitmap(width, height);

                Graphics gr = Graphics.FromImage(result);

                int pixel = 0;
                for (int i = 0; i < pgmImage.Height; ++i)
                {
                    for (int j = 0; j < pgmImage.Width; ++j)
                    {
                        int        pixelColor = pgmImage.Data[pixel++];
                        Color      c          = Color.FromArgb(pixelColor, pixelColor, pixelColor);
                        SolidBrush sb         = new SolidBrush(c);
                        gr.FillRectangle(sb, j, i, 1, 1);
                    }
                }

                return(result);
            }
            return(null);
        }
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dlgResult = openFileDialog1.ShowDialog();

            if (dlgResult == DialogResult.OK)
            {
                try {
                    pgmOriginalImage = new PgmImage(openFileDialog1.FileName);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
                pictureBoxOriginalImage.Image = PgmToBitmap(pgmOriginalImage);

                if (pictureBoxOriginalImage.Image != null)
                {
                    inputFileName = openFileDialog1.FileName;

                    tableLayoutPanelEffect.Enabled = true;

                    outputFileName = null;
                    pictureBoxResultImage.Visible = true;
                    pictureBoxResultImage.Image   = null;

                    labelMagicNumber.Text = "Magic Number: " + pgmOriginalImage.MagicNumber;
                    labelWidth.Text       = "Width: " + pgmOriginalImage.Width + " px";
                    labelHieght.Text      = "Height: " + pgmOriginalImage.Height + " px";
                    labelMaxVal.Text      = "Maximum Gray Value: " + pgmOriginalImage.MaxVal;
                }
                else
                {
                    inputFileName  = null;
                    outputFileName = null;
                    pictureBoxResultImage.Visible = false;

                    pictureBoxOriginalImage.Image = pictureBoxOriginalImage.ErrorImage;

                    tableLayoutPanelEffect.Enabled = false;

                    labelMagicNumber.Text = "Magic Number:";
                    labelWidth.Text       = "Width:";
                    labelHieght.Text      = "Height:";
                    labelMaxVal.Text      = "Maximum Gray Value:";
                    labelThreshold.Text   = "Threshold:";
                }
            }
        }
        public PgmImage Negative(PgmImage img, ImagePart imgPart)
        {
            if (img != null)
            {
                PgmImage result = new PgmImage(img.Width, img.Height, img.MagicNumber, null, img.MaxVal);
                Array.Copy(img.Data, result.Data, img.Data.Length);

                switch (imgPart)
                {
                case ImagePart.Full:
                    for (int i = 0; i < img.Data.Length; i++)
                    {
                        result.Data[i] = (byte)(maxValue - result.Data[i]);
                    }
                    break;

                case ImagePart.Right:
                    for (int i = img.Width / 2; i < img.Data.Length; i += img.Width)
                    {
                        int innerLen = (img.Width / 2) + i;
                        for (int j = i; j < innerLen; j++)
                        {
                            result.Data[j] = (byte)(maxValue - result.Data[j]);
                        }
                    }
                    break;

                case ImagePart.Left:
                    for (int i = 0; i < img.Data.Length; i += img.Width)
                    {
                        int innerLen = (img.Width / 2) + i;
                        for (int j = i; j < innerLen; j++)
                        {
                            result.Data[j] = (byte)(maxValue - result.Data[j]);
                        }
                    }
                    break;
                }

                return(result);
            }
            return(null);
        }