summaryrefslogtreecommitdiffstats
path: root/ue2/imgsynth2/cbitmap.cpp
blob: 82ba4ae3263233e51555a7dbcf48c825196bd099 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
 * @module cbitmap
 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
 * @brief  Abstract implementation of CFile handling Bitmaps.
 * @date   17.04.2009
 */

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

using namespace std;

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

CBitmap::~CBitmap()
{
  /* delete pixeldata */
  if (m_pixeldata != NULL)
    delete[] m_pixeldata;
  m_pixeldata = NULL;

  /* delete pixelformat handlers */
  set<CPixelFormat *>::iterator it;
  for (it = m_handlers.begin(); it != m_handlers.end(); it++)
    delete *it;
  m_pixelformat = NULL;
}

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

void CBitmap::callFunc(const std::string& func, const std::list<std::string>& params)
{
  if (func.empty())
    throw FileError("Function name is empty.");

  if (func == "fillrect")
    fillrect(params);
  else if (func == "mirror_x")
    mirror_x(params);
  else if (func == "mirror_y")
    mirror_y(params);
  else if (func == "invert")
    invert(params);
  else
    throw FileError("Unknown function '" + func + "'.");
}

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

void CBitmap::fillrect(std::list<std::string> params)
{
  /* check prerequirements */
  if (params.size() != 7)
    throw FileError("Invalid number of function parameters (must be 7).");

  /* do nothing if no pixel exists */
  if (m_pixeldata == NULL || m_pixelformat == NULL)
    return;

  /* convert parameters */
  uint32_t pparams[7];
  int i = 0;
  try
  {
    for(i = 0; i < 7; i++)
    {
      pparams[i] = boost::lexical_cast<uint32_t>(params.front());
      params.pop_front();
    }
  }
  catch(boost::bad_lexical_cast& ex)
  {
    throw FileError("Invalid parameter (" + params.front() + ").");
  }

  /* check parameter values are in range */
  if (pparams[0] < 0 || pparams[0] > getWidth()
   || pparams[1] < 0 || pparams[1] > getHeight())
    throw FileError("At least one x/y-parameter is out of range.");

  /* check parameter values are in range */
  unsigned int max[3];
  m_pixelformat->getMaxColor(&max[0], &max[1], &max[2]);
  if (pparams[4] < 0 || pparams[4] > max[0]
   || pparams[5] < 0 || pparams[5] > max[1]
   || pparams[6] < 0 || pparams[6] > max[2])
    throw FileError("At least one pixel color parameter is out of range.");

  if (pparams[2] < 0 || pparams[2] + pparams[0] > getWidth()
   || pparams[3] < 0 || pparams[3] + pparams[1] > getHeight())
    throw FileError("At least one w/h-parameter is out of range.");

  /* call setPixel for every pixel in the rectangel */
  for(uint32_t i = pparams[0]; i < pparams[2] + pparams[0]; i++)
  {
    for(uint32_t j = pparams[1]; j < pparams[3] + pparams[1]; j++)
    {
      try
      {
        m_pixelformat->setPixel(&pparams[4], i, j);
      }
      catch(CPixelFormat::PixelFormatError& ex)
      {
        stringstream errstr;
        errstr << "Can't set pixel (pos=[" << i << "," << j << "] col=["
          << pparams[4] << "," << pparams[5] << "," << pparams[6] << "]): "
          << ex.what();
        throw FileError(errstr.str());
      }
    }
  }
}

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

/* TODO */
void CBitmap::invert(std::list<std::string> params)
{
  /* check prerequirements */
  if (params.size() != 0)
    throw FileError("Invalid number of function parameters (must be 0).");

  /* do nothing if no pixel exists */
  if (m_pixeldata == NULL || m_pixelformat == NULL)
    return;

  /* TODO pixelwidth */
  unsigned int pixelwidth = m_pixelformat->getBitCount()/8;

  /* calc rowsize - boundary is 32 */
  uint32_t rowsize = 4 * static_cast<uint32_t>(
    ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32
  );

  for(uint32_t i = 0; i < getHeight(); i++)
  {
    for(uint32_t j = 0; j <= getWidth(); j++)
    {
      /*TODO cout << j << endl;*/
      break;
    }
  }

#if 0
    uint32_t offset = i * rowsize;

    for(uint32_t j = 0; j <= getWidth()/2; j++)
    {
      uint32_t poffset  = offset + j * pixelwidth;
      uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth;

      /* boundary check */
      if (pbackset > m_infoheader.biSizeImage)
        throw FileError("Mirrored pixel position is out of range.");

      /* mirroring, backup right data first */
      copy(m_pixeldata + pbackset - pixelwidth, m_pixeldata + pbackset, buf);
      copy(m_pixeldata + poffset,  m_pixeldata + poffset + pixelwidth, m_pixeldata + pbackset - pixelwidth);
      copy(buf, buf + pixelwidth, m_pixeldata + poffset);
    }
  }
#endif
}

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

/* TODO */
void CBitmap::brightness(std::list<std::string> params)
{
}

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

void CBitmap::mirror_y(std::list<std::string> params)
{
  /* check prerequirements */
  if (params.size() != 0)
    throw FileError("Invalid number of function parameters (must be 0).");

  /* do nothing if no pixel exists */
  if (m_pixeldata == NULL || m_pixelformat == NULL)
    return;

  /* calc rowsize - boundary is 32 */
  uint32_t rowsize = 4 * static_cast<uint32_t>(
    ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32
  );

  uint8_t *buf = new uint8_t[rowsize];
  for(uint32_t i = 0; i < getHeight()/2; i++)
  {
    uint32_t j = getHeight() - i - 1;
    uint32_t offset  = i * rowsize;
    uint32_t backset = j * rowsize;

    /* boundary check */
    if (offset + rowsize > getPixelDataSize()
     || backset + rowsize > getPixelDataSize())
      throw FileError("Mirrored pixel position is out of range.");

    /* mirroring, backup lower data first */
    copy(m_pixeldata + backset, m_pixeldata + backset + rowsize, buf);
    copy(m_pixeldata + offset,  m_pixeldata + offset + rowsize, m_pixeldata + backset);
    copy(buf, buf + rowsize, m_pixeldata + offset);
  }
  delete[] buf;
}

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

void CBitmap::mirror_x(std::list<std::string> params)
{
  /* check prerequirements */
  if (params.size() != 0)
    throw FileError("Invalid number of function parameters (must be 0).");

  /* do nothing if no pixel exists */
  if (m_pixeldata == NULL || m_pixelformat == NULL)
    return;

  /* calc rowsize - boundary is 32 */
  uint32_t rowsize = 4 * static_cast<uint32_t>(
    ((m_pixelformat->getBitCount() * getWidth()) + 31) / 32
  );

  /* calc pixelwidth */
  unsigned int pixelwidth = m_pixelformat->getBitCount()/8;

  uint8_t *buf = new uint8_t[pixelwidth];
  for(uint32_t i = 0; i < getHeight(); i++)
  {
    uint32_t offset = i * rowsize;

    for(uint32_t j = 0; j <= getWidth()/2; j++)
    {
      uint32_t poffset  = offset + j * pixelwidth;
      uint32_t pbackset = offset + getWidth() * pixelwidth - j * pixelwidth;

      /* boundary check */
      if (pbackset > getPixelDataSize())
        throw FileError("Mirrored pixel position is out of range.");

      /* mirroring, backup right data first */
      copy(m_pixeldata + pbackset - pixelwidth, m_pixeldata + pbackset, buf);
      copy(m_pixeldata + poffset,  m_pixeldata + poffset + pixelwidth, m_pixeldata + pbackset - pixelwidth);
      copy(buf, buf + pixelwidth, m_pixeldata + poffset);
    }
  }
  delete[] buf;
}

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