Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
4.2 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <string.h>
  8. #include "fileimage.h"
  9. #include "winlite.h"
  10. #include "vgui_internal.h"
  11. #include "filesystem.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. // TGA header.
  15. #pragma pack(1)
  16. class TGAFileHeader
  17. {
  18. public:
  19. unsigned char m_IDLength;
  20. unsigned char m_ColorMapType;
  21. unsigned char m_ImageType;
  22. unsigned short m_CMapStart;
  23. unsigned short m_CMapLength;
  24. unsigned char m_CMapDepth;
  25. unsigned short m_XOffset;
  26. unsigned short m_YOffset;
  27. unsigned short m_Width;
  28. unsigned short m_Height;
  29. unsigned char m_PixelDepth;
  30. unsigned char m_ImageDescriptor;
  31. };
  32. #pragma pack()
  33. // ---------------------------------------------------------------------------------------- //
  34. // FileImageStream_Memory.
  35. // ---------------------------------------------------------------------------------------- //
  36. FileImageStream_Memory::FileImageStream_Memory(void *pData, int dataLen)
  37. {
  38. m_pData = (unsigned char*)pData;
  39. m_DataLen = dataLen;
  40. m_CurPos = 0;
  41. m_bError = false;
  42. }
  43. void FileImageStream_Memory::Read(void *pData, int len)
  44. {
  45. unsigned char *pOut;
  46. int i;
  47. pOut = (unsigned char*)pData;
  48. for(i=0; i < len; i++)
  49. {
  50. if(m_CurPos < m_DataLen)
  51. {
  52. pOut[i] = m_pData[m_CurPos];
  53. ++m_CurPos;
  54. }
  55. else
  56. {
  57. pOut[i] = 0;
  58. m_bError = true;
  59. }
  60. }
  61. }
  62. bool FileImageStream_Memory::ErrorStatus()
  63. {
  64. bool ret=m_bError;
  65. m_bError=false;
  66. return ret;
  67. }
  68. // ---------------------------------------------------------------------------------------- //
  69. // Encode/decode functions.
  70. // ---------------------------------------------------------------------------------------- //
  71. static void WriteRun(unsigned char *pColor, FileHandle_t fp, int runLength)
  72. {
  73. unsigned char runCount;
  74. runCount = runLength - 1;
  75. runCount |= (1 << 7);
  76. g_pFullFileSystem->Write( &runCount, 1, fp );
  77. g_pFullFileSystem->Write( pColor, 4, fp );
  78. }
  79. // Load in a 32-bit TGA file.
  80. bool Load32BitTGA(
  81. FileImageStream *fp,
  82. FileImage *pImage)
  83. {
  84. TGAFileHeader hdr;
  85. char dummyChar;
  86. int i, x, y;
  87. long color;
  88. int runLength, curOut;
  89. unsigned char *pLine;
  90. unsigned char packetHeader;
  91. pImage->Term();
  92. // Read and verify the header.
  93. fp->Read(&hdr, sizeof(hdr));
  94. if(hdr.m_PixelDepth != 32 || hdr.m_ImageType != 10)
  95. return false;
  96. // Skip the ID area..
  97. for(i=0; i < hdr.m_IDLength; i++)
  98. fp->Read(&dummyChar, 1);
  99. pImage->m_Width = hdr.m_Width;
  100. pImage->m_Height = hdr.m_Height;
  101. pImage->m_pData = new unsigned char[hdr.m_Width * hdr.m_Height * 4];
  102. if(!pImage->m_pData)
  103. return false;
  104. // Read in the data..
  105. for(y=pImage->m_Height-1; y >= 0; y--)
  106. {
  107. pLine = &pImage->m_pData[y*pImage->m_Width*4];
  108. curOut = 0;
  109. while(curOut < pImage->m_Width)
  110. {
  111. fp->Read(&packetHeader, 1);
  112. runLength = (int)(packetHeader & ~(1 << 7)) + 1;
  113. if(curOut + runLength > pImage->m_Width)
  114. return false;
  115. if(packetHeader & (1 << 7))
  116. {
  117. fp->Read(&color, 4);
  118. for(x=0; x < runLength; x++)
  119. {
  120. *((long*)pLine) = color;
  121. pLine += 4;
  122. }
  123. }
  124. else
  125. {
  126. for(x=0; x < runLength; x++)
  127. {
  128. fp->Read(&color, 4);
  129. *((long*)pLine) = color;
  130. pLine += 4;
  131. }
  132. }
  133. curOut += runLength;
  134. }
  135. }
  136. return true;
  137. }
  138. // Write a 32-bit TGA file.
  139. void Save32BitTGA(
  140. FileHandle_t fp,
  141. FileImage *pImage)
  142. {
  143. TGAFileHeader hdr;
  144. int y, runStart, x;
  145. unsigned char *pLine;
  146. memset(&hdr, 0, sizeof(hdr));
  147. hdr.m_PixelDepth = 32;
  148. hdr.m_ImageType = 10; // Run-length encoded RGB.
  149. hdr.m_Width = pImage->m_Width;
  150. hdr.m_Height = pImage->m_Height;
  151. g_pFullFileSystem->Write(&hdr, sizeof(hdr), fp );
  152. // Lines are written bottom-up.
  153. for(y=pImage->m_Height-1; y >= 0; y--)
  154. {
  155. pLine = &pImage->m_pData[y*pImage->m_Width*4];
  156. runStart = 0;
  157. for(x=0; x < pImage->m_Width; x++)
  158. {
  159. if((x - runStart) >= 128 ||
  160. *((long*)&pLine[runStart*4]) != *((long*)&pLine[x*4]))
  161. {
  162. // Encode this Run.
  163. WriteRun(&pLine[runStart*4], fp, x - runStart);
  164. runStart = x;
  165. }
  166. }
  167. // Encode the last Run.
  168. if(x - runStart > 0)
  169. {
  170. WriteRun(&pLine[runStart*4], fp, x - runStart);
  171. }
  172. }
  173. }