Bridges-C++  3.2.0
Bridges(C++API)
ColorGrid.h
Go to the documentation of this file.
1 
2 #include <string>
3 using namespace std;
4 
5 #ifndef COLOR_GRID_H
6 #define COLOR_GRID_H 1
7 
8 #include "Grid.h"
9 #include "Color.h"
10 #include "base64.h"
11 
12 namespace bridges {
13  namespace datastructure {
22  class ColorGrid : public Grid<Color> {
23  private:
24  int debug() const {
25  return 0;
26  }
27  Color baseColor = Color("black");
28 
35  void initializeGrid (const Color& col) {
36  // fill elements with base color
37  for (int i = 0; i < gridSize[0]; i++)
38  for (int j = 0; j < gridSize[1]; j++)
39  grid[i][j] = col;
40  }
41 
42  public:
43  virtual const string getDStype() const override {
44  return "ColorGrid";
45  }
46 
51  ColorGrid () : Grid<Color> () {
52  initializeGrid(baseColor);
53  }
54 
61  ColorGrid (int rows, int cols) : Grid<Color> (rows, cols) {
62  initializeGrid(baseColor);
63  }
64 
73  ColorGrid (int rows, int cols, Color color) : Grid<Color> (rows, cols) {
74  baseColor = color;
75  initializeGrid(color);
76  }
77 
78 
82  ColorGrid (const ColorGrid& cg)
83  : Grid<Color> (cg),
84  baseColor(cg.baseColor) {
85  }
86 
87  ColorGrid& operator= (const ColorGrid& cg) {
88  Grid::operator=(cg);
89 
90  this->baseColor = cg.baseColor;
91 
92  return *this;
93  }
94 
100  int getHeight() {
101  return gridSize[0];
102  }
103 
109  int getWidth() {
110  return gridSize[1];
111  }
112 
113  private:
114 
118  std::vector<BYTE> getRAWencoding() const {
119  std::vector<BYTE> byte_buf(4 * gridSize[0] * gridSize[1]);
120 
121  int k = 0;
122  for (int i = 0; i < gridSize[0]; i++) {
123  for (int j = 0; j < gridSize[1]; j++) {
124  byte_buf[k++] = grid[i][j].getRed();
125  byte_buf[k++] = grid[i][j].getGreen();
126  byte_buf[k++] = grid[i][j].getBlue();
127  byte_buf[k++] = grid[i][j].getAlpha();
128  }
129  }
130  return byte_buf;
131 
132  }
133 
137  std::vector<BYTE> getRLEencoding() const {
138  std::vector<BYTE> vec;
139 
140  int count = 0;
141  int totalcount = 0;
142  BYTE r, g, b, a;
143 
144  auto commit = [&]() {
145  if (count == 0)
146  return;
147 
148  totalcount += count;
149 
150  BYTE repeat = (BYTE) (count - 1);
151 
152  if (debug() > 1)
153  std::cerr << "RLEencodingstream: " << (int)repeat << " " << (int)r << " " << (int)g << " " << (int)b << " " << (int)a << std::endl;
154 
155  vec.push_back(repeat);
156  vec.push_back(r);
157  vec.push_back(g);
158  vec.push_back(b);
159  vec.push_back(a);
160 
161  count = 0;
162  };
163 
164  int pos = 0;
165  while (pos < gridSize[0]*gridSize[1]) {
166 
167  int posX = pos / gridSize[1];
168  int posY = pos % gridSize[1];
169  BYTE lr = grid[posX][posY].getRed();
170  BYTE lg = grid[posX][posY].getGreen();
171  BYTE lb = grid[posX][posY].getBlue();
172  BYTE la = grid[posX][posY].getAlpha();
173 
174  if (count == 0) {
175  count = 1;
176  r = lr;
177  g = lg;
178  b = lb;
179  a = la;
180  }
181  else {
182  if (r == lr &&
183  g == lg &&
184  b == lb &&
185  a == la) { //same color than previous pixel
186  count++;
187  }
188  else { //different color than previous pixel
189  commit();
190  count = 1;
191  r = lr;
192  g = lg;
193  b = lb;
194  a = la;
195  }
196  }
197 
198  if (count == 256) {
199  commit();
200  }
201 
202  pos++;
203  }
204  //there could be something uncommitted at this point.
205  commit();
206 
207  if (totalcount != gridSize[0]*gridSize[1])
208  throw "what happened in RLE construction?";
209 
210  if (debug())
211  std::cerr << "RLE length: " << vec.size()
212  << " raw length: " << gridSize[0]*gridSize[1] * 4
213  << " Compression rate:" << (float)(vec.size()) / (gridSize[0]*gridSize[1] * 4)
214  << std::endl;
215  return vec;
216  }
217 
218  //public:
224  virtual const string getDataStructureRepresentation () const override {
226 
227  // Maintain a bytebuffer for the byte representations of each grid color
228  std::vector<BYTE> byte_buf = getRLEencoding();
229  std::string encoding = "RLE";
230  if ((int)(byte_buf.size()) > gridSize[0]*gridSize[1] * 4) {
231  encoding = "RAW";
232  byte_buf = getRAWencoding();
233  if (debug())
234  std::cerr << "encoding ColorGrid as RAW" << std::endl;
235  }
236  else {
237  if (debug())
238  std::cerr << "encoding ColorGrid as RLE" << std::endl;
239  }
240 
241  // set the grid dimensions for the visualizer
242  // get the base64 representation of the color array
243  string grid_json =
244  QUOTE + "encoding" + QUOTE + COLON + QUOTE + encoding + QUOTE + COMMA +
245  QUOTE + "dimensions" + QUOTE + COLON +
246  OPEN_BOX +
247  JSONencode(gridSize[0]) + COMMA + JSONencode(gridSize[1]) +
248  CLOSE_BOX + COMMA +
249 
250  QUOTE + "nodes" + QUOTE + COLON +
251  OPEN_BOX + QUOTE +
252  base64::encode (&(byte_buf[0]), byte_buf.size()) +
253  QUOTE + CLOSE_BOX +
254  CLOSE_CURLY;
255 
256 
257  return grid_json;
258  }
259 
260 
261  };
262  }
263 } // end namespace bridges
264 
265 #endif
int getHeight()
Definition: ColorGrid.h:100
string encode(BYTE const *buf, unsigned int bufLen)
Definition: base64.h:59
const string COLON
Definition: DataStructure.h:51
ColorGrid()
Definition: ColorGrid.h:51
STL namespace.
int getWidth()
Definition: ColorGrid.h:109
const string OPEN_BOX
Definition: DataStructure.h:54
This is a class in BRIDGES for representing an image.
Definition: ColorGrid.h:22
const string CLOSE_CURLY
Definition: DataStructure.h:53
This is a class in BRIDGES for representing an (n x n) grid.
Definition: Grid.h:31
virtual const string getDStype() const override
Return the data structure type.
Definition: ColorGrid.h:43
This class represents Color, and supports rgba, hexadecimal and named color values.
Definition: Color.h:51
these methods convert byte arrays in to base64 codes and are used in BRIDGES to represent the color a...
Definition: alltypes.h:4
const string CLOSE_BOX
Definition: DataStructure.h:55
unsigned char BYTE
Definition: base64.h:44
ColorGrid(int rows, int cols, Color color)
Definition: ColorGrid.h:73
ColorGrid(int rows, int cols)
Definition: ColorGrid.h:61
ColorGrid(const ColorGrid &cg)
Definition: ColorGrid.h:82
const string COMMA
Definition: DataStructure.h:50
const string QUOTE
Definition: DataStructure.h:49
std::string JSONencode(const T &d)
Definition: JSONutil.h:37