forked from scottyallen/flaschen-taschen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send-image.cc
301 lines (264 loc) · 9.98 KB
/
send-image.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
// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
// Copyright (C) 2016 Henner Zeller <h.zeller@acm.org>
//
// 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 aptitude install libgraphicsmagick++-dev
#include <assert.h>
#include <math.h>
#include <netdb.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <Magick++.h>
#include <magick/image.h>
#include <vector>
#include "udp-flaschen-taschen.h"
volatile bool interrupt_received = false;
static void InterruptHandler(int signo) {
interrupt_received = true;
}
namespace {
::Color ConvertColor(const Magick::Color &c, float brightness_factor) {
return ::Color(brightness_factor * ScaleQuantumToChar(c.redQuantum()),
brightness_factor * ScaleQuantumToChar(c.greenQuantum()),
brightness_factor * ScaleQuantumToChar(c.blueQuantum()));
}
void CopyImage(const Magick::Image &img, float brightness_factor,
FlaschenTaschen *result) {
assert(result->width() >= (int) img.columns()
&& result->height() >= (int) img.rows());
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)
continue;
result->SetPixel(x, y, ConvertColor(c, brightness_factor));
}
}
}
// Frame already prepared as the buffer to be sent over so that animation
// and re-sizing operations don't have to be done online. Also knows about the
// animation delay.
class PreprocessedFrame {
public:
PreprocessedFrame(const Magick::Image &img, float brightness_factor,
const UDPFlaschenTaschen &display)
: content_(display.Clone()) {
int delay_time = img.animationDelay(); // in 1/100s of a second.
if (delay_time < 1) delay_time = 1;
delay_micros_ = delay_time * 10000;
CopyImage(img, brightness_factor, content_);
}
~PreprocessedFrame() { delete content_; }
void Send() { content_->Send(); }
int delay_micros() const { return delay_micros_; }
private:
UDPFlaschenTaschen *content_;
int delay_micros_;
};
} // end anonymous namespace
// 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 fit_height,
std::vector<Magick::Image> *result) {
std::vector<Magick::Image> frames;
fprintf(stderr, "Read image...\n");
readImages(&frames, filename);
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) {
fprintf(stderr, "Assembling animation with %d frames.\n",
(int)frames.size());
Magick::coalesceImages(result, frames.begin(), frames.end());
} else {
result->push_back(frames[0]); // just a single still image.
}
const int img_width = (int)(*result)[0].columns();
const int img_height = (int)(*result)[0].rows();
if (fit_height) {
// In case of scrolling, we only want to scale down to height
target_width = (int) (1.0*img_width * target_height / img_height);
}
fprintf(stderr, "Scale ... %dx%d -> %dx%d\n",
img_width, img_height, target_width, target_height);
for (size_t i = 0; i < result->size(); ++i) {
(*result)[i].scale(Magick::Geometry(target_width, target_height));
}
return true;
}
void DisplayAnimation(const std::vector<Magick::Image> &image_sequence,
float bright,
const UDPFlaschenTaschen &display) {
std::vector<PreprocessedFrame*> frames;
// Convert to preprocessed frames.
for (size_t i = 0; i < image_sequence.size(); ++i) {
frames.push_back(new PreprocessedFrame(image_sequence[i], bright,
display));
}
for (unsigned int i = 0; !interrupt_received; ++i) {
if (i == frames.size()) i = 0;
PreprocessedFrame *frame = frames[i];
frame->Send(); // Simple. just send it.
if (frames.size() == 1) {
return; // We are done.
} else {
usleep(frame->delay_micros());
}
}
// Leaking PreprocessedFrames. Don't care.
}
void DisplayScrolling(const Magick::Image &img, int scroll_delay_ms,
float brightness_factor,
UDPFlaschenTaschen *display) {
// Create a copy from which it is faster to extract relevant content.
UDPFlaschenTaschen copy(-1, img.columns(), img.rows());
CopyImage(img, brightness_factor, ©);
while (!interrupt_received) {
for (int start = 0; start < copy.width(); ++start) {
if (interrupt_received) break;
for (int y = 0; y < display->height(); ++y) {
for (int x = 0; x < display->width(); ++x) {
display->SetPixel(x, y, copy.GetPixel(x + start, y));
}
}
display->Send();
usleep(scroll_delay_ms * 1000);
}
}
}
static int usage(const char *progname) {
fprintf(stderr, "usage: %s [options] <image>\n", progname);
fprintf(stderr, "Options:\n"
"\t-g <width>x<height>[+<off_x>+<off_y>[+<layer>]] : Output geometry. Default 25x20+0+0+0\n"
"\t-l <layer> : Layer 0..15. Default 0 (note if also given in -g, then last counts)\n"
"\t-h <host> : Flaschen-Taschen display hostname.\n"
"\t-s[<ms>] : Scroll horizontally (optionally: delay ms; default 60).\n"
"\t-b<brighness%%> : Brightness percent (default 100)\n"
"\t-C : Just clear given area and exit.\n");
return 1;
}
int main(int argc, char *argv[]) {
Magick::InitializeMagick(*argv);
bool do_scroll = false;
bool do_clear_screen = false;
int width = 45;
int height = 35;
int off_x = 0;
int off_y = 0;
int off_z = 0;
int scroll_delay_ms = 50;
int brighness_percent = 100;
const char *host = NULL;
int opt;
while ((opt = getopt(argc, argv, "g:h:s::Cl:b:")) != -1) {
switch (opt) {
case 'g':
if (sscanf(optarg, "%dx%d%d%d%d", &width, &height, &off_x, &off_y, &off_z)
< 2) {
fprintf(stderr, "Invalid size spec '%s'", optarg);
return usage(argv[0]);
}
break;
case 'h':
host = strdup(optarg); // leaking. Ignore.
break;
case 'l':
if (sscanf(optarg, "%d", &off_z) != 1 || off_z < 0 || off_z >= 16) {
fprintf(stderr, "Invalid layer '%s'\n", optarg);
return usage(argv[0]);
}
break;
case 's':
do_scroll = true;
if (optarg != NULL) {
scroll_delay_ms = atoi(optarg);
if (scroll_delay_ms < 5) scroll_delay_ms = 5;
if (scroll_delay_ms > 999) scroll_delay_ms = 999;
}
break;
case 'b':
brighness_percent = atoi(optarg);
break;
case 'C':
do_clear_screen = true;
break;
default:
return usage(argv[0]);
}
}
if (width < 1 || height < 1) {
fprintf(stderr, "%dx%d is a rather unusual size\n", width, height);
return usage(argv[0]);
}
if (brighness_percent < 0) brighness_percent = 0;
if (brighness_percent > 100) brighness_percent = 100;
if (!do_clear_screen && optind >= argc) {
fprintf(stderr, "Expected image filename.\n");
return usage(argv[0]);
}
int fd = OpenFlaschenTaschenSocket(host);
if (fd < 0) {
fprintf(stderr, "Cannot connect.\n");
return 1;
}
UDPFlaschenTaschen display(fd, width, height);
display.SetOffset(off_x, off_y, off_z);
if (do_clear_screen) {
display.Send();
fprintf(stderr, "Cleared screen area %dx%d%+d%+d%+d.\n",
width, height, off_x, off_y, off_z);
if (optind < argc) {
fprintf(stderr, "FYI: You also supplied filename %s. "
"Ignoring that for clear-screen operation.\n",
argv[optind]);
}
return 0;
}
const char *filename = argv[optind];
std::vector<Magick::Image> frames;
if (!LoadImageAndScale(filename, width, height, do_scroll, &frames)) {
return 1;
}
if (do_scroll && frames.size() > 1) {
fprintf(stderr, "This is an animated image format, "
"scrolling does not make sense\n");
return 1;
}
signal(SIGTERM, InterruptHandler);
signal(SIGINT, InterruptHandler);
const float bright = brighness_percent / 100.0f;
if (do_scroll) {
DisplayScrolling(frames[0], scroll_delay_ms, bright, &display);
} else {
DisplayAnimation(frames, bright, display);
}
// Don't let leftovers cover up content.
if (interrupt_received && off_z > 0) {
display.Clear();
display.Send();
}
close(fd);
fprintf(stderr, "Exit.\n");
return 0;
}