Team Fortress 2 Source Code as on 22/4/2020
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.

184 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include "UtlBuffer.h"
  5. #include "filesystem.h"
  6. #include "filesystem_tools.h"
  7. #include "tier1/strtools.h"
  8. #include "tier0/icommandline.h"
  9. #include "cmdlib.h"
  10. void Usage( void )
  11. {
  12. printf( "Usage: splitskybox blah.pfm\n" );
  13. exit( -1 );
  14. }
  15. static int ReadIntFromUtlBuffer( CUtlBuffer &buf )
  16. {
  17. int val = 0;
  18. int c;
  19. while( 1 )
  20. {
  21. c = buf.GetChar();
  22. if( c >= '0' && c <= '9' )
  23. {
  24. val = val * 10 + ( c - '0' );
  25. }
  26. else
  27. {
  28. return val;
  29. }
  30. }
  31. }
  32. //-----------------------------------------------------------------------------
  33. // Loads a file into a UTLBuffer
  34. //-----------------------------------------------------------------------------
  35. static bool LoadFile( const char *pFileName, CUtlBuffer &buf, bool bFailOnError )
  36. {
  37. FILE *fp = fopen( pFileName, "rb" );
  38. if (!fp)
  39. {
  40. if (!bFailOnError)
  41. return false;
  42. Error( "Can't open: \"%s\"\n", pFileName );
  43. }
  44. fseek( fp, 0, SEEK_END );
  45. int nFileLength = ftell( fp );
  46. fseek( fp, 0, SEEK_SET );
  47. buf.EnsureCapacity( nFileLength );
  48. fread( buf.Base(), 1, nFileLength, fp );
  49. fclose( fp );
  50. buf.SeekPut( CUtlBuffer::SEEK_HEAD, nFileLength );
  51. return true;
  52. }
  53. static bool PFMRead( CUtlBuffer &fileBuffer, int &nWidth, int &nHeight, float **ppImage )
  54. {
  55. fileBuffer.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  56. if( fileBuffer.GetChar() != 'P' )
  57. {
  58. Assert( 0 );
  59. return false;
  60. }
  61. if( fileBuffer.GetChar() != 'F' )
  62. {
  63. Assert( 0 );
  64. return false;
  65. }
  66. if( fileBuffer.GetChar() != 0xa )
  67. {
  68. Assert( 0 );
  69. return false;
  70. }
  71. nWidth = ReadIntFromUtlBuffer( fileBuffer );
  72. nHeight = ReadIntFromUtlBuffer( fileBuffer );
  73. // eat crap until the next newline
  74. while( fileBuffer.GetChar() != 0xa )
  75. {
  76. }
  77. *ppImage = new float[nWidth * nHeight * 3];
  78. int y;
  79. for( y = nHeight-1; y >= 0; y-- )
  80. {
  81. fileBuffer.Get( *ppImage + nWidth * y * 3, nWidth * 3 * sizeof( float ) );
  82. }
  83. return true;
  84. }
  85. static void PFMWrite( float *pFloatImage, const char *pFilename, int width, int height )
  86. {
  87. printf( "filename: %s\n", pFilename );
  88. FileHandle_t fp;
  89. fp = g_pFullFileSystem->Open( pFilename, "wb" );
  90. if( fp == FILESYSTEM_INVALID_HANDLE )
  91. {
  92. fprintf( stderr, "error opening \"%s\"\n", pFilename );
  93. exit( -1 );
  94. }
  95. g_pFullFileSystem->FPrintf( fp, "PF\n%d %d\n-1.000000\n", width, height );
  96. int i;
  97. for( i = height-1; i >= 0; i-- )
  98. // for( i = 0; i < height; i++ )
  99. {
  100. float *pRow = &pFloatImage[3 * width * i];
  101. g_pFullFileSystem->Write( pRow, width * sizeof( float ) * 3, fp );
  102. }
  103. g_pFullFileSystem->Close( fp );
  104. }
  105. void WriteSubRect( int nSrcWidth, int nSrcHeight, const float *pSrcImage,
  106. int nSrcOffsetX, int nSrcOffsetY, int nDstWidth, int nDstHeight,
  107. const char *pSrcFileName, const char *pDstFileNameExtension )
  108. {
  109. float *pDstImage = new float[nDstWidth * nDstHeight * 3];
  110. int x, y;
  111. for( y = 0; y < nDstHeight; y++ )
  112. {
  113. for( x = 0; x < nDstWidth; x++ )
  114. {
  115. pDstImage[(x+(nDstWidth*y))*3+0] = pSrcImage[((nSrcOffsetX+x)+(nSrcOffsetY+y)*nSrcWidth)*3+0];
  116. pDstImage[(x+(nDstWidth*y))*3+1] = pSrcImage[((nSrcOffsetX+x)+(nSrcOffsetY+y)*nSrcWidth)*3+1];
  117. pDstImage[(x+(nDstWidth*y))*3+2] = pSrcImage[((nSrcOffsetX+x)+(nSrcOffsetY+y)*nSrcWidth)*3+2];
  118. }
  119. }
  120. char dstFileName[MAX_PATH];
  121. Q_strcpy( dstFileName, pSrcFileName );
  122. Q_StripExtension( pSrcFileName, dstFileName, MAX_PATH );
  123. Q_strcat( dstFileName, pDstFileNameExtension, sizeof(dstFileName) );
  124. Q_strcat( dstFileName, ".pfm", sizeof(dstFileName) );
  125. PFMWrite( pDstImage, dstFileName, nDstWidth, nDstHeight );
  126. delete [] pDstImage;
  127. }
  128. int main( int argc, char **argv )
  129. {
  130. CommandLine()->CreateCmdLine( argc, argv );
  131. if( argc != 2 )
  132. {
  133. Usage();
  134. }
  135. CmdLib_InitFileSystem( argv[ argc-1 ] );
  136. CUtlBuffer srcFileData;
  137. // Expand the path so that it can run correctly from a shortcut
  138. char source[1024];
  139. strcpy (source, ExpandPath(argv[1]));
  140. LoadFile( argv[1], srcFileData, true );
  141. float *pSrcImage = NULL;
  142. int nSrcWidth, nSrcHeight;
  143. if( !PFMRead( srcFileData, nSrcWidth, nSrcHeight, &pSrcImage ) )
  144. {
  145. printf( "Couldn't read %s\n", argv[1] );
  146. }
  147. int nDstWidth = nSrcWidth / 4;
  148. int nDstHeight = nSrcHeight / 3;
  149. Assert( nDstWidth == nDstHeight );
  150. WriteSubRect( nSrcWidth, nSrcHeight, pSrcImage, nDstWidth * 0, nDstHeight * 1, nDstWidth, nDstHeight, argv[1], "ft" );
  151. WriteSubRect( nSrcWidth, nSrcHeight, pSrcImage, nDstWidth * 1, nDstHeight * 1, nDstWidth, nDstHeight, argv[1], "lf" );
  152. WriteSubRect( nSrcWidth, nSrcHeight, pSrcImage, nDstWidth * 2, nDstHeight * 1, nDstWidth, nDstHeight, argv[1], "bk" );
  153. WriteSubRect( nSrcWidth, nSrcHeight, pSrcImage, nDstWidth * 3, nDstHeight * 1, nDstWidth, nDstHeight, argv[1], "rt" );
  154. WriteSubRect( nSrcWidth, nSrcHeight, pSrcImage, nDstWidth * 3, nDstHeight * 0, nDstWidth, nDstHeight, argv[1], "up" );
  155. WriteSubRect( nSrcWidth, nSrcHeight, pSrcImage, nDstWidth * 3, nDstHeight * 2, nDstWidth, nDstHeight, argv[1], "dn" );
  156. CmdLib_Cleanup();
  157. CmdLib_Exit( 1 );
  158. return 0;
  159. }