Bitmap operations and OpenGL texture combination

Source: Internet
Author: User
Tags fread
//Texture Mapping Functionsvoid CCY457OpenGLView::LoadGLTextures(){    //Create Texture Names    glGenTextures(3, m_Texture);    LoadTexture("Apple.bmp",0);    LoadTexture("Fauve.bmp",1);    LoadTexture("Flower.bmp",2);}void CCY457OpenGLView::LoadTexture (CString fileName, int texName){    //Load Texture    AUX_RGBImageRec* m_texture;    m_texture = auxDIBImageLoad((const char*)fileName);    if(!m_texture)    {       MessageBox("Picture could not be loaded");       exit(1);    }    glBindTexture(GL_TEXTURE_2D, m_Texture[texName]);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_texWrap);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_texWrap);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_texFilter);    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_texFilter);    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_texMode);    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, m_texture->sizeX,m_texture->sizeY, GL_RGB, GL_UNSIGNED_BYTE, m_texture->data);}

 

Bytes ---------------------------------------------------------------------------------------------------------------

Two days ago, we encountered a problem when using bitmap for texture. when 8-Bit Bitmap cannot be used, I came up with an idea and hoped to complete it. so I checked some file structures about bitmap. I will not detail the bitmap structure here. because there are already a lot of online reading methods and many people have implemented many reading methods. refer to this blog post: bitmap Overview

Actually. I didn't know that I already had a powerful open-source tool .. freeimage .. is an image file that supports all current formats .. so I basically wrote it in white. for details about 1 4 16 32, refer to its source code to complete the following classes:

Now I only wrote the source code for one afternoon: the details are not summarized, all of which indicate the comments in the class:

// ================================================ ==================================================//*** @ File BMP loader. h ** file Description: loaded bitmap * Applicable platform: Windows98/2000/NT/XP ** by: acmiyou * Email: acmiyou@qq.com * // ================================================= ==========================================================#include <windows. h> # include <stdio. h> # ifndef _ BMP loader_h _ # DEFINE _ BMP loader_h _ # define bitmap_id 0x4d42/** <symbol of the bitmap file */# define blue 0 # define Green 1 # def INE Red 2 # define swapcolor (a, B) {\ (a) ^ = (B); \ (B) ^ = (a); \ () ^ = (B); \} class BMP loader {public: BMP loader ();~ BMP loader (); void free ();/** release memory */bitmapinfoheader * bitinfo;/** record Bitmap header information */byte * image; /** image data */bool loadbitmap (const char * filename);/** load bitmap file */PRIVATE: void computepalettesize (); /** calculate the number of color table items in the bitmap */INT getimageline (INT width, int bitcount);/** calculate the number of bytes in each line, the result is a multiple of 4 */byte * getscanline (byte * data, int pitch, int scanline ); /** get the starting address of each row of pixel data */bool covertto24bits (byte * data);/** forcibly convert all types to 24-bit */void covertline8bto24b (byte * DST, byte * SRC);/** convert 8-bit behavior to 24-bit rows */rgbquad * p_colortable;/** color table */}; # endif

 

// ================================================ ==================================================//*** @ File BMP loader. CPP ** file Description: loaded bitmap * Applicable platform: Windows98/2000/NT/XP ** Author: acmiyou * Email: acmiyou@qq.com * // ================================================= ============================================================#include "BMP Loader. H "BMP Loader: BMP loader () {bitinfo = 0; image = 0; p_colortable = 0;} BMP Loader ::~ BMP loader () {free ();}/***** release occupied memory */void BMP Loader: Free () {If (image! = NULL) {Delete [] image; image = 0;} If (bitinfo! = NULL) {Delete [] bitinfo; bitinfo = 0;} If (p_colortable! = NULL) {Delete [] p_colortable; p_colortable = 0 ;}/ *** load a bitmap file ** @ Param: ** filename file name */bool BMP Loader :: loadbitmap (const char * filename) {/** if data already exists, release the memory */free ();/** release the memory */file * fp = fopen (filename, "rb"); If (FP = NULL) return false;/** file opening failed */bitmapfileheader header; fread (& header, sizeof (bitmapfileheader), 1, FP);/** read the bitmap file header */If (header. bftype! = Bitmap_id)/** returns */{free (); fclose (FP); Return false;} bitinfo = (bitmapinfoheader *) if it is not a bitmap type *) new byte [sizeof (bitmapinfoheader)]; fread (bitinfo, sizeof (bitmapinfoheader), 1, FP);/** bitmap information header */If (bitinfo-> bisize! = 40) {free (); Return false;/** this class is not compressed in RGB format but does not support reading failure */} computepalettesize (); /** calculate the color table value */If (bitinfo-> biclrused! = 0) {p_colortable = (rgbquad *) New byte [sizeof (rgbquad) * bitinfo-> biclrused]; fread (p_colortable, sizeof (rgbquad) * bitinfo-> biclrused, 1, FP);}/** bitmap pixel data size */bitinfo-> bisizeimage = getimageline (bitinfo-> biwidth, bitinfo-> bibitcount) * bitinfo-> biheight; byte * imagedata = new byte [bitinfo-> bisizeimage]; fseek (FP, header. bfoffbits, seek_set);/** read pixel information */fread (imagedata, bitinfo-> bisizeimage, 1, FP); fclose (F P);/** forced conversion to 24-bit */If (! Covertto24bits (imagedata)/** returns */{free (); Return false;} Delete [] imagedata if the format of 1 4 16 32 bits is not supported; /** convert BGR --> RGB */For (INT duration = 0; duration <bitinfo-> bisizeimage; duration ++ = 3) swapcolor (image [duration], image [watermark + 2]); Return true;}/*** calculate the number of color table items in the bitmap ** @ Param: none */void BMP Loader: computepalettesize () {/** only 1 4 8 has a color table */If (bitinfo-> biclrused = 0) {Switch (bitinfo-> bibitcount) {Case 1: bitinfo-> biclrused = 2; break; c ASE 4: bitinfo-> biclrused = 16; break; case 8: bitinfo-> biclrused = 256; break; Case 16: Case 24: Case 32: bitinfo-> biclrused = 0; break; default: Break ;}}/ *** calculate the number of bytes per line ** @ Param: ** width ** bitcount the number of bits per pixel ** @ return: number of row bytes */int bmp Loader: getimageline (INT width, int bitcount) {return (width * bitcount) + 7)/8 + 3 &~ 3;}/** get the starting address of each row of pixel data ** @ Param: ** data pixel data ** pitch pixel row bytes ** scanline line ** @ return: line start address */byte * BMP Loader: getscanline (byte * data, int pitch, int scanline) {return data + pitch * scanline;}/*** forcibly convert all types to 24-bit ** @ Param: ** data pixel data */bool BMP Loader: covertto24bits (byte * Data) {If (bitinfo = NULL | DATA = NULL) return false; int lineimage = getimageline (bitinfo-> biwidth, 24); int linedata = getimageline (bitinfo-> biwidth, bitinfo-> bibitcount ); bitinfo-> bisizeimage = lineimage * bitinfo-> biheight; image = new byte [bitinfo-> bisizeimage]; memset (image, 0, bitinfo-> bisizeimage ); switch (bitinfo-> bibitcount) {Case 1: Return false; Case 4: Return false; case 8: For (int row = 0; row <bitinfo-> biheight; row ++) covertline8bto24b (getscanline (image, lineimage, row), getscanline (data, linedata, row); break; Case 16: Return false; Case 24: memcpy (image, data, bitinfo-> bisizeimage); break; Case 32: Return false;} return true;}/*** convert 8-bit behavior 24-bit rows ** @ Param: ** DST Destination Address ** SRC Source Address */void BMP Loader: covertline8bto24b (byte * DST, byte * SRC) {for (INT Col = 0; Col <bitinfo-> biwidth; col ++) {/** convert the index value to the color in the color table */DST [Blue] = p_colortable [SRC [col]. rgbblue; DST [Green] = p_colortable [SRC [col]. rgbgreen; DST [Red] = p_colortable [SRC [col]. rgbred; DST + = 3 ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.