www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - arrays...

reply Paolo Invernizzi <arathorn NOSPAM_fastwebnet.it> writes:
I'm missing something....

I've an allocated region of memory and I want to traverse it as 
rectangulars arrays...

ubyte[3][640][480] matrice = cast(ubyte[]) lpData[0..3*640*480];

is compiled but when executed is report an overlapping copy....

Suggestion?

Thanks is advance!

---
Paolo
Feb 04 2005
parent reply "Ben Hinkle" <ben.hinkle gmail.com> writes:
"Paolo Invernizzi" <arathorn NOSPAM_fastwebnet.it> wrote in message 
news:cu09qu$2s9o$1 digitaldaemon.com...
 I'm missing something....

 I've an allocated region of memory and I want to traverse it as 
 rectangulars arrays...

 ubyte[3][640][480] matrice = cast(ubyte[]) lpData[0..3*640*480];

 is compiled but when executed is report an overlapping copy....

 Suggestion?

 Thanks is advance!

 ---
 Paolo
I think you'll have to post more of your code. I don't see any array copying going on so I don't know why it is complaining about overlapping copies. You get that message when you try to do something like x[0..10] = x[5..15];
Feb 05 2005
parent reply Paolo Invernizzi <arathorn NOSPAM_fastwebnet.it> writes:
Ben Hinkle wrote:

 I think you'll have to post more of your code. I don't see any array copying 
 going on so I don't know why it is complaining about overlapping copies. You 
 get that message when you try to do something like
   x[0..10] = x[5..15];
I figured that out (never mind....) but! I've another question to ask... How can I allocate in the heap a rectangular array? ulong[480][640] ii; // in the stack... how in the heap? must use the dynamic ones? --- Paolo
Feb 07 2005
parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Paolo Invernizzi wrote:

 How can I allocate in the heap a rectangular array?
 
   ulong[480][640] ii; // in the stack... how in the heap?
 
 must use the dynamic ones?
I'm not sure how (or if?) you can allocate a static array of arrays ? ("ulong[480][640] ii = new ulong[480][640];" does not compile, and it's not possible to cast either ?) But you can use pointers instead of you like (as in C) :
     ulong* i = new ulong[480*640];
     ulong** ii = new ulong*[640];
     for (int j = 0; j < 640; j++)
       ii[j] = &i[j * 480];
Or you can just use dynamic arrays, as is the new D way :
     ulong ii[][];
     ii.length = 640;
     for (int j = 0; j < 640; j++)
       ii[j].length = 480;
These are so called "jagged arrays", not rectangular arrays. For arrays, you get bounds checking, but not for pointers... (bounds checking is usually turned off with -release anyway) Note: I show column-major ordering, just as you had done above, as well as the uint64_t which is overkill for eg. images. Otherwise the normal is to use row-major ordering, and to use uint32_t for storing RGBA data with 8 bits each ? And you can just ignore the A values if not using alphas, since it's better than using the lame 24-bit RGB format. (multiplies by 4 instead of 3, and it's a full register) Assembling such pixel values is easilly done by bitops. --anders PS. Attached a sample program, that shows all three ways. Changed it to use the more common "uint[640][480] ii", and to output the result as a 640x480x32 PNG image :-) (see previous post for the GD wrapper & D module code)
Feb 07 2005
parent reply Paolo Invernizzi <arathorn NOSPAM_fastwebnet.it> writes:
Anders F Björklund wrote:
 Paolo Invernizzi wrote:
 
 How can I allocate in the heap a rectangular array?

   ulong[480][640] ii; // in the stack... how in the heap?

 must use the dynamic ones?
I'm not sure how (or if?) you can allocate a static array of arrays ? ("ulong[480][640] ii = new ulong[480][640];" does not compile, and it's not possible to cast either ?)
Yep.... Walter is it possible? ;-(
 But you can use pointers instead of you like (as in C) :
 
     ulong* i = new ulong[480*640];
     ulong** ii = new ulong*[640];
     for (int j = 0; j < 640; j++)
       ii[j] = &i[j * 480];
Sure... but a little ugly ;-(
 I show column-major ordering, just as you had done above,
 as well as the uint64_t which is overkill for eg. images.
 Otherwise the normal is to use row-major ordering, and
 to use uint32_t for storing RGBA data with 8 bits each ?
Well... the matrix was not constructed for having the row RGB value pixel by pixel, but an integral of the values... that's why the ulong. But that's another matter ;-) --- Paolo
Feb 08 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
Paolo Invernizzi wrote:

 But you can use pointers instead of you like (as in C) :

     ulong* i = new ulong[480*640];
     ulong** ii = new ulong*[640];
     for (int j = 0; j < 640; j++)
       ii[j] = &i[j * 480];
Sure... but a little ugly ;-(
You don't *have* to do the old lookup table, of course ? (and on modern processors it's a lot faster to ditch it, and just replace it with a simple calculation or pointer) // simpler ? ulong* i = new ulong[480*640]; ulong* p = i + y * 480 + x; *p = c; Then again, with the problems the GC seem to be having one might as well switch to pointers and malloc until the dynamic arrays are ready to handle bigger data... Just kidding, the bounds checking and garbage collection are nice friends to have but sometimes C will do the job. Especially in D, that imports a lot of C stuff directly ?
 Well... the matrix was not constructed for having the row RGB value 
 pixel by pixel, but an integral of the values... that's why the ulong.
 
 But that's another matter ;-)
Sorry, guess the old demo coder got the best of me there :-) 640x480 was just begging to be used as a full-screen image... (Here is the output: http://www.algonet.se/~afb/d/test.png) --anders
Feb 08 2005