Skip to main content
GameDev.net gamedev.net
🔒 Locked

RAW File Format Question

Started by shalrath Dec 23, 2001 at 10:19 AM 1 replies 300+ views
Original Post
shalrath
shalrath
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"); }
TerranFury
TerranFury
Two things:

1 - When posting source code on the forums, begin with [ source] and end with [ / source] but without the spaces.
2 - Make sure that you are not swapping your red and blue bytes.
shalrath
shalrath
If it can be of any help, here is the source I use to load the created RAW into OpenGL:
  
int UploadAlphaTexture(GLuint textureArray[], char *strFileName, int textureID, int width, int height)
{
int imgd[2], imgsz;
imgd[0] = width;
imgd[1] = height;
imgsz = imgd[0] * imgd[1];
FILE *ap = fopen(strFileName, "r");
byte *buf = new byte[imgsz * 4]; // allocate buffer for an RGBA image


if (!ap)
{
ErrorMessage("UploadAlphaTexture - Can''t Find File", strFileName);
return 0;
}
// read the image in

fread(buf, 1, imgsz * 4, ap);
fclose(ap);
#ifdef LOGGING
logfile << "AlphaTex: P,R,G,B,A: " << "25, " << int(buf[100]) << ", " << int(buf[101]) << ", " << int(buf[102]) << ", " << int(buf[103]);
#endif
// release all our memory, image has been uploaded


AlphaImageUpload(buf,textureArray,textureID,imgd[0], imgd[1]);
delete [] buf;
#ifdef LOGGING
logfile << "UploadAlphaTexture - "<< strFileName << " " << textureID << " was successful\n";
#endif
return 1;
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.