示例#1
0
        public IBitmap Apply(BitmapPixelData bmData)
        {
            int width  = bmData.Width;
            int height = bmData.Height;

            PixelFormat dstPixelFormat = FormatTranslations[bmData.PixelFormat];

            // create new image of required format
            IBitmap dstImage = (dstPixelFormat == PixelFormat.Gray8) ?
                               Helper.CreateGrayscaleImage(width, height) :
                               GraphicsEngine.Current.Engine.CreateBitmap(width, height, dstPixelFormat);

            // lock destination bitmap data
            BitmapPixelData dstData = dstImage.LockBitmapPixelData(
                BitmapLockMode.ReadWrite, dstPixelFormat);

            try
            {
                ProcessFilter(bmData, dstData);
            }
            finally
            {
                // unlock destination images
                dstImage.UnlockBitmapPixelData(dstData);
            }

            return(dstImage);
        }
示例#2
0
        public bool IsEmptyBitmap(IBitmap bitmap, ArgbColor backgroundColor)
        {
            BitmapPixelData bmData = null;

            try
            {
                bmData = bitmap.LockBitmapPixelData(BitmapLockMode.ReadOnly, PixelFormat.Rgba32);

                int    stride = bmData.Stride;
                IntPtr Scan0  = bmData.Scan0;

                int backgroundColorValue = ArgbColor.FromArgb(
                    backgroundColor.A,
                    backgroundColor.R,
                    backgroundColor.G,
                    backgroundColor.B).ToArgb();

                unsafe
                {
                    byte *p       = (byte *)(void *)Scan0;
                    int   nOffset = stride - bitmap.Width * 4;

                    for (int y = 0; y < bitmap.Height; ++y)
                    {
                        for (int x = 0; x < bitmap.Width; ++x)
                        {
                            byte blue  = p[0];
                            byte green = p[1];
                            byte red   = p[2];
                            byte alpha = p[3];

                            if (alpha != 0)  // Not transparent
                            {
                                int pixelValue = ArgbColor.FromArgb(alpha, red, green, blue).ToArgb();

                                if (!pixelValue.Equals(backgroundColorValue))
                                {
                                    return(false);
                                }
                            }
                            p += 4;
                        }

                        p += nOffset;
                    }
                }
            }
            finally
            {
                if (bitmap != null && bmData != null)
                {
                    bitmap.UnlockBitmapPixelData(bmData);
                }
            }
            return(true);
        }
示例#3
0
        public double GetStdDev(IBitmap bitmap)
        {
            double total = 0, totalVariance = 0;
            int    count  = 0;
            double stdDev = 0;

            // First get all the bytes
            BitmapPixelData bmData = null;

            try
            {
                bmData = bitmap.LockBitmapPixelData(BitmapLockMode.ReadOnly, bitmap.PixelFormat);

                int    stride = bmData.Stride;
                IntPtr Scan0  = bmData.Scan0;

                unsafe
                {
                    byte *p       = (byte *)(void *)Scan0;
                    int   nOffset = stride - bitmap.Width * 3;

                    for (int y = 0; y < bitmap.Height; ++y)
                    {
                        for (int x = 0; x < bitmap.Width; ++x)
                        {
                            count++;

                            byte blue  = p[0];
                            byte green = p[1];
                            byte red   = p[2];

                            int pixelValue = ArgbColor.FromArgb(0, red, green, blue).ToArgb();

                            total += pixelValue;
                            double avg = total / count;

                            totalVariance += Math.Pow(pixelValue - avg, 2);
                            stdDev         = Math.Sqrt(totalVariance / count);

                            p += 3;
                        }

                        p += nOffset;
                    }
                }
            }
            finally
            {
                if (bmData != null)
                {
                    bitmap.UnlockBitmapPixelData(bmData);
                }
            }

            return(stdDev);
        }
示例#4
0
        public IBitmap Apply(IBitmap bitmap)
        {
            var bmData = bitmap.LockBitmapPixelData(
                BitmapLockMode.ReadWrite, bitmap.PixelFormat);

            IBitmap dstImage = null;

            try
            {
                dstImage = Apply(bmData);
                if ((bitmap.DpiX > 0) && (bitmap.DpiY > 0))
                {
                    dstImage.SetResolution(bitmap.DpiX, bitmap.DpiY);
                }
            }
            finally
            {
                bitmap.UnlockBitmapPixelData(bmData);
            }

            return(dstImage);
        }
示例#5
0
        static public Bitmap ToGdiBitmap(this IBitmap iBitmap)
        {
            if (iBitmap.EngineElement == null)
            {
                return(new Bitmap(1, 1));
            }

            if (iBitmap?.EngineElement is Bitmap)
            {
                return((Bitmap)iBitmap.EngineElement);
            }

            BitmapPixelData bmPixelData = null;

            try
            {
                bmPixelData = iBitmap.LockBitmapPixelData(BitmapLockMode.Copy, PixelFormat.Rgba32);
                var bm = new Bitmap(bmPixelData.Width,
                                    bmPixelData.Height,
                                    bmPixelData.Stride,
                                    System.Drawing.Imaging.PixelFormat.Format32bppArgb,
                                    bmPixelData.Scan0);

                bmPixelData.Scan0 = IntPtr.Zero;  // Don't give it back...

                //bm.Save($"C:\\temp\\gr_{ Guid.NewGuid().ToString() }.png", System.Drawing.Imaging.ImageFormat.Png);

                return(bm);
            }
            finally
            {
                if (bmPixelData != null)
                {
                    iBitmap.UnlockBitmapPixelData(bmPixelData);
                }
            }
        }
示例#6
0
        public void Encode(IBitmap bitmap, Stream stream, ImageFormat format, int quality = 0)
        {
            if (bitmap == null)
            {
                throw new ArgumentException("Bitmap is NULL");
            }

            if (bitmap.EngineElement is System.Drawing.Bitmap)
            {
                ((System.Drawing.Bitmap)bitmap.EngineElement).Save(stream, format.ToImageFormat());
            }
            else
            {
                BitmapPixelData pixelData = null;
                try
                {
                    pixelData = bitmap.LockBitmapPixelData(BitmapLockMode.ReadWrite, bitmap.PixelFormat);
                    using (var bm = new System.Drawing.Bitmap(
                               pixelData.Width,
                               pixelData.Height,
                               pixelData.Stride,
                               pixelData.PixelFormat.ToGdiPixelFormat(),
                               pixelData.Scan0))
                    {
                        bm.Save(stream, format.ToImageFormat());
                    }
                }
                finally
                {
                    if (pixelData != null)
                    {
                        bitmap.UnlockBitmapPixelData(pixelData);
                    }
                }
            }
        }