summaryrefslogtreecommitdiff
path: root/vgui2/src/fileimage.cpp
blob: b28c50ad94cb13f4228909518dbe1754aac84729 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//


#include <string.h>
#include "fileimage.h"

#include "winlite.h"
#include "vgui_internal.h"
#include "filesystem.h"

// memdbgon must be the last include file in a .cpp file!!!
#include "tier0/memdbgon.h"

// TGA header.
#pragma pack(1)
	class TGAFileHeader
	{
	public:
		unsigned char	m_IDLength;
		unsigned char	m_ColorMapType;
		unsigned char	m_ImageType;
		unsigned short	m_CMapStart;
		unsigned short	m_CMapLength;
		unsigned char	m_CMapDepth;
		unsigned short	m_XOffset;
		unsigned short	m_YOffset;
		unsigned short	m_Width;
		unsigned short	m_Height;
		unsigned char	m_PixelDepth;
		unsigned char	m_ImageDescriptor;
	};
#pragma pack()



// ---------------------------------------------------------------------------------------- //
// FileImageStream_Memory.
// ---------------------------------------------------------------------------------------- //
FileImageStream_Memory::FileImageStream_Memory(void *pData, int dataLen)
{
	m_pData = (unsigned char*)pData;
	m_DataLen = dataLen;
	m_CurPos = 0;
	m_bError = false;
}

void FileImageStream_Memory::Read(void *pData, int len)
{
	unsigned char *pOut;
	int i;

	pOut = (unsigned char*)pData;
	for(i=0; i < len; i++)
	{
		if(m_CurPos < m_DataLen)
		{
			pOut[i] = m_pData[m_CurPos];
			++m_CurPos;
		}
		else
		{
			pOut[i] = 0;
			m_bError = true;
		}
	}
}

bool FileImageStream_Memory::ErrorStatus()
{
	bool ret=m_bError;
	m_bError=false;
	return ret;
}



// ---------------------------------------------------------------------------------------- //
// Encode/decode functions.
// ---------------------------------------------------------------------------------------- //
static void WriteRun(unsigned char *pColor, FileHandle_t fp, int runLength)
{
	unsigned char runCount;

	runCount = runLength - 1;
	runCount |= (1 << 7);
	g_pFullFileSystem->Write( &runCount, 1, fp );
	g_pFullFileSystem->Write( pColor, 4, fp );
}


// Load in a 32-bit TGA file.
bool Load32BitTGA(
	FileImageStream *fp,
	FileImage *pImage)
{
	TGAFileHeader hdr;
	char dummyChar;
	int i, x, y;
	long color;
	int runLength, curOut;
	unsigned char *pLine;
	unsigned char packetHeader;


	pImage->Term();

	// Read and verify the header.
	fp->Read(&hdr, sizeof(hdr));
	if(hdr.m_PixelDepth != 32 || hdr.m_ImageType != 10)
		return false;

	// Skip the ID area..
	for(i=0; i < hdr.m_IDLength; i++)
		fp->Read(&dummyChar, 1);

	pImage->m_Width = hdr.m_Width;
	pImage->m_Height = hdr.m_Height;
	pImage->m_pData = new unsigned char[hdr.m_Width * hdr.m_Height * 4];
	if(!pImage->m_pData)
		return false;

	// Read in the data..
	for(y=pImage->m_Height-1; y >= 0; y--)
	{
		pLine = &pImage->m_pData[y*pImage->m_Width*4];

		curOut = 0;
		while(curOut < pImage->m_Width)
		{
			fp->Read(&packetHeader, 1);
			
			runLength = (int)(packetHeader & ~(1 << 7)) + 1;
			if(curOut + runLength > pImage->m_Width)
				return false;

			if(packetHeader & (1 << 7))
			{
				fp->Read(&color, 4);
				for(x=0; x < runLength; x++)
				{
					*((long*)pLine) = color;
					pLine += 4;
				}
			}
			else
			{
				for(x=0; x < runLength; x++)
				{
					fp->Read(&color, 4);
					*((long*)pLine) = color;
					pLine += 4;
				}
			}

			curOut += runLength;
		}
	}

	return true;
}


// Write a 32-bit TGA file.
void Save32BitTGA(
	FileHandle_t fp,
	FileImage *pImage)
{
	TGAFileHeader hdr;
	int y, runStart, x;
	unsigned char *pLine;


	memset(&hdr, 0, sizeof(hdr));
	hdr.m_PixelDepth = 32;
	hdr.m_ImageType = 10;	// Run-length encoded RGB.
	hdr.m_Width = pImage->m_Width;
	hdr.m_Height = pImage->m_Height;

	g_pFullFileSystem->Write(&hdr, sizeof(hdr), fp );
	
	// Lines are written bottom-up.
	for(y=pImage->m_Height-1; y >= 0; y--)
	{
		pLine = &pImage->m_pData[y*pImage->m_Width*4];
		
		runStart = 0;
		for(x=0; x < pImage->m_Width; x++)
		{
			if((x - runStart) >= 128 ||
				*((long*)&pLine[runStart*4]) != *((long*)&pLine[x*4]))
			{
				// Encode this  Run.
				WriteRun(&pLine[runStart*4], fp, x - runStart);
				runStart = x;
			}
		}

		// Encode the last Run.
		if(x - runStart > 0)
		{
			WriteRun(&pLine[runStart*4], fp, x - runStart);
		}
	}
}