Download | Plain Text | No Line Numbers


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