Original Post
I''ve coded this alpha channel adder based on some code by Nate Miller, but when I load the created images into OpenGL, my colors are somewhat different... Here''s the code, if anyone can spot a problem, I''d be really glad!
typedef unsigned char byte;
int imageSize;
int imageDims[2];
int ColorKey[3];
byte *ImageAddAlpha(byte *src)
{
// allocate buffer for a RGBA image
byte *dest = new byte[imageSize * 4];
ColorKey[0] = 0;
ColorKey[1] = 255;
ColorKey[2] = 0;
// walk through the original image and copy it over to the dest image
for (int i = 0, j = 0; i < imageSize * 3; i += 3, j += 4)
{
if (src == ColorKey[0] && src[i + 1] == ColorKey[1] &&
src[i + 2] == ColorKey[2])
dest[j + 3] = 0; //Transparent
else
dest[j + 3] = 255; //Full Alpha
dest[j] = src[i];
dest[j + 1] = src[i + 1];
dest[j + 2] = src[i + 2];
}
return dest;
}
int CreateAlphaTexture(char *strFileName, int textureID, int width, int height)
{
imageDims[0] = width;
imageDims[1] = height;
imageSize = imageDims[0] * imageDims[1];
FILE *in = fopen(strFileName, "r");
FILE *out = fopen("new.spr", "w");
byte *buffer = new byte[imageSize * 3]; // allocate buffer for a RGB image
byte *image = new byte[imageSize * 4];
if (!in)
return 0;
// read the image in
fread(buffer, 1, imageSize * 3, in);
fclose(in);
// release all our memory, image has been uploaded
image = ImageAddAlpha(buffer);
delete [] buffer;
fwrite(image, 1, imageSize * 4, out);
fclose(out);
delete [] image;
return 1;
}
void main (void)
{
if (CreateAlphaTexture ("image.raw", 1, 128, 128))
printf("Image Successfully Added Alpha Channel\n");
else
printf("Failed to add Alpha Channel");
}