Jump to content
  • Advertisement


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


File writing woes and OPEN GL

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!