Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. void Sprite::DrawScaledAdditiveSubpixel( float x, float y, float w, float h, Surface *a_Target )
  2. {
  3. const unsigned int wI = (unsigned int)w >> 1;
  4. const unsigned int hI = (unsigned int)h >> 1;
  5.  
  6. const float mW = m_Width / w;
  7. const float mH = m_Height / h;
  8. Pixel *src = GetBuffer() + m_CurrentFrame * m_Width;
  9. // calculate screen space bounding box of the scaled image
  10. const int ix = ( x - 615 ) * ( x - 615 );
  11. const int iy = ( y - 345 ) * ( y - 345 );
  12. const int x1 = ( x - wI ), y1 = ( y - hI );
  13. const float y1f = ( y - hI );
  14. const int x2 = ( x + 1 + wI ), y2 = ( y + 1 + hI );
  15. const float x2f = ( x - wI );
  16. Pixel *buffer = a_Target->GetBuffer();
  17.  
  18. const int midX = 2 + ( x2 + x1 ) >> 1;
  19. const int midY = 2 + ( y2 + y1 ) >> 1;
  20. const int pitch = a_Target->GetPitch();
  21. //just make everything in the centre white
  22. if ( ix + ( iy << 1 ) < 5500 )
  23. {
  24.  
  25. for ( int j = y1; j < y2; j++ )
  26. {
  27. for ( int i = x1; i < x2; i++ )
  28. {
  29. buffer[i + j * pitch] = 0xFFFFFFFF;
  30. }
  31. }
  32. return;
  33. }
  34.  
  35. const int m_Width_min_1 = m_Width - 1;
  36. const int m_Height_min_1 = m_Height - 1;
  37. const float xw_div_2 = ( x - w / 2.0f );
  38. // loop over target box; do a filtered read from source for each pixel
  39. for ( int j = y1; j < y2; j++ )
  40. {
  41. const float sy = max( 0.0f, ( j - ( y - h / 2.0f ) ) * mH);
  42. const int isy = int( sy );
  43. const float y_in_pixel = sy - isy;
  44. const int y0 = min( m_Height_min_1, isy );
  45. const int y1 = min( m_Height_min_1, y0 + 1 );
  46. const int jpitch = j * pitch;
  47. const int dy = midY - j;
  48.  
  49. const float one_min_y_in_pixel = 1.0f - y_in_pixel;
  50.  
  51. const bool y_check = ( ( dy ^ ( dy >> 31 ) ) - ( dy >> 31 ) ) < 2;
  52.  
  53. for ( int i = x1; i < x2; i++ )
  54. {
  55. int index = i + jpitch;
  56. if ( buffer[index] == 0xFFFFFFFF )
  57. {
  58. continue;
  59. }
  60.  
  61. const int dx = midX - i;
  62. if ( ( ( dx ^ ( dx >> 31 ) ) - ( dx >> 31 ) ) < 2 && y_check)
  63. {
  64. buffer[index] = 0xFFFFFFFF;
  65. continue;
  66. }
  67.  
  68. // determine floating point (sub-pixel) source coordinates
  69. const float sx = ( i - xw_div_2) * mW;
  70. // use bilinear interpolation to read from source image
  71. const int isx = max(0, (int)sx);
  72. const float x_in_pixel = sx - isx;
  73. const int x0 = min( m_Width_min_1, isx );
  74. const int x1 = min( m_Width_min_1, isx + 1 );
  75.  
  76. const float one_min_x_in_pixel = 1.0f - x_in_pixel;
  77.  
  78. const float w0 = one_min_x_in_pixel * one_min_y_in_pixel;
  79. const float w1 = x_in_pixel * one_min_y_in_pixel;
  80. const float w2 = one_min_x_in_pixel * y_in_pixel;
  81. const float w3 = 1.0f - ( w0 + w1 + w2 );
  82.  
  83. Pixel color = 0;
  84.  
  85. const Pixel p0 = src[x0 + ( y0 << 9 )];
  86. if ( p0 > 0xFF000000 )
  87. color += ScaleColor( p0, ( w0 * 256.0f ) );
  88.  
  89. const Pixel p1 = src[x1 + ( y0 << 9 )];
  90. if ( p1 > 0xFF000000 )
  91. color += ScaleColor( p1, ( w1 * 256.0f ) );
  92.  
  93. const Pixel p2 = src[x0 + ( y1 << 9 )];
  94. if ( p2 > 0xFF000000 )
  95. color += ScaleColor( p2, ( w2 * 256.0f ) );
  96.  
  97. const Pixel p3 = src[x1 + ( y1 << 9 )];
  98. if ( p3 > 0xFF000000 )
  99. color += ScaleColor( p3, ( w3 * 256.0f ) );
  100.  
  101. if ( color )
  102. {
  103. buffer[index] = AddBlend( buffer[index], color );
  104. }
  105. }
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement