예제 #1
0
 /// <summary>
 /// Gray process.
 /// </summary>
 /// <param name="src">Source image.</param>
 /// <returns></returns>
 public static WriteableBitmap GrayProcess(this WriteableBitmap src) ////1 灰度化处理
 {
     if (src != null)
     {
         var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
         int             w         = cloneWriteableBitmap.PixelWidth;
         int             h         = cloneWriteableBitmap.PixelHeight;
         WriteableBitmap grayImage = new WriteableBitmap(w, h);
         byte[]          temp      = cloneWriteableBitmap.PixelBuffer.ToArray();
         for (int i = 0; i < temp.Length; i += 4)
         {
             byte tempByte = (byte)((int)(temp[i] * 0.114 + temp[i + 1] * 0.587 + temp[i + 2] * 0.299));
             temp[i]     = tempByte;
             temp[i + 1] = tempByte;
             temp[i + 2] = tempByte;
         }
         Stream sTemp = grayImage.PixelBuffer.AsStream();
         sTemp.Seek(0, SeekOrigin.Begin);
         sTemp.Write(temp, 0, w * 4 * h);
         return(grayImage);
     }
     else
     {
         return(null);
     }
 }
        /// <summary>
        /// Mosaic process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="v">The threshould to control the result of mosaic process.</param>
        /// <returns></returns>
        public static WriteableBitmap MosaicProcess(this WriteableBitmap src, int v)////图像马赛克效果
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w        = cloneWriteableBitmap.PixelWidth;
                int             h        = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[]          temp     = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                int             dR       = 0;
                int             dG       = 0;
                int             dB       = 0;
                int             dstX     = 0;
                int             dstY     = 0;

                dR = tempMask[2];
                dG = tempMask[1];
                dB = tempMask[0];

                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        dstX = i;
                        dstY = j;
                        if (j % v == 0)
                        {
                            if (i % v == 0)
                            {
                                dB = tempMask[dstX * 4 + dstY * w * 4];
                                dG = tempMask[dstX * 4 + 1 + dstY * w * 4];
                                dR = tempMask[dstX * 4 + 2 + dstY * w * 4];
                            }
                            else
                            {
                                temp[dstX * 4 + dstY * w * 4]     = (byte)dB;
                                temp[dstX * 4 + 1 + dstY * w * 4] = (byte)dG;
                                temp[dstX * 4 + 2 + dstY * w * 4] = (byte)dR;
                            }
                        }
                        else
                        {
                            temp[dstX * 4 + dstY * w * 4]     = temp[dstX * 4 + (dstY - 1) * w * 4];
                            temp[dstX * 4 + 1 + dstY * w * 4] = temp[dstX * 4 + 1 + (dstY - 1) * w * 4];
                            temp[dstX * 4 + 2 + dstY * w * 4] = temp[dstX * 4 + 2 + (dstY - 1) * w * 4];
                        }
                    }
                }

                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(srcImage);
            }
            else
            {
                return(null);
            }
        }
예제 #3
0
        /*
         * public static WriteableBitmap WriteableBitmapBlur(this WriteableBitmap writeableBitmapOld, int kernelWidth, int kernelHeight)
         * {
         *  var cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(writeableBitmapOld);
         *  WriteableBitmapConvolutionExtensions.GaussianBlur(cloneWriteableBitmap, kernelWidth, kernelHeight);
         *  cloneWriteableBitmap.Invalidate();
         *  return cloneWriteableBitmap;
         * }
         */

        /// <summary>
        /// 高斯滤波
        /// </summary>
        /// <param name="writeableBitmapOld"></param>
        /// <param name="radius">0 to 100</param>
        /// <param name="sigma">0 to 30</param>
        /// <returns></returns>
        public static WriteableBitmap WriteableBitmapBlur(this WriteableBitmap writeableBitmapOld, int radius, double sigma)
        {
            var cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(writeableBitmapOld);
            var writeableBitmap      = WriteableBitmapConvolutionExtensions.GaussFilter(cloneWriteableBitmap, radius, sigma);

            writeableBitmap.Invalidate();
            return(writeableBitmap);
        }
예제 #4
0
        public static WriteableBitmap WriteableBitmapBlur(this WriteableBitmap writeableBitmapOld)
        {
            var cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(writeableBitmapOld);

            WriteableBitmapConvolutionExtensions.GaussianBlur(cloneWriteableBitmap);
            cloneWriteableBitmap.Invalidate();
            return(cloneWriteableBitmap);
        }
예제 #5
0
        /// <summary>
        /// Sun light process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="A">X location of light source.</param>
        /// <param name="B">Y location of light source.</param>
        /// <param name="thresould">Light intensity value.</param>
        /// <returns></returns>
        public static WriteableBitmap SunlightProcess(this WriteableBitmap src, int X, int Y, float thresould)////41图像光照函数
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w = cloneWriteableBitmap.PixelWidth;
                int             h = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[]          temp = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                double          b = 0, g = 0, r = 0;

                if (X >= w || Y >= h || X < 0 || Y < 0)
                {
                    X = w / 2;
                    Y = h / 2;
                }

                Point Cen  = new Point(X, Y);
                int   R    = Math.Min(X, Y);
                float curR = 0;
                float pixelValue = 0;

                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        b    = tempMask[i * 4 + j * w * 4];
                        g    = tempMask[i * 4 + 1 + j * w * 4];
                        r    = tempMask[i * 4 + 2 + j * w * 4];
                        curR = (float)Math.Sqrt(Math.Pow((i - Cen.X), 2) + Math.Pow((j - Cen.Y), 2));

                        if (curR < R)
                        {
                            pixelValue = thresould * (1.0f - curR / R);
                            b          = b + pixelValue;
                            g          = g + pixelValue;
                            r          = r + pixelValue;
                            temp[i * 4 + j * w * 4]     = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);
                            temp[i * 4 + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);
                            temp[i * 4 + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);
                            b = 0; g = 0; r = 0;
                        }
                    }
                }

                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(srcImage);
            }
            else
            {
                return(null);
            }
        }
예제 #6
0
        public static WriteableBitmap WriteableBitmapBlur(this WriteableBitmap writeableBitmapOld, int x, int y)
        {
            if ((x & 1) == 0 || (y & 1) == 0)
            {
                return(null);
            }
            var cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(writeableBitmapOld);

            WriteableBitmapConvolutionExtensions.BoxBlur(cloneWriteableBitmap, x, y);
            cloneWriteableBitmap.Invalidate();
            return(cloneWriteableBitmap);
        }
예제 #7
0
 public YouTubeFlvToMp3OrAac(string name, IRandomAccessStream ias)
 {
     this._name = name;
     _warnings  = new List <string>();
     if (ias == null)
     {
         return;
     }
     _fs         = WriteableBitmapExpansion.ConvertIRandomAccessStreamToMemoryStream(ias);
     _fileOffset = 0;
     _fileLength = _fs.Length;
     //ExtractStreams();
 }
        /**
         * Extracts image pixels into byte array "pixels"
         */
        protected void GetImagePixels(IRandomAccessStream iRandomAccessStream)
        {
            /*Point pointWH = await WriteableBitmapExpansion.GetPixelWidthAndHeight(iRandomAccessStream);
             * int w = (int)pointWH.X;
             * int h = (int)pointWH.Y;*/
            int count = 0;

            byte[] tempByte = null;

            /*
             * //进行统一尺寸的格式压缩
             * if ((w != width)
             || (h != height)
             || )
             ||{
             || var bytes = await WriteableBitmapExpansion.ResizeBytes(iRandomAccessStream, width, height, BitmapInterpolationMode.Cubic);
             || pixels = new Byte[3 * width * height];
             || pointWH = new Point(width, height);
             || tempByte = bytes;
             ||}
             ||else
             ||{
             || pointWH = await WriteableBitmapExpansion.GetPixelWidthAndHeight(imageIAStream);
             || pixels = new Byte[3 * (int)pointWH.X * (int)pointWH.Y];
             || tempByte = await WriteableBitmapExpansion.WriteableBitmapToBytes(imageIAStream);
             ||}
             */

            pixels = new Byte[3 * (int)width * (int)height];
            /*tempByte = await WriteableBitmapExpansion.WriteableBitmapToBytes(imageIAStream);*/
            Stream inputStream = WindowsRuntimeStreamExtensions.AsStreamForRead(imageIAStream.GetInputStreamAt(0));

            tempByte = inputStream.ConvertStreamTobyte();

            for (int th = 0; th < height; th++)
            {
                for (int tw = 0; tw < width; tw++)
                {
                    Color color = WriteableBitmapExpansion.GetPixel(tempByte, Convert.ToInt32(width), tw, th);
                    pixels[count] = color.R;
                    count++;
                    pixels[count] = color.G;
                    count++;
                    pixels[count] = color.B;
                    count++;
                }
            }
        }
예제 #9
0
        private async void InitFlvToMp3OrAac(StorageFile storageFile, bool isFileSavePicker)
        {
            this.storageFile = storageFile;
            //this._name = Path.GetFileNameWithoutExtension(storageFile.Name);
            this._name            = storageFile.DisplayName;
            this.isFileSavePicker = isFileSavePicker;
            //var ias = storageFile.OpenAsync(FileAccessMode.Read).GetResults();
            var ias = await storageFile.OpenAsync(FileAccessMode.Read);

            _warnings = new List <string>();
            if (ias == null)
            {
                return;
            }
            _fs         = WriteableBitmapExpansion.ConvertIRandomAccessStreamToMemoryStream(ias);
            _fileOffset = 0;
            _fileLength = _fs.Length;
            //ExtractStreams();
        }
예제 #10
0
        private async void MoveOutControlBlankPage_Loaded(object sender, RoutedEventArgs e)
        {
            RandomAccessStreamReference        rass         = RandomAccessStreamReference.CreateFromUri(new Uri("http://ww1.sinaimg.cn/bmiddle/643be833jw1e4nzv4dc12j20dc0hsq4g.jpg", UriKind.RelativeOrAbsolute));
            IRandomAccessStreamWithContentType streamRandom = await rass.OpenReadAsync();

            var cloneStream = streamRandom.CloneStream();

            wb = new WriteableBitmap(1, 1);
            //wb = await (new WriteableBitmap(1, 1).FromStream(streamRandom));
            await wb.SetSourceAsync(streamRandom);

            //this.image.Source = wb;
            wbOriginal = WriteableBitmapExpansion.CopyWriteableBitmap(wb);
            var bi = new BitmapImage();
            await bi.SetSourceAsync(cloneStream);

            this.imageOriginal.Source = bi;

            this.image.Source = BlurEffect.WriteableBitmapBlur(wb, Convert.ToInt32(sliderRadius.Value), sliderSigma.Value);
        }
예제 #11
0
        /// <summary>
        /// Redeye remove Process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static WriteableBitmap RedeyeRemoveProcess(this WriteableBitmap src)////红眼去除
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w = cloneWriteableBitmap.PixelWidth;
                int             h = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[]          temp = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                int             r, g, b;
                int             Rc, Gc, Bc;

                for (int i = 0; i < temp.Length; i += 4)
                {
                    b = tempMask[i];
                    g = tempMask[i + 1];
                    r = tempMask[i + 2];

                    if (r > (int)(g + b))//这里 只是简单的判断一下红眼像素只为说明红眼去除算法,实际上要使用一定的红眼判断算法决策
                    {
                        Rc          = (int)((g + b) / 2);
                        Gc          = (int)((g + Rc) / 2);
                        Bc          = (int)((b + Rc) / 2);
                        temp[i]     = (byte)Bc;
                        temp[i + 1] = (byte)Gc;
                        temp[i + 2] = (byte)Rc;
                    }
                }

                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(srcImage);
            }
            else
            {
                return(null);
            }
        }
예제 #12
0
        /// <summary>
        /// Wallis sharpen process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <returns></returns>
        public static WriteableBitmap WallisSharpen(this WriteableBitmap src)////37Wallis锐化函数
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w = cloneWriteableBitmap.PixelWidth;
                int             h = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap sharpenImage = new WriteableBitmap(w, h);
                byte[]          temp = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                double          b = 0, g = 0, r = 0, srR = 0, srG = 0, srB = 0;

                for (int j = 1; j < h - 1; j++)
                {
                    for (int i = 4; i < w * 4 - 4; i += 4)
                    {
                        srB = tempMask[i + j * w * 4];
                        srG = tempMask[i + 1 + j * w * 4];
                        srR = tempMask[i + 2 + j * w * 4];
                        b   = 46 * Math.Abs(5 * Math.Log(srB + 1) - Math.Log(tempMask[i - 4 + j * w * 4] + 1) - Math.Log(tempMask[i + 4 + j * w * 4] + 1) - Math.Log(tempMask[i + (j - 1) * w * 4] + 1) - Math.Log(tempMask[i + (j + 1) * w * 4] + 1));
                        g   = 46 * Math.Abs(5 * Math.Log(srG + 1) - Math.Log(tempMask[i - 4 + 1 + j * w * 4] + 1) - Math.Log(tempMask[i + 4 + 1 + j * w * 4] + 1) - Math.Log(tempMask[i + 1 + (j - 1) * w * 4] + 1) - Math.Log(tempMask[i + 1 + (j + 1) * w * 4] + 1));
                        r   = 46 * Math.Abs(5 * Math.Log(srR + 1) - Math.Log(tempMask[i - 4 + 2 + j * w * 4] + 1) - Math.Log(tempMask[i + 4 + 2 + j * w * 4] + 1) - Math.Log(tempMask[i + 2 + (j - 1) * w * 4] + 1) - Math.Log(tempMask[i + 2 + (j + 1) * w * 4] + 1));
                        temp[i + j * w * 4]     = (byte)(b > 0 ? (b < 255 ? b : 255) : 0);
                        temp[i + 1 + j * w * 4] = (byte)(g > 0 ? (g < 255 ? g : 255) : 0);
                        temp[i + 2 + j * w * 4] = (byte)(r > 0 ? (r < 255 ? r : 255) : 0);
                        b = 0; g = 0; r = 0; srR = 0; srG = 0; srB = 0;
                    }
                }

                Stream sTemp = sharpenImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(sharpenImage);
            }
            else
            {
                return(null);
            }
        }
        /// <summary>
        /// Atomization process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="v">The threshould to control the effect of atomization.</param>
        /// <returns></returns>
        public static WriteableBitmap AtomizationProcess(this WriteableBitmap src, int v)////45图像雾化
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w        = cloneWriteableBitmap.PixelWidth;
                int             h        = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[]          temp     = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                Random          ran      = new Random();
                int             k        = 0;
                int             dx       = 0;
                int             dy       = 0;

                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        k  = ran.Next(v);
                        dx = (i + k) >= w ? w - 1 : (i + k);
                        dy = (j + k) >= h ? h - 1 : (j + k);
                        temp[i * 4 + j * w * 4]     = (byte)tempMask[dx * 4 + dy * w * 4];
                        temp[i * 4 + 1 + j * w * 4] = (byte)tempMask[dx * 4 + 1 + dy * w * 4];
                        temp[i * 4 + 2 + j * w * 4] = (byte)tempMask[dx * 4 + 2 + dy * w * 4];
                    }
                }

                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(srcImage);
            }
            else
            {
                return(null);
            }
        }
예제 #14
0
        /// <summary>
        /// Entropy max method of image segmention.
        /// </summary>
        /// <param name="src">The source iamge.</param>
        /// <returns></returns>
        public static WriteableBitmap EntropymaxThSegment(this WriteableBitmap src) ////一维熵最大法阈值分割
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w        = cloneWriteableBitmap.PixelWidth;
                int             h        = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap dstImage = new WriteableBitmap(w, h);
                byte[]          temp     = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                //定义灰度图像信息存储变量
                int[] srcData = new int[w * h];
                //定义阈值变量
                int Th = 0;
                //定义直方图存储变量
                int[] histogram = new int[256];
                //定义熵值变量
                double Ht    = 0.0;
                double Hl    = 0.0;
                double sigma = 0.0;
                //定义灰度最值变量
                int max = 0;
                int min = 255;

                //定义临时变量
                double t = 0.0, pt = 0.0, tempMax = 0.0;
                int    tempV = 0;

                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        tempV = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4] * 0.299);
                        srcData[i + j * w] = tempV;
                        histogram[tempV]++;

                        if (tempV > max)
                        {
                            max = tempV;
                        }

                        if (tempV < min)
                        {
                            min = tempV;
                        }
                    }
                }

                for (int i = min; i < max; i++)
                {
                    t = (double)((double)histogram[i] / (double)(w * h));
                    if (t > 0.00000001)
                    {
                        Hl += -t *Math.Log10(t);
                    }
                    else
                    {
                        continue;
                    }
                }
                for (int i = min; i < max; i++)
                {
                    t   = (double)((double)histogram[i] / (double)(w * h));
                    pt += t;
                    if (t > 0.00000001)
                    {
                        Ht += -t *Math.Log10(t);

                        sigma = Math.Log10(pt * (1 - pt)) * Ht / pt + (Hl - Ht) / (1 - pt);
                        if (sigma > tempMax)
                        {
                            tempMax = (int)sigma;
                            Th      = i;
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255);
                    }
                }
                Stream sTemp = dstImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(dstImage);
            }
            else
            {
                return(null);
            }
        }
예제 #15
0
        /// <summary>
        /// P-Parameter method of image segmention.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="P">The ratio of object, from 0 to 1.</param>
        /// <returns></returns>
        public static WriteableBitmap PParameterThSegment(this WriteableBitmap src, double P) ////P参数法阈值分割
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w        = cloneWriteableBitmap.PixelWidth;
                int             h        = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap dstImage = new WriteableBitmap(w, h);
                byte[]          temp     = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                //定义灰度图像信息存储变量
                int[] srcData = new int[w * h];
                //定义背景和目标像素个数变量
                int C1 = 0, C2 = 0;
                //定义阈值变量
                int Th = 0;

                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        srcData[i + j * w] = (int)((double)tempMask[i * 4 + j * w * 4] * 0.114 + (double)tempMask[i * 4 + 1 + j * w * 4] * 0.587 + (double)tempMask[i * 4 + 2 + j * w * 4] * 0.299);
                    }
                }

                for (int T = 0; T <= 255; T++)
                {
                    for (int i = 0; i < srcData.Length; i++)
                    {
                        if (srcData[i] > T)
                        {
                            C1++;
                        }
                        else
                        {
                            C2++;
                        }
                    }
                    double t = Math.Abs((double)((double)C1 / ((double)C1 + (double)C2)) - P);
                    if (t < 0.01)
                    {
                        Th = T;
                        break;
                    }
                    C1 = 0;
                    C2 = 0;
                }
                for (int j = 0; j < h; j++)
                {
                    for (int i = 0; i < w; i++)
                    {
                        temp[i * 4 + j * w * 4] = temp[i * 4 + 1 + j * w * 4] = temp[i * 4 + 2 + j * w * 4] = (byte)(srcData[i + j * w] < Th ? 0 : 255);
                    }
                }
                Stream sTemp = dstImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(dstImage);
            }
            else
            {
                return(null);
            }
        }
예제 #16
0
        /// <summary>
        /// Motion blur process.
        /// </summary>
        /// <param name="src">The source image.</param>
        /// <param name="k">The offset of motion, from 0 to 200.</param>
        /// <param name="direction">The direction of motion, x:1, y:2.</param>
        /// <returns></returns>
        public static WriteableBitmap MotionblurProcess(WriteableBitmap src, int k, int direction)////运动模糊处理
        {
            if (src != null)
            {
                var             cloneWriteableBitmap = WriteableBitmapExpansion.CopyWriteableBitmap(src);
                int             w = cloneWriteableBitmap.PixelWidth;
                int             h = cloneWriteableBitmap.PixelHeight;
                WriteableBitmap srcImage = new WriteableBitmap(w, h);
                byte[]          temp = cloneWriteableBitmap.PixelBuffer.ToArray();
                byte[]          tempMask = (byte[])temp.Clone();
                int             b, g, r;
                for (int y = 0; y < h; y++)
                {
                    for (int x = 0; x < w; x++)
                    {
                        b = g = r = 0;
                        switch (direction)
                        {
                        case 1:
                            if (x >= k)
                            {
                                for (int i = 0; i <= k; i++)
                                {
                                    b += (int)tempMask[(x - i) * 4 + y * w * 4];
                                    g += (int)tempMask[(x - i) * 4 + 1 + y * w * 4];
                                    r += (int)tempMask[(x - i) * 4 + 2 + y * w * 4];
                                }
                                temp[x * 4 + y * w * 4]     = (byte)(b / (k + 1));
                                temp[x * 4 + 1 + y * w * 4] = (byte)(g / (k + 1));
                                temp[x * 4 + 2 + y * w * 4] = (byte)(r / (k + 1));
                            }
                            else
                            {
                                if (x > 0)
                                {
                                    for (int i = 0; i < x; i++)
                                    {
                                        b += (int)tempMask[(x - i) * 4 + y * w * 4];
                                        g += (int)tempMask[(x - i) * 4 + 1 + y * w * 4];
                                        r += (int)tempMask[(x - i) * 4 + 2 + y * w * 4];
                                    }
                                    temp[x * 4 + y * w * 4]     = (byte)(b / (x + 1));
                                    temp[x * 4 + 1 + y * w * 4] = (byte)(g / (x + 1));
                                    temp[x * 4 + 2 + y * w * 4] = (byte)(r / (x + 1));
                                }
                                else
                                {
                                    temp[x * 4 + y * w * 4]     = (byte)(tempMask[x * 4 + y * w * 4] / k);
                                    temp[x * 4 + 1 + y * w * 4] = (byte)(tempMask[x * 4 + 1 + y * w * 4] / k);
                                    temp[x * 4 + 2 + y * w * 4] = (byte)(tempMask[x * 4 + 2 + y * w * 4] / k);
                                }
                            }
                            break;

                        case 2:
                            if (y >= k)
                            {
                                for (int i = 0; i <= k; i++)
                                {
                                    b += (int)tempMask[x * 4 + (y - i) * w * 4];
                                    g += (int)tempMask[x * 4 + 1 + (y - i) * w * 4];
                                    r += (int)tempMask[x * 4 + 2 + (y - i) * w * 4];
                                }
                                temp[x * 4 + y * w * 4]     = (byte)(b / (k + 1));
                                temp[x * 4 + 1 + y * w * 4] = (byte)(g / (k + 1));
                                temp[x * 4 + 2 + y * w * 4] = (byte)(r / (k + 1));
                            }
                            else
                            {
                                if (y > 0)
                                {
                                    for (int i = 0; i < y; i++)
                                    {
                                        b += (int)tempMask[x * 4 + (y - i) * w * 4];
                                        g += (int)tempMask[x * 4 + 1 + (y - i) * w * 4];
                                        r += (int)tempMask[x * 4 + 2 + (y - i) * w * 4];
                                    }
                                    temp[x * 4 + y * w * 4]     = (byte)(b / (y + 1));
                                    temp[x * 4 + 1 + y * w * 4] = (byte)(g / (y + 1));
                                    temp[x * 4 + 2 + y * w * 4] = (byte)(r / (y + 1));
                                }
                                else
                                {
                                    temp[x * 4 + y * w * 4]     = (byte)(tempMask[x * 4 + y * w * 4] / k);
                                    temp[x * 4 + 1 + y * w * 4] = (byte)(tempMask[x * 4 + 1 + y * w * 4] / k);
                                    temp[x * 4 + 2 + y * w * 4] = (byte)(tempMask[x * 4 + 2 + y * w * 4] / k);
                                }
                            }
                            break;

                        default:
                            break;
                        }
                    }
                }

                Stream sTemp = srcImage.PixelBuffer.AsStream();
                sTemp.Seek(0, SeekOrigin.Begin);
                sTemp.Write(temp, 0, w * 4 * h);
                return(srcImage);
            }
            else
            {
                return(null);
            }
        }