Counter Strike : Global Offensive Source Code
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.

118 lines
3.1 KiB

  1. //===== Copyright � 1996-2006, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include <tier0/platform.h>
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #include <stdlib.h>
  11. #include "bitmap/floatbitmap.h"
  12. #include "vstdlib/vstdlib.h"
  13. #include "vstdlib/random.h"
  14. #include "tier1/strtools.h"
  15. // NOTE: This has to be the last file included!
  16. #include "tier0/memdbgon.h"
  17. void FloatBitMap_t::InitializeWithRandomPixelsFromAnotherFloatBM( FloatBitMap_t const & other )
  18. {
  19. for( int z = 0; z < NumSlices(); z++ )
  20. {
  21. for( int y = 0;y < NumRows();y++ )
  22. {
  23. for( int x = 0;x < NumCols();x++ )
  24. {
  25. int x1 = RandomInt( 0, other.NumCols() - 1 );
  26. int y1 = RandomInt( 0, other.NumRows() - 1 );
  27. int z1 = RandomInt( 0, other.NumSlices() - 1 );
  28. for( int c = 0; c < 4; c++ )
  29. {
  30. Pixel( x, y, z, c ) = other.Pixel( x1, y1, z1, c );
  31. }
  32. }
  33. }
  34. }
  35. }
  36. void FloatBitMap_t::QuarterSizeWithGaussian( FloatBitMap_t *pBitmap )
  37. {
  38. // generate a new bitmap half on each axis, using a separable gaussian.
  39. static float kernel[]={.05, .25, .4, .25, .05};
  40. pBitmap->Init( NumCols() / 2, NumRows() / 2 );
  41. for( int y = 0;y < NumRows() / 2;y++ )
  42. for( int x = 0;x < NumCols() / 2;x++ )
  43. {
  44. for( int c = 0;c < 4;c++ )
  45. {
  46. float sum = 0;
  47. float sumweights = 0; // for versatility in handling the
  48. // offscreen case
  49. for( int xofs =- 2;xofs <= 2;xofs++ )
  50. {
  51. int orig_x = MAX( 0, MIN( NumCols() - 1, x * 2 + xofs ));
  52. for( int yofs =- 2;yofs <= 2;yofs++ )
  53. {
  54. int orig_y = MAX( 0, MIN( NumRows() - 1, y * 2 + yofs ));
  55. float coeff = kernel[xofs + 2]* kernel[yofs + 2];
  56. sum += Pixel( orig_x, orig_y, 0, c ) * coeff;
  57. sumweights += coeff;
  58. }
  59. }
  60. pBitmap->Pixel( x, y, 0, c ) = sum / sumweights;
  61. }
  62. }
  63. }
  64. FloatImagePyramid_t::FloatImagePyramid_t( FloatBitMap_t const & src, ImagePyramidMode_t mode )
  65. {
  66. memset( m_pLevels, 0, sizeof( m_pLevels ));
  67. m_nLevels = 1;
  68. m_pLevels[0]= new FloatBitMap_t( & src );
  69. ReconstructLowerResolutionLevels( 0 );
  70. }
  71. void FloatImagePyramid_t::ReconstructLowerResolutionLevels( int start_level )
  72. {
  73. while( ( m_pLevels[start_level]-> NumCols() > 1 ) && ( m_pLevels[start_level]-> NumRows() > 1 ) )
  74. {
  75. if ( m_pLevels[start_level + 1] )
  76. delete m_pLevels[start_level + 1];
  77. m_pLevels[start_level + 1] = new FloatBitMap_t;
  78. m_pLevels[start_level]->QuarterSizeWithGaussian( m_pLevels[start_level + 1] );
  79. start_level++;
  80. }
  81. m_nLevels = start_level + 1;
  82. }
  83. float & FloatImagePyramid_t::Pixel( int x, int y, int component, int level ) const
  84. {
  85. Assert( level < m_nLevels );
  86. x <<= level;
  87. y <<= level;
  88. return m_pLevels[level]-> Pixel( x, y, 0, component );
  89. }
  90. void FloatImagePyramid_t::WriteTGAs( char const * basename ) const
  91. {
  92. for( int l = 0;l < m_nLevels;l++ )
  93. {
  94. char bname_out[1024];
  95. Q_snprintf(bname_out,sizeof(bname_out),"%s_%02d.tga",basename,l);
  96. m_pLevels[l]-> WriteTGAFile( bname_out );
  97. }
  98. }
  99. FloatImagePyramid_t::~FloatImagePyramid_t( void )
  100. {
  101. for( int l = 0;l < m_nLevels;l++ )
  102. if ( m_pLevels[l] )
  103. delete m_pLevels[l];
  104. }