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.

108 lines
2.6 KiB

  1. //========= Copyright 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/float_bm.h"
  12. #include "vstdlib/vstdlib.h"
  13. #include "vstdlib/random.h"
  14. #include "tier1/strtools.h"
  15. void FloatBitMap_t::InitializeWithRandomPixelsFromAnotherFloatBM(FloatBitMap_t const &other)
  16. {
  17. for(int y=0;y<Height;y++)
  18. for(int x=0;x<Width;x++)
  19. {
  20. float x1=RandomInt(0,other.Width-1);
  21. float y1=RandomInt(0,other.Height-1);
  22. for(int c=0;c<4;c++)
  23. {
  24. Pixel(x,y,c)=other.Pixel(x1,y1,c);
  25. }
  26. }
  27. }
  28. FloatBitMap_t *FloatBitMap_t::QuarterSizeWithGaussian(void) const
  29. {
  30. // generate a new bitmap half on each axis, using a separable gaussian.
  31. static float kernel[]={.05,.25,.4,.25,.05};
  32. FloatBitMap_t *newbm=new FloatBitMap_t(Width/2,Height/2);
  33. for(int y=0;y<Height/2;y++)
  34. for(int x=0;x<Width/2;x++)
  35. {
  36. for(int c=0;c<4;c++)
  37. {
  38. float sum=0;
  39. float sumweights=0; // for versatility in handling the
  40. // offscreen case
  41. for(int xofs=-2;xofs<=2;xofs++)
  42. {
  43. int orig_x=max(0,min(Width-1,x*2+xofs));
  44. for(int yofs=-2;yofs<=2;yofs++)
  45. {
  46. int orig_y=max(0,min(Height-1,y*2+yofs));
  47. float coeff=kernel[xofs+2]*kernel[yofs+2];
  48. sum+=Pixel(orig_x,orig_y,c)*coeff;
  49. sumweights+=coeff;
  50. }
  51. }
  52. newbm->Pixel(x,y,c)=sum/sumweights;
  53. }
  54. }
  55. return newbm;
  56. }
  57. FloatImagePyramid_t::FloatImagePyramid_t(FloatBitMap_t const &src, ImagePyramidMode_t mode)
  58. {
  59. memset(m_pLevels,0,sizeof(m_pLevels));
  60. m_nLevels=1;
  61. m_pLevels[0]=new FloatBitMap_t(&src);
  62. ReconstructLowerResolutionLevels(0);
  63. }
  64. void FloatImagePyramid_t::ReconstructLowerResolutionLevels(int start_level)
  65. {
  66. while( (m_pLevels[start_level]->Width>1) && (m_pLevels[start_level]->Height>1) )
  67. {
  68. if (m_pLevels[start_level+1])
  69. delete m_pLevels[start_level+1];
  70. m_pLevels[start_level+1]=m_pLevels[start_level]->QuarterSizeWithGaussian();
  71. start_level++;
  72. }
  73. m_nLevels=start_level+1;
  74. }
  75. float & FloatImagePyramid_t::Pixel(int x, int y, int component, int level) const
  76. {
  77. assert(level<m_nLevels);
  78. x<<=level;
  79. y<<=level;
  80. return m_pLevels[level]->Pixel(x,y,component);
  81. }
  82. void FloatImagePyramid_t::WriteTGAs(char const *basename) const
  83. {
  84. for(int l=0;l<m_nLevels;l++)
  85. {
  86. char bname_out[1024];
  87. Q_snprintf(bname_out,sizeof(bname_out),"%s_%02d.tga",basename,l);
  88. m_pLevels[l]->WriteTGAFile(bname_out);
  89. }
  90. }
  91. FloatImagePyramid_t::~FloatImagePyramid_t(void)
  92. {
  93. for(int l=0;l<m_nLevels;l++)
  94. if (m_pLevels[l])
  95. delete m_pLevels[l];
  96. }