Public Group

# Simple Array Error

This topic is 4330 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So I know that this is a simple error. I can't figure out why it does not work. Code that works:
map[x][y] = BMPmap[((x*mapX) + y))];
Code that does not work:
map[x][y] = BMPmap[(((x*mapX) + y)*4)];
I know it is that one line. x,mapX, and y are all integers. map is a 2d array of of floats. BMPmap is an array of chars. I have no idea why that would not work. I know that it is not over the array limit. It crashes with a windows crash window. Whats wrong!?!

##### Share on other sites
What is mapX?
What is the array size of BMPmap?
What is the range of x and y?

I would suspect that you are over the array limit seeing as you got a crash with that second line of code.

Maybe you could post the code if that's ok.

##### Share on other sites
k then.

void SetUp ( void ) {        BMPImg pic;	pic.Load("test.bmp");						unsigned char* BMPmap = pic.GetImg();						int mapX = pic.GetWidth(), mapY = pic.GetHeight();											for ( int x = 0; x < mapX; ++x )		for ( int y = 0; y < mapY; ++y )			map[x][y] = map[x][y] = BMPmap[(((x*mapX) + y)*4)];}

and then the BMPmap is an array with the exact amount of slots to hold one color. (XSIZE and YSIZE are macros that are the size of the BMP).
float map[XSIZE][YSIZE];

The BMPImg code is copied and pasted from here.

I believe thats all you would need and thanks for fast response.

##### Share on other sites
What I do notice looking on it a second time is the curious way of indexing into the BMPmap array. I do not know how the image is layed out in memory but this line looks like it's the wrong way to index:
(x*mapX) + y

In fact, I have just done some calculations and noticed that if your width is greater than your height, you will no doubt overflow the array.

The way I would normally index into an array is something like
(y*mapX) + x

Do I assume correctly that you are multiplying by 4 because your image is 32 bits per pixel?

Also, if your image is 32bpp, your method won't work to get all the color information into your float map. You would only get either the red or blue channel depending on if the bmp encodes as BGRA or RGBA

##### Share on other sites
Quote:
 Original post by Adam HamiltonWhat I do notice looking on it a second time is the curious way of indexing into the BMPmap array. I do not know how the image is layed out in memory but this line looks like it's the wrong way to index:(x*mapX) + yIn fact, I have just done some calculations and noticed that if your width is greater than your height, you will no doubt overflow the array.The way I would normally index into an array is something like(y*mapX) + x

about the indexing yes that was messed up. I believe it should be BMPmap[(((x*mapY) + y)*4)]. But that does not work either. I still get MS error/crash message.

Quote:
 Do I assume correctly that you are multiplying by 4 because your image is 32 bits per pixel?Also, if your image is 32bpp, your method won't work to get all the color information into your float map. You would only get either the red or blue channel depending on if the bmp encodes as BGRA or RGBA

Exactly what I want. ;)

EDIT: never mind, I found a workaround on Google. I will post the code for someone else that may have the problem.

	for ( int x = 0, place = 0; x < mapX; ++x ) 		for ( int y = 0; y < mapY; ++y, ++place ) 			map[x][y] = BMPmap[ place * 4 ];

[Edited by - slymr on November 14, 2006 10:55:52 PM]

1. 1
2. 2
Rutin
18
3. 3
4. 4
5. 5
frob
12

• 9
• 21
• 13
• 9
• 17
• ### Forum Statistics

• Total Topics
632608
• Total Posts
3007391

×