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
|
package XXV::MODULES::GRAB;
use strict;
use Tools;
# This module method must exist for XXV
# ------------------
sub module {
# ------------------
my $self = shift || return error('No object defined!');
my $args = {
Name => 'GRAB',
Prereq => {
'GD' => 'image manipulation routines'
,'Template' => 'Front-end module to the Template Toolkit '
,'MIME::Base64' => 'Encoding and decoding of base64 strings'
,'Font::TTF::Font' => 'String table for a TTF font'
},
Description => gettext('This module grab a picture from video output.'),
Preferences => {
xsize => {
description => gettext('Image width'),
default => 320,
type => 'integer',
required => gettext('This is required!'),
check => sub{
my $value = shift || 0;
if($value =~ /^\d+$/sig and $value >= 8 and $value < 4096) {
return int($value);
} else {
return undef, gettext('Value incorrect!');
}
},
},
ysize => {
description => gettext('Image height'),
default => 240,
type => 'integer',
required => gettext('This is required!'),
check => sub{
my $value = shift || 0;
if($value =~ /^\d+$/sig and $value >= 8 and $value < 4096) {
return int($value);
} else {
return undef, gettext('Value incorrect!');
}
}
},
overlay => {
description => gettext('Text to display in the grabbed picture.'),
default => "<< event.POS >>.<< event.Channel >>\|<< event.Title >> << event.Subtitle >>",
type => 'string',
check => sub{
my $value = shift;
$value = join('|',(split(/[\r\n]/, $value)));
return $value;
},
},
vpos => {
description => gettext('Vertical position of displayed text, in pixels.'),
default => 10,
type => 'integer',
check => sub{
my $value = shift || 0;
if($value =~ /^\d+$/sig and $value >= 8 and $value < 4096) {
return int($value);
} else {
return undef, gettext('Value incorrect!');
}
}
},
font => {
description => gettext('TrueType font to draw overlay text'),
default => 'VeraIt.ttf',
type => 'list',
choices => Tools::findttf($self->{paths}->{FONTPATH})
},
imgfontsize => {
description => gettext('Font size to draw image text (only for ttf font!).'),
default => 10,
type => 'integer',
check => sub{
my $value = shift || 0;
if($value =~ /^\d+$/sig and $value >= 1 and $value < 100) {
return int($value);
} else {
return undef, gettext('Value incorrect!');
}
}
},
imgquality => {
description => gettext('Quality from image in percent.'),
default => 80,
type => 'integer',
check => sub{
my $value = shift || 0;
if($value =~ /^\d+$/sig and $value >= 1 and $value < 100) {
return int($value);
} else {
return undef, gettext('Value incorrect!');
}
}
},
},
Commands => {
gdisplay => {
hidden => 'yes',
short => 'gd',
callback => sub{ $self->display(@_) },
Level => 'user',
DenyClass => 'remote',
binary => 'nocache'
},
},
};
return $args;
}
# ------------------
sub new {
# ------------------
my($class, %attr) = @_;
my $self = {};
bless($self, $class);
$self->{charset} = delete $attr{'-charset'};
if($self->{charset} eq 'UTF-8'){
eval 'use utf8';
}
# paths
$self->{paths} = delete $attr{'-paths'};
# who am I
$self->{MOD} = $self->module;
# all configvalues to $self without parents (important for ConfigModule)
map {
$self->{$_} = $attr{'-config'}->{$self->{MOD}->{Name}}->{$_};
$self->{$_} = $self->{MOD}->{Preferences}->{$_}->{default} unless($self->{$_});
} keys %{$self->{MOD}->{Preferences}};
# Try to use the Requirments
map {
eval "use $_";
if($@) {
my $m = (split(/ /, $_))[0];
if($m eq 'Template') {
error("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
$self->{DisableTemplate} = 1;
} elsif($m eq 'GD') {
error("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
$self->{DisableGD} = 1;
} else {
return panic("\nCouldn't load perl module: $m\nPlease install this module on your system:\nperl -MCPAN -e 'install $m'");
}
}
} keys %{$self->{MOD}->{Prereq}};
unless($self->{DisableTemplate}) {
# create Template object
$self->{tt} = Template->new(
START_TAG => '\<\<', # Tagstyle
END_TAG => '\>\>', # Tagstyle
INTERPOLATE => 1, # expand "$var" in plain text
PRE_CHOMP => 0, # cleanup whitespace
EVAL_PERL => 0, # evaluate Perl code blocks
);
}
$self->_init or return error('Problem to initialize modul!');
return $self;
}
# ------------------
sub _init {
# ------------------
my $self = shift || return error('No object defined!');
main::after(sub{
$self->{svdrp} = main::getModule('SVDRP');
unless($self->{svdrp}) {
panic ("Couldn't get modul SVDRP");
return 0;
}
return 1;
}, "GRAB: init modul ...");
return 1;
}
# ------------------
sub _grab {
# ------------------
my $self = shift || return error('No object defined!');
my $width = shift || $self->{xsize};
my $height = shift || $self->{ysize};
my $vid = shift;
# command for get inline data (JPEG BASE64 coded)
my $cmd = sprintf('grab - %d %d %d', $self->{imgquality}, $width, $height);
my ($data,$error) = $self->{svdrp}->command($cmd, $vid);
my $binary;
unless($error) {
foreach my $l (@{$data}) {
if($l =~ /^216-/sg) {
$l =~ s/^216-//g;
$binary .= decode_base64($l);
}
}
}
unless($self->{DisableGD}) {
# create noised image as failback.
$binary = $self->_noise($width,$height)
unless($binary);
}
if(!$self->{DisableGD} && !$self->{DisableTemplate}
&& $self->{overlay} && length($self->{overlay})
&& $data && $binary) {
# Make overlay on image
$binary = $self->makeImgText($binary, $self->{overlay}, $height, $vid)
}
return $binary;
}
# ------------------
sub display {
# ------------------
my $self = shift || return error('No object defined!');
my $console = shift || return error('No console defined!');
my $config = shift || return error('No config defined!');
my $data = shift;
my $params = shift;
return $console->err(gettext("Sorry, get image is'nt supported"))
if ($console->{TYP} ne 'HTML');
my $width = $console->{cgi} && $console->{cgi}->param('width')
? $console->{cgi}->param('width')
: $self->{xsize};
unless($width =~ /^\d+$/sig and $width >= 8 and $width < 4096) {
error sprintf("Image width incorrect! : %s", $width );
return $console->err(gettext('Value incorrect!'));
}
my $height = $console->{cgi} && $console->{cgi}->param('height')
? $console->{cgi}->param('height')
: $self->{ysize};
unless($height =~ /^\d+$/sig and $height >= 8 and $height < 4096) {
error sprintf("Image height incorrect! : %s", $height );
return $console->err(gettext('Value incorrect!'));
}
my $binary = $self->_grab($width,$height, $params && $params->{vdr} ? $params->{vdr} : undef);
if($binary && length($binary)) {
$console->{nocache} = 1;
$console->{nopack} = 1;
my %args = ();
$args{'Last-Modified'} = datum(time,'header');
$args{'attachment'} = 'grab.jpg';
$args{'Content-Length'} = length($binary);
return $console->out($binary, 'image/jpeg', %args );
} else {
return $console->statusmsg(500,"Couldn't load image data !",gettext("Failed"));
}
}
# ------------------
sub makeImgText {
# ------------------
my $self = shift || return error('No object defined!');
my $binary = shift || return error ('No data to create overlay defined!');
my $text = shift || return error ('No text to display defined!');
my $height = shift || $self->{ysize};
my $vid = shift;
my $image = GD::Image->newFromJpegData($binary);
unless($image && $image->width > 8 && $image->height > 8) {
return error("Data has'nt jpeg data or jpeg data contains errors!");
}
my $color = $image->colorClosest(255,255,255);
my $shadow = $image->colorClosest(0,0,0);
my $event = main::getModule('EPG')->NowOnChannel(undef,undef,undef,$vid);
# Hier sollten noch mehr Informationen dazu kommen
my $vars = {
event => $event,
};
my $output = '';
$self->{tt}->process(\$text, $vars, \$output)
or return error(sprintf("Can't parse grab overlay template : %s", $self->{tt}->error()));
my $vpos = CORE::int(($height / $self->{ysize}) * $self->{vpos});
my $imgfontsize = CORE::int(($height / $self->{ysize}) * $self->{imgfontsize});
#lg sprintf("height: %d vpos: %d imgfontsize: %d",$height,$vpos,$imgfontsize);
my $font = sprintf("%s/%s",$self->{paths}->{FONTPATH},$self->{font});
if($self->{paths}->{FONTPATH} and $self->{font} and -r $font) {
my $h = ($imgfontsize + 2);
$h *= -1 if($vpos > ($height / 2));
my $offset = $imgfontsize/2;
foreach my $zeile (split(/\|/, $output)) {
$image->stringFT($shadow,$font,$imgfontsize,0,11,($vpos-1)+$offset,$zeile);
$image->stringFT($color,$font,$imgfontsize,0,10,($vpos)+$offset,$zeile);
$offset += $h;
}
} else {
my $h = ($imgfontsize + 2);
$h *= -1 if($vpos > ($height / 2));
my $offset = 0;
foreach my $zeile (split(/\|/, $output)) {
$image->string(&gdGiantFont,11, ($vpos-1) + $offset,$zeile,$shadow); # Schatten
$image->string(&gdGiantFont,10, ($vpos) + $offset,$zeile,$color); # Text
$offset += $h;
}
}
my $img_data = $image->jpeg($self->{imgquality});
return $img_data;
}
sub _noise {
my $self = shift || return error('No object defined!');
my $width = shift || $self->{xsize};
my $height = shift || $self->{ysize};
my $image = GD::Image->new($width, $height,1);
my $colors;
push( @{$colors}, $image->colorClosest(255,255,255));
push( @{$colors}, $image->colorClosest(128,128,128));
push( @{$colors}, $image->colorClosest(0,0,0));
$self->_noise_rect($image,0,0,$width,$height,$colors);
my $img_data = $image->jpeg($self->{imgquality});
return $img_data;
}
sub _noise_rect {
my $self = shift || return error('No object defined!');
my $image = shift;
my $x1 = shift; my $y1 = shift;
my $x2 = shift; my $y2 = shift;
my $colors_ref = shift;
my $colorcount = scalar @{$colors_ref};
return if $x2 <= $x1; # refuse to create a zero- or negative-size box
return if $y2 <= $y1;
for (my $x = $x1; $x < $x2; ++$x) {
for (my $y = $y1; $y < $y2; ++$y) {
$image->setPixel($x, $y,$colors_ref->[CORE::int(rand($colorcount))]);
}
}
return;
}
1;
|