summaryrefslogtreecommitdiffstats
path: root/ue2/imgsynth2/cpixmap.cpp
blob: 83a2a0bfef30aa2183fa418040261f09da9d822e (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
 * @module CPixmap
 * @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
 * @brief  Implementation of CFile handling Windows Bitmaps.
 * @date   27.04.2009
 */

#include <sstream>
#include <iomanip>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <assert.h>
#ifdef DEBUG
# include <iostream>
#endif
#include "cpixmap.h"
#include "cpixelformat_indexed8.h"

using namespace std;
using namespace boost;

CPixmap::CPixmap()
  : m_imagename("")
{
  m_types.insert("XPM");

  /* add our handlers */
  m_handlers.insert(new CPixelFormat_Indexed8(this));
}

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

std::string CPixmap::getLine(std::ifstream& in, bool ignore_comments)
{
  string line("");
  while(!in.eof() && in.good())
  {
    getline(in, line);
    trim(line);
    /* ignore simple ansi c comments. only one-liners */
    if (ignore_comments && line.find_first_of("/*") == 0)
      continue;
    if (!line.empty())
      break;
  }
  return line;
}

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

std::string CPixmap::getCArrayLine(std::ifstream& in)
{
  string line = getLine(in, true);
  if (line.empty())
    return line;

  /* this stuff isn't correct too, but we are no c-parser anyway */
  if (line[0] != '"')
    throw FileError("Pixmap array has invalid c-syntax.");
  if (line[line.length() - 1] != ',' && line[line.length() - 1] != '}')
    throw FileError("Pixmap array has invalid c-syntax.");
  size_t end = line.find_last_of("\"");
  if (end == string::npos)
    throw FileError("Pixmap array has invalid c-syntax.");

  return line.substr(1, end - 1);
}

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

void CPixmap::read(std::ifstream& in)
{
  /*
   * <C-Comment> XPM <C-Comment>
   * static char * <pixmap_name>[] = {
   * <Values>
   * <Colors>
   * <Pixels>
   * <Extensions>
   * };
   */

  string line, tmp;
  stringstream istr;
  std::vector<std::string> list;
  m_fileheader._XPMEXT  = false;
  m_fileheader._HOTSPOT = false;

  /* get pixelformat instance first */
  m_pixelformat = NULL;
  set<CPixelFormat *>::iterator it;
  for (it = m_handlers.begin(); it != m_handlers.end(); it++)
  {
    /* we only have one! */
    m_pixelformat = *it;
    break;
  }
  if (m_pixelformat == NULL)
    throw FileError("Pixmap color mode is not supported.");

  /* first line has to be PIXMAP_IDENTIFIER */
  line = getLine(in, false);
  if (line != PIXMAP_IDENTIFIER)
    throw FileError("Pixmap has no identifier.");

  /* second line is a c array. we don't do much syntax checking */
  line = getLine(in);
  istr.str(line);
  while(m_imagename.empty() && !istr.eof() && istr.good())
  {
    size_t end;
    istr >> tmp;
    if ((end = tmp.find_first_of("[]")) != string::npos)
    {
      /* <xxx>*<imagename>[]<yyy> */
      size_t start = tmp.find_first_of('*');
      start = (start == string::npos) ? 0 : start + 1;
      m_imagename = tmp.substr(start, end - start);
    }
  }
  if (m_imagename.empty())
    throw FileError("Pixmap has no imagename.");

  /* additional: check "{" exists */
  assert(!line.empty());
  if (line[line.length() - 1] != '{')
    throw FileError("Pixmap array has no opening bracket.");

  /* parse <Values>-section */
  line = getCArrayLine(in);
  if (line.empty())
    throw FileError("Pixmap has no Values-section.");
  algorithm::split(list, line, is_any_of(" \t"));
  if (list.size() < 4)
    throw FileError("Pixmap has invalid Values-section.");
  try
  {
    m_fileheader.width  = lexical_cast<uint32_t>(list[0]);
    m_fileheader.height = lexical_cast<uint32_t>(list[1]);
    m_fileheader.nColor = lexical_cast<uint32_t>(list[2]);
    m_fileheader.nChar  = lexical_cast<uint32_t>(list[3]);

    if (list.size() > 4)
    {
      if (list.size() >= 6)
      {
        m_fileheader._HOTSPOT = true;
        m_fileheader.xHotspot = lexical_cast<uint32_t>(list[4]);
        m_fileheader.yHotspot = lexical_cast<uint32_t>(list[5]);
      }
      if (list.size() != 6)
      {
        if (list[list.size() - 1] != "XPMEXT")
          throw FileError("Unknown parameter count in Values-Section.");
        else
          m_fileheader._XPMEXT = true;
      }
    }
  }
  catch(bad_lexical_cast& ex)
  {
    throw FileError("Value of Values-section is invalid: " + string(ex.what()));
  }

  /* parse <Colors>-table */
  string character;
  /* map[id][colortype] = color */
  map<string, map<string, CPixelFormat::RGBPIXEL *> > colors;
  /* map[id] = indices */
  map<string, uint32_t> colornr;
  uint32_t index = 0;
  for(uint32_t i = 0; i < m_fileheader.nColor; i++)
  {
    line = getCArrayLine(in);
    if (line.empty())
      throw FileError("Pixmap has missing colortable-entry.");
    algorithm::split(list, line, is_any_of(" \t"));
    if (list.size() < 3)
      throw FileError("Pixmap colortable-entry is invalid.");

    /* read pixel character */
    character = list[0];
    if (character.length() != m_fileheader.nChar)
      throw FileError("Pixmap colorcharacter is invalid.");
    if (colors.find(character) != colors.end())
      throw FileError("Duplicate colorcharacter found.");

    /* read colors */
    if ((list.size() - 1) % 2 != 0)
      throw FileError("Pixmap color entrys are invalid.");
    for(uint32_t j = 1; j < list.size(); j = j + 2)
    {
      /* we only support hex-color notations */
      if (list[j + 1].length() != 7)
        throw FileError("Pixmap color value is invalid.");
      if (list[j + 1].at(0) != '#')
        throw FileError("Pixmap color table value is not hexadecimal.");

      /* we only support c-colors! - remove only if you free the pixels */
      if (list[j] != "c")
        continue;

      CPixelFormat::RGBPIXEL *pixel = new CPixelFormat::RGBPIXEL;
      pixel->red   = strtoul(list[j + 1].substr(1, 2).c_str(), NULL, 16);
      pixel->green = strtoul(list[j + 1].substr(3, 2).c_str(), NULL, 16);
      pixel->blue  = strtoul(list[j + 1].substr(5, 2).c_str(), NULL, 16);
      colors[ character ][ list[j] ] = pixel;
    }

    /* we only support c-colors! */
    if (colors[ character ].find("c") == colors[ character ].end())
      throw FileError("Pixmap color entry has missing c-value.");

    /* add pixel to colortable */
    colornr[ character ] = index;
    m_colortable[ index ] = colors[ character ]["c"];
    index++;
  }

  /* read pixel data */
  if (getPixelDataSize() > 0)
  {
    if (m_pixeldata != NULL)
      delete[] m_pixeldata;
    m_pixeldata = new uint8_t[getPixelDataSize()];

    for (uint32_t y = 0; y < getHeight(); y++)
    {
      line = getCArrayLine(in);
      if (line.empty())
        throw FileError("Pixmap has no pixel data.");
      if (line.length() != getWidth())
        throw FileError("Pixmap pixeldata width is larger than header width.");

      /* convert color identifier to our own identifiers */
      for(uint32_t x = 0; x < getWidth(); x++)
      {
        character = line.substr(x * m_fileheader.nChar, m_fileheader.nChar);
        assert(!character.empty());
        if (colornr.find(character) == colornr.end())
          throw FileError("Pixel has no reference in colortable.");

        uint32_t offset = y * getWidth() + x;

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

        *((uint32_t *)m_pixeldata + offset) = colornr[ character ];
      }
    }
  }

  /* get extension */
  if (m_fileheader._XPMEXT)
    getline(in, m_fileheader.extension, '}');
  if (!in.good())
    throw FileError("Pixmap array isn't closed properly.");

  /* set rowsize */
  m_rowsize = sizeof(uint32_t) * getWidth();
}

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

const std::string CPixmap::getXPMColorID(unsigned int index, unsigned int length)
{
  static const char code[] = PIXMAP_COLORCHARS;
  assert(strlen(code) > 0);
  assert(length > 0);
  string str("");
  for(unsigned int i = length - 1; i > 0; i--)
  {
    str += code[index % strlen(code)];
    index /= strlen(code);
  }
  str += code[index];
  assert(!str.empty());
  return str;
}

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

void CPixmap::write(std::ofstream& out)
{
  m_fileheader.nColor = m_colortable.size();
  m_fileheader.nChar  = m_fileheader.nColor / strlen(PIXMAP_COLORCHARS) + 1;

  /* header comment */
  out << PIXMAP_IDENTIFIER << endl;

  /* variables*/
  assert(!m_imagename.empty());
  out << "static char * " << m_imagename << "[] = {" << endl;
  out << "\"" << m_fileheader.width << " " << m_fileheader.height
      << " " << m_fileheader.nColor << " " << m_fileheader.nChar;

  /* optional values */
  if (m_fileheader._HOTSPOT)
    out << " " << m_fileheader.xHotspot << " " << m_fileheader.yHotspot;
  if (m_fileheader._XPMEXT)
    out << " " << "XPMEXT";
  out << "\"," << endl;

  /* color table */
  map<uint32_t, CPixelFormat::RGBPIXEL *>::iterator it;
  for (it = m_colortable.begin(); it != m_colortable.end(); it++)
  {
    out << "\"" << getXPMColorID((*it).first, m_fileheader.nChar);
    /* we only support c-colors! */
    out << "\tc #";
    out << setfill('0') << setw(2) << hex << uppercase << (*it).second->red
        << setfill('0') << setw(2) << hex << uppercase << (*it).second->green
        << setfill('0') << setw(2) << hex << uppercase << (*it).second->blue;
    out << "\"," << endl;
  }

  /* pixel data */
  for (uint32_t y = 0; y < getHeight(); y++)
  {
    out << "\"";
    for(uint32_t x = 0; x < getWidth(); x++)
    {
      uint32_t offset = y * getWidth() + x;

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

      uint32_t color = *((uint32_t *)m_pixeldata + offset);

      if ((it = m_colortable.find(color)) == m_colortable.end())
        throw FileError("Pixel has no reference in colortable.");
      out << getXPMColorID((*it).first, m_fileheader.nChar);
    }
    out << "\"," << endl;
  }

  /* extension */
  if (m_fileheader._XPMEXT)
    out << m_fileheader.extension;

  out <<"};";
}

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

#ifdef DEBUG
void CPixmap::dump(std::ostream& out)
{
  /* values*/
  cout << "[XPM Header Values]" << endl
       << "width="     << m_fileheader.width     << endl
       << "height="    << m_fileheader.height    << endl
       << "nColor="    << m_fileheader.nColor    << endl
       << "nChar="     << m_fileheader.nChar     << endl
       << "Hotspot="   << m_fileheader.xHotspot  << endl
       << "yHotspot="  << m_fileheader.yHotspot  << endl
       << "_HOTSPOT="  << m_fileheader._HOTSPOT  << endl
       << "_XPMEXT="   << m_fileheader._XPMEXT   << endl
       << "extension=" << m_fileheader.extension << endl
       << endl;

  /* colors*/
  map<uint32_t, CPixelFormat::RGBPIXEL *>::iterator it;
  cout << "[Color Table]" << endl;
  for (it = m_colortable.begin(); it != m_colortable.end(); it++)
  {
    out << (*it).first << ": "
        << setfill('0') << setw(3) << (*it).second->red   << " "
        << setfill('0') << setw(3) << (*it).second->green << " "
        << setfill('0') << setw(3) << (*it).second->blue  << " "
        << endl;
  }
}
#endif

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