/*************************************************************************** texture.cpp ---------------------------------------------------------------------------- begin : june 2003 copyright : (C) 2003 by Pierre Alliez - INRIA email : pierre.alliez@sophia.inria.fr ***************************************************************************/ #include #include #include "texture.h" ////////////////////////////////////////////// // CONSTRUCTORS ////////////////////////////////////////////// //******************************************** // Constructor //******************************************** Texture::Texture() { m_pData = NULL; m_Width = 0; m_WidthByte32 = 0; m_Height = 0; m_Depth = 0; } //******************************************** // Destructor //******************************************** Texture::~Texture() { Free(); } ////////////////////////////////////////////// // DATA ////////////////////////////////////////////// //******************************************** // Alloc //******************************************** int Texture::Alloc(unsigned int width, unsigned int height, unsigned int depth) { Free(); unsigned int Width32 = WidthByte32(width,depth); m_pData = new unsigned char [Width32 * height]; if(m_pData == NULL) { return 0; } // Set members variables m_Width = width; m_WidthByte32 = Width32; m_Height = height; m_Depth = depth; UpdateHeader(); return 1; } //******************************************** // Free //******************************************** void Texture::Free() { if(m_pData != NULL) { delete [] m_pData; m_pData = NULL; } m_Width = 0; m_Height = 0; m_Depth = 0; } //******************************************** // UpdateWidthByte32 //******************************************** void Texture::UpdateWidthByte32() { m_WidthByte32 = WidthByte32(m_Width,m_Depth); } //******************************************** // WidthByte32 //******************************************** unsigned int Texture::WidthByte32(unsigned int width, unsigned int depth) { // 32 bits alignment (4 bytes) int rest=(width*depth/8)%4; if(rest != 0) return (width*depth/8 + 4-rest); else return (width*depth/8); } //******************************************** // UpdateHeader //******************************************** void Texture::UpdateHeader() { UpdateWidthByte32(); } ////////////////////////////////////////////// ////////////////////////////////////////////// // CHECKING ////////////////////////////////////////////// ////////////////////////////////////////////// //******************************************** // IsValid //******************************************** int Texture::IsValid() { int success = 0; success = (m_Depth == 24) || (m_Depth == 32); success &= (m_Width != 0); success &= (m_Height != 0); success &= (m_pData != NULL); return success; } //******************************************** // HigherPowerOfTwo //******************************************** int Texture::HigherPowerOfTwo(int value) { if(value <= 0) return value; int power = 1; int x = 0; while(1) { x = (int)pow(2.0,(double)power); if(x >= value) return x; power++; } } //******************************************** // LowerPowerOfTwo //******************************************** int Texture::LowerPowerOfTwo(int value) { if(value <= 0) return value; int power = 1; int x = 0; while(1) { x = (int)pow(2.0,(double)power); if(x >= value) return (int)pow(2.0,(double)power-1); power++; } } //******************************************** // SameSize //******************************************** int Texture::SameSize(Texture *pTexture) { int success = (m_Width == pTexture->GetWidth()); success &= (m_Height == pTexture->GetHeight()); return success; } //******************************************** // Flip BGR to RGB //******************************************** int Texture::BGRtoRGB() { if(!IsValid()) return 0; unsigned char pixel; int BytePerPixel = m_Depth/8; for(unsigned int j=0;j= right || top >= bottom) return 0; if(left < 0 || left >= (int)m_Width || right < 0 || right >= (int)m_Width) return 0; if(top < 0 || top >= (int)m_Height || bottom < 0 || bottom >= (int)m_Height) return 0; int NewWidth = right-left+1; int NewWidthByte32 = WidthByte32(NewWidth,m_Depth); int NewHeight = bottom-top+1; int BytePerPixel = m_Depth / 8; int i,j,k; //TRACE("Start extracting...\n"); //TRACE("New width : %d\n",NewWidth); //TRACE("New height : %d\n",NewHeight); // Alloc unsigned char *pData = new unsigned char[NewWidthByte32*NewHeight]; if(pData == NULL) { //TRACE("Insufficiant memory"); return 0; } for(j=0;jIsValid()) return 0; if(!SameSize(pTexture)) return 0; if(!AddAlphaLayer(0)) return 0; // Fill new data unsigned char *pData = pTexture->GetData(); int size = m_Width * m_Height; int BytePerPixel = pTexture->GetDepth() / 8; for(int i=0;iGetData(); if(pBuffer == NULL) return; unsigned int width = pTexture->GetWidth(); unsigned int height = pTexture->GetHeight(); unsigned int depth = pTexture->GetDepth(); if(!Alloc(width,height,depth)) return; unsigned int BytePerPixel = depth / 8; for(unsigned int j=0;j