add monochrome packet support to simulator
Stefan Schuermans

Stefan Schuermans commited on 2018-08-14 19:32:07
Showing 2 changed files, with 17 additions and 11 deletions.

... ...
@@ -279,18 +279,19 @@ bool Distri::on_packet(Glib::IOCondition condition)
279 279
       // check magic, 3 channels and maxval 0xFF
280 280
       if (buffer[ 0] == 0x23 && buffer[ 1] == 0x54 &&
281 281
           buffer[ 2] == 0x26 && buffer[ 3] == 0x66 &&
282
-          buffer[ 8] == 0x00 && buffer[ 9] == 0x03 &&
282
+          buffer[ 8] == 0x00 && (buffer[9] == 0x01 || buffer[9] == 0x03) &&
283 283
           buffer[10] == 0x00 && buffer[11] == 0xFF) {
284
-        // get outputs (height) and pixels (width)
284
+        // get outputs (height), pixels (width) and channels
285 285
         unsigned int outputs = (unsigned int)buffer[4] << 8 |
286 286
                                (unsigned int)buffer[5];
287 287
         unsigned int pixels = (unsigned int)buffer[6] << 8 |
288 288
                               (unsigned int)buffer[7];
289
+        unsigned int channels = buffer[9];
289 290
         // check data length
290
-        if (sz >= 12 + outputs * pixels * 3) {
291
+        if (sz >= 12 + outputs * pixels * channels) {
291 292
           uint8_t const *data = buffer + 12;
292 293
           // process pixel data
293
-          procPixelData(outputs, pixels, data);
294
+          procPixelData(outputs, pixels, channels, data);
294 295
         }
295 296
       }
296 297
     }
... ...
@@ -305,10 +306,11 @@ bool Distri::on_packet(Glib::IOCondition condition)
305 306
  * @brief process pixel data
306 307
  * @param[in] outputs number of outputs in pixel data
307 308
  * @param[in] pixels number of pixels per output in pixel data
309
+ * @param[in] channels number of channels in pixel data
308 310
  * @param[in] data pixel data
309 311
  */
310 312
 void Distri::procPixelData(unsigned long outputs, unsigned long pixels,
311
-                           uint8_t const *data)
313
+                           unsigned int channels, uint8_t const *data)
312 314
 {
313 315
   // get mappings
314 316
   Mapping const & mapRed   = m_mappings[Mapping::Red  ];
... ...
@@ -316,26 +318,29 @@ void Distri::procPixelData(unsigned long outputs, unsigned long pixels,
316 318
   Mapping const & mapBlue  = m_mappings[Mapping::Blue ];
317 319
 
318 320
   // traverse all outputs and pixels
321
+  unsigned int ofs_r = 0;
322
+  unsigned int ofs_g = channels >= 2 ? 1 : 0;
323
+  unsigned int ofs_b = channels >= 3 ? 2 : 0;
319 324
   OutputMap::iterator out = m_outputMap.begin();
320 325
   OutputMap::iterator outend = m_outputMap.end();
321 326
   unsigned long o = 0;
322 327
   unsigned long oi = 0;
323
-  for (; out != outend; ++out, ++o, oi += pixels * 3) {
328
+  for (; out != outend; ++out, ++o, oi += pixels * channels) {
324 329
     Output &output = out->second;
325 330
     std::vector<Pixel>::iterator pix = output.m_pixels.begin();
326 331
     std::vector<Pixel>::iterator pixend = output.m_pixels.end();
327 332
     unsigned long p = 0;
328 333
     unsigned long pi = oi;
329
-    for (; pix != pixend; ++pix, ++p, pi += 3) {
334
+    for (; pix != pixend; ++pix, ++p, pi += channels) {
330 335
       Pixel &pixel = *pix;
331 336
 
332 337
       // default color: black (in case pixel is not in pixel data)
333 338
       uint8_t r = 0, g = 0, b = 0;
334 339
       // get color of pixel from data
335 340
       if (o < outputs && p < pixels) {
336
-        r = mapRed  .display2video(data[pi + 0]);
337
-        g = mapGreen.display2video(data[pi + 1]);
338
-        b = mapBlue .display2video(data[pi + 2]);
341
+        r = mapRed  .display2video(data[pi + ofs_r]);
342
+        g = mapGreen.display2video(data[pi + ofs_g]);
343
+        b = mapBlue .display2video(data[pi + ofs_b]);
339 344
       }
340 345
       // store color
341 346
       pixel.setColor(r, g, b);
... ...
@@ -132,10 +132,11 @@ protected:
132 132
    * @brief process pixel data
133 133
    * @param[in] outputs number of outputs in pixel data
134 134
    * @param[in] pixels number of pixels per output in pixel data
135
+   * @param[in] channels number of channels in pixel data
135 136
    * @param[in] data pixel data
136 137
    */
137 138
   void procPixelData(unsigned long outputs, unsigned long pixels,
138
-                     uint8_t const *data);
139
+                     unsigned int channels, uint8_t const *data);
139 140
 
140 141
 protected:
141 142
   /// output data
142 143