From d97a917a5e67ef639559aeefe4211e1d1d0aea50 Mon Sep 17 00:00:00 2001 From: Anakin Date: Thu, 8 Sep 2016 15:55:12 +0200 Subject: [PATCH] rle files are now supported --- MshViewer/Source/OpenGlController.cpp | 2 +- MshViewer/Source/Texture.cpp | 57 +++++++++++++++++++-------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/MshViewer/Source/OpenGlController.cpp b/MshViewer/Source/OpenGlController.cpp index f9c5865..06012e0 100644 --- a/MshViewer/Source/OpenGlController.cpp +++ b/MshViewer/Source/OpenGlController.cpp @@ -11,7 +11,7 @@ #define VERTEX_SHADER "Shader/VertexTextureShader.mv2shdr" #define FRAGMENT_SHADER "Shader/FragmentTextureShader.mv2shdr" -#define TEXTURE_NAME "Textures/dice.tga" +#define TEXTURE_NAME "Textures/texture32R.tga" ///////////////////////////////////////////////////////////////////////// // public constructor/destructor diff --git a/MshViewer/Source/Texture.cpp b/MshViewer/Source/Texture.cpp index 8ba9c0c..06f50b4 100644 --- a/MshViewer/Source/Texture.cpp +++ b/MshViewer/Source/Texture.cpp @@ -12,8 +12,8 @@ TextureTGA::TextureTGA(const char * filePath) throw std::invalid_argument(std::string("file not found: ") += filePath); // read in the header - std::uint8_t ui8x18Header[18] = { 0 }; - fsPicture.read(reinterpret_cast(&ui8x18Header), sizeof(ui8x18Header)); + std::uint8_t ui8x18Header[19] = { 0 }; + fsPicture.read(reinterpret_cast(&ui8x18Header), sizeof(ui8x18Header)-1); // extract all information from header ui32IDLength = ui8x18Header[0]; @@ -56,7 +56,45 @@ TextureTGA::TextureTGA(const char * filePath) // else if compressed 24 or 32 bit else if (ui32PicType == 10 && (ui32BpP == 24 || ui32BpP == 32)) // compressed { - throw std::invalid_argument("Invaild File Format! Don't compress the image."); + std::uint8_t tempChunkHeader; + std::uint8_t tempData[5]; + int tempByteIndex = 0; + std::size_t tempPixelIndex = 0; + + do { + fsPicture.read(reinterpret_cast(&tempChunkHeader), sizeof(tempChunkHeader)); + + if (tempChunkHeader >> 7) // repeat count + { + // just use the first 7 bits + tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1); + + fsPicture.read(reinterpret_cast(&tempData), ui32BpP/8); + + for (int i = 0; i <= tempChunkHeader; i++) + { + vui8Pixels[tempByteIndex++] = tempData[0]; + vui8Pixels[tempByteIndex++] = tempData[1]; + vui8Pixels[tempByteIndex++] = tempData[2]; + if(ui32BpP == 32) vui8Pixels[tempByteIndex++] = tempData[3]; + } + } + else // data count + { + // just use the first 7 bits + tempChunkHeader = (uint8_t(tempChunkHeader << 1) >> 1); + + for (int i = 0; i <= tempChunkHeader; i++) + { + fsPicture.read(reinterpret_cast(&tempData), ui32BpP/8); + + vui8Pixels[tempByteIndex++] = tempData[0]; + vui8Pixels[tempByteIndex++] = tempData[1]; + vui8Pixels[tempByteIndex++] = tempData[2]; + if (ui32BpP == 32) vui8Pixels[tempByteIndex++] = tempData[3]; + } + } + } while (tempByteIndex < ui32Size); } // not useable format else @@ -65,19 +103,6 @@ TextureTGA::TextureTGA(const char * filePath) throw std::invalid_argument("Invaild File Format! Required 24 or 31 Bit Image."); } - - //fix color mix - /*std::uint8_t temp; - std::uint32_t it = 0; - - while (it + 2 < ui32Size) - { - temp = vui8Pixels[it]; - vui8Pixels[it] = vui8Pixels[it + 2]; - vui8Pixels[it + 2] = temp; - ui32BpP == 32 ? it += 4 : it += 3; - }*/ - fsPicture.close(); }