Archived

This topic is now archived and is closed to further replies.

shalrath

File writing woes and OPEN GL

Recommended Posts

shalrath    128
Here''s some code I''ve been using to write RAW files with an alpha channel to display with OPEN GL, only trouble is, when they get displayed all the colours and alpha go whak. Maybe one of you guys can spot something really stupid that I''ve done. //========================================== // ShalRath''s Alpha Adding Util // Original Code from the following Tutorial: /* ============= Color Keying Author : Nate ''m|d'' Miller Email : nkmiller@calpoly.edu Web : http://nate.scuzzy.net Date : 03/10/00 ============= */ //========================================== #include 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"); }

Share this post


Link to post
Share on other sites
python_regious    929
One thing I have noticed is that you allocate an RGBA array in your ImageAddAlpha() function like so:


byte *dest = new byte[imageSize * 4];


Then in your CreateAlphaTexture() function, you allocate memory to your image pointer, then redirect it to the memory allocated above. Hence you have a memory leak, because the later delete[] is deleting the memory allocated in ImageAddAlpha() not CreateAlphaTexture().

Other than that, I can''t really see anything wrong.... Your alpha generation looks fine, I have used an almost identicle method before.

How are you viewing the image that you save?

Share this post


Link to post
Share on other sites