/** ******************************************************************************* * @file ewolTexture.cpp * @brief ewol Texture loading system (sources) * @author Edouard DUPIN * @date 28/10/2011 * @par Project * ewol * * @par Copyright * Copyright 2011 Edouard DUPIN, all right reserved * * This software is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY. * * Licence summary : * You can modify and redistribute the sources code and binaries. * You can send me the bug-fix * * Term of the licence in in the file licence.txt. * ******************************************************************************* */ #include extern "C" { #pragma pack(push,1) typedef struct { int16_t bfType; int32_t bfSize; int32_t bfReserved; int32_t bfOffBits; } bitmapFileHeader_ts; typedef struct { int32_t biSize; int32_t biWidth; int32_t biHeight; int16_t biPlanes; int16_t biBitCount; int32_t biCompression; int32_t biSizeImage; int32_t biXPelsPerMeter; int32_t biYPelsPerMeter; int32_t biClrUsed; int32_t biClrImportant; } bitmapInfoHeader_ts; #pragma pack(pop) typedef enum { BITS_16_R5G6B5, BITS_16_X1R5G5B5, BITS_24_R8G8B8, BITS_32_X8R8G8B8, BITS_32_A8R8G8B8 } modeBitmap_te; }; class Bitmap { private: modeBitmap_te m_dataMode; int32_t m_width; int32_t m_height; int32_t m_size; uint8_t * m_data; uint8_t * m_dataGenerate; bitmapFileHeader_ts m_FileHeader; bitmapInfoHeader_ts m_InfoHeader; public: Bitmap(etk::File & fileName) : m_data(NULL), m_dataGenerate(NULL) { m_dataMode = BITS_16_R5G6B5; m_width = 0; m_height = 0; m_size = 0; FILE *File=NULL; // Get the fileSize ... if (fileName.Size() < sizeof(bitmapFileHeader_ts) + sizeof(bitmapInfoHeader_ts) ) { EWOL_ERROR("not enought data in the file named=\"" << fileName << "\""); return; } unsigned int ImageIdx=0; File=fopen(fileName.GetCompleateName().c_str(),"rb"); if(NULL == File) { EWOL_ERROR("Can not find the file name=\"" << fileName << "\""); return; } // get the data : if (fread(&m_FileHeader,sizeof(bitmapFileHeader_ts),1,File) != 1) { EWOL_ERROR("error loading file header"); fclose(File); return; } if (fread(&m_InfoHeader,sizeof(bitmapInfoHeader_ts),1,File) != 1) { EWOL_ERROR("error loading file header"); fclose(File); return; } // TODO : do otherwise ... fseek(File,m_FileHeader.bfOffBits,SEEK_SET); if(ferror(File)) { EWOL_ERROR("error with the 'bfOffBits' in the file named=\"" << fileName << "\""); fclose(File); return; } // Check the header error : if (m_FileHeader.bfType != 0x4D42) { EWOL_ERROR("the file=\"" << fileName << "\" is not a bitmap file ..."); fclose(File); return; } if (m_FileHeader.bfReserved != 0x00000000) { EWOL_ERROR("the bfReserved feald is not at 0 ==> not supported format ..."); fclose(File); return; } if( m_InfoHeader.biBitCount == 16 && m_InfoHeader.biCompression == 0) { m_dataMode = BITS_16_X1R5G5B5; } else if( m_InfoHeader.biBitCount == 16 && m_InfoHeader.biCompression == 3) { m_dataMode = BITS_16_R5G6B5; } else if( m_InfoHeader.biBitCount == 24 && m_InfoHeader.biCompression == 0) { m_dataMode = BITS_24_R8G8B8; } else if( m_InfoHeader.biBitCount == 32 && m_InfoHeader.biCompression == 3) { m_dataMode = BITS_32_X8R8G8B8; } else if( m_InfoHeader.biBitCount == 32 && m_InfoHeader.biCompression == 0) { m_dataMode = BITS_32_A8R8G8B8; } else { EWOL_ERROR("the biBitCount & biCompression fealds are unknow ==> not supported format ..."); fclose(File); return; } m_width = m_InfoHeader.biWidth; m_height = m_InfoHeader.biHeight; if(0 != m_InfoHeader.biSizeImage) { m_data=new uint8_t[m_InfoHeader.biSizeImage]; fread(m_data,m_InfoHeader.biSizeImage,1,File); // allocate the destination data ... m_dataGenerate=new uint8_t[m_width*m_height*4]; } fclose(File); // need now to generate RGBA data ... switch(m_dataMode) { case BITS_16_R5G6B5: { uint16_t * pointer = (uint16_t*)m_data; uint8_t * pointerdst = m_dataGenerate; for(int32_t yyy=0; yyy> 8); m_dataGenerate[4*((m_height-yyy-1) * m_width + xxx ) + 1] = (int8_t)((*pointer & 0x07E0) >> 3); m_dataGenerate[4*((m_height-yyy-1) * m_width + xxx ) + 2] = (int8_t)(*pointer << 3); m_dataGenerate[4*((m_height-yyy-1) * m_width + xxx ) + 3] = 0xFF; pointer++; } } } break; case BITS_16_X1R5G5B5: { uint16_t * pointer = (uint16_t*)m_data; uint8_t * pointerdst = m_dataGenerate; for(int32_t yyy=0; yyy> 7); m_dataGenerate[4*((m_height-yyy-1) * m_width + xxx ) + 1] = (int8_t)((*pointer & 0x03E0) >> 2); m_dataGenerate[4*((m_height-yyy-1) * m_width + xxx ) + 2] = (int8_t)(*pointer << 3); m_dataGenerate[4*((m_height-yyy-1) * m_width + xxx ) + 3] = 0xFF; pointer++; } } } break; case BITS_24_R8G8B8: { uint8_t * pointer = m_data; for(int32_t yyy=0; yyy #include int32_t ewol::LoadTexture(etk::File fileName) { etk::String fileExtention = fileName.GetExtention(); if (fileExtention == "bmp") { if (false == fileName.Exist()) { EWOL_ERROR("File does not Exist ... " << fileName); return -1; } Bitmap myBitmap(fileName); myBitmap.Display(); if (myBitmap.LoadOK() == true) { GLuint textureid; glGenTextures(1, &textureid); glBindTexture(GL_TEXTURE_2D, textureid); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //--- mode nearest //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); //--- Mode linear glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, myBitmap.Width(), myBitmap.Height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, myBitmap.Data()); return textureid; } else { return -1; } } else { EWOL_ERROR("Extention not manage " << fileName); return -1; } } void ewol::UnLoadTexture(int32_t textureID) { }