Surface.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "Surface.h"
  2. #ifdef WIN32
  3. #include <windows.h>
  4. #endif
  5. #include <memory.h>
  6. Surface::Surface():
  7. pixels( NULL ),
  8. width( 0 ),
  9. height( 0 )
  10. {
  11. }
  12. Surface::Surface( const Surface &copy ):
  13. pixels( NULL ),
  14. width( copy.width ),
  15. height( copy.height )
  16. {
  17. if( width && height )
  18. {
  19. pixels = copy.pixels;
  20. }
  21. }
  22. Surface&
  23. Surface::operator =( const Surface &copy )
  24. {
  25. if( copy.width && copy.height )
  26. {
  27. width = copy.width;
  28. height = copy.height;
  29. pixels = copy.pixels;
  30. }
  31. return *this;
  32. }
  33. Surface::~Surface()
  34. {
  35. }
  36. Surface*
  37. CreateSurface( const int width, const int height )
  38. {
  39. Surface* image = new Surface();
  40. image->width = width;
  41. image->height = height;
  42. image->pixels.resize( width * height * 4 );
  43. return image;
  44. }
  45. void
  46. CopyToSurface( Surface* dest, const int x_d, const int y_d, Surface* src )
  47. {
  48. if( dest == NULL || src == NULL )
  49. {
  50. return;
  51. }
  52. for( int y_from = y_d, y_to = y_d + src->height; y_from < y_to; ++y_from )
  53. {
  54. memcpy( dest->pixels.data() + y_from * dest->width * 4 + x_d * 4, src->pixels.data() + ( y_from - y_d ) * src->width * 4, src->width * 4 );
  55. }
  56. }
  57. Surface*
  58. CreateSubSurface( const int x, const int y, const int width, const int height, Surface* surface )
  59. {
  60. Surface* image = CreateSurface( width, height );
  61. if( surface != NULL )
  62. {
  63. for( int y_from = y, y_to = y + image->height; y_from < y_to; ++y_from )
  64. {
  65. memcpy( image->pixels.data() + ( y_from - y ) * image->width * 4, surface->pixels.data() + ( y_from * surface->width + x ) * 4, image->width * 4) ;
  66. }
  67. }
  68. return image;
  69. }
  70. Surface*
  71. CreateSurfaceFrom( const int width, const int height, unsigned char* pixels )
  72. {
  73. Surface* image = CreateSurface( width, height );
  74. if( pixels != NULL )
  75. {
  76. memcpy( image->pixels.data(), pixels, width * height * 4 );
  77. }
  78. return image;
  79. }