summaryrefslogtreecommitdiffstats
path: root/ue2/imgsynth2/cpixelformat_indexed8.cpp
blob: b314f0a49d8c97f830e9d6356a78f9b7cc71cfc2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @module CPixelFormat_Indexed8
 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
 * @brief  Implementation of CPixelFormat handling 24bit indexed bitmaps.
 * @date   02.05.2009
 */

#include <boost/numeric/conversion/cast.hpp>
#include <assert.h>
#include "cpixelformat_indexed8.h"
#include "cbitmap.h"

using namespace std;

void CPixelFormat_Indexed8::getPixel(RGBPIXEL& pixel, uint32_t x, uint32_t y)
{
  if (m_bitmap->getPixelData() == NULL)
    throw PixelFormatError("No pixelbuffer allocated.");
  if (m_bitmap->getColorTable().size() == 0)
    return;
  assert(m_bitmap->getPixelDataSize() > 0);

  uint32_t offset = y * m_bitmap->getWidth() + x;

  /* boundary check */
  if (offset * sizeof(uint32_t) + sizeof(uint32_t) > m_bitmap->getPixelDataSize())
    throw PixelFormatError("Pixel position is out of range.");

  uint32_t color = *((uint32_t *)m_bitmap->getPixelData() + offset);

  map<uint32_t, RGBPIXEL *>::iterator it;
  if ((it = m_bitmap->getColorTable().find(color)) == m_bitmap->getColorTable().end())
    throw PixelFormatError("Pixel has no reference in colortable.");

  pixel.red   = (*it).second->red;
  pixel.green = (*it).second->green;
  pixel.blue  = (*it).second->blue;
}

/*----------------------------------------------------------------------------*/

void CPixelFormat_Indexed8::setPixel(const RGBPIXEL& pixel, uint32_t x, uint32_t y)
{
  if (m_bitmap->getPixelData() == NULL)
    throw PixelFormatError("No pixelbuffer allocated.");
  /* if colortable is empty there are no pixels */
  if (m_bitmap->getColorTable().size() == 0)
    return;
  assert(m_bitmap->getPixelDataSize() > 0);

  uint32_t offset = y * m_bitmap->getWidth() + x;

  /* boundary check */
  if (offset * sizeof(uint32_t) + sizeof(uint32_t) > m_bitmap->getPixelDataSize())
    throw PixelFormatError("Pixel position is out of range.");

  /* try to look up color in colortable */
  map<uint32_t, RGBPIXEL *>::iterator it;
  for(it = m_bitmap->getColorTable().begin(); it != m_bitmap->getColorTable().end(); it++)
  {
    if ((*it).second->red   == pixel.red &&
        (*it).second->green == pixel.green &&
        (*it).second->blue  == pixel.blue)
      break;
  }

  uint32_t index = (*it).first;
  /* need to get a new entry for our color */
  if (it == m_bitmap->getColorTable().end())
  {
    index = (*it).first + 1;
    RGBPIXEL *pixelptr = new RGBPIXEL;
    pixelptr->red   = pixel.red;
    pixelptr->green = pixel.green;
    pixelptr->blue  = pixel.blue;
    m_bitmap->getColorTable()[ index ] = pixelptr;
  }

  /* set color */
  *((uint32_t *)m_bitmap->getPixelData() + offset) = index;
}

/* vim: set et sw=2 ts=2: */