summaryrefslogtreecommitdiff
path: root/utils/vtfscrew/vtfscrew.cpp
blob: 24d7152db1fd2d5e7d226c19f3a914cd6cd05d97 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//
#include <stdlib.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "vtf/vtf.h"
#include "UtlBuffer.h"

int main( int argc, char **argv )
{
	if( argc != 5 )
	{
		fprintf( stderr, "Usage: vtfscreen blah.vtf 0 1 2\n" );
		exit( -1 );
	}
	const char *pSrcFilename = argv[1];
	int rOffset = atoi( argv[2] );
	int gOffset = atoi( argv[3] );
	int bOffset = atoi( argv[4] );
	CUtlBuffer srcData;

	struct	_stat buf;
	if( _stat( pSrcFilename, &buf ) == -1 )
	{
		fprintf( stderr, "Couldn't stat %s\n", pSrcFilename );
		exit( -1 );
	}

	int srcFileSize = buf.st_size;

	srcData.EnsureCapacity( srcFileSize );

	FILE *fp = fopen( pSrcFilename, "rb" );
	if( !fp )
	{
		fprintf( stderr, "Couldn't open %s\n", pSrcFilename );
		exit( -1 );
	}

	int nBytesRead = fread( srcData.Base(), 1, srcFileSize, fp );
	fclose( fp );

	srcData.SeekPut( CUtlBuffer::SEEK_HEAD, nBytesRead );

	IVTFTexture *pSrcTexture = CreateVTFTexture();
	
	pSrcTexture->Unserialize( srcData );
	ImageFormat srcFormat = pSrcTexture->Format();
	pSrcTexture->ConvertImageFormat( IMAGE_FORMAT_DEFAULT, false );

	int totalSize = pSrcTexture->ComputeTotalSize();

	unsigned char *pImageData = pSrcTexture->ImageData();
	int i;
	for( i = 0; i < totalSize; i += 4 )
	{
#if 0
		// ATI BEFORE E32003
		int r, g, b;
		r = ( int )pImageData[i+0];
		g = ( int )pImageData[i+1];
		b = ( int )pImageData[i+2];

		int grey = r + g + b;
		grey = grey / 3;
		pImageData[i+0] = 0;
		pImageData[i+1] = grey;
		pImageData[i+2] = 0;
#endif
#if 0
		// NVIDIA - first e3 build
		pImageData[i+1] = 0;
		pImageData[i+2] = 0;
#endif
#if 0
		// ATI e3 build on . .get the green component and stomp all channels with it.
		pImageData[i+0] = pImageData[i+1];
		pImageData[i+2] = pImageData[i+1];
#endif
#if 0
		// Intel . .get the blue component and stomp all channels with it.
		pImageData[i+0] = pImageData[i+2];
		pImageData[i+1] = pImageData[i+2];
#endif
		// leave alpha alone
		int tmpR, tmpG, tmpB;
		tmpR = pImageData[i+rOffset];
		tmpG = pImageData[i+gOffset];
		tmpB = pImageData[i+bOffset];
		pImageData[i+0] = tmpR;
		pImageData[i+1] = tmpG;
		pImageData[i+2] = tmpB;
	}

	pSrcTexture->ConvertImageFormat( srcFormat, false );
	CUtlBuffer dstData;

	pSrcTexture->Serialize( dstData );

	fp = fopen( pSrcFilename, "wb" );
	fwrite( dstData.Base(), 1, dstData.TellPut(), fp );
	fclose( fp );

	return 0;
}