pixelbuffer.cs

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

CS
1,714
字号
                    break;
                case ResizePivot.BottomCenter:
                    if (inflateX)
                    {
                        srcX = 0;
                        destX = midDeltaX;
                    }
                    else
                    {
                        srcX = midDeltaX;
                        destX = 0;
                    }
                    if (inflateY)
                    {
                        srcY = 0;
                        destY = deltaY;
                    }
                    else
                    {
                        srcY = deltaY;
                        destY = 0;
                    }
                    break;
                case ResizePivot.BottomRight:
                    if (inflateX)
                    {
                        srcX = 0;
                        destX = deltaX;
                    }
                    else
                    {
                        srcX = deltaX;
                        destX = 0;
                    }
                    if (inflateY)
                    {
                        srcY = 0;
                        destY = deltaY;
                    }
                    else
                    {
                        srcY = deltaY;
                        destY = 0;
                    }
                    break;
            }

            if (inflateX && inflateY)
            {
                newBuffer.SetSubBuffer(_buffer, destX, destY, dataWidth, dataHeight);
            }
            else
            {
                PixelBuffer blitBuffer = this.GetSubBuffer(srcX, srcY, dataWidth, dataHeight);
                newBuffer.SetSubBuffer(blitBuffer, destX, destY);
            }

            this.Initialize(width, height);
            Array.Copy(newBuffer.InternalBuffer, _buffer, _buffer.Length);

            newBuffer.Dispose();

        }
        public unsafe void SetBuffer(int[] buffer)
        {
            fixed (int* src = buffer)
            {
                fixed (int* dest = _buffer)
                {
                    CopyBuffer(dest, src, buffer.Length);
                }
            }
        }
        public unsafe void SetBufferPixel(int index, int color)
        {
            if (index < 0 || index >= _pixelsCount)
            {
                return;
            }
            fixed (int* buff = _buffer)
            {
                SetPixelColorByIndex(buff, index, color);
            }
        }
        public unsafe void SetBufferPixel(int x, int y, int color)
        {
            if (x < 0 || y < 0 || x >= _width || y >= _height)
            {
                return;
            }

            int index = GetPixelBytesIndex(x, y, _width);
            SetBufferPixel(index, color);
        }
        public unsafe void SetPixel(int index, Color color)
        {
            SetBufferPixel(index, color.ToArgb());
        }
        public unsafe void SetPixel(int x, int y, Color color)
        {
            SetBufferPixel(x, y, color.ToArgb());
        }
        public unsafe void SetPixels(int startPixelX, int startPixelY, int[] offsets, Color color)
        {
            int index = GetPixelBytesIndex(startPixelX, startPixelY, _width);
            fixed (int* buff = _buffer)
            {
                fixed (int* offsetsArray = offsets)
                {
                    SetPixels(buff, index, offsetsArray, offsets.Length, color.ToArgb());
                }
            }
        }
        public unsafe void SetSubBuffer(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))
            {
                SetBufferRectColorOutside(color, x, y, width, height);
                return;
            }

            fixed (int* dest = _buffer)
            {
                SetBufferRectColor(dest, color, x, y, width, height);
            }
        }
        public unsafe void SetSubBuffer(Color color, int x, int y, int width, int height)
        {
            SetSubBuffer(color.ToArgb(), x, y, width, height);
        }
        public unsafe void SetSubBuffer(PixelBuffer subBuffer, int x, int y)
        {
            SetSubBuffer(subBuffer.InternalBuffer, x, y, subBuffer.Width, subBuffer.Height);
        }
        public unsafe void SetSubBuffer(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))
            {

                SetBufferRectBufferOutside(subBuffer, x, y, width, height);
                return;
            }

            fixed (int* src = subBuffer)
            {
                fixed (int* dest = _buffer)
                {
                    SetBufferRectBuffer(dest, src, x, y, width, height);
                }
            }
        }
        public void TranslateBuffer(int dx, int dy, Color backColor)
        {
            int absDX = Math.Abs(dx);
            int absDY = Math.Abs(dy);

            if ((absDX > _width) || (absDY > _height))
            {
                this.ClearBuffer(backColor.ToArgb());
                return;
            }

            int newWidth = _width - absDX;
            int newHeight = _height - absDY;

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

            if (dx > 0)
            {
                srcX = 0;
                destX = dx;
            }
            else
            {
                srcX = -dx;
                destX = 0;
            }
            if (dy > 0)
            {
                srcY = 0;
                destY = dy;
            }
            else
            {
                srcY = -dy;
                destY = 0;
            }

            PixelBuffer subBuffer = this.GetSubBuffer(srcX, srcY, newWidth, newHeight);

            this.ClearBuffer(backColor.ToArgb());

            this.SetSubBuffer(subBuffer, destX, destY);

            subBuffer.Dispose();
        }
        public Bitmap ToBitmap()
        {
            return CopyBufferToBitmap();
        }

        public int BytesCount
        {
            get
            {
                return _bytesCount;
            }
        }
        public int BytesPerLine
        {
            get
            {
                return _width * 4;
            }
        }
        public int Height
        {
            get
            {
                return _height;
            }
        }
        public int PixelsCount
        {
            get
            {
                return _pixelsCount;
            }
        }
        public unsafe int this[int pixelIndex]
        {
            get
            {
                return GetBufferPixel(pixelIndex);
            }
        }
        public int Width
        {
            get
            {
                return _width;
            }
        }
        public int[] InternalBuffer
        {
            get
            {
                return _buffer;
            }
        }
        public PixelBufferGraphics Graphics
        {
            get
            {
                return _graphics;
            }
            set
            {
                _graphics = value;
            }
        }
        public PixelBufferRenderer Renderer
        {
            get { return _renderer; }
            set { _renderer = value; }
        }

        #endregion

        #region Stubs

        public void ResizeBuffer(int width, int height, bool stretch)
        {
            if (!stretch)
            {
                ResizeBuffer(ResizePivot.TopLeft, width, height, Color.Transparent);
            }
            else
            {
                throw new NotImplementedException();
            }
        }
        public void RotateBuffer(float angle)
        {
            throw new NotImplementedException();
        }

        //public unsafe PixelBuffer[] InterpolateBuffers(PixelBuffer pixelBuffer1, PixelBuffer pixelBuffer2, int steps)
        //{
        //    IntPtr arrayPtr = Marshal.AllocHGlobal((steps + 2) * 4);
        //    int** array = (int*)(void*)arrayPtr;

        //    PixelBuffer[] buffers = new PixelBuffer[steps + 2];
        //    buffers[0] = pixelBuffer1.Clone();
        //    fixed (int* fbuff = buffers[0].InternalBuffer)
        //    {
        //        *array = fbuff;
        //        array++;
        //    }
        //    for (int i = 1; i < buffers.Length - 1; i++)
        //    {
        //        buffers[i] = new PixelBuffer(pixelBuffer1.Width, pixelBuffer1.Height);
        //        fixed (int* sbuff = buffers[i].InternalBuffer)
        //        {
        //            *array = sbuff;
        //            array++;
        //        }
        //    }
        //    buffers[buffers.Length - 1] = pixelBuffer2.Clone();
        //    fixed (int* ebuff = buffers[buffers.Length - 1].InternalBuffer)
        //    {
        //        *array = ebuff;
        //        array++;
        //    }

        //    array -= buffers.Length;

        //    fixed (int* buff1 = pixelBuffer1.InternalBuffer)
        //    {
        //        fixed (int* buff2 = pixelBuffer2.InternalBuffer)
        //        {
        //            InterpolateBuffers(buff1, buff2, array, pixelBuffer1.PixelsCount);
        //        }
        //    }

        //}
        private unsafe void InterpolateBuffers(int* buffer1, int* buffer2, int* destBufferArray, int count,int steps)
        {

            for (int i = 0; i < count; i++)
            {
                int color1 = *buffer1;
                int b1 = (color1 & 0xff);
                int g1 = ((color1 >> 8) & 0xff);
                int r1 = ((color1 >> 16) & 0xff);
                int a1 = (color1 >> 24) & 0xff;

                int color2 = *buffer2;
                int b2 = (color2 & 0xff);
                int g2 = ((color2 >> 8) & 0xff);
                int r2 = ((color2 >> 16) & 0xff);
                int a2 = (color2 >> 24) & 0xff;

                int dB = ((b2 - b1) / (steps + 1));
                int dG = ((g2 - g1) / (steps + 1));
                int dR = ((r2 - r1) / (steps + 1));
                int dA = ((a2 - a1) / (steps + 1));

                for (int j = 0; j < steps; j++)
                {
                    int* dest = (int*)*destBufferArray;
                    dest += i;
                    
                    *dest = (((a1 + (dA * j)) << 24) | ((r1 + (dR * j)) << 16) | ((g1 + (dG * j)) << 8) | (b1 + (dB * j)));

                    destBufferArray++;
                }

                destBufferArray -= steps;

                buffer1++;
                buffer2++;
            }

        }
        private unsafe void InterpolateBuffers(int* buffer1, int* buffer2, int* destBuffer, int count)
        {
            InterpolateBuffers(buffer1, buffer2, destBuffer, count, 1, 0);
        }
        private unsafe void InterpolateBuffers(int* buffer1, int* buffer2, int* destBuffer, int count,int steps,int index)
        {
            if (index < 0 || index >= steps)
            {
                throw new ArgumentOutOfRangeException("index");
            }

            for (int i = 0; i < count; i++)
            {
                InterpolatePixel(buffer1, buffer2, destBuffer, steps, index);

                buffer1++;
                buffer2++;
                destBuffer++;
            }
        }
        private unsafe void InterpolatePixel(int* pix1, int* pix2,int* destPix, int steps, int index)
        {
            steps++;
            index++;

            int color1 = *pix1;
            int b1 = (color1 & 0xff);
            int g1 = ((color1 >> 8) & 0xff);
            int r1 = ((color1 >> 16) & 0xff);
            int a1 = (color1 >> 24) & 0xff;

            int color2 = *pix2;
            int b2 = (color2 & 0xff);
            int g2 = ((color2 >> 8) & 0xff);
            int r2 = ((color2 >> 16) & 0xff);
            int a2 = (color2 >> 24) & 0xff;

            int b = b1 + (((b2 - b1) / steps) * index);
            int g = g1 + (((g2 - g1) / steps) * index);
            int r = r1 + (((r2 - r1) / steps) * index);
            int a = a1 + (((a2 - a1) / steps) * index);

            *destPix = ((a << 24) | (r << 16) | (g << 8) | b);
        }


        #endregion

    }
}

⌨️ 快捷键说明

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