-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
led-image-viewer.cc
523 lines (470 loc) · 18.4 KB
/
led-image-viewer.cc
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
// Copyright (C) 2015 Henner Zeller <[email protected]>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation version 2.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://gnu.org/licenses/gpl-2.0.txt>
// To use this image viewer, first get image-magick development files
// $ sudo apt-get install libgraphicsmagick++-dev libwebp-dev
//
// Then compile with
// $ make led-image-viewer
#include "led-matrix.h"
#include "pixel-mapper.h"
#include "content-streamer.h"
#include <fcntl.h>
#include <math.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <string>
#include <vector>
#include <Magick++.h>
#include <magick/image.h>
using rgb_matrix::Canvas;
using rgb_matrix::FrameCanvas;
using rgb_matrix::RGBMatrix;
using rgb_matrix::StreamReader;
typedef int64_t tmillis_t;
static const tmillis_t distant_future = (1LL<<40); // that is a while.
struct ImageParams {
ImageParams() : anim_duration_ms(distant_future), wait_ms(1500),
anim_delay_ms(-1), loops(-1), vsync_multiple(1) {}
tmillis_t anim_duration_ms; // If this is an animation, duration to show.
tmillis_t wait_ms; // Regular image: duration to show.
tmillis_t anim_delay_ms; // Animation delay override.
int loops;
int vsync_multiple;
};
struct FileInfo {
ImageParams params; // Each file might have specific timing settings
bool is_multi_frame = false;
rgb_matrix::StreamIO *content_stream = nullptr;
};
volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
interrupt_received = true;
}
static tmillis_t GetTimeInMillis() {
struct timeval tp;
gettimeofday(&tp, NULL);
return tp.tv_sec * 1000 + tp.tv_usec / 1000;
}
static void SleepMillis(tmillis_t milli_seconds) {
if (milli_seconds <= 0) return;
struct timespec ts;
ts.tv_sec = milli_seconds / 1000;
ts.tv_nsec = (milli_seconds % 1000) * 1000000;
nanosleep(&ts, NULL);
}
static void StoreInStream(const Magick::Image &img, int delay_time_us,
bool do_center,
rgb_matrix::FrameCanvas *scratch,
rgb_matrix::StreamWriter *output) {
scratch->Clear();
const int x_offset = do_center ? (scratch->width() - img.columns()) / 2 : 0;
const int y_offset = do_center ? (scratch->height() - img.rows()) / 2 : 0;
for (size_t y = 0; y < img.rows(); ++y) {
for (size_t x = 0; x < img.columns(); ++x) {
const Magick::Color &c = img.pixelColor(x, y);
if (c.alphaQuantum() < 255) {
scratch->SetPixel(x + x_offset, y + y_offset,
ScaleQuantumToChar(c.redQuantum()),
ScaleQuantumToChar(c.greenQuantum()),
ScaleQuantumToChar(c.blueQuantum()));
}
}
}
output->Stream(*scratch, delay_time_us);
}
static void CopyStream(rgb_matrix::StreamReader *r,
rgb_matrix::StreamWriter *w,
rgb_matrix::FrameCanvas *scratch) {
uint32_t delay_us;
while (r->GetNext(scratch, &delay_us)) {
w->Stream(*scratch, delay_us);
}
}
// Load still image or animation.
// Scale, so that it fits in "width" and "height" and store in "result".
static bool LoadImageAndScale(const char *filename,
int target_width, int target_height,
bool fill_width, bool fill_height,
std::vector<Magick::Image> *result,
std::string *err_msg) {
std::vector<Magick::Image> frames;
try {
readImages(&frames, filename);
} catch (std::exception& e) {
if (e.what()) *err_msg = e.what();
return false;
}
if (frames.size() == 0) {
fprintf(stderr, "No image found.");
return false;
}
// Put together the animation from single frames. GIFs can have nasty
// disposal modes, but they are handled nicely by coalesceImages()
if (frames.size() > 1) {
Magick::coalesceImages(result, frames.begin(), frames.end());
} else {
result->push_back(frames[0]); // just a single still image.
}
const int img_width = (*result)[0].columns();
const int img_height = (*result)[0].rows();
const float width_fraction = (float)target_width / img_width;
const float height_fraction = (float)target_height / img_height;
if (fill_width && fill_height) {
// Scrolling diagonally. Fill as much as we can get in available space.
// Largest scale fraction determines that.
const float larger_fraction = (width_fraction > height_fraction)
? width_fraction
: height_fraction;
target_width = (int) roundf(larger_fraction * img_width);
target_height = (int) roundf(larger_fraction * img_height);
}
else if (fill_height) {
// Horizontal scrolling: Make things fit in vertical space.
// While the height constraint stays the same, we can expand to full
// width as we scroll along that axis.
target_width = (int) roundf(height_fraction * img_width);
}
else if (fill_width) {
// dito, vertical. Make things fit in horizontal space.
target_height = (int) roundf(width_fraction * img_height);
}
for (size_t i = 0; i < result->size(); ++i) {
(*result)[i].scale(Magick::Geometry(target_width, target_height));
}
return true;
}
void DisplayAnimation(const FileInfo *file,
RGBMatrix *matrix, FrameCanvas *offscreen_canvas) {
const tmillis_t duration_ms = (file->is_multi_frame
? file->params.anim_duration_ms
: file->params.wait_ms);
rgb_matrix::StreamReader reader(file->content_stream);
int loops = file->params.loops;
const tmillis_t end_time_ms = GetTimeInMillis() + duration_ms;
const tmillis_t override_anim_delay = file->params.anim_delay_ms;
for (int k = 0;
(loops < 0 || k < loops)
&& !interrupt_received
&& GetTimeInMillis() < end_time_ms;
++k) {
uint32_t delay_us = 0;
while (!interrupt_received && GetTimeInMillis() <= end_time_ms
&& reader.GetNext(offscreen_canvas, &delay_us)) {
const tmillis_t anim_delay_ms =
override_anim_delay >= 0 ? override_anim_delay : delay_us / 1000;
const tmillis_t start_wait_ms = GetTimeInMillis();
offscreen_canvas = matrix->SwapOnVSync(offscreen_canvas,
file->params.vsync_multiple);
const tmillis_t time_already_spent = GetTimeInMillis() - start_wait_ms;
SleepMillis(anim_delay_ms - time_already_spent);
}
reader.Rewind();
}
}
static int usage(const char *progname) {
fprintf(stderr, "usage: %s [options] <image> [option] [<image> ...]\n",
progname);
fprintf(stderr, "Options:\n"
"\t-O<streamfile> : Output to stream-file instead of matrix (Don't need to be root).\n"
"\t-C : Center images.\n"
"\t-m : if this is a stream, mmap() it. This can work around IO latencies in SD-card and refilling kernel buffers. This will use physical memory so only use if you have enough to map file size\n"
"\nThese options affect images FOLLOWING them on the command line,\n"
"so it is possible to have different options for each image\n"
"\t-w<seconds> : Regular image: "
"Wait time in seconds before next image is shown (default: 1.5).\n"
"\t-t<seconds> : "
"For animations: stop after this time.\n"
"\t-l<loop-count> : "
"For animations: number of loops through a full cycle.\n"
"\t-D<animation-delay-ms> : "
"For animations: override the delay between frames given in the\n"
"\t gif/stream animation with this value. Use -1 to use default value.\n"
"\t-V<vsync-multiple> : For animation (expert): Only do frame vsync-swaps on multiples of refresh (default: 1)\n"
"\t (Tip: use --led-limit-refresh for stable rate)\n"
"\nOptions affecting display of multiple images:\n"
"\t-f : "
"Forever cycle through the list of files on the command line.\n"
"\t-s : If multiple images are given: shuffle.\n"
);
fprintf(stderr, "\nGeneral LED matrix options:\n");
rgb_matrix::PrintMatrixFlags(stderr);
fprintf(stderr,
"\nSwitch time between files: "
"-w for static images; -t/-l for animations\n"
"Animated gifs: If both -l and -t are given, "
"whatever finishes first determines duration.\n");
fprintf(stderr, "\nThe -w, -t and -l options apply to the following images "
"until a new instance of one of these options is seen.\n"
"So you can choose different durations for different images.\n");
return 1;
}
int main(int argc, char *argv[]) {
Magick::InitializeMagick(*argv);
RGBMatrix::Options matrix_options;
rgb_matrix::RuntimeOptions runtime_opt;
// If started with 'sudo': make sure to drop privileges to same user
// we started with, which is the most expected (and allows us to read
// files as that user).
runtime_opt.drop_priv_user = getenv("SUDO_UID");
runtime_opt.drop_priv_group = getenv("SUDO_GID");
if (!rgb_matrix::ParseOptionsFromFlags(&argc, &argv,
&matrix_options, &runtime_opt)) {
return usage(argv[0]);
}
bool do_mmap = false;
bool do_forever = false;
bool do_center = false;
bool do_shuffle = false;
// We remember ImageParams for each image, which will change whenever
// there is a flag modifying them. This map keeps track of filenames
// and their image params (also for unrelated elements of argv[], but doesn't
// matter).
// We map the pointer instad of the string of the argv parameter so that
// we can have two times the same image on the commandline list with different
// parameters.
std::map<const void *, struct ImageParams> filename_params;
// Set defaults.
ImageParams img_param;
for (int i = 0; i < argc; ++i) {
filename_params[argv[i]] = img_param;
}
const char *stream_output = NULL;
int opt;
while ((opt = getopt(argc, argv, "w:t:l:fr:c:P:LhCR:sO:V:D:m")) != -1) {
switch (opt) {
case 'w':
img_param.wait_ms = roundf(atof(optarg) * 1000.0f);
break;
case 't':
img_param.anim_duration_ms = roundf(atof(optarg) * 1000.0f);
break;
case 'l':
img_param.loops = atoi(optarg);
break;
case 'D':
img_param.anim_delay_ms = atoi(optarg);
break;
case 'm':
do_mmap = true;
break;
case 'f':
do_forever = true;
break;
case 'C':
do_center = true;
break;
case 's':
do_shuffle = true;
break;
case 'r':
fprintf(stderr, "Instead of deprecated -r, use --led-rows=%s instead.\n",
optarg);
matrix_options.rows = atoi(optarg);
break;
case 'c':
fprintf(stderr, "Instead of deprecated -c, use --led-chain=%s instead.\n",
optarg);
matrix_options.chain_length = atoi(optarg);
break;
case 'P':
matrix_options.parallel = atoi(optarg);
break;
case 'L':
fprintf(stderr, "-L is deprecated. Use\n\t--led-pixel-mapper=\"U-mapper\" --led-chain=4\ninstead.\n");
return 1;
break;
case 'R':
fprintf(stderr, "-R is deprecated. "
"Use --led-pixel-mapper=\"Rotate:%s\" instead.\n", optarg);
return 1;
break;
case 'O':
stream_output = strdup(optarg);
break;
case 'V':
img_param.vsync_multiple = atoi(optarg);
if (img_param.vsync_multiple < 1) img_param.vsync_multiple = 1;
break;
case 'h':
default:
return usage(argv[0]);
}
// Starting from the current file, set all the remaining files to
// the latest change.
for (int i = optind; i < argc; ++i) {
filename_params[argv[i]] = img_param;
}
}
const int filename_count = argc - optind;
if (filename_count == 0) {
fprintf(stderr, "Expected image filename.\n");
return usage(argv[0]);
}
// Prepare matrix
runtime_opt.do_gpio_init = (stream_output == NULL);
RGBMatrix *matrix = RGBMatrix::CreateFromOptions(matrix_options, runtime_opt);
if (matrix == NULL)
return 1;
FrameCanvas *offscreen_canvas = matrix->CreateFrameCanvas();
printf("Size: %dx%d. Hardware gpio mapping: %s\n",
matrix->width(), matrix->height(), matrix_options.hardware_mapping);
// These parameters are needed once we do scrolling.
const bool fill_width = false;
const bool fill_height = false;
// In case the output to stream is requested, set up the stream object.
rgb_matrix::StreamIO *stream_io = NULL;
rgb_matrix::StreamWriter *global_stream_writer = NULL;
if (stream_output) {
int fd = open(stream_output, O_CREAT|O_WRONLY, 0644);
if (fd < 0) {
perror("Couldn't open output stream");
return 1;
}
stream_io = new rgb_matrix::FileStreamIO(fd);
global_stream_writer = new rgb_matrix::StreamWriter(stream_io);
}
const tmillis_t start_load = GetTimeInMillis();
fprintf(stderr, "Loading %d files...\n", argc - optind);
// Preparing all the images beforehand as the Pi might be too slow to
// be quickly switching between these. So preprocess.
std::vector<FileInfo*> file_imgs;
for (int imgarg = optind; imgarg < argc; ++imgarg) {
const char *filename = argv[imgarg];
FileInfo *file_info = NULL;
std::string err_msg;
std::vector<Magick::Image> image_sequence;
if (LoadImageAndScale(filename, matrix->width(), matrix->height(),
fill_width, fill_height, &image_sequence, &err_msg)) {
file_info = new FileInfo();
file_info->params = filename_params[filename];
file_info->content_stream = new rgb_matrix::MemStreamIO();
file_info->is_multi_frame = image_sequence.size() > 1;
rgb_matrix::StreamWriter out(file_info->content_stream);
for (size_t i = 0; i < image_sequence.size(); ++i) {
const Magick::Image &img = image_sequence[i];
int64_t delay_time_us;
if (file_info->is_multi_frame) {
delay_time_us = img.animationDelay() * 10000; // unit in 1/100s
} else {
delay_time_us = file_info->params.wait_ms * 1000; // single image.
}
if (delay_time_us <= 0) delay_time_us = 100 * 1000; // 1/10sec
StoreInStream(img, delay_time_us, do_center, offscreen_canvas,
global_stream_writer ? global_stream_writer : &out);
}
} else {
// Ok, not an image. Let's see if it is one of our streams.
int fd = open(filename, O_RDONLY);
if (fd >= 0) {
file_info = new FileInfo();
file_info->params = filename_params[filename];
if (do_mmap) {
rgb_matrix::MemMapViewInput *stream_input =
new rgb_matrix::MemMapViewInput(fd);
if (stream_input->IsInitialized()) {
file_info->content_stream = stream_input;
} else {
delete stream_input;
}
}
if (!file_info->content_stream) {
file_info->content_stream = new rgb_matrix::FileStreamIO(fd);
}
StreamReader reader(file_info->content_stream);
if (reader.GetNext(offscreen_canvas, NULL)) { // header+size ok
file_info->is_multi_frame = reader.GetNext(offscreen_canvas, NULL);
reader.Rewind();
if (global_stream_writer) {
CopyStream(&reader, global_stream_writer, offscreen_canvas);
}
} else {
err_msg += "; Can't read as image or compatible stream";
delete file_info->content_stream;
delete file_info;
file_info = NULL;
}
}
else {
perror("Opening file");
}
}
if (file_info) {
file_imgs.push_back(file_info);
} else {
fprintf(stderr, "%s skipped: Unable to open (%s)\n",
filename, err_msg.c_str());
}
}
if (stream_output) {
delete global_stream_writer;
delete stream_io;
if (file_imgs.size()) {
fprintf(stderr, "Done: Output to stream %s; "
"this can now be opened with led-image-viewer with the exact same panel configuration settings such as rows, chain, parallel and hardware-mapping\n", stream_output);
}
if (do_shuffle)
fprintf(stderr, "Note: -s (shuffle) does not have an effect when generating streams.\n");
if (do_forever)
fprintf(stderr, "Note: -f (forever) does not have an effect when generating streams.\n");
// Done, no actual output to matrix.
return 0;
}
// Some parameter sanity adjustments.
if (file_imgs.empty()) {
// e.g. if all files could not be interpreted as image.
fprintf(stderr, "No image could be loaded.\n");
return 1;
} else if (file_imgs.size() == 1) {
// Single image: show forever.
file_imgs[0]->params.wait_ms = distant_future;
} else {
for (size_t i = 0; i < file_imgs.size(); ++i) {
ImageParams ¶ms = file_imgs[i]->params;
// Forever animation ? Set to loop only once, otherwise that animation
// would just run forever, stopping all the images after it.
if (params.loops < 0 && params.anim_duration_ms == distant_future) {
params.loops = 1;
}
}
}
fprintf(stderr, "Loading took %.3fs; now: Display.\n",
(GetTimeInMillis() - start_load) / 1000.0);
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
do {
if (do_shuffle) {
std::random_shuffle(file_imgs.begin(), file_imgs.end());
}
for (size_t i = 0; i < file_imgs.size() && !interrupt_received; ++i) {
DisplayAnimation(file_imgs[i], matrix, offscreen_canvas);
}
} while (do_forever && !interrupt_received);
if (interrupt_received) {
fprintf(stderr, "Caught signal. Exiting.\n");
}
// Animation finished. Shut down the RGB matrix.
matrix->Clear();
delete matrix;
// Leaking the FileInfos, but don't care at program end.
return 0;
}