www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - converting a byte array to a struct array?

reply Trass3r <mrmocool gmx.de> writes:
I got some RGB palette in a byte array which I'd like to convert or 
"map" to an RGB struct array, isn't this easily possible without using 
dozens of struct constructors?


RGB[256] PALETTE = cast(RGB[256]) [
     0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
     0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...

doesn't work cause of "non-constant expression"

RGB[256] PALETTE = (cast(RGB[]) [
     0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
     0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...
]) (0 .. 256);

compiles, but yields empty structs (and doesn't seem right anyway).
Dec 29 2009
next sibling parent Trass3r <mrmocool gmx.de> writes:
Just remembered the old casting hack:

RGB[] PALETTE = (cast(RGB*) cast(ubyte[]) [...])[0..256];

It works but is there perhaps another nicer possibility?
Dec 29 2009
prev sibling next sibling parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 29 Dec 2009 07:56:04 -0500, Trass3r <mrmocool gmx.de> wrote:

 I got some RGB palette in a byte array which I'd like to convert or  
 "map" to an RGB struct array, isn't this easily possible without using  
 dozens of struct constructors?


 RGB[256] PALETTE = cast(RGB[256]) [
      0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
      0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...

 doesn't work cause of "non-constant expression"

 RGB[256] PALETTE = (cast(RGB[]) [
      0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
      0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...
 ]) (0 .. 256);

 compiles, but yields empty structs (and doesn't seem right anyway).
What is RGB's structure? I would expect something like this to work: RGB[256] PALETTE = [ {0x00, 0x00, 0x00}, {0xE3, 0x53, 0x00}, ... ]; Assuming RGB is a struct of 3 ubyte members... -Steve
Dec 29 2009
parent reply Trass3r <mrmocool gmx.de> writes:
Steven Schveighoffer schrieb:
 What is RGB's structure?
 
 I would expect something like this to work:
 
 RGB[256] PALETTE = [
 {0x00, 0x00, 0x00},
 {0xE3, 0x53, 0x00},
 ...
 ];
 
 Assuming RGB is a struct of 3 ubyte members...
 
Yeah, simply 3 ubytes. But IIRC I read that struct literals will be removed in D2 so I didn't test that approach.
Dec 29 2009
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r <mrmocool gmx.de> wrote:

 Steven Schveighoffer schrieb:
 What is RGB's structure?
  I would expect something like this to work:
  RGB[256] PALETTE = [
 {0x00, 0x00, 0x00},
 {0xE3, 0x53, 0x00},
 ...
 ];
  Assuming RGB is a struct of 3 ubyte members...
Yeah, simply 3 ubytes. But IIRC I read that struct literals will be removed in D2 so I didn't test that approach.
You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose. -Steve
Dec 29 2009
parent reply "Steven Schveighoffer" <schveiguy yahoo.com> writes:
On Tue, 29 Dec 2009 10:37:43 -0500, Steven Schveighoffer  
<schveiguy yahoo.com> wrote:

 On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r <mrmocool gmx.de> wrote:

 Steven Schveighoffer schrieb:
 What is RGB's structure?
  I would expect something like this to work:
  RGB[256] PALETTE = [
 {0x00, 0x00, 0x00},
 {0xE3, 0x53, 0x00},
 ...
 ];
  Assuming RGB is a struct of 3 ubyte members...
Yeah, simply 3 ubytes. But IIRC I read that struct literals will be removed in D2 so I didn't test that approach.
You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose.
And the answer (created by bearophile): http://codepad.org/8HnF62s2 -Steve
Dec 29 2009
parent Trass3r <mrmocool gmx.de> writes:
Steven Schveighoffer schrieb:
 You are right, I asked the question on that thread of whether it was 
 worth keeping struct literals for this purpose.
And the answer (created by bearophile): http://codepad.org/8HnF62s2
Yeah, I know but if you got that data out of some file like the resource section of a game exe in my case, adding the struct constructors is cumbersome.
Dec 30 2009
prev sibling parent reply grauzone <none example.net> writes:
Trass3r wrote:
 I got some RGB palette in a byte array which I'd like to convert or 
 "map" to an RGB struct array, isn't this easily possible without using 
 dozens of struct constructors?
 
 
 RGB[256] PALETTE = cast(RGB[256]) [
     0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
     0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...
 
 doesn't work cause of "non-constant expression"
 
 RGB[256] PALETTE = (cast(RGB[]) [
     0x00, 0x00, 0x00, 0xE3, 0x53, 0x00,
     0xCF, 0x4B, 0x07, 0xBF, 0x43, 0x0F, ...
 ]) (0 .. 256);
 
 compiles, but yields empty structs (and doesn't seem right anyway).
You've hit both an anti-feature and a bug. First the anti-feature: [0xAB, ...] will yield an int[], not a ubyte[] (I guess ubyte[] is what you're expecting). If you cast two arrays, the compiler will reinterpret cast all data. The result won't be what you intended. Second, the bug: casting arrays at compiletime seems to behave differently from casting at runtime. Casting at compiletime doesn't reinterpret cast, it does conversion! Demonstration here: http://codepad.org/OGjXADdu Feel free to file some bug reports.
Dec 29 2009
next sibling parent Trass3r <mrmocool gmx.de> writes:
grauzone schrieb:
 First the anti-feature: [0xAB, ...] will yield an int[], not a ubyte[] 
 (I guess ubyte[] is what you're expecting). If you cast two arrays, the 
 compiler will reinterpret cast all data. The result won't be what you 
 intended.
 
Yeah I already encountered that problem. That's why I had to cast(ubyte[]) it (where conversion is expected) for my reinterpret casting: RGB[] PALETTE = (cast(RGB*) cast(ubyte[]) [...])[0..256];
 Second, the bug: casting arrays at compiletime seems to behave 
 differently from casting at runtime. Casting at compiletime doesn't 
 reinterpret cast, it does conversion! Demonstration here: 
 http://codepad.org/OGjXADdu
 
You're right. But it only concerns casting to a struct(?) or an array of structs, right?!. Conversion is correct for standard arrays, but struct arrays need to be reinterpret cast (correct me if I'm wrong, I can't imagine how conversion could be reasonably used. Only thing might be casting an array of structs to another array of structs but I don't know any valid use-case).
Dec 30 2009
prev sibling parent reply Trass3r <mrmocool gmx.de> writes:
grauzone schrieb:
 Second, the bug: casting arrays at compiletime seems to behave 
 differently from casting at runtime. Casting at compiletime doesn't 
 reinterpret cast, it does conversion! Demonstration here: 
 http://codepad.org/OGjXADdu
 
 Feel free to file some bug reports.
Just found out this is stated as a feature in the docs. http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral http://codepad.org/bvk63OPw
Jan 02 2010
parent grauzone <none example.net> writes:
Trass3r wrote:
 grauzone schrieb:
 Second, the bug: casting arrays at compiletime seems to behave 
 differently from casting at runtime. Casting at compiletime doesn't 
 reinterpret cast, it does conversion! Demonstration here: 
 http://codepad.org/OGjXADdu

 Feel free to file some bug reports.
Just found out this is stated as a feature in the docs. http://www.digitalmars.com/d/2.0/expression.html#ArrayLiteral
Interesting. then this is an anti-feature too. At least it sounds like a very bad idea. The compiler/language tries to be "smart" here, but only introduces inconsistencies.
 http://codepad.org/bvk63OPw
Jan 02 2010