c++ - Casting unsigned byte array to array of Structs -
using c++ (gcc specifically, should have set sooner), i'm storing raw texture info in array of unsigned bytes, in rgba format, 32 bits per pixel (8 bits per color value alpha, on , forth...). thing is, want write function returns raw info array of colors, color struct defined following:
struct color { uint8 r; uint8 g; uint8 b; uint8 a; };
plus functions , whatnot, variables in struct. thinking since each color 4 bytes long, can somehow cast raw byte array color array 1/4 of original size (in "length" of array, not in absolute size). think reinterpret_cast looking for, cannot find after google search confirms 100% can convert array of structs instead of 1 struct.
so guess asking either confirm indeed possible reinterpret_cast
, or if there different cast or way this. thanks.
edit: wording little weird, arbitrary illustration i'd somehow cast array of 16 unsigned bytes array of 4 colors.
edit: know it's kind of little late, cant seem find how cast little portion of array @ specific place single struct using reinterpret_cast, if possible, without copying smaller array , casting that. help problem appreciated.
as arbitrary illustration i'd somehow cast array of 16 unsigned bytes array of 4 colors.
like this:
#pragma pack(push, 1) struct color { uint8 r; uint8 g; uint8 b; uint8 a; }; #pragma pack(pop)
uint8 bytearray[16]; ... color *colorarray = reinterpret_cast<color*>(bytearray);
then can things this:
for (int idx = 0; idx < 4; ++idx) { color &c = colorarray[idx]; // utilize c.r, c.g, c.b, c.a needed... }
c++ pointers c++11 struct casting
No comments:
Post a Comment