Esempio n. 1
0
        public static Bitmap GrayScale(Bitmap image, RGBcoefficientType otype)
        {
            Bitmap dstimage = null;
            try
            {
                GrayScaleCoef coef;
                if (otype == RGBcoefficientType.BT709)
                    coef = new GrayScaleCoef(0.2125, 0.7154, 0.0721);
                else
                    coef = new GrayScaleCoef(0.5, 0.419, 0.081);

                BitmapData sourceData, destinationData;

                sourceData = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                dstimage = CreateGrayscaleImage(image.Width, image.Height);
                destinationData = dstimage.LockBits(new Rectangle(0, 0, dstimage.Width, dstimage.Height), ImageLockMode.ReadWrite, dstimage.PixelFormat);
                GetGrayScaleImage(sourceData, destinationData, coef);
                dstimage.UnlockBits(destinationData);
                image.UnlockBits(sourceData);
            }
            catch
            {

            }
            return dstimage;
        }
Esempio n. 2
0
        public static Bitmap GrayScale(Bitmap image, RGBcoefficientType otype)
        {
            Bitmap dstimage = null;

            try
            {
                GrayScaleCoef coef;
                if (otype == RGBcoefficientType.BT709)
                {
                    coef = new GrayScaleCoef(0.2125, 0.7154, 0.0721);
                }
                else
                {
                    coef = new GrayScaleCoef(0.5, 0.419, 0.081);
                }


                BitmapData sourceData, destinationData;

                sourceData      = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, image.PixelFormat);
                dstimage        = CreateGrayscaleImage(image.Width, image.Height);
                destinationData = dstimage.LockBits(new Rectangle(0, 0, dstimage.Width, dstimage.Height), ImageLockMode.ReadWrite, dstimage.PixelFormat);
                GetGrayScaleImage(sourceData, destinationData, coef);
                dstimage.UnlockBits(destinationData);
                image.UnlockBits(sourceData);
            }
            catch
            {
            }
            return(dstimage);
        }
Esempio n. 3
0
        private static unsafe void GetGrayScaleImage(BitmapData sourceData, BitmapData destinationData, GrayScaleCoef coef)
        {
            // get width and height
            int width = sourceData.Width;
            int height = sourceData.Height;

            PixelFormat srcPixelFormat = sourceData.PixelFormat;

            if (
                (srcPixelFormat == PixelFormat.Format24bppRgb) ||
                (srcPixelFormat == PixelFormat.Format32bppRgb) ||
                (srcPixelFormat == PixelFormat.Format32bppArgb))
            {
                int pixelSize = (srcPixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
                int srcOffset = sourceData.Stride - width * pixelSize;
                int dstOffset = destinationData.Stride - width;

                // do the job
                byte* src = (byte*)sourceData.Scan0.ToPointer();
                byte* dst = (byte*)destinationData.Scan0.ToPointer();

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, src += pixelSize, dst++)
                    {
                        *dst = (byte)(coef.cr * src[2] + coef.cg * src[1] + coef.cb * src[0]);
                    }
                    src += srcOffset;
                    dst += dstOffset;
                }
            }
        }
Esempio n. 4
0
        private static unsafe void GetGrayScaleImage(BitmapData sourceData, BitmapData destinationData, GrayScaleCoef coef)
        {
            // get width and height
            int width  = sourceData.Width;
            int height = sourceData.Height;


            PixelFormat srcPixelFormat = sourceData.PixelFormat;

            if (
                (srcPixelFormat == PixelFormat.Format24bppRgb) ||
                (srcPixelFormat == PixelFormat.Format32bppRgb) ||
                (srcPixelFormat == PixelFormat.Format32bppArgb))
            {
                int pixelSize = (srcPixelFormat == PixelFormat.Format24bppRgb) ? 3 : 4;
                int srcOffset = sourceData.Stride - width * pixelSize;
                int dstOffset = destinationData.Stride - width;

                // do the job
                byte *src = (byte *)sourceData.Scan0.ToPointer();
                byte *dst = (byte *)destinationData.Scan0.ToPointer();

                // for each line
                for (int y = 0; y < height; y++)
                {
                    // for each pixel
                    for (int x = 0; x < width; x++, src += pixelSize, dst++)
                    {
                        *dst = (byte)(coef.cr * src[2] + coef.cg * src[1] + coef.cb * src[0]);
                    }
                    src += srcOffset;
                    dst += dstOffset;
                }
            }
        }