예제 #1
0
        public void fromYCbCr()
        {
            #region doc_ycbcr
            // This example shows how to convert to and from various
            // pixel representations. For example, let's say we start
            // with a YCbCr pixel with the value (0.8, 0.2, 0.5):
            YCbCr yCbCr = new YCbCr(y: 0.8f, cb: 0.2f, cr: 0.5f);

            // The value of this pixel in RGB format would be:
            RGB rgb = yCbCr.ToRGB();  // should be (255, 95, 255)

            // The value of this pixel in HSL format would be:
            HSL hsl = HSL.FromRGB(rgb); // should be (299, 0.99999994, 0.6862745)

            // Note: we can also convert using casting:
            RGB a = (RGB)yCbCr;
            HSL b = (HSL)yCbCr;
            #endregion

            Assert.AreEqual(255, rgb.Red);
            Assert.AreEqual(95, rgb.Green);
            Assert.AreEqual(255, rgb.Blue);
            Assert.AreEqual(255, rgb.Alpha);
            Assert.AreEqual(299, hsl.Hue, 1);
            Assert.AreEqual(0.99999994, hsl.Saturation, 1e-6);
            Assert.AreEqual(0.6862745, hsl.Luminance, 1e-6);
            Assert.AreEqual(0.8, yCbCr.Y, 1e-6);
            Assert.AreEqual(0.2, yCbCr.Cb, 1e-6);
            Assert.AreEqual(0.5, yCbCr.Cr, 1e-6);

            Assert.AreEqual(rgb, a);
            Assert.AreEqual(hsl, b);
        }
예제 #2
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num   = (image.PixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
            int   left  = rect.Left;
            int   top   = rect.Top;
            int   num2  = left + rect.Width;
            int   num3  = top + rect.Height;
            int   num4  = image.Stride - rect.Width * num;
            RGB   rGB   = new RGB();
            YCbCr yCbCr = new YCbCr();
            byte *ptr   = (byte *)image.ImageData.ToPointer();

            ptr += top * image.Stride + left * num;
            for (int i = top; i < num3; i++)
            {
                int num5 = left;
                while (num5 < num2)
                {
                    bool flag = false;
                    rGB.Red   = ptr[2];
                    rGB.Green = ptr[1];
                    rGB.Blue  = *ptr;
                    YCbCr.FromRGB(rGB, yCbCr);
                    if (yCbCr.Y >= yRange.Min && yCbCr.Y <= yRange.Max && yCbCr.Cb >= cbRange.Min && yCbCr.Cb <= cbRange.Max && yCbCr.Cr >= crRange.Min && yCbCr.Cr <= crRange.Max)
                    {
                        if (!fillOutsideRange)
                        {
                            if (updateY)
                            {
                                yCbCr.Y = fillY;
                            }
                            if (updateCb)
                            {
                                yCbCr.Cb = fillCb;
                            }
                            if (updateCr)
                            {
                                yCbCr.Cr = fillCr;
                            }
                            flag = true;
                        }
                    }
                    else if (fillOutsideRange)
                    {
                        if (updateY)
                        {
                            yCbCr.Y = fillY;
                        }
                        if (updateCb)
                        {
                            yCbCr.Cb = fillCb;
                        }
                        if (updateCr)
                        {
                            yCbCr.Cr = fillCr;
                        }
                        flag = true;
                    }
                    if (flag)
                    {
                        YCbCr.ToRGB(yCbCr, rGB);
                        ptr[2] = rGB.Red;
                        ptr[1] = rGB.Green;
                        *ptr = rGB.Blue;
                    }
                    num5++;
                    ptr += num;
                }
                ptr += num4;
            }
        }
예제 #3
0
        private void OpenMrgFile_btn_Click(object sender, EventArgs e)
        {
            // Displays an OpenFileDialog so the user can select a Cursor.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Filter = "MRG Files(*.MRG)|*.MRG|All files (*.*)|*.*";
            openFileDialog1.Title  = "Select an MRG File";

            // Show the Dialog.



            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                using (FileStream originalFileStream = (FileStream)openFileDialog1.OpenFile())
                {
                    using (FileStream decompressedFileStream = File.Create("temp"))
                    {
                        using (GZipStream decompressionStream = new GZipStream(originalFileStream, CompressionMode.Decompress))
                        {
                            decompressionStream.CopyTo(decompressedFileStream);
                        }
                    }
                }
            }

            int Quality = 0;
            int width   = 0;
            int height  = 0;

            string line;

            StreamReader file = new StreamReader("temp");

            if ((line = file.ReadLine()) != null)
            {
                int x = 0;
                int.TryParse(line, out x);
                Quality = x;
            }

            if ((line = file.ReadLine()) != null)
            {
                int x = 0;
                int.TryParse(line, out x);
                width = x;
            }
            if ((line = file.ReadLine()) != null)
            {
                int x = 0;
                int.TryParse(line, out x);
                height = x;
            }

            List <int> zigzagY = new List <int>();
            List <int> zigzagU = new List <int>();
            List <int> zigzagV = new List <int>();

            for (int i = 0; i < (width * height); i++)
            {
                if ((line = file.ReadLine()) != null)
                {
                    int x = 0;
                    int.TryParse(line, out x);
                    zigzagY.Add(x);
                }
            }

            for (int i = 0; i < (width * height); i++)
            {
                if ((line = file.ReadLine()) != null)
                {
                    int x = 0;
                    int.TryParse(line, out x);
                    zigzagU.Add(x);
                }
            }

            for (int i = 0; i < (width * height); i++)
            {
                if ((line = file.ReadLine()) != null)
                {
                    int x = 0;
                    int.TryParse(line, out x);
                    zigzagV.Add(x);
                }
            }


            Quality_found.Text = Quality.ToString();
            width_found.Text   = width.ToString();
            height_found.Text  = height.ToString();

            //Create mrgfile object to work with

            MRGfile mrgfile = new MRGfile(zigzagY, zigzagU, zigzagV, Quality, width, height);

            QualityMatrixAlter(Quality);


            //////////
            /////////////// convert ZigZag format to array
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            myYcBc MyOwnYcbc = new myYcBc(width, height);


            int dezigcount = 0;

            for (int y = 0; y < mrgfile.height; y += 8)
            {
                for (int x = 0; x < mrgfile.width; x += 8)
                {
                    for (int index = 0; index < 64; index++)
                    {
                        Coords Targetcoords;

                        Targetcoords = ZZStraverseValue(index);

                        MyOwnYcbc.ycbcrArr[Targetcoords.xvalue + x, Targetcoords.yvalue + y].Yquant = mrgfile.zigzagY[dezigcount];
                        MyOwnYcbc.ycbcrArr[Targetcoords.xvalue + x, Targetcoords.yvalue + y].Uquant = mrgfile.zigzagU[dezigcount];
                        MyOwnYcbc.ycbcrArr[Targetcoords.xvalue + x, Targetcoords.yvalue + y].Vquant = mrgfile.zigzagV[dezigcount];

                        dezigcount++;
                    }
                }
            }


            //////////
            /////////////// Reverse Quantization
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            for (int y = 0; y < MyOwnYcbc.height; y += 8)
            {
                for (int x = 0; x < MyOwnYcbc.width; x += 8)
                {
                    for (int ity = y; ity < y + 8; ity++)
                    {
                        for (int itx = x; itx < x + 8; itx++)
                        {
                            if (itx < MyOwnYcbc.width && ity < MyOwnYcbc.height)
                            {
                                MyOwnYcbc.ycbcrArr[itx, ity].Ydct = MyOwnYcbc.ycbcrArr[itx, ity].Yquant * QuantMatrix[itx - x, ity - y];
                                MyOwnYcbc.ycbcrArr[itx, ity].Udct = MyOwnYcbc.ycbcrArr[itx, ity].Uquant * QuantMatrix[itx - x, ity - y];
                                MyOwnYcbc.ycbcrArr[itx, ity].Vdct = MyOwnYcbc.ycbcrArr[itx, ity].Vquant * QuantMatrix[itx - x, ity - y];
                            }
                        }
                    }
                }
            }


            //////////
            /////////////// Inverse DCT
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////



            double[,] YtempIDCT = new double[8, 8];
            double[,] UtempIDCT = new double[8, 8];
            double[,] VtempIDCT = new double[8, 8];


            for (int y = 0; y < MyOwnYcbc.height; y += 8)
            {
                for (int x = 0; x < MyOwnYcbc.width; x += 8)
                {
                    if (x <= MyOwnYcbc.width - 8 && y <= MyOwnYcbc.height - 8) //safeguard against out of bounds error
                    {
                        for (int ity = y; ity < y + 8; ity++)
                        {
                            for (int itx = x; itx < x + 8; itx++)
                            {
                                YtempIDCT[ity - y, itx - x] = MyOwnYcbc.ycbcrArr[itx, ity].Ydct;
                                UtempIDCT[ity - y, itx - x] = MyOwnYcbc.ycbcrArr[itx, ity].Udct;
                                VtempIDCT[ity - y, itx - x] = MyOwnYcbc.ycbcrArr[itx, ity].Vdct;
                            }
                        }

                        CosineTransform.IDCT(YtempIDCT);
                        CosineTransform.IDCT(UtempIDCT);
                        CosineTransform.IDCT(VtempIDCT);

                        for (int ity = y; ity < y + 8; ity++)
                        {
                            for (int itx = x; itx < x + 8; itx++)
                            {
                                MyOwnYcbc.ycbcrArr[itx, ity].Ynorm = (int)YtempIDCT[ity - y, itx - x];
                                MyOwnYcbc.ycbcrArr[itx, ity].Unorm = (int)UtempIDCT[ity - y, itx - x];
                                MyOwnYcbc.ycbcrArr[itx, ity].Vnorm = (int)VtempIDCT[ity - y, itx - x];
                            }
                        }
                    }
                }
            }


            //////////
            /////////////// DEnormalize from -128 <-> 127 range to 0 <-> 255
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            for (int y = 0; y < MyOwnYcbc.height; y++)
            {
                for (int x = 0; x < MyOwnYcbc.width; x++)
                {
                    Yuv ycColorspaceEdit = MyOwnYcbc.ycbcrArr[x, y];

                    ycColorspaceEdit.Yf = (ycColorspaceEdit.Ynorm) + 128;

                    if (y % 2 == 0 && x % 2 == 0)
                    {
                        ycColorspaceEdit.Uf = (ycColorspaceEdit.Unorm) + 128;
                        ycColorspaceEdit.Vf = (ycColorspaceEdit.Vnorm) + 128;
                    }
                    else
                    {
                        ycColorspaceEdit.Uf = (ycColorspaceEdit.Unorm);
                        ycColorspaceEdit.Vf = (ycColorspaceEdit.Vnorm);
                    }

                    MyOwnYcbc.ycbcrArr[x, y] = ycColorspaceEdit;
                }
            }


            //////////
            /////////////// Convert from YUV to RGB and assign to bitmap image
            ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

            Bitmap Finalbitmap = new Bitmap(MyOwnYcbc.width, MyOwnYcbc.height);

            for (int y = 0; y < MyOwnYcbc.height; y++)
            {
                for (int x = 0; x < MyOwnYcbc.width; x++)
                {
                    YCbCr ycrcb    = new YCbCr();
                    RGB   rgbcolor = new RGB();

                    ycrcb.Y  = (float)(MyOwnYcbc.ycbcrArr[x, y].Yf / 255);
                    ycrcb.Cr = (float)(MyOwnYcbc.ycbcrArr[x, y].Uf / 255);
                    ycrcb.Cb = (float)(MyOwnYcbc.ycbcrArr[x, y].Vf / 255);

                    //rgbcolor.Red = (byte)(ycrcb.Cr * 255);
                    //rgbcolor.Blue = (byte)(ycrcb.Cr * 255);
                    //rgbcolor.Green = (byte)(ycrcb.Cr * 255);
                    YCbCr.ToRGB(ycrcb, rgbcolor);

                    Finalbitmap.SetPixel(x, y, rgbcolor.Color);
                }
            }


            System.Drawing.Image.GetThumbnailImageAbort myCallback = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);


            DecompressedResultBox.Image = Finalbitmap.GetThumbnailImage(DecompressedResultBox.Width, DecompressedResultBox.Height, myCallback, IntPtr.Zero);



            Coords testcoords;

            testcoords.xvalue = 920;
            testcoords.yvalue = 0;

            for (int y = testcoords.yvalue; y < testcoords.yvalue + 8; y++)
            {
                for (int x = testcoords.xvalue; x < testcoords.xvalue + 8; x++)
                {
                    Matrixviewer.Rows[y - testcoords.yvalue].Cells[x - testcoords.xvalue].Value = MyOwnYcbc.ycbcrArr[x, y].Yquant;
                    //Matrixviewer.Rows[x].Cells[y].Value = QuantMatrix[x, y];
                    //Matrixviewer.Rows[x].Cells[y].Value = testoutputmatrix[x, y];
                }
            }


            //for (int y = 0; y < 8; y++)
            //{
            //    for (int x = 0; x <  8; x++)
            //    {
            //        Matrixviewer.Rows[x].Cells[y].Value = QuantMatrix[x, y];
            //        //Matrixviewer.Rows[x].Cells[y].Value = testoutputmatrix[x, y];
            //    }
            //}
        }
예제 #4
0
        public Image process(Image imageIn)
        {
            Color rgb   = new Color();
            YCbCr ycbcr = new YCbCr();

            float ky = 0, by = 0;
            float kcb = 0, bcb = 0;
            float kcr = 0, bcr = 0;

            // Y line parameters
            if (inY.Max != inY.Min)
            {
                ky = (outY.Max - outY.Min) / (inY.Max - inY.Min);
                by = outY.Min - ky * inY.Min;
            }
            // Cb line parameters
            if (inCb.Max != inCb.Min)
            {
                kcb = (outCb.Max - outCb.Min) / (inCb.Max - inCb.Min);
                bcb = outCb.Min - kcb * inCb.Min;
            }
            // Cr line parameters
            if (inCr.Max != inCr.Min)
            {
                kcr = (outCr.Max - outCr.Min) / (inCr.Max - inCr.Min);
                bcr = outCr.Min - kcr * inCr.Min;
            }

            for (int x = 0; x < imageIn.getWidth(); x++)
            {
                for (int y = 0; y < imageIn.getHeight(); y++)
                {
                    rgb.R = (byte)imageIn.getRComponent(x, y);
                    rgb.G = (byte)imageIn.getGComponent(x, y);
                    rgb.B = (byte)imageIn.getBComponent(x, y);

                    // convert to YCbCr
                    ycbcr = YCbCr.FromRGB(rgb, ycbcr);

                    // correct Y
                    if (ycbcr.Y >= inY.Max)
                    {
                        ycbcr.Y = outY.Max;
                    }
                    else if (ycbcr.Y <= inY.Min)
                    {
                        ycbcr.Y = outY.Min;
                    }
                    else
                    {
                        ycbcr.Y = ky * ycbcr.Y + by;
                    }

                    // correct Cb
                    if (ycbcr.Cb >= inCb.Max)
                    {
                        ycbcr.Cb = outCb.Max;
                    }
                    else if (ycbcr.Cb <= inCb.Min)
                    {
                        ycbcr.Cb = outCb.Min;
                    }
                    else
                    {
                        ycbcr.Cb = kcb * ycbcr.Cb + bcb;
                    }

                    // correct Cr
                    if (ycbcr.Cr >= inCr.Max)
                    {
                        ycbcr.Cr = outCr.Max;
                    }
                    else if (ycbcr.Cr <= inCr.Min)
                    {
                        ycbcr.Cr = outCr.Min;
                    }
                    else
                    {
                        ycbcr.Cr = kcr * ycbcr.Cr + bcr;
                    }

                    // convert back to RGB
                    rgb = YCbCr.ToRGB(ycbcr, rgb);

                    imageIn.setPixelColor(x, y, rgb.R, rgb.G, rgb.B);
                }
            }
            return(imageIn);
        }
예제 #5
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int   num   = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int   left  = rect.Left;
            int   top   = rect.Top;
            int   num2  = left + rect.Width;
            int   num3  = top + rect.Height;
            int   num4  = image.Stride - rect.Width * num;
            RGB   rGB   = new RGB();
            YCbCr yCbCr = new YCbCr();
            float num5  = 0f;
            float num6  = 0f;
            float num7  = 0f;
            float num8  = 0f;
            float num9  = 0f;
            float num10 = 0f;

            if (inY.Max != inY.Min)
            {
                num5 = (outY.Max - outY.Min) / (inY.Max - inY.Min);
                num6 = outY.Min - num5 * inY.Min;
            }
            if (inCb.Max != inCb.Min)
            {
                num7 = (outCb.Max - outCb.Min) / (inCb.Max - inCb.Min);
                num8 = outCb.Min - num7 * inCb.Min;
            }
            if (inCr.Max != inCr.Min)
            {
                num9  = (outCr.Max - outCr.Min) / (inCr.Max - inCr.Min);
                num10 = outCr.Min - num9 * inCr.Min;
            }
            byte *ptr = (byte *)image.ImageData.ToPointer();

            ptr += top * image.Stride + left * num;
            for (int i = top; i < num3; i++)
            {
                int num11 = left;
                while (num11 < num2)
                {
                    rGB.Red   = ptr[2];
                    rGB.Green = ptr[1];
                    rGB.Blue  = *ptr;
                    YCbCr.FromRGB(rGB, yCbCr);
                    if (yCbCr.Y >= inY.Max)
                    {
                        yCbCr.Y = outY.Max;
                    }
                    else if (yCbCr.Y <= inY.Min)
                    {
                        yCbCr.Y = outY.Min;
                    }
                    else
                    {
                        yCbCr.Y = num5 * yCbCr.Y + num6;
                    }
                    if (yCbCr.Cb >= inCb.Max)
                    {
                        yCbCr.Cb = outCb.Max;
                    }
                    else if (yCbCr.Cb <= inCb.Min)
                    {
                        yCbCr.Cb = outCb.Min;
                    }
                    else
                    {
                        yCbCr.Cb = num7 * yCbCr.Cb + num8;
                    }
                    if (yCbCr.Cr >= inCr.Max)
                    {
                        yCbCr.Cr = outCr.Max;
                    }
                    else if (yCbCr.Cr <= inCr.Min)
                    {
                        yCbCr.Cr = outCr.Min;
                    }
                    else
                    {
                        yCbCr.Cr = num9 * yCbCr.Cr + num10;
                    }
                    YCbCr.ToRGB(yCbCr, rGB);
                    ptr[2] = rGB.Red;
                    ptr[1] = rGB.Green;
                    *ptr = rGB.Blue;
                    num11++;
                    ptr += num;
                }
                ptr += num4;
            }
        }
예제 #6
0
        protected unsafe override void ProcessFilter(UnmanagedImage image, Rectangle rect)
        {
            int        num        = System.Drawing.Image.GetPixelFormatSize(image.PixelFormat) / 8;
            int        width      = image.Width;
            int        height     = image.Height;
            int        left       = rect.Left;
            int        top        = rect.Top;
            int        num2       = left + rect.Width;
            int        num3       = top + rect.Height;
            int        num4       = image.Stride - rect.Width * num;
            BitmapData bitmapData = null;
            int        num5       = 0;
            byte *     ptr;

            if (channelImage != null)
            {
                if (width != channelImage.Width || height != channelImage.Height)
                {
                    throw new InvalidImagePropertiesException("Channel image size does not match source image size.");
                }
                bitmapData = channelImage.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                ptr        = (byte *)bitmapData.Scan0.ToPointer();
                num5       = bitmapData.Stride;
            }
            else
            {
                if (width != unmanagedChannelImage.Width || height != unmanagedChannelImage.Height)
                {
                    throw new InvalidImagePropertiesException("Channel image size does not match source image size.");
                }
                ptr  = (byte *)(void *)unmanagedChannelImage.ImageData;
                num5 = unmanagedChannelImage.Stride;
            }
            int   num6  = num5 - rect.Width;
            RGB   rGB   = new RGB();
            YCbCr yCbCr = new YCbCr();
            byte *ptr2  = (byte *)image.ImageData.ToPointer();

            ptr2 += top * image.Stride + left * num;
            ptr  += top * num5 + left;
            for (int i = top; i < num3; i++)
            {
                int num7 = left;
                while (num7 < num2)
                {
                    rGB.Red   = ptr2[2];
                    rGB.Green = ptr2[1];
                    rGB.Blue  = *ptr2;
                    YCbCr.FromRGB(rGB, yCbCr);
                    switch (channel)
                    {
                    case 0:
                        yCbCr.Y = (float)(int)(*ptr) / 255f;
                        break;

                    case 1:
                        yCbCr.Cb = (float)(int)(*ptr) / 255f - 0.5f;
                        break;

                    case 2:
                        yCbCr.Cr = (float)(int)(*ptr) / 255f - 0.5f;
                        break;
                    }
                    YCbCr.ToRGB(yCbCr, rGB);
                    ptr2[2] = rGB.Red;
                    ptr2[1] = rGB.Green;
                    *ptr2 = rGB.Blue;
                    num7++;
                    ptr2 += num;
                    ptr++;
                }
                ptr2 += num4;
                ptr  += num6;
            }
            if (bitmapData != null)
            {
                channelImage.UnlockBits(bitmapData);
            }
        }