开发者

How can I copy byte[] into byte[,,]?

开发者 https://www.devze.com 2023-02-03 18:16 出处:网络
This question is related to: How to convert Bitmap to byte[,,] faster? I have byte[] which has: [r0, g0, b0, r1, g1, b1 ... ]

This question is related to: How to convert Bitmap to byte[,,] faster?

I have byte[] which has:

[r0, g0, b0, r1, g1, b1 ... ]

(r0 is the r-value of zeroth pixel and so on)

How can I copy this quickly in开发者_如何学JAVAto byte[,,]?

Or maybe I can get byte[,,] ditectly from BitmapData?


Based on Martinho's answer but perhaps a bit faster(don't have time for benchmarking now):

struct BitmapDataAccessor
{
    private readonly byte[] data;
    private readonly int[] rowStarts;
    public readonly int Height;
    public readonly int Width;

    public BitmapDataAccessor(byte[] data, int width, int height)
    {
        this.data = data;
        this.Height = height;
        this.Width = width;
        rowStarts = new int[height];
        for(int y=0;y<height;y++)
          rowStarts[y]=y*width;
    }

    public byte this[int x, int y, int color] // Maybe use an enum with Red = 0, Green = 1, and Blue = 2 members?
    {
        get { return data[(rowStarts[y] + x) *3 + color]; }
        set { data[(rowStarts[y] + x) *3 + color] = value; }
    }

    public byte[] Data
    {
        get { return data; }
    }
}


Ok, let's say you've got your data in a one-dimensional byte array. Do you really need to push it over into a three-dimensional array? If all you want is an easier way to access the pixel data, why don't you simply write such a simple interface to that array? Something along these lines:

class BitmapDataAccessor
{
    private readonly byte[] data;
    private readonly int rows;
    private readonly int columns;
    public BitmapDataAccessor(byte[] data, int rows, int columns)
    {
        this.data = data;
        this.rows = rows;
        this.columns = columns;
    }

    public byte this[int row, int column, int color] // Maybe use an enum with Red = 0, Green = 1, and Blue = 2 members?
    {
        get { return data[(row * columns + column) * 3 + color]; }
        set { data[(row * columns + column) * 3 + color] = value; }
    }

    public byte[] Data
    {
        get { return data; }
    }
}


How about using something like this for ease of accessing the discrete bytes:

    class ByteIndexer
    {
        private readonly byte[] _bits;
        private readonly int _width;
        public ByteIndexer(byte[] bits, int width)
        {
            _bits = bits;
            _width = width;
        }
        public byte this[int x, int y, int c]
        { get { return _bits[(((_width * y) + x) * 3) + c]; } }
    }

You could even make it easier, overload this[] with:

        public Color this[int x, int y]
        { get { return Color.FromArgb(this[x,y,0], this[x,y,1], this[x,y,2]); }


You could also consider using the built-in Bitmap in System.Drawing. Here's an example with a 4x3 image.

var image = new byte[] {255,255,255,0,0,0,255,255,255,0,0,0,
                        255,255,255,0,0,0,255,127,255,0,0,0,
                        0,0,0,255,255,255,0,0,0,255,255,255};

Bitmap bmp = new Bitmap(4, 3, PixelFormat.Format24bppRgb);
BitmapData bmpData = bmp.LockBits(
                        new Rectangle(0, 0, bmp.Width, bmp.Height),
                        ImageLockMode.WriteOnly, bmp.PixelFormat);
Marshal.Copy(image, 0, bmpData.Scan0, image.Length);
bmp.UnlockBits(bmpData);

var testPixel = bmp.GetPixel(2, 1);

testPixel will be a System.Drawing.Color set to {Color [A=255, R=255, G=127, B=255]}

0

精彩评论

暂无评论...
验证码 换一张
取 消