加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 将256色位图转换为RGBA位图

发布时间:2020-12-16 09:38:13 所属栏目:百科 来源:网络整理
导读:我正在用C写一个渲染器.我看到类似的问题已得到解答,我只是在寻找更多信息.我有一个256色的位图,我加载到内存然后加载到OpenGL.我无法手动转换文件,需要在内存中进行.我希望能够将其转换为RGBA位图以利用alpha通道.有没有人有关于图书馆的任何代码或建议可以
我正在用C写一个渲染器.我看到类似的问题已得到解答,我只是在寻找更多信息.我有一个256色的位图,我加载到内存然后加载到OpenGL.我无法手动转换文件,需要在内存中进行.我希望能够将其转换为RGBA位图以利用alpha通道.有没有人有关于图书馆的任何代码或建议可以帮助我这个?

解决方法

我在某处有一些代码,但它只有20行或更少. BMP格式非常简单,只需读取标题,然后读取每个颜色,它用作调色板数组中的索引.请记住,BMP是颠倒的:

//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,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,x_size-x_padding,f);
           fread (read_buffer,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_LINEAR);
           glTexParameteri(GL_TEXTURE_2D,GL_LINEAR_MIPMAP_NEAREST);
           glTexParameteri(GL_TEXTURE_2D,GL_GENERATE_MIPMAP_SGIS,GL_TRUE);
       }
   else
       {
           glTexParameteri(GL_TEXTURE_2D,GL_LINEAR);
       }

   if(have_arb_compression)
       {
           if(have_s3_compression)
           glTexImage2D(GL_TEXTURE_2D,COMPRESSED_RGBA_S3TC_DXT5_EXT,GL_RGBA,GL_UNSIGNED_BYTE,texture_mem);
           else
           glTexImage2D(GL_TEXTURE_2D,COMPRESSED_RGBA_ARB,texture_mem);

       }
   else
   glTexImage2D(GL_TEXTURE_2D,texture_mem);

   check_gl_errors();
   free(texture_mem);
   return texture;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读