Integer array or array of structures - which is better?

In my application, I store data Bitmapin a two-dimensional integer array ( int[,]). To access the values ​​of R, G, and B, I use something like this:

// read:
int i = _data[x, y];
byte B = (byte)(i >> 0);
byte G = (byte)(i >> 8);
byte R = (byte)(i >> 16);
// write:
_data[x, y] = BitConverter.ToInt32(new byte[] { B, G, R, 0 }, 0);

I use whole arrays instead of the actual one System.Drawing.Bitmap, because my application runs on Windows Mobile devices, where the memory available for creating raster images is very limited.

I am interested, however, if it would be more appropriate to declare such a structure:

public struct RGB
{
    public byte R;
    public byte G;
    public byte B;
}

... RGB int. , R, G B . - , byte, 32- , a byte 4 1 (, , Visual Basic).

(, RGB) , int, 3/4 3 ints?

+5
4

, , int[] , IL int (. OpCodes.Ldelem_I4). , (OpCodes.Ldelema), (OpCodes.Ldobj) - .

, int . - - , . , int - int[] :

MyColor col = intArr[12];

(, )

, :

; R/G/B, .

class Program
{
    static void Main()
    {
        int[] i = { -1 };
        RGB rgb = i[0];
    }
}
[StructLayout( LayoutKind.Explicit)]
public struct RGB
{
    public RGB(int value) {
        this.R = this.G = this.B = 0; this.Value = value;
    }
    [FieldOffset(0)]
    public int Value;
    [FieldOffset(2)]
    public byte R;
    [FieldOffset(1)]
    public byte G;
    [FieldOffset(0)]
    public byte B;

    public static implicit operator RGB(int value) {
        return new RGB(value);
    }
    public static implicit operator int(RGB value) {
        return value.Value;
    }
}
+2

Color System.Drawing? . , 4 ( Alpha), , , 3 4- . .

+2

, -, , . Marshal.SizeOf, RGB . (, , CLR , -, , .)

CLR RGB , , CLR .. x86 300 RGB , 900 300 ints 1200 . , , 2.0 CLR x86 25% . ( , , , , Traveling Tech Guy, , 3- 4- . - .) CF : , .

+2

, id , . , , Readabl struct,

unsafe { }

Bitmap. ( , - ) , , , , , INT - , , , . ints ( ), RGB , Marks, . , .

+1

All Articles