Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6764719
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:37:36+00:00 2026-05-26T14:37:36+00:00

I am writing a renderer in C++. I see that a similar question has

  • 0

I am writing a renderer in C++. I see that a similar question has been answered, I was just looking for a bit more information. I have a 256 color bitmap that I load into memory and then into OpenGL. I am UNABLE to convert the files before hand and need to do it in memory. I want to be able to convert it to an RGBA bitmap to make use of the alpha channel. Does anyone have any code or suggestions about libraries that may help me with this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:37:37+00:00Added an answer on May 26, 2026 at 2:37 pm

    I have some code somewhere, but it’s just 20 lines or less. The BMP format is pretty simple, just read the header, then read every color, which serves as an index in the palette array. And keep in mind that BMPs are upside down:

    //load a bmp texture, with the specified global alpha
    GLuint load_bmp8_fixed_alpha(char * FileName, Uint8 a)
    {
       int x,y,x_padding,x_size,y_size,colors_no,r,g,b,current_pallete_entry; //i unused?
       Uint8 * file_mem;
       Uint8 * file_mem_start;
       Uint8 * texture_mem;
       Uint8 * read_buffer;
       Uint8 * color_pallete;
       FILE *f = NULL;
       GLuint texture;
    
         f = fopen (FileName, "rb");
         if (!f) return 0;
         file_mem = (Uint8 *) calloc ( 20000, sizeof(Uint8));
         file_mem_start=file_mem;
         fread (file_mem, 1, 50, f);//header only
         //now, check to see if our bmp file is indeed a bmp file, and if it is 8 bits, uncompressed
         if(*((short *) file_mem)!=19778)//BM (the identifier)
           {
               free(file_mem_start);
               fclose (f);
               return 0;
           }
       file_mem+=18;
       x_size=*((int *) file_mem);
       file_mem+=4;
       y_size=*((int *) file_mem);
       file_mem+=6;
       if(*((short *)file_mem)!=8)//8 bit/pixel?
           {
               free(file_mem_start);
               fclose (f);
               return 0;
           }
    
       file_mem+=2;
       if(*((int *)file_mem)!=0)//any compression?
           {
               free(file_mem_start);
               fclose (f);
               return 0;
           }
       file_mem+=16;
    
       colors_no=*((int *)file_mem);
       if(!colors_no)colors_no=256;
       file_mem+=8;//here comes the pallete
    
       color_pallete=file_mem+4;
       fread (file_mem, 1, colors_no*4+4, f);//header only
       file_mem+=colors_no*4;
    
       x_padding=x_size%4;
       if(x_padding)x_padding=4-x_padding;
    
       //now, allocate the memory for the file
       texture_mem = (Uint8 *) calloc ( x_size*y_size*4, sizeof(Uint8));
       read_buffer = (Uint8 *) calloc ( 2000, sizeof(Uint8));
    
    
       for(y=0;y<y_size;y++)
           {
               //fread (texture_mem+y*x_size, 1, x_size-x_padding, f);
               fread (read_buffer, 1, x_size-x_padding, f);
    
               for(x=0;x<x_size;x++)
                   {
                       current_pallete_entry=*(read_buffer+x);
                       b=*(color_pallete+current_pallete_entry*4);
                       g=*(color_pallete+current_pallete_entry*4+1);
                       r=*(color_pallete+current_pallete_entry*4+2);
                       *(texture_mem+(y*x_size+x)*4)=r;
                       *(texture_mem+(y*x_size+x)*4+1)=g;
                       *(texture_mem+(y*x_size+x)*4+2)=b;
                       *(texture_mem+(y*x_size+x)*4+3)=a;
                   }
    
           }
    
       free(file_mem_start);
       free(read_buffer);
       fclose (f);
       //ok, now, hopefully, the file is loaded and converted...
       //so, assign the texture, and such
    
       glGenTextures(1, &texture);
       glBindTexture(GL_TEXTURE_2D, texture);    //failsafe
       bind_texture_id(texture);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    
       if(poor_man)
           {
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
           }
       else if(use_mipmaps)
           {
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
               glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE);
           }
       else
           {
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
               glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
           }
    
       if(have_arb_compression)
           {
               if(have_s3_compression)
               glTexImage2D(GL_TEXTURE_2D,0,COMPRESSED_RGBA_S3TC_DXT5_EXT,x_size, y_size,0,GL_RGBA,GL_UNSIGNED_BYTE,texture_mem);
               else
               glTexImage2D(GL_TEXTURE_2D,0,COMPRESSED_RGBA_ARB,x_size, y_size,0,GL_RGBA,GL_UNSIGNED_BYTE,texture_mem);
    
           }
       else
       glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,x_size, y_size,0,GL_RGBA,GL_UNSIGNED_BYTE,texture_mem);
    
       check_gl_errors();
       free(texture_mem);
       return texture;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Edit This was a bug that has been fixed in https://github.com/threerings/playn/commit/137ce50dddd2716f84c38ec1568d7ae5a368434b . See Par
I am writing a game which uses opengles. I have created my renderer class
Writing Classic ASP code in JScript has a lot going for it: more humane
I am writing my own MVC framework and has come to the view renderer.
We're in the process of writing an app that has 4 tabs: Map, People,
I am writing a renderer plugin to 3ds max that is based on DirectX
I am looking at writing a cookie that will be updated everytime the 'news
I'm checking to see if anyone has written an MVC extension for Html.ActionLink that
I'm writing an Android application that uses OpenGL ES (GLSurfaceView and GLSurfaceView.Renderer). The problem
Writing htaccess that allows me to remove index.php from the URL can confuse search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.