Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module cpixelformat_BGR555
  3.  * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
  4.  * @brief Implementation of CPixelFormat handling BGR555 color (real color)
  5.  * Windows Bitmaps.
  6.  * @date 18.04.2009
  7.  */
  8.  
  9. #include <boost/numeric/conversion/cast.hpp>
  10. #include <assert.h>
  11. #include "cpixelformat_bgr555.h"
  12. #include "cbitmap.h"
  13.  
  14. using namespace std;
  15.  
  16. void CPixelFormat_BGR555::getPixel(RGBPIXEL& pixel, uint32_t x, uint32_t y)
  17. {
  18. if (m_bitmap->getPixelData() == NULL)
  19. throw PixelFormatError("No pixelbuffer allocated.");
  20. assert(m_bitmap->getPixelDataSize() > 0);
  21. assert(m_bitmap->getRowSize() > 0);
  22.  
  23. /* if the y-coordinates are mirrored */
  24. if (m_bitmap->isMirrored())
  25. y = m_bitmap->getHeight() - y - 1;
  26. uint32_t offset = y * m_bitmap->getRowSize() + x * (4 * getBitCount() / 32);
  27.  
  28. /* boundary check */
  29. if (offset + getBitCount()/8 > m_bitmap->getPixelDataSize())
  30. throw PixelFormatError("Pixel position is out of range.");
  31.  
  32. /* get pixel */
  33. uint8_t *o = m_bitmap->getPixelData() + offset;
  34. pixel.blue = (*(uint16_t *)o & BGR555_BLUE_MASK) >> BGR555_BLUE_SHIFT;
  35. pixel.green = (*(uint16_t *)o & BGR555_GREEN_MASK) >> BGR555_GREEN_SHIFT;
  36. pixel.red = (*(uint16_t *)o & BGR555_RED_MASK) >> BGR555_RED_SHIFT;
  37. }
  38.  
  39. /*----------------------------------------------------------------------------*/
  40.  
  41. void CPixelFormat_BGR555::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t y)
  42. {
  43. if (m_bitmap->getPixelData() == NULL)
  44. throw PixelFormatError("No pixelbuffer allocated.");
  45. assert(m_bitmap->getPixelDataSize() > 0);
  46. assert(m_bitmap->getRowSize() > 0);
  47.  
  48. /* if the y-coordinates are mirrored */
  49. if (m_bitmap->isMirrored())
  50. y = m_bitmap->getHeight() - y - 1;
  51. uint32_t offset = y * m_bitmap->getRowSize() + x * (4 * getBitCount() / 32);
  52.  
  53. /* boundary check */
  54. if (offset + getBitCount()/8 > m_bitmap->getPixelDataSize())
  55. throw PixelFormatError("Pixel position is out of range.");
  56.  
  57. /* convert color values to correct types */
  58. uint8_t *o = m_bitmap->getPixelData() + offset;
  59. *(uint16_t *)o = (uint16_t)(((pixel.blue << BGR555_BLUE_SHIFT) & BGR555_BLUE_MASK) |
  60. ((pixel.green << BGR555_GREEN_SHIFT) & BGR555_GREEN_MASK) |
  61. ((pixel.red << BGR555_RED_SHIFT) & BGR555_RED_MASK));
  62. }
  63.  
  64. /* vim: set et sw=2 ts=2: */
  65.