Hey all!

Ok, so I'm sure almost everyone here knows how to store an image in a 3d array right? So for example, let's say I was storing a 32x32 sprite. Here's what I would do:

- Code: Select all
`int MySprite[32][32][4];//This will store red, green, blue, and alpha`

So integers are the standard method for storing numbers. A normal integer is composed of 4 bytes (or 32 bits). This means that it can store values from 0-4294967296. Yeah, that's a lot right? ^^. But the colors in Ge range from 0-255 (1 byte). This means that there is a 4294967040 difference between normal ints and colors. So in other words, when we store 4 ints (red, green, blue, and alpha), we are accessing 16 bytes of memory (128 bits), but we are only using 4 bytes (32 bits) of it. This means that there is a (calculating...) 3.40 E 38 difference (That's a ton). And if you multiply that by 32 and 32, well, I think you get the point ^^. So anyways, I found a really simple way around this.

Ok so if we have 4 colors, that makes 4 bytes right? And an integer is composed of 4 bytes right? RIGHT!!! So, let's say we take 4 numbers (each ranging from 0-255), and put them into one integer. How do we do that? Well, let's study some more and then I'll tell you =D

So, here's what an int looks like inside a computer when it's initialized:

- Code: Select all
`0000 0000 0000 0000`

- Code: Select all
`ALPHA BLUE GREEN RED`

Ok, how do we do this? Well, first of all, we know that in a computer, the number counting scheme goes from 1-256 and then restarts (This is like the roman counting scheme wich goes from 1-10 and restarts). So by multiplying a number (let's say 2) by 256, you can shift it's postition (Like if you multiplied 2 * 10, you'd move the two one place to the left). Here's an example:

- Code: Select all
`0000 0000 0000 0010`

- Code: Select all
`0000 0000 0010 0000`

So can you see where I'm going here? We can simply use this "shifting" method to store all 4 numbers in the 4 bytes. So here's the code to do that:

- Code: Select all
`int StoreColors(int red, int green, int blue, int alpha)`

{

return (red) + (green * 256) + (blue * 256 * 256) + (alpha * 256 * 256 * 256);

}

So now we need code to retrieve it. I'm not going to explain all this because I'm way to tired xD. But if you read it line by line, you should be able to understand it

- Code: Select all
`int GetColor(int input, int color)`

{

int temp = input;

for(int i = 0; i < color; i++)

{

temp /= 256;

}

for(int i = 0; i < color + 3 - (2 * id); i++)

{

temp %= 256;

}

return temp;

}

So here's some example code:

- Code: Select all
`int image[32][32];//Notice that we no longer have to add that extra 3. This saves us even more space`

image[0][0] = StoreColor(255, 255, 0, 0);//Stores the color yellow =)

setpen(GetColor(image[0][0], 0), GetColor(image[0][0], 1), GetColor(image[0][0], 2), 0, 1);

putpixel(0, 0);

I hope this helps you guys, and thanks for reading

SuperSonic

EDIT* Ummm, after submitting this tutorial, I remembered that Ge deosn't store alpha as an int, it stores it as a float. So this code won't work with alpha (but it will with the other three colors)