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.

174 lines
4.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //===========================================================================//
  8. #include <stdlib.h>
  9. #include <stdio.h>
  10. #include <direct.h>
  11. #include "bitmap/tgaloader.h"
  12. #include "mathlib/mathlib.h"
  13. #include "tier2/tier2.h"
  14. #include "tier1/utlbuffer.h"
  15. #include "tier1/strtools.h"
  16. #include "filesystem.h"
  17. void Usage( void )
  18. {
  19. printf( "Fuzzy compare of two tga files.\n\n" );
  20. printf( "Usage: tgamse src1.tga src2.tga \n" );
  21. printf( "Return: MSE\n\n" );
  22. printf( "Usage: tgamse src1.tga src2.tga threshhold \n" );
  23. printf( "Return: PASS\\FAIL\n" );
  24. exit( -1 );
  25. }
  26. int main( int argc, char **argv )
  27. {
  28. if ( argc != 3 && argc != 4 )
  29. {
  30. Usage();
  31. }
  32. MathLib_Init( 2.2f, 2.2f, 0.0f, 2.0f );
  33. InitDefaultFileSystem();
  34. char pCurrentDirectory[MAX_PATH];
  35. if ( _getcwd( pCurrentDirectory, sizeof(pCurrentDirectory) ) == NULL )
  36. {
  37. fprintf( stderr, "Unable to get the current directory\n" );
  38. return -1;
  39. }
  40. Q_FixSlashes( pCurrentDirectory );
  41. Q_StripTrailingSlash( pCurrentDirectory );
  42. char pBuf[3][MAX_PATH];
  43. const char *pFileName[2];
  44. for ( int i = 0; i < 2; ++i )
  45. {
  46. if ( !Q_IsAbsolutePath( argv[i+1] ) )
  47. {
  48. Q_snprintf( pBuf[i], sizeof(pBuf[i]), "%s\\%s", pCurrentDirectory, argv[i+1] );
  49. pFileName[i] = pBuf[i];
  50. }
  51. else
  52. {
  53. pFileName[i] = argv[i+1];
  54. }
  55. }
  56. float fMSE = 10000.0f;
  57. if ( argc == 4 )
  58. {
  59. fMSE = atof(argv[3]);
  60. }
  61. int width1, height1;
  62. ImageFormat imageFormat1;
  63. float gamma1;
  64. CUtlBuffer buf1;
  65. if ( !g_pFullFileSystem->ReadFile( pFileName[0], NULL, buf1 ) )
  66. {
  67. fprintf( stderr, "%s not found\n", pFileName[0] );
  68. return -1;
  69. }
  70. if( !TGALoader::GetInfo( buf1, &width1, &height1, &imageFormat1, &gamma1 ) )
  71. {
  72. printf( "error loading %s\n", pFileName[0] );
  73. exit( -1 );
  74. }
  75. int width2, height2;
  76. ImageFormat imageFormat2;
  77. float gamma2;
  78. CUtlBuffer buf2;
  79. if ( !g_pFullFileSystem->ReadFile( pFileName[1], NULL, buf2 ) )
  80. {
  81. fprintf( stderr, "%s not found\n", pFileName[1] );
  82. return -1;
  83. }
  84. if( !TGALoader::GetInfo( buf2, &width2, &height2, &imageFormat2, &gamma2 ) )
  85. {
  86. printf( "error loading %s\n", pFileName[1] );
  87. exit( -1 );
  88. }
  89. if( width1 != width2 || height1 != height2 )
  90. {
  91. printf( "image dimensions different (%dx%d!=%dx%d): can't do diff for %s\n",
  92. width1, height1, width2, height2, pFileName[0] );
  93. exit( -1 );
  94. }
  95. #if 0
  96. // have to allow for different formats for now due to *.txt file screwup.
  97. if( imageFormat1 != imageFormat2 )
  98. {
  99. printf( "image format different (%s!=%s). . can't do diff for %s\n",
  100. ImageLoader::GetName( imageFormat1 ), ImageLoader::GetName( imageFormat2 ), pDstFileName );
  101. exit( -1 );
  102. }
  103. #endif
  104. if( gamma1 != gamma2 )
  105. {
  106. printf( "image gamma different (%f!=%f). . can't do diff for %s\n", gamma1, gamma2, pFileName[0] );
  107. exit( -1 );
  108. }
  109. unsigned char *pImage1Tmp = new unsigned char[ImageLoader::GetMemRequired( width1, height1, 1, imageFormat1, false )];
  110. unsigned char *pImage2Tmp = new unsigned char[ImageLoader::GetMemRequired( width2, height2, 1, imageFormat2, false )];
  111. buf1.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  112. if( !TGALoader::Load( pImage1Tmp, buf1, width1, height1, imageFormat1, 2.2f, false ) )
  113. {
  114. printf( "error loading %s\n", pFileName[0] );
  115. exit( -1 );
  116. }
  117. buf2.SeekGet( CUtlBuffer::SEEK_HEAD, 0 );
  118. if( !TGALoader::Load( pImage2Tmp, buf2, width2, height2, imageFormat2, 2.2f, false ) )
  119. {
  120. printf( "error loading %s\n", pFileName[1] );
  121. exit( -1 );
  122. }
  123. unsigned char *pImage1 = new unsigned char[ImageLoader::GetMemRequired( width1, height1, 1, IMAGE_FORMAT_ABGR8888, false )];
  124. unsigned char *pImage2 = new unsigned char[ImageLoader::GetMemRequired( width2, height2, 1, IMAGE_FORMAT_ABGR8888, false )];
  125. ImageLoader::ConvertImageFormat( pImage1Tmp, imageFormat1, pImage1, IMAGE_FORMAT_ABGR8888, width1, height1, 0, 0 );
  126. ImageLoader::ConvertImageFormat( pImage2Tmp, imageFormat2, pImage2, IMAGE_FORMAT_ABGR8888, width2, height2, 0, 0 );
  127. int sizeInBytes = ImageLoader::SizeInBytes( IMAGE_FORMAT_ABGR8888 );
  128. float mse = 0.0f;
  129. for( int i = 0; i < width1 * height1 * sizeInBytes; i++ )
  130. {
  131. float d;
  132. d = ( float )( pImage2[i] - pImage1[i] );
  133. mse += ( d * d );
  134. }
  135. mse /= ( float )( width1 * height1 * sizeInBytes );
  136. if ( argc == 3 )
  137. {
  138. printf( "MSE=%f\n", mse );
  139. }
  140. else
  141. {
  142. if ( mse > fMSE )
  143. {
  144. printf( "FAIL" );
  145. }
  146. else
  147. {
  148. printf( "PASS" );
  149. }
  150. }
  151. return 0;
  152. }