• [MetaHook] Load TGA texture to OpenGL


    This function load a *.tga texture file and convert to OpenGL pixel format, uncompress only.

     1 #pragma pack(1)
     2 
     3 struct TgaHeader
     4 {
     5     unsigned char m_IDLength;
     6     unsigned char m_ColorMapType;
     7     unsigned char m_ImageType;
     8     unsigned short m_CMapStart;
     9     unsigned short m_CMapLength;
    10     unsigned char m_CMapDepth;
    11     unsigned short m_XOffset;
    12     unsigned short m_YOffset;
    13     unsigned short m_Width;
    14     unsigned short m_Height;
    15     unsigned char m_PixelDepth;
    16     unsigned char m_ImageDescriptor;
    17 };
    18 
    19 #pragma pack()
     1 bool LoadTGA(const char *pszFileName, unsigned char *pBuffer, int iBufferSize, int *pInternalFormat, int *pWidth, int *pHeight)
     2 {
     3     FileHandle_t pFile = g_pFileSystem->Open(pszFileName, "rb");
     4 
     5     if (!pFile)
     6         return false;
     7 
     8     TgaHeader Header;
     9     memset(&Header, 0, sizeof(Header));
    10 
    11     g_pFileSystem->Read(&Header, sizeof(Header), pFile);
    12 
    13     if (Header.m_ImageType != 2)
    14     {
    15         g_pFileSystem->Close(pFile);
    16         return false;
    17     }
    18 
    19     if (Header.m_PixelDepth != 24 && Header.m_PixelDepth != 32)
    20     {
    21         g_pFileSystem->Close(pFile);
    22         return false;
    23     }
    24 
    25     uint32 iPixelSize, iImageSize;
    26 
    27     switch (Header.m_PixelDepth)
    28     {
    29     case 24:
    30         iPixelSize = 3;
    31         *pInternalFormat = GL_RGB;
    32         break;
    33     case 32:
    34         iPixelSize = 4;
    35         *pInternalFormat = GL_RGBA;
    36         break;
    37     }
    38 
    39     iImageSize = Header.m_Width * Header.m_Height * iPixelSize;
    40 
    41     if (iImageSize > (uint32)iBufferSize)
    42     {
    43         g_pFileSystem->Close(pFile);
    44         return false;
    45     }
    46 
    47     uint32 iLineSize = Header.m_Width * iPixelSize;
    48     uint32 iLinePos;
    49 
    50     for (uint32 y = 0; y < Header.m_Height; ++y)
    51     {
    52         iLinePos = (Header.m_Height - y - 1) * iLineSize;
    53         g_pFileSystem->Read(&pBuffer[iLinePos], iLineSize, pFile);
    54     }
    55 
    56     for (uint32 i = 0; i < iImageSize; i += iPixelSize)
    57     {
    58         pBuffer[i + 0] ^= pBuffer[i + 2];
    59         pBuffer[i + 2] ^= pBuffer[i + 0];
    60         pBuffer[i + 0] ^= pBuffer[i + 2];
    61     }
    62 
    63     *pWidth = Header.m_Width;
    64     *pHeight = Header.m_Height;
    65 
    66     g_pFileSystem->Close(pFile);
    67     return true;
    68 }
  • 相关阅读:
    [BestCoder Round #3] hdu 4907 Task schedule (模拟简单题)
    .NET中的PublicKeyToken以及强命名问题
    bug统计分析续(一)基于SQL的Bug统计方法
    iOS项目开发实战——通过Http Get方式与server通信
    TCP与UDP的区别(转)
    Telit GPRS模块测试报告
    GPRS优点介绍及GPRS上网相关知识(转)
    MC34063中文资料及应用实例(转)
    MC34063+MOSFET扩流 12V-5V 折腾出了高效率电路(转)
    Bluez SPP实现代码分析(转)
  • 原文地址:https://www.cnblogs.com/crsky/p/4706262.html
Copyright © 2020-2023  润新知