} //****************************** end ImageToBitmapOld ****************************************/ public void LabToBitmap(Bitmap bmp, CImageComp Image) { if (Image.N_Bits != 8) { MessageBox.Show("LabToBitmap: Not suitable format of 'Image'; N_Bits=" + Image.N_Bits); return; } switch (bmp.PixelFormat) { case PixelFormat.Format24bppRgb: nbyte = 3; break; case PixelFormat.Format8bppIndexed: MessageBox.Show("LabToBitmap: Not suitable pixel format=" + bmp.PixelFormat); return; default: MessageBox.Show("LabToBitmap: Not suitable pixel format=" + bmp.PixelFormat); return; } Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); BitmapData bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int size = bmp.Width * bmp.Height; int bytes = Math.Abs(bmpData.Stride) * bmp.Height; byte[] rgbValues = new byte[bytes]; Color color; int index = 0; int Len = bmp.Height, jump, nStep = 50; if (Len > 2 * nStep) { jump = Len / nStep; } else { jump = 2; } progressBar1.Step = 1; progressBar1.Visible = true; for (int y = 0; y < bmp.Height; y++) { if (y % jump == jump - 1) { progressBar1.PerformStep(); } for (int x = 0; x < bmp.Width; x++) { color = Image.Palette[Image.Lab[x + bmp.Width * y] & 255]; index = 3 * x + Math.Abs(bmpData.Stride) * y; rgbValues[index + 0] = color.B; rgbValues[index + 1] = color.G; rgbValues[index + 2] = color.R; } } System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); bmp.UnlockBits(bmpData); } //****************************** end LabToBitmap ***********************
public CImageComp(ref CImageComp inp) // copy-constructor { width = inp.width; height = inp.height; N_Bits = inp.N_Bits; Grid = new byte[width * height * (N_Bits / 8)]; Lab = new int[width * height]; for (int i = 0; i < width * height * (N_Bits / 8); i++) { Grid[i] = inp.Grid[i]; } for (int p = 0; p < 256; p++) { Palette[p] = inp.Palette[p]; } }
private void button1_Click(object sender, EventArgs e) // Open image { label1.Visible = false; label2.Visible = false; label3.Visible = false; label4.Visible = false; label5.Visible = false; label6.Visible = false; button2.Visible = false; button3.Visible = false; button4.Visible = false; button5.Visible = false; button6.Visible = false; button8.Visible = false; numericUpDown1.Visible = false; numericUpDown2.Visible = false; OpenFileDialog openFileDialog1 = new OpenFileDialog(); if (openFileDialog1.ShowDialog() == DialogResult.OK) { try { OrigBmp = new Bitmap(openFileDialog1.FileName); OpenImageFile = openFileDialog1.FileName; } catch (Exception ex) { MessageBox.Show("Open image; Could not read file from disk. Error: " + ex.Message); } } else { return; } label1.Visible = true; label3.Text = "Opened image:" + openFileDialog1.FileName; label3.Visible = true; button4.Visible = true; label6.Visible = true; label6.Text = "Click 'Segment'"; width = OrigBmp.Width; height = OrigBmp.Height; ORIG = true; progressBar1.Maximum = 100; progressBar1.Value = 0; progressBar1.Step = 1; nLoop = 1; denomProg = progressBar1.Maximum / progressBar1.Step; if (OrigBmp.PixelFormat == PixelFormat.Format8bppIndexed) { OrigIm = new CImage(width, height, 8); BitmapToImageOld(OrigBmp, OrigIm); } else if (OrigBmp.PixelFormat == PixelFormat.Format24bppRgb) { OrigIm = new CImage(width, height, 24); BitmapToImage(OrigBmp, OrigIm); } else { MessageBox.Show("Not suitable pixel format=" + OrigBmp.PixelFormat); return; } pictureBox1.Image = OrigBmp; ImpulseIm = new CImage(width, height, 8); //, Grid); SegmentIm = new CImage(width, height, 8); BreadthFirIm = new CImageComp(width, height, 8); RootIm = new CImageComp(width, height, 8); progressBar1.Value = 0; label1.Text = " Original image "; } //****************************** end Open image ****************************************