digitalmars.D.learn - converting a byte array to a struct array?
- Trass3r (12/12) Dec 29 2009 I got some RGB palette in a byte array which I'd like to convert or
 - Trass3r (3/3) Dec 29 2009 Just remembered the old casting hack:
 - Steven Schveighoffer (10/22) Dec 29 2009 What is RGB's structure?
 - Trass3r (4/16) Dec 29 2009 Yeah, simply 3 ubytes.
 - Steven Schveighoffer (4/17) Dec 29 2009 You are right, I asked the question on that thread of whether it was wor...
 - Steven Schveighoffer (4/21) Dec 29 2009 And the answer (created by bearophile): http://codepad.org/8HnF62s2
 - Trass3r (4/9) Dec 30 2009 Yeah, I know but if you got that data out of some file like the resource...
 - grauzone (11/28) Dec 29 2009 You've hit both an anti-feature and a bug.
 
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
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
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
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
On Tue, 29 Dec 2009 10:03:50 -0500, Trass3r <mrmocool gmx.de> wrote:Steven Schveighoffer schrieb:You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose. -SteveWhat 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
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:And the answer (created by bearophile): http://codepad.org/8HnF62s2 -SteveSteven Schveighoffer schrieb:You are right, I asked the question on that thread of whether it was worth keeping struct literals for this purpose.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
Steven Schveighoffer schrieb: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.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
 Dec 30 2009
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
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/OGjXADduYou'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
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
Trass3r wrote:grauzone schrieb: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.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#ArrayLiteralhttp://codepad.org/bvk63OPw
 Jan 02 2010








 
 
 
 Trass3r <mrmocool gmx.de> 