c++ pointers and multidimensional arrays

c++ pointers and multidimensional arrays

Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Wow, actually using the forum. Who'd a thunk. So I am trying to set up a multidimensional array referenced by a pointer but when I try to compile
unsigned char* bitmap = new unsigned char[256][256][256]

it dies on me with
 
 
 
2009 Jan 13 at 21:47 UTC — Ed. 2009 Jan 13 at 21:48 UTC
SRAW
Rocket Man

2007 Nov 6 • 2525
601 ₧
okok i got a very nice idea... USE PYTHON
Free Steam Games
 
 
 
2009 Jan 14 at 09:18 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
That is quite a good idea but I don't really know Python (well I know the syntax is actually not difficult but you know what I mean) but since I am learning Java this semester I figure I might as well learn an OO language that isn't as bad as Java. Not that all OO concepts sit well with me.
 
 
 
2009 Jan 14 at 09:21 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Down Rodeo said:
unsigned char* bitmap = new unsigned char[256][256][256]


You are declaring bitmap to be a pointer to char. But it needs to be a pointer to an array[256] of array[256] of chars.

I said:
unsigned char (*bitmap)[256][256] = new unsigned char[256][256][256]


Unlike what they teach in school, arrays are NOT pointers.

Note that you need the parentheses there to change the order of types within the declaration. If you take away the parentheses, bitmap will be an array[256] of array[256] of pointers to char.


 
 
 
2009 Jan 14 at 17:46 UTC — Ed. 2009 Jan 14 at 18:19 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Also: why are you allocating 16 MEGABYTES for a (3-D?) bitmap???
 
 
 
2009 Jan 14 at 18:21 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Cheers. I've not been taught this anyway :)

Eh I'm making a bitmap comprising all the colours in RGB colourspace, or at least something like that. The end files are 48 MB. I shall give that a go and let you know how it goes.
 
 
 
2009 Jan 14 at 18:25 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Seems like it would make more sense to have a new unsigned char[4096][4096][3] then. That is, 16 megapixel with 3 bytes per pixel.

Still not sure why you'd do that though.
 
 
 
2009 Jan 14 at 18:32 UTC — Ed. 2009 Jan 14 at 18:32 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
To make it easier for me to run over the various colours by simple loops. So that bit works, but somewhat retardedly I have introduced an infinite loop elsewhere. Back in a tick...
 
 
 
2009 Jan 14 at 18:37 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Down Rodeo said:
To make it easier...


Ok... but the color you're looking for in any case IS THE ADDRESS IT'S AT.

OMGSRSLY said:
bitmap[ 0xff ][ 0x80 ][ 0x00 ] == 0xff8000;


EDIT: Or maybe not since the array only stores bytes...

EDIT2: Ok you've made me too curious. What the hell that fits in one char are you storing in this array?
 
 
 
2009 Jan 14 at 18:42 UTC — Ed. 2009 Jan 14 at 18:47 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
The intensity of a colour. From 0-255. I also have it working now; I'll upload a (reduced) screenshot. I mean this picture is pathologic; no conventional lossless compression format could do much with this. Apart from the code I wrote of course.
 
 
 
2009 Jan 14 at 18:52 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
I hate life; it didn't work :(

Well your code did but for some reason it wrote the file out incorrectly. Gah and indeed fuck. Yay bughunting!
 
 
 
2009 Jan 14 at 19:11 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Are you just trying to create a bitmap with all 16.7 million colors in it? Or is there more to this?

For example:

Magical Code Fairy said:
for(i=0;i<16777216;i++) writecolor(i);
 
 
 
2009 Jan 14 at 19:16 UTC — Ed. 2009 Jan 14 at 19:16 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Jan 14 at 19:21 UTC — Ed. 2009 Jan 14 at 19:21 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Agh you are right... spending more time actually thinking rather than trying to code would have been the best idea here.
 
 
 
2009 Jan 14 at 19:26 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Down Rodeo said:
Agh you are right... spending more time actually thinking rather than trying to code would have been the best idea here.

Don't worry I make the same mistake all the time.
 
 
 
2009 Jan 17 at 02:01 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Jan 17 at 14:39 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
You're kind of close.

ImageMagick says you have ~4 million colors in there.

Terminal said:
jwilson@jwilson-desktop:~$ identify -format %k 4096.png
4312640
 
 
 
2009 Jan 19 at 21:04 UTC — Ed. 2009 Jan 19 at 21:05 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
Ahah, interesting. That's kind of a strange number though. My code is not that tough (it is dead simple) so I wonder where that bug is cropping up. I might post it here when I get back. You'll recognise the code somewhat, I *ahem* reused some of the PixelMachine stuff for the bitmap saving section. It works well! Though it is blatant copying, I do feel kinda bad. I am still trying to do this whole thing.

EDIT: Yeah, what I meant to say, it seems an odd number. It's not a power of two or anything... strange.
 
 
 
2009 Jan 19 at 22:37 UTC — Ed. 2010 Dec 4 at 22:13 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Don't feel bad! I shared the pixelmachine source so people could re-use it or learn from it. That's the whole point!

I did notice that number was strange. When I first saw it I thought it was 16777216/4. But it's not.

I'm pretty sure ImageMagick can count colors correctly...
 
 
 
2009 Jan 20 at 18:15 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
This be the important bit (everything else is copied from you):

int * i = new int[16777216];
for(int n = 0; n<16777216; n++)
{
i[n] = (wchar_t)n;
//cout<<i[n]<<"\n";
}


The cout was just to see if it was working but it slows down things by a factor of, I dunno, far too friggin' much? Wait, why have I done it like that? I dunno. At least I think it saves it properly...
 
 
 
2009 Jan 20 at 18:36 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
You are storing each pixel as an int but that is incorrect.

Each pixel is three bytes. Not four. (R,G,B)

You can use 3 chars per pixel instead of 1 int per pixel.

Even better, you can do the whole thing without that giant 64MB array.
 
 
 
2009 Jan 20 at 21:04 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
 
 
 
2009 Jan 20 at 21:21 UTC
Down Rodeo
Cap'n Moth of the Firehouse

Find the Hole II Participation Medal
2007 Oct 19 • 5486
57,583 ₧
IT'S ALIVE!

I got it working, fucking finally, I used the PPM format. It's awfully simple (and frighteningly huge) but it works, motherfuckers! It doesn't look like I thought it would. Such is life. I'll post it if anyone's interested. It will be much reduced though; in fact it's likely easier to post the code!
#include <stdio.h> int main(int argc, char *argv[]) { if(argc!=2) { printf("Usage: %s filename.ppm\n",argv[0]); return 1; } FILE* ppmFile; ppmFile = fopen(argv[1], "w"); fprintf(ppmFile, "P3\n4096\n4096\n255\n"); int r,g,b; for(r = 0;r<256;++r) { for(g = 0; g<256; ++g) { for(b = 0;b<256; ++b) { fprintf(ppmFile, "%d %d %d\n",r,g,b); } } } fclose(ppmFile); return 0; }
I feel leet. Obviously there are no comments in this code but I'd be happy to explain anything that isn't immediately transparently obvious. Painfully so.
 
 
 
2009 Mar 13 at 09:33 UTC — Ed. 2009 Mar 13 at 09:34 UTC
the_cloud_system
polly pushy pants

2008 Aug 1 • 3081
-6 ₧
nice...
I drink to forget but I always remember.
 
 
 
2009 Mar 13 at 16:03 UTC
SuperJer
Websiteman

2005 Mar 21 • 6666
Nice! You did it.

That is only one way to order all the colors though. There are like trillions of trillions of trillions of ... other ways it could look.
 
 
 
2009 Mar 13 at 21:26 UTC — Ed. 2009 Mar 13 at 21:27 UTC
Page [1] 2