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); using (Graphics gr = Graphics.FromImage(result)) { for (int i = 0; i < pgmImage.Height; ++i) { for (int j = 0; j < pgmImage.Width; ++j) { int pixelColor = pgmImage.Data[i][j]; 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; outputFileName = null; pictureBoxHistogram.Visible = true; pictureBoxHistogram.Image = null; saveAsToolStripMenuItem.Enabled = true; saveToolStripMenuItem.Enabled = true; 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; Cursor.Current = Cursors.WaitCursor; pgmHistogramImage = CalculateHistogramOf(pgmOriginalImage); pictureBoxHistogram.Image = PgmToBitmap(pgmHistogramImage); checkBoxOriginalSize.Enabled = true; Cursor.Current = Cursors.Default; } else { inputFileName = null; outputFileName = null; pictureBoxHistogram.Visible = false; checkBoxOriginalSize.Enabled = false; saveAsToolStripMenuItem.Enabled = false; saveToolStripMenuItem.Enabled = false; pictureBoxOriginalImage.Image = pictureBoxOriginalImage.ErrorImage; labelMagicNumber.Text = "Magic Number:"; labelWidth.Text = "Width:"; labelHieght.Text = "Height:"; labelMaxVal.Text = "Maximum Gray Value:"; } } }
public PgmImage CalculateHistogramOf(PgmImage originalImage) { PgmImage result = null; if (originalImage != null) { int[] histogramArr = new int[256]; int max = 0; for (int i = 0; i < originalImage.Height; i++) { for (int j = 0; j < originalImage.Width; j++) { int curr = ++histogramArr[originalImage.Data[i][j]]; if (max < curr) { max = curr; } } } // скалираме масива, т.к понякога се получават много големи числа и изхвърля изключение histogramArr = Scale(histogramArr, 0, 1024); result = new PgmImage(histogramArr.Length, histogramArr.Max(), "P2", null, histogramArr.Max()); int ind = 0; bool drawBar; for (int i = 0; i < result.Width; i++) { drawBar = true; for (int j = result.Height - 1; j >= 0; j--) { if (drawBar) { for (int k = 0; k < histogramArr[ind]; k++, j--) { result.Data[j][i] = 0; } ind++; drawBar = false; } else { result.Data[j][i] = 155; } } } } return(result); }