Image.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  3. Permission is hereby granted, free of charge, to any person
  4. obtaining a copy of this software and associated documentation
  5. files (the "Software"), to deal in the Software without
  6. restriction, including without limitation the rights to use,
  7. copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the
  9. Software is furnished to do so, subject to the following
  10. conditions:
  11. The above copyright notice and this permission notice shall be
  12. included in all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  15. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  17. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  18. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  20. OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. #include "Image.h"
  23. #include "Log.h"
  24. #include "Types.h"
  25. #include "Stream.h"
  26. namespace crown
  27. {
  28. Image::Image() :
  29. mPixelFormat(PF_UNKNOWN),
  30. mWidth(0),
  31. mHeight(0),
  32. mBuffer(NULL)
  33. {
  34. }
  35. Image::Image(PixelFormat pixelFormat, uint32_t width, uint32_t height, uint8_t* data) :
  36. mPixelFormat(pixelFormat),
  37. mWidth(width),
  38. mHeight(height),
  39. mBuffer(data)
  40. {
  41. if (data == NULL)
  42. {
  43. CreateBuffer();
  44. SetUniformColorImage(Color4::WHITE);
  45. }
  46. }
  47. Image::~Image()
  48. {
  49. DestroyImage();
  50. }
  51. void Image::CreateImage(PixelFormat pixelFormat, uint32_t width, uint32_t height, uint8_t* data)
  52. {
  53. mPixelFormat = pixelFormat;
  54. mWidth = width;
  55. mHeight = height;
  56. mBuffer = data;
  57. }
  58. void Image::DestroyImage()
  59. {
  60. if (mBuffer)
  61. {
  62. delete[] mBuffer;
  63. }
  64. mPixelFormat = PF_UNKNOWN;
  65. mWidth = 0;
  66. mHeight = 0;
  67. mBuffer = NULL;
  68. }
  69. void Image::SetUniformColorImage(Color4 color)
  70. {
  71. AssertRGB8();
  72. int32_t bpp = GetBytesPerPixel();
  73. uint8_t red = (uint8_t)(color.r * 255.0f);
  74. uint8_t green = (uint8_t)(color.g * 255.0f);
  75. uint8_t blue = (uint8_t)(color.b * 255.0f);
  76. for(uint32_t i = 0; i < mHeight; i++)
  77. {
  78. int32_t rowOffset = i * mWidth * bpp;
  79. for(uint32_t j = 0; j < mWidth; j++)
  80. {
  81. int32_t offset = rowOffset + bpp * j;
  82. mBuffer[offset ] = red;
  83. mBuffer[offset + 1] = green;
  84. mBuffer[offset + 2] = blue;
  85. }
  86. }
  87. }
  88. uint32_t Image::GetWidth() const
  89. {
  90. return mWidth;
  91. }
  92. uint32_t Image::GetHeight() const
  93. {
  94. return mHeight;
  95. }
  96. PixelFormat Image::GetFormat() const
  97. {
  98. return mPixelFormat;
  99. }
  100. uint32_t Image::GetBitsPerPixel() const
  101. {
  102. return Pixel::GetBitsPerPixel(mPixelFormat);
  103. }
  104. uint32_t Image::GetBytesPerPixel() const
  105. {
  106. return Pixel::GetBytesPerPixel(mPixelFormat);
  107. }
  108. uint8_t* Image::GetBuffer()
  109. {
  110. return mBuffer;
  111. }
  112. const uint8_t* Image::GetBuffer() const
  113. {
  114. return mBuffer;
  115. }
  116. void Image::ApplyColorKeying(const Color4& color)
  117. {
  118. assert(mPixelFormat == PF_RGBA_8);
  119. for (uint64_t i = 0; i < mWidth * mHeight * 4; i += 4)
  120. {
  121. if (Color4(mBuffer[i], mBuffer[i+1], mBuffer[i+2]) == color)
  122. {
  123. mBuffer[i] = 0;
  124. mBuffer[i+1] = 0;
  125. mBuffer[i+2] = 0;
  126. mBuffer[i+3] = 0;
  127. }
  128. }
  129. }
  130. void Image::ApplyGreyscaleToAlpha(Image* greyscaleImage)
  131. {
  132. if (mPixelFormat != PF_RGBA_8)
  133. {
  134. Log::e("Image::ApplyGreyscaleToAlpha: Can apply alpha only on RGBA8 pixel formats.");
  135. return;
  136. }
  137. if (greyscaleImage == NULL)
  138. {
  139. Log::e("Image::ApplyGreyscaleToAlpha: greyscaleImage is NULL.");
  140. return;
  141. }
  142. if (greyscaleImage->mPixelFormat != PF_RGBA_8)
  143. {
  144. Log::e("Image::ApplyGreyscaleToAlpha: greyscaleImage must have pixel format RGBA8.");
  145. return;
  146. }
  147. if (mWidth != greyscaleImage->mWidth || mHeight != greyscaleImage->mHeight)
  148. {
  149. Log::e("Image::ApplyGreyscaleToAlpha: greyscaleImage must have the same dimensions of the image.");
  150. return;
  151. }
  152. for (uint64_t i = 0; i < mWidth * mHeight * 4; i += 4)
  153. {
  154. mBuffer[i+3] = greyscaleImage->mBuffer[i];
  155. }
  156. }
  157. void Image::AlphaToGreyscale()
  158. {
  159. for (uint64_t i = 0; i < mWidth * mHeight * 4; i += 4)
  160. {
  161. mBuffer[i] = mBuffer[i+3];
  162. mBuffer[i+1] = mBuffer[i+3];
  163. mBuffer[i+2] = mBuffer[i+3];
  164. mBuffer[i+3] = 255;
  165. }
  166. }
  167. void Image::CreateBuffer()
  168. {
  169. if (mBuffer == NULL)
  170. {
  171. mBuffer = new uint8_t[mWidth * mHeight * GetBytesPerPixel()];
  172. }
  173. }
  174. void Image::AssertRGB8()
  175. {
  176. assert(mPixelFormat == PF_RGB_8);
  177. }
  178. void Image::SetPixel(uint32_t x, uint32_t y, Color4 color)
  179. {
  180. /*AssertRGB8();
  181. if (x >= mWidth || y >= mHeight)
  182. {
  183. throw ArgumentException("Coordinates outside the Image");
  184. }*/
  185. int32_t bpp = 3;//GetBytesPerPixel();
  186. int32_t offset = (y * mWidth + x) * GetBytesPerPixel();
  187. mBuffer[offset ] = (uint8_t)(color.r * 255);
  188. mBuffer[offset + 1] = (uint8_t)(color.g * 255);
  189. mBuffer[offset + 2] = (uint8_t)(color.b * 255);
  190. }
  191. void Image::CopyTo(Image& dest) const
  192. {
  193. dest.mWidth = mWidth;
  194. dest.mHeight = mHeight;
  195. dest.mPixelFormat = mPixelFormat;
  196. dest.CreateBuffer();
  197. for (uint64_t i = 0; i < mWidth * mHeight * GetBytesPerPixel(); i++)
  198. {
  199. dest.mBuffer[i] = mBuffer[i];
  200. }
  201. }
  202. void Image::ConvertToRGBA8()
  203. {
  204. assert(mPixelFormat != PF_UNKNOWN);
  205. uint8_t* newBuf = new uint8_t[mWidth * mHeight * 4];
  206. uint32_t j = 0;
  207. for (uint32_t i = 0; i < mWidth * mHeight * 2; i += 2)
  208. {
  209. newBuf[j + 0] = mBuffer[i + 0];
  210. newBuf[j + 1] = mBuffer[i + 0];
  211. newBuf[j + 2] = mBuffer[i + 0];
  212. newBuf[j + 3] = mBuffer[i + 1];
  213. j += 4;
  214. }
  215. delete[] mBuffer;
  216. mBuffer = newBuf;
  217. mPixelFormat = PF_RGBA_8;
  218. }
  219. } // namespace crown