Example #1
0
        public void readImage(Bitmap bmp)
        {
            img     = new PixelClassRGB[bmp.Width, bmp.Height];
            imghsv  = new PixelClassHSV[bmp.Width, bmp.Height];
            imgcmyk = new PixelClassCMYK[bmp.Width, bmp.Height];
            imgyuv  = new PixelClassYUV[bmp.Width, bmp.Height];

            imgChartRGB  = new PixelClassRGB[bmp.Width, bmp.Height];
            imgChartCMYK = new PixelClassCMYK[bmp.Width, bmp.Height];
            imgChartHSV  = new PixelClassHSV[bmp.Width, bmp.Height];
            imgChartYUV  = new PixelClassYUV[bmp.Width, bmp.Height];
            histOriginal = new Histogram();
            histEdited   = new Histogram();

            var bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadOnly, bmp.PixelFormat);

            IntPtr ptr = IntPtr.Zero;
            int    pixelComponents;


            if (bmpData.PixelFormat == PixelFormat.Format24bppRgb)
            {
                pixelComponents = 3;
            }
            else if (bmpData.PixelFormat == PixelFormat.Format32bppArgb)
            {
                pixelComponents = 4;
            }
            else
            {
                pixelComponents = 0;
            }
            var line = new byte[bmp.Width * pixelComponents];

            for (int y = 0; y < bmpData.Height; y++)
            {
                ptr = bmpData.Scan0 + y * bmpData.Stride;
                Marshal.Copy(ptr, line, 0, line.Length);

                for (int x = 0; x < bmpData.Width; x++)
                {
                    img[x, y]     = new PixelClassRGB(line[pixelComponents * x + 2], line[pixelComponents * x + 1], line[pixelComponents * x]);
                    imghsv[x, y]  = new PixelClassHSV(img[x, y].R, img[x, y].G, img[x, y].B);
                    imgcmyk[x, y] = new PixelClassCMYK(img[x, y].R, img[x, y].G, img[x, y].B);
                    imgyuv[x, y]  = new PixelClassYUV(img[x, y].R, img[x, y].G, img[x, y].B);

                    imgChartCMYK[x, y] = new PixelClassCMYK(img[x, y].R, img[x, y].G, img[x, y].B);
                    imgChartRGB[x, y]  = new PixelClassRGB(line[pixelComponents * x + 2], line[pixelComponents * x + 1], line[pixelComponents * x]);
                    imgChartYUV[x, y]  = new PixelClassYUV(img[x, y].R, img[x, y].G, img[x, y].B);
                    imgChartHSV[x, y]  = new PixelClassHSV(img[x, y].R, img[x, y].G, img[x, y].B);

                    img[x, y].X = x - bmpData.Width / 2;
                    img[x, y].Y = y - bmpData.Height / 2;
                }
            }
            bmp.UnlockBits(bmpData);
            histOriginal.readGraph(img);
        }
Example #2
0
        public Bitmap drawImage(string mode)
        {
            if (img != null)
            {
                IntPtr ptr     = IntPtr.Zero;
                int    Height  = img.GetLength(1);
                int    Width   = img.GetLength(0);
                var    bmp     = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
                var    bmpData = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height),
                                              ImageLockMode.WriteOnly, bmp.PixelFormat);
                var line = new byte[bmp.Width * 3];

                for (int y = 0; y < bmpData.Height; y++)
                {
                    for (int x = 0; x < bmpData.Width; x++)
                    {
                        switch (mode)
                        {
                        case "transformation":
                        {
                            if (((img[x, y].X + x0) > 0) && ((img[x, y].Y + y0) > 0) &&
                                (((img[x, y].X + x0) < bmpData.Width) && ((img[x, y].Y + y0) < bmpData.Height)))
                            {
                                line[3 * x + 2] = img[img[x, y].X + x0, y0 + img[x, y].Y].R;
                                line[3 * x + 1] = img[img[x, y].X + x0, y0 + img[x, y].Y].G;
                                line[3 * x]     = img[img[x, y].X + x0, y0 + img[x, y].Y].B;
                            }
                            else
                            {
                                line[3 * x + 2] = 0;
                                line[3 * x + 1] = 0;
                                line[3 * x]     = 0;
                            }
                            break;
                        }

                        case "RGB":
                        {
                            line[3 * x]         = img[x, y].B;
                            line[3 * x + 1]     = img[x, y].G;
                            line[3 * x + 2]     = img[x, y].R;
                            imgChartRGB[x, y].B = line[3 * x];
                            imgChartRGB[x, y].G = line[3 * x + 1];
                            imgChartRGB[x, y].R = line[3 * x + 2];
                            imgChartRGB[x, y].I = img[x, y].I;
                            break;
                        }

                        case "R":
                        {
                            line[3 * x]         = 0;
                            line[3 * x + 1]     = 0;
                            line[3 * x + 2]     = img[x, y].R;
                            imgChartRGB[x, y].B = 0;
                            imgChartRGB[x, y].G = 0;
                            imgChartRGB[x, y].R = img[x, y].R;
                            imgChartRGB[x, y].I = 0;
                            break;
                        }

                        case "G":
                        {
                            line[3 * x]         = 0;
                            line[3 * x + 1]     = img[x, y].G;
                            line[3 * x + 2]     = 0;
                            imgChartRGB[x, y].B = 0;
                            imgChartRGB[x, y].G = img[x, y].G;
                            imgChartRGB[x, y].R = 0;
                            imgChartRGB[x, y].I = 0;
                            break;
                        }

                        case "B":
                        {
                            line[3 * x]         = img[x, y].B;
                            line[3 * x + 1]     = 0;
                            line[3 * x + 2]     = 0;
                            imgChartRGB[x, y].B = img[x, y].B;
                            imgChartRGB[x, y].G = 0;
                            imgChartRGB[x, y].R = 0;
                            imgChartRGB[x, y].I = 0;
                            break;
                        }

                        case "I":
                        {
                            line[3 * x]         = img[x, y].I;
                            line[3 * x + 1]     = img[x, y].I;
                            line[3 * x + 2]     = img[x, y].I;
                            imgChartRGB[x, y].B = 0;
                            imgChartRGB[x, y].G = 0;
                            imgChartRGB[x, y].R = 0;
                            imgChartRGB[x, y].I = img[x, y].I;
                            break;
                        }

                        case "Invert":
                        {
                            line[3 * x]       = Convert.ToByte(255 - img[x, y].B);
                            line[3 * x + 1]   = Convert.ToByte(255 - img[x, y].G);
                            line[3 * x + 2]   = Convert.ToByte(255 - img[x, y].R);
                            imgChartRGB[x, y] = new PixelClassRGB(line[3 * x], line[3 * x + 1], line[3 * x + 2]);
                            break;
                        }

                        case "Strech":
                        {
                            line[3 * x]     = imgChartRGB[x, y].B;
                            line[3 * x + 1] = imgChartRGB[x, y].G;
                            line[3 * x + 2] = imgChartRGB[x, y].R;
                            break;
                        }

                        case "transition":
                        {
                            line[3 * x]     = img[x, y].B;
                            line[3 * x + 1] = img[x, y].G;
                            line[3 * x + 2] = img[x, y].R;
                            break;
                        }

                        case "Strech R":
                        {
                            line[3 * x]     = 0;
                            line[3 * x + 1] = 0;
                            line[3 * x + 2] = imgChartRGB[x, y].R;
                            break;
                        }

                        case "Strech G":
                        {
                            line[3 * x]     = 0;
                            line[3 * x + 1] = imgChartRGB[x, y].G;
                            line[3 * x + 2] = 0;
                            break;
                        }

                        case "Strech B":
                        {
                            line[3 * x]     = imgChartRGB[x, y].B;
                            line[3 * x + 1] = 0;
                            line[3 * x + 2] = 0;
                            break;
                        }

                        case "Strech I":
                        {
                            line[3 * x]     = imgChartRGB[x, y].I;
                            line[3 * x + 1] = imgChartRGB[x, y].I;
                            line[3 * x + 2] = imgChartRGB[x, y].I;
                            break;
                        }

                        case "HSV":
                        {
                            line[3 * x]       = img[x, y].hsvToRGB(imghsv[x, y].H, imghsv[x, y].S, imghsv[x, y].V).B;
                            line[3 * x + 1]   = img[x, y].hsvToRGB(imghsv[x, y].H, imghsv[x, y].S, imghsv[x, y].V).G;
                            line[3 * x + 2]   = img[x, y].hsvToRGB(imghsv[x, y].H, imghsv[x, y].S, imghsv[x, y].V).R;
                            imgChartHSV[x, y] = new PixelClassHSV(imghsv[x, y]);
                            break;
                        }

                        case "Strech HSV":
                        {
                            line[3 * x]     = img[x, y].hsvToRGB(imgChartHSV[x, y].H, imgChartHSV[x, y].S, imgChartHSV[x, y].V).B;
                            line[3 * x + 1] = img[x, y].hsvToRGB(imgChartHSV[x, y].H, imgChartHSV[x, y].S, imgChartHSV[x, y].V).G;
                            line[3 * x + 2] = img[x, y].hsvToRGB(imgChartHSV[x, y].H, imgChartHSV[x, y].S, imgChartHSV[x, y].V).R;
                            break;
                        }

                        case "Strech S":
                        {
                            line[3 * x]     = imgChartHSV[x, y].S;
                            line[3 * x + 1] = imgChartHSV[x, y].S;
                            line[3 * x + 2] = imgChartHSV[x, y].S;
                            break;
                        }

                        case "H":
                        {
                            line[3 * x]       = img[x, y].hsvToRGB(imghsv[x, y].H, 255, 255).B;
                            line[3 * x + 1]   = img[x, y].hsvToRGB(imghsv[x, y].H, 255, 255).G;
                            line[3 * x + 2]   = img[x, y].hsvToRGB(imghsv[x, y].H, 255, 255).R;
                            imgChartHSV[x, y] = new PixelClassHSV(imghsv[x, y].H, 0, 0, 0);
                            break;
                        }

                        case "S":
                        {
                            line[3 * x]       = imghsv[x, y].S;
                            line[3 * x + 1]   = imghsv[x, y].S;
                            line[3 * x + 2]   = imghsv[x, y].S;
                            imgChartHSV[x, y] = new PixelClassHSV(0, imghsv[x, y].S, 0, 0);
                            break;
                        }

                        case "V":
                        {
                            line[3 * x]       = imghsv[x, y].V;
                            line[3 * x + 1]   = imghsv[x, y].V;
                            line[3 * x + 2]   = imghsv[x, y].V;
                            imgChartHSV[x, y] = new PixelClassHSV(0, 0, imghsv[x, y].V, 0);
                            break;
                        }

                        case "CMYK":
                        {
                            line[3 * x]        = img[x, y].cmykToRGB(imgcmyk[x, y].C, imgcmyk[x, y].M, imgcmyk[x, y].Y, imgcmyk[x, y].K).B;
                            line[3 * x + 1]    = img[x, y].cmykToRGB(imgcmyk[x, y].C, imgcmyk[x, y].M, imgcmyk[x, y].Y, imgcmyk[x, y].K).G;
                            line[3 * x + 2]    = img[x, y].cmykToRGB(imgcmyk[x, y].C, imgcmyk[x, y].M, imgcmyk[x, y].Y, imgcmyk[x, y].K).R;
                            imgChartCMYK[x, y] = new PixelClassCMYK(imgcmyk[x, y]);
                            break;
                        }

                        case "C":
                        {
                            line[3 * x]        = img[x, y].cmykToRGB(imgcmyk[x, y].C, 0, 0, 0).B;
                            line[3 * x + 1]    = img[x, y].cmykToRGB(imgcmyk[x, y].C, 0, 0, 0).G;
                            line[3 * x + 2]    = img[x, y].cmykToRGB(imgcmyk[x, y].C, 0, 0, 0).R;
                            imgChartCMYK[x, y] = new PixelClassCMYK(imgcmyk[x, y].C, 0, 0, 0);

                            break;
                        }

                        case "M":
                        {
                            line[3 * x]        = img[x, y].cmykToRGB(0, imgcmyk[x, y].M, 0, 0).B;
                            line[3 * x + 1]    = img[x, y].cmykToRGB(0, imgcmyk[x, y].M, 0, 0).G;
                            line[3 * x + 2]    = img[x, y].cmykToRGB(0, imgcmyk[x, y].M, 0, 0).R;
                            imgChartCMYK[x, y] = new PixelClassCMYK(0, imgcmyk[x, y].M, 0, 0);
                            break;
                        }

                        case "Y":
                        {
                            line[3 * x]        = img[x, y].cmykToRGB(0, 0, imgcmyk[x, y].Y, 0).B;
                            line[3 * x + 1]    = img[x, y].cmykToRGB(0, 0, imgcmyk[x, y].Y, 0).G;
                            line[3 * x + 2]    = img[x, y].cmykToRGB(0, 0, imgcmyk[x, y].Y, 0).R;
                            imgChartCMYK[x, y] = new PixelClassCMYK(0, 0, imgcmyk[x, y].Y, 0);
                            break;
                        }

                        case "K":
                        {
                            line[3 * x]        = img[x, y].cmykToRGB(0, 0, 0, imgcmyk[x, y].K).B;
                            line[3 * x + 1]    = img[x, y].cmykToRGB(0, 0, 0, imgcmyk[x, y].K).G;
                            line[3 * x + 2]    = img[x, y].cmykToRGB(0, 0, 0, imgcmyk[x, y].K).R;
                            imgChartCMYK[x, y] = new PixelClassCMYK(0, 0, 0, imgcmyk[x, y].K);
                            break;
                        }

                        case "YUV":
                        {
                            line[3 * x]       = img[x, y].yuvToRGB(imgyuv[x, y].Y, imgyuv[x, y].U, imgyuv[x, y].V).B;
                            line[3 * x + 1]   = img[x, y].yuvToRGB(imgyuv[x, y].Y, imgyuv[x, y].U, imgyuv[x, y].V).G;
                            line[3 * x + 2]   = img[x, y].yuvToRGB(imgyuv[x, y].Y, imgyuv[x, y].U, imgyuv[x, y].V).R;
                            imgChartYUV[x, y] = new PixelClassYUV(imgyuv[x, y]);
                            break;
                        }

                        case "Y1":
                        {
                            line[3 * x]       = imgyuv[x, y].Y;
                            line[3 * x + 1]   = imgyuv[x, y].Y;
                            line[3 * x + 2]   = imgyuv[x, y].Y;
                            imgChartYUV[x, y] = new PixelClassYUV(imgyuv[x, y].Y, 0, 0, 0);
                            break;
                        }

                        case "U1":
                        {
                            line[3 * x]       = img[x, y].yuvToRGB(0, imgyuv[x, y].U, 0).B;
                            line[3 * x + 1]   = img[x, y].yuvToRGB(0, imgyuv[x, y].U, 0).G;
                            line[3 * x + 2]   = img[x, y].yuvToRGB(0, imgyuv[x, y].U, 0).R;
                            imgChartYUV[x, y] = new PixelClassYUV(0, imgyuv[x, y].U, 0, 0);
                            break;
                        }

                        case "V1":
                        {
                            line[3 * x]       = img[x, y].yuvToRGB(0, 0, imgyuv[x, y].V).B;
                            line[3 * x + 1]   = img[x, y].yuvToRGB(0, 0, imgyuv[x, y].V).G;
                            line[3 * x + 2]   = img[x, y].yuvToRGB(0, 0, imgyuv[x, y].V).R;
                            imgChartYUV[x, y] = new PixelClassYUV(0, 0, imgyuv[x, y].V, 0);
                            break;
                        }
                        }
                    }
                    ptr = bmpData.Scan0 + y * bmpData.Stride;
                    Marshal.Copy(line, 0, ptr, line.Length);
                }
                bmp.UnlockBits(bmpData);
                switch (mode)
                {
                case "RGB":
                case "R":
                case "G":
                case "B":
                case "I": histEdited.readGraph(imgChartRGB); break;

                case "CMYK":
                case "C":
                case "M":
                case "Y":
                case "K": histEdited.readGraph(imgChartCMYK); break;

                case "HSV":
                case "H":
                case "S":
                case "V": histEdited.readGraph(imgChartHSV); break;

                case "YUV":
                case "Y1":
                case "U1":
                case "V1": histEdited.readGraph(imgChartYUV); break;
                }
                return(bmp);
            }
            else
            {
                return(null);
            }
        }
Example #3
0
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                double kX      = (double)pictureBox1.Image.Width / pictureBox1.Width;
                double kY      = (double)pictureBox1.Image.Height / pictureBox1.Height;
                double k       = Math.Max(kX, kY);
                double nobideX = (pictureBox1.Width * k - pictureBox1.Image.Width) / 2;
                double nobideY = (pictureBox1.Height * k - pictureBox1.Image.Height) / 2;
                double kx      = Math.Round(e.X * k - nobideX);
                double ky      = Math.Round(e.Y * k - nobideY);

                label7.Text = "X: " + kx.ToString() + ", Y: " + ky.ToString();

                imgData imgDateObj = new imgData();
                Color[] pixels     = getPixel((int)kx, (int)ky, new Bitmap(pictureBox1.Image), new Bitmap(pictureBox2.Image));
                label2.Text = "R: " + pixels[0].R.ToString();
                label3.Text = "G: " + pixels[0].G.ToString();
                label4.Text = "B: " + pixels[0].B.ToString();

                if (radioButton1.Checked)
                {
                    PixelClassRGB rgbi = new PixelClassRGB(pixels[1].R, pixels[1].G, pixels[1].B);
                    label12.Text = "R: " + rgbi.R.ToString();
                    label11.Text = "G: " + rgbi.G.ToString();
                    label10.Text = "B: " + rgbi.B.ToString();
                    label9.Text  = "I: " + rgbi.I.ToString();
                }
                else if (radioButton2.Checked)
                {
                    if (inverted)
                    {
                        PixelClassHSV hsv = new PixelClassHSV(pixels[1].R, pixels[1].G, pixels[1].B);
                        label12.Text = "H: " + hsv.H.ToString();
                        label11.Text = "S: " + hsv.S.ToString();
                        label10.Text = "V: " + hsv.V.ToString();
                    }
                    else
                    {
                        PixelClassHSV hsv = new PixelClassHSV(pixels[0].R, pixels[0].G, pixels[0].B);
                        label12.Text = "H: " + hsv.H.ToString();
                        label11.Text = "S: " + hsv.S.ToString();
                        label10.Text = "V: " + hsv.V.ToString();
                    }
                }
                else if (radioButton8.Checked)
                {
                    if (inverted)
                    {
                        PixelClassCMYK cmyk = new PixelClassCMYK(pixels[1].R, pixels[1].G, pixels[1].B);
                        label12.Text = "C: " + cmyk.C.ToString();
                        label11.Text = "M: " + cmyk.M.ToString();
                        label10.Text = "Y: " + cmyk.Y.ToString();
                        label9.Text  = "K: " + cmyk.K.ToString();
                    }
                    else
                    {
                        PixelClassCMYK cmyk = new PixelClassCMYK(pixels[0].R, pixels[0].G, pixels[0].B);
                        label12.Text = "C: " + cmyk.C.ToString();
                        label11.Text = "M: " + cmyk.M.ToString();
                        label10.Text = "Y: " + cmyk.Y.ToString();
                        label9.Text  = "K: " + cmyk.K.ToString();
                    }
                }
                else if (radioButton9.Checked)
                {
                    if (inverted)
                    {
                        PixelClassYUV yuv = new PixelClassYUV(pixels[1].R, pixels[1].G, pixels[1].B);
                        label12.Text = "Y: " + yuv.Y.ToString();
                        label11.Text = "U: " + yuv.U.ToString();
                        label10.Text = "V: " + yuv.V.ToString();
                    }
                    else
                    {
                        PixelClassYUV yuv = new PixelClassYUV(pixels[0].R, pixels[0].G, pixels[0].B);
                        label12.Text = "Y: " + yuv.Y.ToString();
                        label11.Text = "U: " + yuv.U.ToString();
                        label10.Text = "V: " + yuv.V.ToString();
                    }
                }
            }
        }
 public PixelClassYUV(PixelClassYUV img)
 {
     Y = img.Y;
     U = img.U;
     V = img.V;
 }