QGearsTexFile.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. -----------------------------------------------------------------------------
  3. The MIT License (MIT)
  4. Copyright (c) 2013-07-30 Tobias Peters <tobias.peters@kreativeffekt.at>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. -----------------------------------------------------------------------------
  21. */
  22. #include "data/QGearsTexFile.h"
  23. #include <OgreException.h>
  24. #include <OgreImageCodec.h>
  25. using Ogre::Exception;
  26. using Ogre::ImageCodec;
  27. using Ogre::MemoryDataStream;
  28. using Ogre::uint32;
  29. namespace QGears
  30. {
  31. //---------------------------------------------------------------------
  32. const Ogre::uint8 TexFile::USE_REFERENCE_ALPHA( 0xFE );
  33. //---------------------------------------------------------------------
  34. TexFile::TexFile()
  35. {
  36. //ctor
  37. }
  38. //---------------------------------------------------------------------
  39. TexFile::~TexFile()
  40. {
  41. //dtor
  42. }
  43. //---------------------------------------------------------------------
  44. void TexFile::read( Ogre::DataStreamPtr& input )
  45. {
  46. readHeader( input );
  47. size_t image_pixel_count(
  48. m_header.image_data.width
  49. * m_header.image_data.height
  50. );
  51. m_image_data.resize( image_pixel_count );
  52. memset( &m_image_data[0], 0
  53. , image_pixel_count * sizeof( t_ImageData::value_type )
  54. );
  55. if( m_header.palette_data.flag )
  56. {
  57. readPalleted( input );
  58. }
  59. else
  60. {
  61. OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED
  62. , "PixelFormat Data not yet implemented"
  63. , "QGears::TexFile::read"
  64. );
  65. }
  66. }
  67. //---------------------------------------------------------------------
  68. void TexFile::readHeader( Ogre::DataStreamPtr& input )
  69. {
  70. // Read header in full
  71. input->read( &m_header, sizeof( m_header ) );
  72. // Endian flip if required, all 32-bit values
  73. flipEndian( &m_header, 4, sizeof( m_header ) / 4 );
  74. if ( m_header.version != 0x1 )
  75. {
  76. OGRE_EXCEPT( Exception::ERR_NOT_IMPLEMENTED
  77. , "Only TEX Version 0x1 implemented"
  78. , "QGears::TexFile::read"
  79. );
  80. }
  81. }
  82. //---------------------------------------------------------------------
  83. void TexFile::readPalleted( Ogre::DataStreamPtr& input )
  84. {
  85. size_t image_pixel_count( m_image_data.size() );
  86. entry_t total_color_count( m_header.palette_data.total_color_count );
  87. if( !total_color_count )
  88. {
  89. total_color_count = m_header.palette_total_color_count;
  90. }
  91. entry_t current_palette( 0 );
  92. entry_t color_key_flag( m_header.color_key_flag );
  93. if( m_header.color_key_array_flag )
  94. {
  95. //color_key_flag = color_key_array[current_palette];
  96. }
  97. size_t palette_data_size(
  98. total_color_count
  99. * sizeof( t_Palette::value_type )
  100. );
  101. m_palette.resize( total_color_count );
  102. void *ptr_palette( &m_palette[0] );
  103. input->read( ptr_palette, palette_data_size );
  104. flipEndian( ptr_palette, sizeof( t_Palette::value_type )
  105. , total_color_count
  106. );
  107. typedef std::vector<Ogre::uint8> t_IndexData;
  108. t_IndexData index_data( image_pixel_count );
  109. input->read( &index_data[0], image_pixel_count );
  110. for( size_t i( 0 ); i < image_pixel_count; ++i )
  111. {
  112. t_IndexData::value_type index( index_data[i] );
  113. if( index < m_palette.size() )
  114. {
  115. m_image_data[i] = m_palette[index];
  116. }
  117. else
  118. {
  119. OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS
  120. , "PaletteIndexOutOfBounds"
  121. , "QGears::TexFile::readPalleted"
  122. );
  123. }
  124. }
  125. }
  126. //---------------------------------------------------------------------
  127. MemoryDataStream* TexFile::getPixelData() const
  128. {
  129. size_t data_size( m_image_data.size()
  130. * sizeof( t_ImageData::value_type )
  131. );
  132. MemoryDataStream* data( OGRE_NEW MemoryDataStream( data_size ) );
  133. memcpy( data->getPtr(), &m_image_data[0], data_size );
  134. return data;
  135. }
  136. //---------------------------------------------------------------------
  137. ImageCodec::ImageData* TexFile::getImageData() const
  138. {
  139. ImageCodec::ImageData *img_data = OGRE_NEW ImageCodec::ImageData();
  140. const ImageData &data( m_header.image_data );
  141. img_data->width = data.width;
  142. img_data->height = data.height;
  143. img_data->size = m_image_data.size() * sizeof( t_ImageData::value_type );
  144. img_data->num_mipmaps = 0;
  145. img_data->flags = 0;
  146. img_data->depth = 1;
  147. img_data->format = Ogre::PF_A8R8G8B8;
  148. return img_data;
  149. }
  150. //---------------------------------------------------------------------
  151. void TexFile::flipEndian(void * pData, size_t size, size_t count) const
  152. {
  153. #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
  154. for(unsigned int index = 0; index < count; index++)
  155. {
  156. flipEndian((void *)((long)pData + (index * size)), size);
  157. }
  158. #endif
  159. }
  160. //---------------------------------------------------------------------
  161. void TexFile::flipEndian(void * pData, size_t size) const
  162. {
  163. #if OGRE_ENDIAN == OGRE_ENDIAN_BIG
  164. char swapByte;
  165. for(unsigned int byteIndex = 0; byteIndex < size/2; byteIndex++)
  166. {
  167. swapByte = *(char *)((long)pData + byteIndex);
  168. *(char *)((long)pData + byteIndex) = *(char *)((long)pData + size - byteIndex - 1);
  169. *(char *)((long)pData + size - byteIndex - 1) = swapByte;
  170. }
  171. #endif
  172. }
  173. }