c++ pointers and multidimensional arrays
Pages: [1], 2
|
|
Programming Help
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
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
/home/donn/Desktop/4096.cpp |31| error: cannot convert ‘byte (*)[256][256]’ to ‘byte*’ in initialization
Any ideas gang?
Everyone stares when you walk in the room, they stare when you go....
|
|
|
|
|
(Edited 2009 Jan 13 at 13:48)
2009 Jan 13 at 13:47
|
|
|
SRAW
Rocket Man

2007 Nov 6 • 2140
601 ₧
|
okok i got a very nice idea... USE PYTHON
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
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.
Everyone stares when you walk in the room, they stare when you go....
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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.
|
|
|
|
|
(Edited 2009 Jan 14 at 10:19)
2009 Jan 14 at 09:46
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
Also: why are you allocating 16 MEGABYTES for a (3-D?) bitmap???
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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.
|
|
|
|
|
(Edited 2009 Jan 14 at 10:32)
2009 Jan 14 at 10:32
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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?
|
|
|
|
|
(Edited 2009 Jan 14 at 10:47)
2009 Jan 14 at 10:42
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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);
|
|
|
|
|
(Edited 2009 Jan 14 at 11:16)
2009 Jan 14 at 11:16
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
(Edited 2009 Jan 14 at 11:21)
2009 Jan 14 at 11:21
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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.
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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
|
|
|
|
|
(Edited 2009 Jan 19 at 13:05)
2009 Jan 19 at 13:04
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
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.
Everyone stares when you walk in the room, they stare when you go....
|
|
|
|
|
(Edited 2010 Dec 4 at 14:13)
2009 Jan 19 at 14:37
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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...
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
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...
Everyone stares when you walk in the room, they stare when you go....
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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.
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
57,583 ₧
|
|
|
|
|
|
Down Rodeo
Cap'n Moth of the Firehouse


2007 Oct 19 • 5325
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.
Everyone stares when you walk in the room, they stare when you go....
|
|
|
|
|
(Edited 2009 Mar 13 at 02:34)
2009 Mar 13 at 02:33
|
|
|
the_cloud_system
polly pushy pants

2008 Aug 1 • 2829
-6 ₧
|
nice...
I am on a journey that I hope I will never finish.
|
|
|
|
|
superjer
superjer

2005 Mar 20 • 3762
|
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.
|
|
|
|
|
(Edited 2009 Mar 13 at 14:27)
2009 Mar 13 at 14:26
|
|
|
|
|