|
Archives
D Programming
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.ide
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger
D.gnu
D
C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows
digitalmars.empire
digitalmars.DMDScript
electronics
|
digitalmars.D.learn - converting a byte array to a struct array?
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).
Just remembered the old casting hack:
RGB[] PALETTE = (cast(RGB*) cast(ubyte[]) [...])[0..256];
It works but is there perhaps another nicer possibility?
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
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.
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.
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
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
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.
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.
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).
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
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
|
|