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.

103 lines
2.6 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. #include "tier0/platform.h"
  3. #include "tier0/progressbar.h"
  4. #include "bitmap/float_bm.h"
  5. #include "mathlib/mathlib.h"
  6. #include "tier2/tier2.h"
  7. #include "tier0/memdbgon.h"
  8. #define SQ(x) ((x)*(x))
  9. int main( int argc, char ** argv )
  10. {
  11. InitCommandLineProgram( argc, argv );
  12. int nCurArg = 1;
  13. float flSpread = 1.0;
  14. bool bCodeChannel[4];
  15. bCodeChannel[0] = false;
  16. bCodeChannel[1] = false;
  17. bCodeChannel[2] = false;
  18. bCodeChannel[3] = true;
  19. while (( nCurArg < argc ) && ( argv[nCurArg][0] == '-' ) )
  20. {
  21. switch( argv[nCurArg][1] )
  22. {
  23. case 's': // spread
  24. {
  25. flSpread = atof( argv[++nCurArg] );
  26. }
  27. break;
  28. case '4': // encode all 4 channels
  29. {
  30. bCodeChannel[0] = true;
  31. bCodeChannel[1] = true;
  32. bCodeChannel[2] = true;
  33. }
  34. break;
  35. default:
  36. printf("unrecogized option %s\n", argv[nCurArg] );
  37. exit(1);
  38. }
  39. nCurArg++;
  40. }
  41. argc -= ( nCurArg - 1 );
  42. argv += ( nCurArg - 1 );
  43. if ( argc != 5 )
  44. {
  45. printf( "format is dist2alpha src_bm.tga dest_bm.tga dest_width dest_height\n" );
  46. exit( 1 );
  47. }
  48. FloatBitMap_t hires_image( argv[1] );
  49. int out_w = atoi( argv[3] );
  50. int out_h = atoi( argv[4] );
  51. FloatBitMap_t lores( out_w, out_h );
  52. float max_rad = 2.0 * flSpread * max( hires_image.Width * ( 1.0 / out_w ), hires_image.Height * ( 1.0 / out_h ));
  53. int irad = ceil( max_rad );
  54. for( int comp = 0;comp < 4;comp ++ )
  55. {
  56. for( int y = 0;y < out_h;y ++ )
  57. for( int x = 0;x < out_w;x ++ )
  58. {
  59. int orig_x = FLerp( 0, hires_image.Width - 1, 0, out_w - 1, x );
  60. int orig_y = FLerp( 0, hires_image.Height - 1, 0, out_h - 1, y );
  61. if ( bCodeChannel[ comp ] )
  62. {
  63. float closest_dist = 100000;
  64. int inside_test = ( hires_image.Pixel( orig_x, orig_y, comp ) >.9 );
  65. for( int oy =- irad;oy <= irad;oy ++ )
  66. for( int ox =- irad;ox <= irad;ox ++ )
  67. {
  68. int x1 = orig_x + ox;
  69. int y1 = orig_y + oy;
  70. if ( ( x1 >= 0 ) && ( x1 < hires_image.Width ) && ( y1 >= 0 ) && ( y1 < hires_image.Height ) &&
  71. ( inside_test != ( hires_image.Pixel( orig_x + ox, orig_y + oy, comp ) >.9 )) )
  72. {
  73. float d2 = sqrt( (float ) ( SQ( ox ) + SQ( oy )) );
  74. if ( d2 < closest_dist )
  75. closest_dist = d2;
  76. }
  77. }
  78. // done
  79. float dnum = min( 0.5f, FLerp( 0, .5, 0, max_rad, closest_dist ));
  80. if ( !inside_test )
  81. dnum =- dnum;
  82. lores.Pixel( x, y, comp ) = 0.5 + dnum;
  83. }
  84. else
  85. lores.Pixel( x, y, comp ) = hires_image.Pixel( orig_x, orig_y, comp );
  86. }
  87. }
  88. lores.WriteTGAFile( argv[2] );
  89. return 0;
  90. }