Ejemplo n.º 1
0
        public void addWordData(Skeleton s,
                                WriteableBitmap colorLeft, bool isColorLeftMove,
                                WriteableBitmap colorRight, bool isColorRightMove,
                                WriteableBitmap depthLeft, bool isDepthLeftMove,
                                WriteableBitmap depthRight, bool isDepthRightMove)
        {
            if (!isColorLeftMove)
            {
                if (wordDataList.Count == 0)
                {
                    colorLeft = blackTemplate.Clone();
                }
                else
                {
                    colorLeft = wordDataList.Last().colorLeftBitmapData.Clone();
                }
            }
            if (!isColorRightMove)
            {
                if (wordDataList.Count == 0)
                {
                    colorRight = blackTemplate.Clone();
                }
                else
                {
                    colorRight = wordDataList.Last().colorRightBitmapData.Clone();
                }
            }
            if (!isDepthLeftMove)
            {
                if (wordDataList.Count == 0)
                {
                    depthLeft = blackTemplate.Clone();
                }
                else
                {
                    depthLeft = wordDataList.Last().depthLeftBitmapData.Clone();
                }
            }
            if (!isDepthRightMove)
            {
                if (wordDataList.Count == 0)
                {
                    depthRight = blackTemplate.Clone();
                }
                else
                {
                    depthRight = wordDataList.Last().depthRightBitmapData.Clone();
                }
            }

            wordDataList.Add(new WordDataContainer(s, colorLeft, colorRight, depthLeft, depthRight));
        }
Ejemplo n.º 2
0
 public IImage Clone()
 {
     if (pixelWorking == null)
     {
         ImageAdapter adapter = new ImageAdapter();
         adapter.FromBitmapSource(Target.Clone());
         adapter.AnimationDelay = this.AnimationDelay;
         return(adapter);
     }
     else
     {
         throw new InvalidOperationException("Pixel working is active. Can't clone");
     }
 }
Ejemplo n.º 3
0
        private void blurGeneratorWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            this.GenerationProgressIndicator.IsVisible = false;

            if (!e.Cancelled && e.Error == null)
            {
                WriteableBitmap bitmap = new WriteableBitmap((e.Result as BlurGenTaskData).width, (e.Result as BlurGenTaskData).height);

                (e.Result as BlurGenTaskData).pixels.CopyTo(bitmap.Pixels, 0);

                this.editedImageChanged = true;
                this.selectedMode       = MODE_SCROLL;

                this.undoStack.Clear();

                this.originalBitmap = this.loadedBitmap.Clone();
                this.effectedBitmap = bitmap.Clone();
                this.editedBitmap   = bitmap.Clone();

                this.loadedBitmap = null;

                if (this.editedBitmap.PixelWidth > this.editedBitmap.PixelHeight)
                {
                    this.currentScale = this.EditorGrid.ActualWidth / this.editedBitmap.PixelWidth;
                }
                else
                {
                    this.currentScale = this.EditorGrid.ActualHeight / this.editedBitmap.PixelHeight;
                }

                this.EditorImage.Visibility = System.Windows.Visibility.Visible;
                this.EditorImage.Source     = this.editedBitmap;

                this.EditorImageGrid.Width  = MAX_IMAGE_WIDTH;
                this.EditorImageGrid.Height = MAX_IMAGE_HEIGHT;

                this.EditorImageTransform.TranslateX = 0.0;
                this.EditorImageTransform.TranslateY = 0.0;
                this.EditorImageTransform.ScaleX     = this.currentScale;
                this.EditorImageTransform.ScaleY     = this.currentScale;

                int brush_width = Math.Max(1, Math.Min(Math.Min((int)(this.brushRadius / this.currentScale) * 2, this.editedBitmap.PixelWidth), this.editedBitmap.PixelHeight));

                this.brushBitmap = this.brushTemplateBitmap.Resize(brush_width, brush_width, WriteableBitmapExtensions.Interpolation.NearestNeighbor);

                UpdateModeButtons();
            }
        }
Ejemplo n.º 4
0
 /**
  * @fn		public void fn_SetSimulFrame(WriteableBitmap wb)
  * @brief	Simul Frame Setting
  * @return	void
  * @param	WriteableBitmap wb : Setting Image
  * @remark
  * -
  * @author	선경규(Kyeong Kyu - Seon)
  * @date	2020/10/5  16:21
  */
 public void fn_SetSimulFrame(WriteableBitmap wb)
 {
     if (wb != null)
     {
         m_SimulFrame = wb.Clone();
     }
 }
Ejemplo n.º 5
0
        private void SetBitmapImage()
        {
            BitmapSource img = new BitmapImage(imagePath);

            if (img.PixelHeight > height || img.PixelWidth > width)
            {
                img = new TransformedBitmap(img,
                                            new ScaleTransform(width / (double)(img.PixelWidth + 2 * minMargin), height / (double)(img.PixelHeight + 2 * minMargin)));
            }
            if (img.Format != PixelFormats.Bgr24)
            {
                img = new FormatConvertedBitmap(img, PixelFormats.Bgr24, null, 0);
            }

            this.imageBitmap = new WriteableBitmap(img);
            var host = new Image();

            Canvas.SetLeft(host, (width - imageBitmap.Width) / 2);
            Canvas.SetTop(host, (height - imageBitmap.Height) / 2);
            host.Source = imageBitmap;

            algorithmBitmap = imageBitmap.Clone();

            mainCanvas.Children.Clear();
            mainCanvas.Children.Add(host);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Draws rectangle on bitmap
        /// </summary>
        /// <param name="layer">Layer to operate on</param>
        /// <param name="coordinates">Starting pixel coordinate</param>
        /// <param name="color">Rectangle color</param>
        private async void RectangleAsync(Layer layer, Coordinates coordinates, Color color)
        {
            WriteableBitmap wb = layer.LayerBitmap;

            _toolIsExecuting = true;
            WriteableBitmap writeableBitmap = wb.Clone();

            while (Mouse.LeftButton == MouseButtonState.Pressed || Mouse.RightButton == MouseButtonState.Pressed)
            {
                //Two lines below are responsible for clearing last rectangle (on mouse move), to live show rectangle on bitmap
                wb.Clear();
                wb.Blit(new Rect(new Size(layer.Width, layer.Height)), writeableBitmap, new Rect(new Size(layer.Width, layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
                //Those ifs are changing direction of rectangle. In other words: flips rectangle on X and Y axis when needed
                if (coordinates.X > _activeCoordinates.X && coordinates.Y > _activeCoordinates.Y)
                {
                    wb.DrawRectangle(_activeCoordinates.X, _activeCoordinates.Y, coordinates.X, coordinates.Y, color);
                }
                else if (coordinates.X < _activeCoordinates.X && coordinates.Y < _activeCoordinates.Y)
                {
                    wb.DrawRectangle(coordinates.X, coordinates.Y, _activeCoordinates.X, _activeCoordinates.Y, color);
                }
                else if (coordinates.Y > _activeCoordinates.Y)
                {
                    wb.DrawRectangle(coordinates.X, _activeCoordinates.Y, _activeCoordinates.X, coordinates.Y, color);
                }
                else
                {
                    wb.DrawRectangle(_activeCoordinates.X, coordinates.Y, coordinates.X, _activeCoordinates.Y, color);
                }
                await Task.Delay(_asyncDelay);
            }
            _toolIsExecuting = false;
        }
Ejemplo n.º 7
0
        /// Event handler for Kinect sensor's ColorFrameReady event
        private void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
            {
                if (colorFrame != null)
                {
                    if (!(recording && readRgb))
                    {
                        // Copy the pixel data from the image to a temporary array
                        colorFrame.CopyPixelDataTo(this.colorPixels);

                        if (this.colorBitmap.IsFrozen)
                        {
                            this.colorBitmap_temp = null;
                            GC.Collect();
                            this.colorBitmap_temp = new WriteableBitmap(this.sensor.ColorStream.FrameWidth, this.sensor.ColorStream.FrameHeight, 96.0, 96.0, PixelFormats.Bgr32, null);
                        }
                        // Write the pixel data into our bitmap
                        this.colorBitmap.WritePixels(
                            new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight),
                            this.colorPixels,
                            this.colorBitmap.PixelWidth * sizeof(int),
                            0);
                        if (recording)
                        {
                            colorBitmap_temp = colorBitmap.Clone();
                            colorBitmap_temp.Freeze();
                            readRgb = true;
                        }
                    }
                }
            }
        }
Ejemplo n.º 8
0
        private void ImgSmall_MouseMove(object sender, MouseEventArgs e)
        {
            if (!_mouseDown)
            {
                return;
            }

            if (sender is Image image)
            {
                if (!image.IsMouseCaptured)
                {
                    return;
                }

                Point point = e.GetPosition(image);

                int x = (int)Math.Floor(point.X / 8);
                int y = (int)Math.Floor(point.Y / 8);

                if (x < 0 || y < 0 || x >= 8 || y >= 8)
                {
                    return;
                }

                WriteableBitmap writeableBmp = BitmapFactory.ConvertToPbgra32Format(image.Source as BitmapSource);

                SignalManager.Get <SelectedPixelSignal>().Dispatch(writeableBmp.Clone(), new Point(x, y));
            }
        }
Ejemplo n.º 9
0
        private void LoadFont(WriteableBitmap fontImage, string fontDefine)
        {
            SourceImage = fontImage.Clone();
            FntData     = string.Copy(fontDefine);

            LoadFontFromLocalData();
        }
Ejemplo n.º 10
0
 private void Button_Click_2(object sender, RoutedEventArgs e)
 {
     //revert
     editBitmap       = originalBitmap.Clone();
     MainImage.Source = editBitmap;
     vm.ImgChanged    = false;
 }
Ejemplo n.º 11
0
        /// <summary>
        /// Takes a bitmap whites out all the pixels of a color not of the given color
        /// </summary>
        /// <param name="bitmap">The bitmap to start with</param>
        /// <param name="color">The color to keep</param>
        /// <returns>A bitmap that only has the given color remaining</returns>
        public static WriteableBitmap EraseAllButCertainColor(WriteableBitmap bitmap, Color color)
        {
            int stride = (bitmap.PixelWidth * bitmap.Format.BitsPerPixel + 7) / 8;

            byte[] pixelByteArray    = new byte[bitmap.PixelHeight * stride];
            byte[] newPixelByteArray = new byte[bitmap.PixelHeight * stride];
            bitmap.CopyPixels(pixelByteArray, stride, 0);

            for (int i = 0; i < bitmap.PixelWidth; i++)
            {
                for (int j = 0; j < bitmap.PixelHeight; j++)
                {
                    int index = j * stride + 4 * i;
                    if (GetPixelColor(pixelByteArray, index) != color)
                    {
                        newPixelByteArray[index]     = 255;
                        newPixelByteArray[index + 1] = 255;
                        newPixelByteArray[index + 2] = 255;
                    }
                    else
                    {
                        newPixelByteArray[index]     = color.B;
                        newPixelByteArray[index + 1] = color.G;
                        newPixelByteArray[index + 2] = color.R;
                    }
                }
            }
            var newBitmap = bitmap.Clone();

            newBitmap.WritePixels(new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight), newPixelByteArray, stride, 0);

            return(newBitmap);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// Draws circle on bitmap.
        /// </summary>
        /// <param name="layer">Layer to operate on.</param>
        /// <param name="coordinates">Starting pixel coordinates.</param>
        /// <param name="color">Circle color.</param>
        private async void CircleAsync(Layer layer, Coordinates coordinates, Color color)
        {
            WriteableBitmap wb = layer.LayerBitmap;

            //Basically does the same like rectangle method, but with different shape
            _toolIsExecuting = true;
            WriteableBitmap bitmap = wb.Clone();

            while (Mouse.LeftButton == MouseButtonState.Pressed || Mouse.RightButton == MouseButtonState.Pressed)
            {
                wb.Clear();
                wb.Blit(new Rect(new Size(layer.Width, layer.Height)), bitmap, new Rect(new Size(layer.Width, layer.Height)), WriteableBitmapExtensions.BlendMode.Additive);
                if (coordinates.X > _activeCoordinates.X && coordinates.Y > _activeCoordinates.Y)
                {
                    wb.DrawEllipse(_activeCoordinates.X, _activeCoordinates.Y, coordinates.X, coordinates.Y, color);
                }
                else if (coordinates.X < _activeCoordinates.X && coordinates.Y < _activeCoordinates.Y)
                {
                    wb.DrawEllipse(coordinates.X, coordinates.Y, _activeCoordinates.X, _activeCoordinates.Y, color);
                }
                else if (coordinates.Y > _activeCoordinates.Y)
                {
                    wb.DrawEllipse(coordinates.X, _activeCoordinates.Y, _activeCoordinates.X, coordinates.Y, color);
                }
                else
                {
                    wb.DrawEllipse(_activeCoordinates.X, coordinates.Y, coordinates.X, _activeCoordinates.Y, color);
                }
                await Task.Delay(_asyncDelay);
            }
            _toolIsExecuting = false;
        }
Ejemplo n.º 13
0
        /// <summary>
        /// 画像をグレースケールにします。
        /// </summary>
        /// <param name="bitmap"></param>
        /// <returns></returns>
        private WriteableBitmap Grayscare(WriteableBitmap bitmap)
        {
            var edit = bitmap.Clone();

            edit.ToGrayScale();

            return(edit);

            //using (var s = PictureExtension.PhotoStreamToMemoryStream(bitmap))
            //{
            //    var size = PictureExtension.GetOriginalSize(s);
            //    WriteableBitmap edit = null;

            //    this.originBitmap = new WriteableBitmap((int)size.Width, (int)size.Height);
            //    edit = new WriteableBitmap((int)size.Width, (int)size.Height);

            //    // WriteableBitmapに画像を読み込ませる
            //    s.Seek(0, SeekOrigin.Begin);
            //    this.originBitmap.LoadJpeg(s);
            //    // editableの方にも同じのをコピー
            //    edit = this.originBitmap.Clone();

            //    // グレスケ化
            //    edit = this.originBitmap.ToGrayScale();

            //    return edit;
            //}
        }
Ejemplo n.º 14
0
 private void Button_Click_1(object sender, RoutedEventArgs e)
 {
     //save
     vm.ImgChanged = false;
     editBitmap.SaveAsAsync(vm.PathToImage);
     originalBitmap = editBitmap.Clone();
 }
Ejemplo n.º 15
0
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog OFD = new OpenFileDialog();
                OFD.Filter = "PNG images (.png)|*.png";

                if (OFD.ShowDialog() == true)
                {
                    BitmapSource bs = BitmapFrame.Create(new Uri(OFD.FileName), BitmapCreateOptions.None, BitmapCacheOption.Default);
                    if (bs.Format != PixelFormats.Bgra32)
                    {
                        bs = new FormatConvertedBitmap(bs, PixelFormats.Bgra32, null, 1);
                    }

                    WriteableBitmap wbmp = new WriteableBitmap(bs.PixelWidth, bs.PixelHeight, bs.DpiX, bs.DpiY, bs.Format, bs.Palette);

                    Int32Rect rect = new Int32Rect(0, 0, bs.PixelWidth, bs.PixelHeight);
                    wbmp.Lock();
                    bs.CopyPixels(rect, wbmp.BackBuffer, wbmp.BackBufferStride * wbmp.PixelHeight, wbmp.BackBufferStride);
                    wbmp.AddDirtyRect(rect);
                    wbmp.Unlock();
                    WBMain     = wbmp.Clone();
                    img.Source = WBMain;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Ejemplo n.º 16
0
        private void img_move(object sender, MouseEventArgs e)
        {
            if (isDrawable)
            {
                Point P = e.GetPosition(img);
                Paint      = Buf.Clone();
                img.Source = Paint;
                switch (tool)
                {
                case 1:
                    Paint.DrawLine((int)point.X, (int)point.Y, (int)P.X, (int)P.Y, color);
                    point = e.GetPosition(img);
                    Buf   = Paint.Clone();
                    break;

                case 2:
                    Paint.DrawLineAa((int)point.X, (int)point.Y, (int)P.X, (int)P.Y, color, thickness);
                    break;

                case 3:
                    Paint.DrawEllipse((int)point.X, (int)point.Y, (int)P.X, (int)P.Y, color);
                    break;
                }
            }
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Rotates the bitmap in 90° steps clockwise and returns a new rotated WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="angle">The angle in degress the bitmap should be rotated in 90° steps clockwise.</param>
        /// <returns>A new WriteableBitmap that is a rotated version of the input.</returns>
        public static WriteableBitmap Rotate(this WriteableBitmap bmp, int angle)
        {
            // Use refs for faster access (really important!) speeds up a lot!
            var             w      = bmp.PixelWidth;
            var             h      = bmp.PixelHeight;
            var             p      = bmp.Pixels;
            var             i      = 0;
            WriteableBitmap result = null;

            angle %= 360;

            if (angle > 0 && angle <= 90)
            {
                result = new WriteableBitmap(h, w);
                var rp = result.Pixels;
                for (var x = 0; x < w; x++)
                {
                    for (var y = h - 1; y >= 0; y--)
                    {
                        var srcInd = y * w + x;
                        rp[i] = p[srcInd];
                        i++;
                    }
                }
            }
            else if (angle > 90 && angle <= 180)
            {
                result = new WriteableBitmap(w, h);
                var rp = result.Pixels;
                for (var y = h - 1; y >= 0; y--)
                {
                    for (var x = w - 1; x >= 0; x--)
                    {
                        var srcInd = y * w + x;
                        rp[i] = p[srcInd];
                        i++;
                    }
                }
            }
            else if (angle > 180 && angle <= 270)
            {
                result = new WriteableBitmap(h, w);
                var rp = result.Pixels;
                for (var x = w - 1; x >= 0; x--)
                {
                    for (var y = 0; y < h; y++)
                    {
                        var srcInd = y * w + x;
                        rp[i] = p[srcInd];
                        i++;
                    }
                }
            }
            else
            {
                result = bmp.Clone();
            }
            return(result);
        }
Ejemplo n.º 18
0
        public static WriteableBitmap Overlay(WriteableBitmap bmp, WriteableBitmap overlay, Point location)
        {
            var result = bmp.Clone();
            var size   = new Size(overlay.PixelWidth, overlay.PixelHeight);

            result.Blit(new Rect(location, size), overlay, new Rect(new Point(0, 0), size), WriteableBitmapExtensions.BlendMode.Alpha);
            return(result);
        }
Ejemplo n.º 19
0
        private async void LoadBaseImage(string path)
        {
            vm.PathToImage = path;
            editBitmap     = await LoadWritableBitmap(path);

            originalBitmap = editBitmap.Clone();

            MainImage.Source = editBitmap;
        }
Ejemplo n.º 20
0
 public void SetData(mapcontent map, WriteableBitmap bitmap)
 {
     this.map        = map;
     this.background = bitmap.Clone();
     title           = new MapTitle(map.name, new Point(30, 10));
     legend          = new MapLegend(map, new Point(bitmap.Width * 3 / 4, bitmap.Height * 3 / 4));
     bmp             = background.Clone();
     Refresh();
 }
Ejemplo n.º 21
0
        }//render available bitmap

        private async Task RenderAsync()
        {
            //if preview is on and rendering of the previous frame finished
            if (!isRendering && isPreviewing)
            {
                //flag-on
                isRendering = true;

                //render frame
                await bitmapRenderer.RenderAsync();

                //clone frame to array
                await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
                    CoreDispatcherPriority.High, (DispatchedHandler)(() =>
                {
                    //if capture is on and array is not filled
                    if (isCapturing && frameIndex < maxFrameIndex)
                    {
                        //clone rendered frame
                        var newimage = currentWriteableBitmap.Clone();

                        //add cloned frame
                        Array.Resize(ref frameArray, frameArray.Length + 1);
                        frameArray[frameIndex] = newimage;

                        //go to editing if array is filled
                        if (frameIndex + 1 == maxFrameIndex)
                        {
                            Frame.Navigate(typeof(Edit), frameArray);
                        }

                        //copy frame to previewImage if such option is enabled
                        if (isPreviEwenabled)
                        {
                            previewImage.Source = newimage;
                        }

                        //change frameIndex
                        frameIndex++;
                        frameIndexBoxRefresh();

                        //stop capturing if capturingmodeIsPhoto
                        isCapturing = !capturingmodeIsPhoto;

                        //dispose cloned image
                        newimage = null;
                    }

                    //invalidate currentWriteableBitmap
                    currentWriteableBitmap.Invalidate();
                    GC.Collect();
                }));

                //flag-off
                isRendering = false;
            }
        }//render available frame
Ejemplo n.º 22
0
        private void BtnSaveAs_Click(object sender, RoutedEventArgs e)
        {
            //Brings up another dialog box for the user to choose a path to save the file
            //Same formats as loading dialog
            SaveFileDialog save = new SaveFileDialog
            {
                Title  = "Save your picture",
                Filter = "All supported graphics | *.jpg; *.jpeg; *.png; *.bmp | " +
                         "JPEG (*.jpg;*.jpeg)|*.jpg;*.jpeg|" +
                         "Portable Network Graphic (*.png)|*.png|" +
                         "Bitmap file format (*.bmp)|*.bmp"
            };

            if (save.ShowDialog() == true)
            {
                SaveBitmap(save.FileName, backBuffer.Clone());
            }
        }
Ejemplo n.º 23
0
 public WordDataContainer(Skeleton s,
                          WriteableBitmap colorLeft, WriteableBitmap colorRight,
                          WriteableBitmap depthLeft, WriteableBitmap DepthRight)
 {
     skeletonData         = s;
     colorLeftBitmapData  = colorLeft.Clone();
     colorRightBitmapData = colorRight.Clone();
     depthLeftBitmapData  = depthLeft.Clone();
     depthRightBitmapData = DepthRight.Clone();
 }
Ejemplo n.º 24
0
 private void RenderWorldFinish(WriteableBitmap img)
 {
     if (img != null)
     {
         WorldImage.Image = img.Clone();
     }
     RaisePropertyChanged("WorldZoomedHeight");
     RaisePropertyChanged("WorldZoomedWidth");
     CommandManager.InvalidateRequerySuggested();
 }
Ejemplo n.º 25
0
        private void Save(object sender, RoutedEventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();

            saveFileDialog.Filter = "LogiCC File (*.bmp)|*.bmp";
            if (saveFileDialog.ShowDialog() == true)
            {
                CreateThumbnail(saveFileDialog.FileName, bmp.Clone());
            }
        }
Ejemplo n.º 26
0
        public static WriteableBitmap Overlay(WriteableBitmap bmp, WriteableBitmap overlay, System.Windows.Media.Imaging.Point location)
        {
            var result = bmp.Clone();
            var size   = new System.Windows.Media.Imaging.Size(overlay.PixelWidth, overlay.PixelHeight);

            result.Blit(new Rect(location, size), overlay,
                        new Rect(new System.Windows.Media.Imaging.Point(0, 0), size),
                        WriteableBitmapExtensions.BlendMode.Multiply);
            return(result);
        }
Ejemplo n.º 27
0
        private unsafe void AverageImage(WriteableBitmap bitmap)
        {
            if (AverageCount == 0)
            {
                return;
            }
            if (_lastBitmap == null || _lastBitmap.PixelWidth != bitmap.PixelWidth ||
                _lastBitmap.PixelHeight != bitmap.PixelHeight)
            {
                _lastBitmap = bitmap.Clone();
                _lastBitmap.Freeze();
                return;
            }
            try
            {
                _lastBitmap = _lastBitmap.Clone();
                using (BitmapContext lastContext = _lastBitmap.GetBitmapContext())
                {
                    using (BitmapContext bitmapContext = bitmap.GetBitmapContext())
                    {
                        for (var i = 0; i < bitmapContext.Width * bitmapContext.Height; i++)
                        {
                            var c    = IntToColor(bitmapContext.Pixels[i]);
                            var oldc = IntToColor(lastContext.Pixels[i]);
                            c.R = (byte)((c.R + (double)AverageCount * oldc.R) / (AverageCount + 1));
                            c.G = (byte)((c.G + (double)AverageCount * oldc.G) / (AverageCount + 1));
                            c.B = (byte)((c.B + (double)AverageCount * oldc.B) / (AverageCount + 1));
                            //byte grayScale = (byte)((c.R * 0.299) + (c.G * 0.587) + (c.B * 0.114));
                            bitmapContext.Pixels[i] = ColorstoInt(c.A, c.R, c.G, c.B);
                            //bitmapContext.Pixels[i] = ColorstoInt(c.A, grayScale, grayScale, grayScale);
                        }
                    }
                }

                _lastBitmap = bitmap.Clone();
                _lastBitmap.Freeze();
            }
            catch (Exception ex)
            {
                Log.Error("Error average image");
            }
        }
        public static void MedianFilter(WriteableBitmap Input, int maskSize)
        {
            int width  = Input.PixelWidth;
            int height = Input.PixelHeight;

            WriteableBitmap originalImage = Input.Clone();

            using (BitmapContext contextOriginal = originalImage.GetBitmapContext())
            {
                using (BitmapContext context = Input.GetBitmapContext())
                {
                    // red, green and blue are a 2D square of odd size like 3x3, 5x5, 7x7, ... For simplicity stored it into 1D array.
                    byte[] red, green, blue;

                    /** Median Filter operation */
                    for (int y = 0; y < height; y++)
                    {
                        for (int x = 0; x < width; x++)
                        {
                            byte a = PixelHelper.GetPixel(contextOriginal, x, y).A;
                            red   = new byte[maskSize * maskSize];
                            green = new byte[maskSize * maskSize];
                            blue  = new byte[maskSize * maskSize];
                            int count = 0;
                            for (int r = y - (maskSize / 2); r <= y + (maskSize / 2); r++)
                            {
                                for (int c = x - (maskSize / 2); c <= x + (maskSize / 2); c++)
                                {
                                    if (r < 0 || r >= height || c < 0 || c >= width)
                                    {
                                        /** Some portion of the mask is outside the image. */
                                        continue;
                                    }
                                    else
                                    {
                                        Color tmp = PixelHelper.GetPixel(contextOriginal, c, r);
                                        red[count]   = tmp.R;
                                        green[count] = tmp.G;
                                        blue[count]  = tmp.B;
                                        ++count;
                                    }
                                }
                            }
                            Array.Sort(red);
                            Array.Sort(green);
                            Array.Sort(blue);

                            int index = (count % 2 == 0) ? count / 2 - 1 : count / 2;
                            PixelHelper.SetPixel(context, x, y, a, red[index], green[index], blue[index]);
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Save WriteableBitmap as PNG image
        /// </summary>
        public static void Save(this WriteableBitmap wb, string filename)
        {
            BitmapSource image = wb.Clone();

            using (FileStream stream = new FileStream(filename, FileMode.Create))
            {
                PngBitmapEncoder encoder = new PngBitmapEncoder();
                encoder.Frames.Add(BitmapFrame.Create(image));
                encoder.Save(stream);
            }
        }
Ejemplo n.º 30
0
 public Image(WriteableBitmap img)
 {
     this.image = img.Clone();
     formatName = "jpg";
     width      = img.PixelWidth;
     height     = img.PixelHeight;
     //img.Dispatcher.BeginInvoke(() =>
     // {
     destImage = new WriteableBitmap(width, height);
     //});
     updateColorArray();
 }