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.

91 lines
2.5 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include <tier0/platform.h>
  7. #include "bitmap/float_bm.h"
  8. #include "mathlib/mathlib.h"
  9. #include <tier2/tier2.h>
  10. #define N_TEXTURES_OUTPUT 3
  11. #define TEXTURE_TO_TEXTURE_SCALE 8.0
  12. #define BASE_SCALE 0.25
  13. static float Truncate8( float f )
  14. {
  15. uint8 px= (uint8) min(255.0, (f*255.0));
  16. return px*(1.0/255.0);
  17. }
  18. static float ErrorWeights[]={ 0.3, 0.59, 0.11 };
  19. void main(int argc,char **argv)
  20. {
  21. InitCommandLineProgram( argc, argv );
  22. if ( argc==3)
  23. {
  24. FloatBitMap_t src_texture;
  25. src_texture.LoadFromPFM( argv[1] );
  26. float sfactor=atof(argv[2]);
  27. // we will split the float texture into 2 textures.
  28. FloatBitMap_t output_textures[N_TEXTURES_OUTPUT];
  29. for(int o=0; o<N_TEXTURES_OUTPUT; o++)
  30. {
  31. output_textures[o].AllocateRGB( src_texture.Width, src_texture.Height );
  32. }
  33. for(int y=0; y < src_texture.Height; y++)
  34. for(int x=0; x< src_texture.Width; x++)
  35. {
  36. for(int c=0;c<3;c++)
  37. src_texture.Pixel(x,y,c) *= sfactor;
  38. float curscale=BASE_SCALE;
  39. float px_error[N_TEXTURES_OUTPUT];
  40. float sum_error=0;
  41. for(int o=0; o<N_TEXTURES_OUTPUT; o++)
  42. {
  43. px_error[o]=0.;
  44. for(int c=0;c<3;c++)
  45. {
  46. float opixel=Truncate8( src_texture.Pixel( x, y, c )/curscale );
  47. output_textures[o].Pixel( x, y, c )= opixel;
  48. float expanded_pixel = curscale * opixel;
  49. float err= fabs( src_texture.Pixel( x, y, c )- expanded_pixel );
  50. if (src_texture.Pixel(x,y,c) > curscale)
  51. err *=2;
  52. px_error[o] += ErrorWeights[o] * err;
  53. }
  54. sum_error += px_error[o];
  55. curscale *= TEXTURE_TO_TEXTURE_SCALE;
  56. }
  57. // now, set the weight for each map based upon closeness of fit between the scales
  58. for(int o=0; o<N_TEXTURES_OUTPUT; o++)
  59. {
  60. if (sum_error == 0) // possible, I guess, for black
  61. {
  62. if ( o == 0)
  63. src_texture.Pixel(x, y, 3) = 1.0;
  64. else
  65. src_texture.Pixel(x, y, 3) = 0.0;
  66. }
  67. else
  68. output_textures[o].Pixel(x, y, 3)=1.0-(px_error[o]/sum_error);
  69. }
  70. }
  71. // now, output results
  72. for(int o=0;o<N_TEXTURES_OUTPUT;o++)
  73. {
  74. char fnamebuf[1024];
  75. strcpy(fnamebuf, argv[1]);
  76. char *dot=strchr(fnamebuf,'.');
  77. if (! dot)
  78. dot=fnamebuf+strlen(fnamebuf);
  79. sprintf(dot,"_%d.tga",o);
  80. output_textures[o].WriteTGAFile(fnamebuf);
  81. }
  82. }
  83. else
  84. printf("format is 'pfm2tgas xxx.pfm scalefactor'\n");
  85. }