ImageData.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "ImageData.h"
  2. #include "ImageUtils.h"
  3. USING_NS_BF;
  4. ImageData::ImageData()
  5. {
  6. mSrcData = NULL;
  7. mOwnsSrcData = false;
  8. mKeepSrcDataValid = false;
  9. mBits = NULL;
  10. mHWBitsType = 0;
  11. mHWBitsLength = 0;
  12. mHWBits = NULL;
  13. mX = 0;
  14. mY = 0;
  15. mWidth = 0;
  16. mHeight = 0;
  17. mWantsAlphaPremultiplied = true;
  18. mAlphaPremultiplied = false;
  19. mIsAdditive = false;
  20. mSrcDataLen = 0;
  21. }
  22. ImageData::~ImageData()
  23. {
  24. delete [] mBits;
  25. delete [] mSrcData;
  26. }
  27. void ImageData::SwapRAndB()
  28. {
  29. int aSize = mWidth*mHeight;
  30. uint32* aPtr = mBits;
  31. for (int i = 0; i < aSize; i++)
  32. {
  33. uint32 aColor = *aPtr;
  34. int a = (aColor & 0xFF000000) >> 24;
  35. int r = (aColor & 0x00FF0000) >> 16;
  36. int g = (aColor & 0x0000FF00) >> 8;
  37. int b = (aColor & 0x000000FF);
  38. *(aPtr++) = (a << 24) | (b << 16) | (g << 8) | r;
  39. }
  40. }
  41. void ImageData::CreateNew(int x, int y, int width, int height, bool clear)
  42. {
  43. CreateNew(width, height, clear);
  44. mX = x;
  45. mY = y;
  46. }
  47. void ImageData::CreateNew(int width, int height, bool clear)
  48. {
  49. mWidth = width;
  50. mHeight = height;
  51. mBits = new uint32[mWidth*mHeight];
  52. if (clear)
  53. memset(mBits, 0, mWidth*mHeight*sizeof(uint32));
  54. }
  55. void ImageData::Fill(uint32 color)
  56. {
  57. int size = mWidth*mHeight;
  58. for (int i = 0; i < size; i++)
  59. mBits[i] = color;
  60. }
  61. ImageData* ImageData::Duplicate()
  62. {
  63. ImageData* copy = new ImageData();
  64. copy->CreateNew(mWidth, mHeight);
  65. copy->mX = mX;
  66. copy->mY = mY;
  67. copy->mAlphaPremultiplied = mAlphaPremultiplied;
  68. copy->mIsAdditive = mIsAdditive;
  69. memcpy(copy->mBits, mBits, mWidth*mHeight*sizeof(uint32));
  70. return copy;
  71. }
  72. bool ImageData::LoadFromFile(const StringImpl& path)
  73. {
  74. int size = 0;
  75. uint8* aData = LoadBinaryData(path, &size);
  76. if (aData == NULL)
  77. return false;
  78. SetSrcData(aData, size);
  79. bool result = ReadData();
  80. if (mKeepSrcDataValid)
  81. {
  82. mOwnsSrcData = true;
  83. }
  84. else
  85. {
  86. delete [] mSrcData;
  87. mSrcData = NULL;
  88. }
  89. return result;
  90. }
  91. void ImageData::SetSrcData(uint8* data, int dataLen)
  92. {
  93. mSrcData = data;
  94. mSrcDataLen = dataLen;
  95. }
  96. void ImageData::PremultiplyAlpha()
  97. {
  98. if (mBits == NULL)
  99. return;
  100. if (!mAlphaPremultiplied)
  101. {
  102. mAlphaPremultiplied = true;
  103. int size = mWidth*mHeight;
  104. for (int i = 0; i < size; i++)
  105. {
  106. PackedColor* packedColor = (PackedColor*) (mBits + i);
  107. packedColor->r = (packedColor->r * packedColor->a) / 255;
  108. packedColor->g = (packedColor->g * packedColor->a) / 255;
  109. packedColor->b = (packedColor->b * packedColor->a) / 255;
  110. if (mIsAdditive)
  111. packedColor->a = 0;
  112. }
  113. }
  114. }