portableimage.cpp
Go to the documentation of this file.
2 
3 namespace IZ
4 {
5  static unsigned char* writeValue(unsigned char* p, unsigned char whiteSpace, int value)
6  {
7  unsigned int v = value;
8  *p++ = whiteSpace;
9  bool first = true;
10  unsigned int k = 10000;
11  while (k > 0)
12  {
13  if (v >= k || !first)
14  {
15  *p++ = '0' + v / k;
16  v %= k;
17  first = false;
18  }
19  k /= 10;
20  }
21  return p;
22  }
23 
24  static const unsigned char* readValue(const unsigned char* p, int& value)
25  {
26  // skip white space
27  unsigned char c;
28  while (c = *p, true)
29  {
30  if (c == ' ' || (c >= '\011' && c <= '\015'))
31  {
32  ++p;
33  }
34  else if (c == '#')
35  {
36  do
37  {
38  ++p;
39  } while (*p != '\n');
40  }
41  else
42  {
43  break;
44  }
45  }
46  int v = 0;
47  unsigned int d;
48  while (d = *p - '0', d < 10U)
49  {
50  ++p;
51  v = 10 * v + d;
52  }
53  value = v;
54  return p;
55  }
56 
57  unsigned char* PortableImage::writeHeader(unsigned char* p)
58  {
59  *p++ = 'P';
60 
61  switch (m_components)
62  {
63  case 3:
64  *p++ = '6';
65  break;
66  case 1:
67  *p++ = '5';
68  break;
69  default:
70  return p;
71  }
72 
73  p = writeValue(p, '\n', m_width);
74  p = writeValue(p, ' ', m_height);
75  p = writeValue(p, '\n', 255);
76 
78 
79  *p++ = '\n';
80  m_data = p;
81  return p;
82  }
83 
84  bool PortableImage::readHeader(const unsigned char* p)
85  {
86  if (*p++ != 'P')
87  {
88  return false;
89  }
90 
91  switch (*p++)
92  {
93  case '6':
94  m_components = 3;
95  break;
96  case '5':
97  m_components = 1;
98  break;
99  default:
100  return false;
101  }
102 
103  p = readValue(p, m_width);
104  p = readValue(p, m_height);
105  p = readValue(p, m_maxVal);
106 
107  if (m_maxVal != 255 || m_width < 1 || m_height < 1)
108  {
109  return false;
110  }
111 
113 
114  ++p; // skip single space
115  m_data = (unsigned char*) p;
116  return true;
117  }
118 
120  : m_components(0)
121  {
122  }
123 
125  {
126  }
127 }
unsigned char * m_data
Definition: image.h:29
static const unsigned char * readValue(const unsigned char *p, int &value)
Definition: bitcoder.h:6
static unsigned char * writeValue(unsigned char *p, unsigned char whiteSpace, int value)
int m_width
Definition: image.h:26
int m_height
Definition: image.h:27
void setSamplesPerLine(ptrdiff_t samplesPerLine)
Definition: image.h:22
unsigned char * writeHeader(unsigned char *data)
bool readHeader(const unsigned char *data)


imagezero
Author(s):
autogenerated on Mon Jun 10 2019 13:37:22