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