Download | Plain Text | No Line Numbers


  1. /**
  2.  * @module CPixelFormat_Indexed8
  3.  * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
  4.  * @brief Implementation of CPixelFormat handling 24bit indexed bitmaps.
  5.  * @date 02.05.2009
  6.  */
  7.  
  8. #include <boost/numeric/conversion/cast.hpp>
  9. #include <assert.h>
  10. #include "cpixelformat_indexed8.h"
  11. #include "cbitmap.h"
  12.  
  13. using namespace std;
  14.  
  15. void CPixelFormat_Indexed8::getPixel(RGBPIXEL& pixel, uint32_t x, uint32_t y)
  16. {
  17. if (m_bitmap->getPixelData() == NULL)
  18. throw PixelFormatError("No pixelbuffer allocated.");
  19. if (m_bitmap->getColorTable().size() == 0)
  20. return;
  21. assert(m_bitmap->getPixelDataSize() > 0);
  22.  
  23. uint32_t offset = y * m_bitmap->getWidth() + x;
  24.  
  25. /* boundary check */
  26. if (offset * sizeof(uint32_t) + sizeof(uint32_t) > m_bitmap->getPixelDataSize())
  27. throw PixelFormatError("Pixel position is out of range.");
  28.  
  29. uint32_t color = *((uint32_t *)m_bitmap->getPixelData() + offset);
  30.  
  31. map<uint32_t, RGBPIXEL *>::iterator it;
  32. if ((it = m_bitmap->getColorTable().find(color)) == m_bitmap->getColorTable().end())
  33. throw PixelFormatError("Pixel has no reference in colortable.");
  34.  
  35. pixel.red = (*it).second->red;
  36. pixel.green = (*it).second->green;
  37. pixel.blue = (*it).second->blue;
  38. }
  39.  
  40. /*----------------------------------------------------------------------------*/
  41.  
  42. void CPixelFormat_Indexed8::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t y)
  43. {
  44. if (m_bitmap->getPixelData() == NULL)
  45. throw PixelFormatError("No pixelbuffer allocated.");
  46. /* if colortable is empty there are no pixels */
  47. if (m_bitmap->getColorTable().size() == 0)
  48. return;
  49. assert(m_bitmap->getPixelDataSize() > 0);
  50.  
  51. uint32_t offset = y * m_bitmap->getWidth() + x;
  52.  
  53. /* boundary check */
  54. if (offset * sizeof(uint32_t) + sizeof(uint32_t) > m_bitmap->getPixelDataSize())
  55. throw PixelFormatError("Pixel position is out of range.");
  56.  
  57. /* try to look up color in colortable */
  58. map<uint32_t, RGBPIXEL *>::iterator it;
  59. for(it = m_bitmap->getColorTable().begin(); it != m_bitmap->getColorTable().end(); it++)
  60. {
  61. if ((*it).second->red == pixel.red &&
  62. (*it).second->green == pixel.green &&
  63. (*it).second->blue == pixel.blue)
  64. break;
  65. }
  66.  
  67. uint32_t index = (*it).first;
  68. /* need to get a new entry for our color */
  69. if (it == m_bitmap->getColorTable().end())
  70. {
  71. index = (*it).first + 1;
  72. RGBPIXEL *pixelptr = new RGBPIXEL;
  73. pixelptr->red = pixel.red;
  74. pixelptr->green = pixel.green;
  75. pixelptr->blue = pixel.blue;
  76. m_bitmap->getColorTable()[ index ] = pixelptr;
  77. }
  78.  
  79. /* set color */
  80. *((uint32_t *)m_bitmap->getPixelData() + offset) = index;
  81. }
  82.  
  83. /* vim: set et sw=2 ts=2: */
  84.