pixelbuffer.cs

来自「Fireball.CodeEditor is an source code ed」· CS 代码 · 共 1,714 行 · 第 1/4 页

CS
1,714
字号

        }

        #endregion

        #region Public Members

        public unsafe void BlendBufferColor(int color)
        {
            fixed (int* dest = _buffer)
            {
                BlendBufferColor(dest, color);
            }
        }
        public unsafe void BlendBufferColor(Color color)
        {
            BlendBufferColor(color.ToArgb());
        }
        public unsafe void BlendSubBuffer(int color, int x, int y, int width, int height)
        {
            if ((x >= _width) || (y >= _height) || ((x + width) <= 0) || ((y + height) <= 0))
            {
                return;
            }
            if ((x < 0) || (y < 0) || ((x + width) > _width) || ((y + height) > _height))
            {
                BlendBufferRectColorOutside(color, x, y, width, height);
                return;
            }

            fixed (int* dest = _buffer)
            {
                BlendBufferRectColor(dest, color, x, y, width, height);
            }
        }
        public unsafe void BlendSubBuffer(Color color, int x, int y, int width, int height)
        {
            BlendSubBuffer(color.ToArgb(), x, y, width, height);
        }
        public unsafe void BlendSubBuffer(int[] subBuffer, int x, int y, int width, int height)
        {
            if ((x >= _width) || (y >= _height) || ((x + width) <= 0) || ((y + height) <= 0))
            {
                return;
            }
            if ((x < 0) || (y < 0) || ((x + width) > _width) || ((y + height) > _height))
            {
                BlendBufferRectBufferOutside(subBuffer, x, y, width, height);
                return;
            }

            fixed (int* src = subBuffer)
            {
                fixed (int* dest = _buffer)
                {
                    BlendBufferRectBuffer(dest, src, x, y, width, height);
                }
            }
        }
        public unsafe void BlendSubBuffer(PixelBuffer subBuffer, int x, int y)
        {
            BlendSubBuffer(subBuffer.InternalBuffer, x, y, subBuffer.Width, subBuffer.Height);
        }
        public unsafe int ChangeColor(int oldColor, int newColor)
        {
            fixed (int* buff = _buffer)
            {
                return ChangeColor(buff, oldColor, newColor);
            }
        }
        public unsafe int ChangeColor(Color oldColor, Color newColor)
        {
            return ChangeColor(oldColor.ToArgb(), newColor.ToArgb());
        }
        public unsafe void ClearBuffer()
        {
            fixed (int* buff = _buffer)
            {
                FillZeroBuffer(buff);
            }
            this._graphics.ClearGraphics();
        }
        public unsafe void ClearBuffer(int color)
        {
            fixed (int* buff = _buffer)
            {
                SetBufferColor(buff, color);
            }
        }
        public unsafe void ClearBuffer(Color color)
        {
            ClearBuffer(color.ToArgb());
        }
        public PixelBuffer Clone()
        {
            return new PixelBuffer(_width, _height, _buffer);
        }
        public unsafe void CopyTo(IntPtr destDeviceData, int length)
        {
            Marshal.Copy(_buffer, 0, destDeviceData, length);
        }
        public unsafe void CopyTo(int[] destDeviceData, int length)
        {
            fixed (int* src = _buffer)
            {
                fixed (int* dest = destDeviceData)
                {
                    CopyBuffer(src, dest, length);
                }
            }
        }
        public void CopyBitmap(Bitmap bmp)
        {
            int w = bmp.Width;
            int h = bmp.Height;
            bool resize = false;
            if (w != _width)
            {
                w = _width;
                resize = true;
            }
            if (h != _height)
            {
                h = _height;
                resize = true;
            }
            if (resize)
            {
                bmp = new Bitmap(bmp, new Size(w, h));
            }
            this.CopyBitmapToBuffer(bmp);
        }
        public unsafe int CountColor(int color)
        {
            fixed (int* buff = _buffer)
            {
                return CountColor(buff, color);
            }
        }
        public unsafe int CountColor(Color color)
        {
            return CountColor(color.ToArgb());
        }
        public unsafe void FlipBuffer(FlipMode flipMode)
        {
            PixelBuffer pb = new PixelBuffer(_width, _height);
            fixed (int* src = _buffer)
            {
                fixed (int* dest = pb.InternalBuffer)
                {
                    switch (flipMode)
                    {
                        default:
                        case FlipMode.FlipHorizontal:
                            FlipBufferHorizontal(src, dest);
                            break;
                        case FlipMode.FlipVertical:
                            FlipBufferVertical(src, dest);
                            break;
                        case FlipMode.FlipBoth:
                            FlipBufferBoth(src, dest);
                            break;
                    }
                }
            }
            fixed (int* src = pb.InternalBuffer)
            {
                fixed (int* dest = _buffer)
                {
                    CopyBuffer(dest, src, _pixelsCount);
                }
            }
            pb.Dispose();
        }
        public unsafe Color[] GetAllPixels()
        {
            int[] icols = new int[0];
            fixed (int* src = _buffer)
            {
                icols = GetBufferColorArray(src);
            }
            Color[] cols = new Color[icols.Length];
            for (int i = 0; i < icols.Length; i++)
            {
                cols[i] = Color.FromArgb(icols[i]);
            }
            return cols;
        }
        public unsafe int[] GetBuffer()
        {
            int[] retBuffer = new int[_pixelsCount];

            fixed (int* src = _buffer)
            {
                fixed (int* dest = retBuffer)
                {
                    CopyBuffer(dest, src, _pixelsCount);
                }
            }
            return retBuffer;
        }
        public unsafe int GetBufferPixel(int index)
        {
            if (index < 0 || index >= _pixelsCount)
            {
                return 0;
            }

            fixed (int* buff = _buffer)
            {
                return GetPixelColorByIndex(buff, index);
            }
        }
        public unsafe int GetBufferPixel(int x, int y)
        {
            if (x < 0 || y < 0 || x >= _width || y >= _height)
            {
                return 0;
            }

            int index = GetPixelBytesIndex(x, y, _width);
            return GetBufferPixel(index);
        }
        public unsafe int[] GetBufferUsedColors()
        {
            int[] icols = new int[0];
            fixed (int* src = _buffer)
            {
                icols = GetBufferUsedColorArray(src);
            }
            return icols;
        }
        public unsafe Color GetPixel(int index)
        {
            return Color.FromArgb(GetBufferPixel(index));
        }
        public unsafe Color GetPixel(int x, int y)
        {
            return Color.FromArgb(GetBufferPixel(x, y));
        }
        public PixelIterator GetPixelIterator()
        {
            return GetPixelIterator(0, 0, _width, _height);
        }
        public PixelIterator GetPixelIterator(Rectangle rect)
        {
            return GetPixelIterator(rect.X, rect.Y, rect.Width, rect.Height);
        }
        public unsafe PixelIterator GetPixelIterator(int x, int y, int width, int height)
        {
            fixed (int* buff = _buffer)
            {
                if (!ClipRect(ref x, ref y, ref width, ref height, _width, _height)) return null;
                return new PixelIterator(buff, x, y, width, height, _width);
            }
        }
        public unsafe PixelBuffer GetSubBuffer(int x, int y, int width, int height)
        {
            if ((x >= _width) || (y >= _height) || ((x + width) <= 0) || ((y + height) <= 0))
            {
                new PixelBuffer(width, height);
            }
            if ((x < 0) || (y < 0) || ((x + width) > _width) || ((y + height) > _height))
            {
                return GetSubBufferOutside(x, y, width, height);
            }

            //TODO: use ClipRect(...)

            int[] pixels = new int[width * height];
            fixed (int* src = _buffer)
            {
                fixed (int* dest = pixels)
                {
                    CopyBufferRect(dest, src, x, y, width, height);
                }
            }
            return new PixelBuffer(width, height, pixels);
        }
        public unsafe Color[] GetUsedColors()
        {
            int[] icols = GetBufferUsedColors();
            Color[] cols = new Color[icols.Length];
            for (int i = 0; i < icols.Length; i++)
            {
                cols[i] = Color.FromArgb(icols[i]);
            }
            return cols;
        }
        public unsafe void InvertBufferRGB()
        {
            fixed (int* src = _buffer)
            {
                InvertBufferRGB(src);
            }
        }
        public void ResizeBuffer(ResizePivot pivot, int width, int height, Color backColor)
        {
            if (width == _width && height == _height) return;

            PixelBuffer newBuffer = new PixelBuffer(width, height);
            newBuffer.ClearBuffer(backColor);

            int dataWidth = Math.Min(width, _width);
            int dataHeight = Math.Min(height, _height);

            int deltaX = Math.Abs(width - _width);
            int deltaY = Math.Abs(height - _height);
            int midDeltaX = (int)Math.Round(deltaX / 2f, MidpointRounding.AwayFromZero);
            int midDeltaY = (int)Math.Round(deltaY / 2f, MidpointRounding.AwayFromZero);

            bool inflateX = (width > _width);
            bool inflateY = (height > _height);

            int srcX = 0;
            int srcY = 0;
            int destX = 0;
            int destY = 0;

            int srcW = width;
            int srcH = height;

            switch (pivot)
            {
                case ResizePivot.TopLeft:
                    srcX = 0;
                    destX = 0;
                    srcY = 0;
                    destY = 0;
                    break;
                case ResizePivot.TopCenter:
                    if (inflateX)
                    {
                        srcX = 0;
                        destX = midDeltaX;
                    }
                    else
                    {
                        srcX = midDeltaX;
                        destX = 0;
                    }
                    srcY = 0;
                    destY = 0;
                    break;
                case ResizePivot.TopRight:
                    if (inflateX)
                    {
                        srcX = 0;
                        destX = deltaX;
                    }
                    else
                    {
                        srcX = deltaX;
                        destX = 0;
                    }
                    srcY = 0;
                    destY = 0;
                    break;
                case ResizePivot.MiddleLeft:
                    srcX = 0;
                    destX = 0;
                    if (inflateY)
                    {
                        srcY = 0;
                        destY = midDeltaY;
                    }
                    else
                    {
                        srcY = midDeltaY;
                        destY = 0;
                    }
                    break;
                case ResizePivot.MiddleCenter:
                    if (inflateX)
                    {
                        srcX = 0;
                        destX = midDeltaX;
                    }
                    else
                    {
                        srcX = midDeltaX;
                        destX = 0;
                    }
                    if (inflateY)
                    {
                        srcY = 0;
                        destY = midDeltaY;
                    }
                    else
                    {
                        srcY = midDeltaY;
                        destY = 0;
                    }
                    break;
                case ResizePivot.MiddleRight:
                    if (inflateX)
                    {
                        srcX = 0;
                        destX = deltaX;
                    }
                    else
                    {
                        srcX = deltaX;
                        destX = 0;
                    }
                    if (inflateY)
                    {
                        srcY = 0;
                        destY = midDeltaY;
                    }
                    else
                    {
                        srcY = midDeltaY;
                        destY = 0;
                    }
                    break;
                case ResizePivot.BottomLeft:
                    srcX = 0;
                    destX = 0;
                    if (inflateY)
                    {
                        srcY = 0;
                        destY = deltaY;
                    }
                    else
                    {
                        srcY = deltaY;
                        destY = 0;
                    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?