Quiq/ImageMagick.pm

filter: Statementstmt 37.9% 41 / 108 Branchbran 37.5% 9 / 24 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 64.7% 11 / 17 Podpod 100% 10 / 10 totaltotal 42.6% 72 / 169 CC 15 SCAR 18.6
CC
15
Cov
37%
resizeFill
20.0
morph
12.0
addCommand
6.0
CRAP 6.4
« Quiq/Exit.pm ↑ Summary Quiq/Array.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::ImageMagick - Konstruiere eine ImageMagick-Kommandozeile
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 ImageMagick Online-Dokumentation:
16 L<http://www.imagemagick.org/Usage/>
17  
18 Ein Objekt der Klasse repräsentiert eine
19 ImageMagick-Kommandozeile. Die Klasse verfügt einerseits über
20 I<elementare> (Objekt-)Methoden, um eine solche Kommandozeile
21 sukzessive aus ihren elementaren Bestandteilen konstruieren zu
22 können und I<höhere> (Klassen-)Methoden, die eine bestimmte
23 Funktion durch das Hinzufügen einer Reihe von Optionen
24 realisieren.
25  
26 =cut
27  
28 # -----------------------------------------------------------------------------
29  
30 package Quiq::ImageMagick;
31 2 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 2 our $VERSION = '1.240';
38  
39 2 ▶use Quiq::Shell;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use Quiq::File::Image;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use Quiq::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # -----------------------------------------------------------------------------
44  
45 =head1 METHODS
46  
47 =head2 Konstruktor
48  
49 =head3 new() - Konstruktor
50  
51 =head4 Synopsis
52  
53 $cmd = $class->new;
54  
55 =head4 Description
56  
57 Instantiiere ein ImageMagick-Kommandozeilen-Objekt und liefere
58 eine Referenz auf dieses Objekt zurück.
59  
60 =cut
61  
62 # -----------------------------------------------------------------------------
63  
64 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
65 5 my $class = shift;
66  
67 5 return $class->SUPER::new(
68 cmd => '',
69 );
70 }
71
72  
73 # -----------------------------------------------------------------------------
74  
75 =head2 Kommando konstruieren
76  
77 =head3 addElement() - Füge ein Kommandozeilen-Element hinzu
78  
79 =head4 Synopsis
80  
81 $cmd->addElement($str);
82  
83 =head4 Description
84  
85 Ergänze die Kommandozeile um Kommandozeilen-Element $str.
86 Ein Kommandozeilen-Element ist ein durch Whiltespace getrennter
87 elementarer Teil der Kommandozeile, wie z.B. das Kommando, eine Option,
88 ein Optionsargument, ein Dateiname usw.
89  
90 Enthält $str Whitespace oder andere, spezielle Zeichen, wird
91 $str in einfache Anführungsstriche eingefasst.
92  
93 =head4 Examples
94  
95 Ohne Whitespace:
96  
97 $cmd->addElement('input.jpg');
98 =>
99 input.gif
100  
101 Mit Whitespace:
102  
103 $cmd->addElement('Sonne am Abend.jpg');
104 =>
105 'Sonne am Abend.jpg'
106  
107 =cut
108  
109 # -----------------------------------------------------------------------------
110  
111 5 ▶sub addElement {
Subroutine addElement: called CC=3 SCAR=11.0
Pod documented
112 5 my ($self,$arg) = @_;
113  
114 5 my $ref = $self->getRef('cmd');
115 5 ▶ if ($$ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref) 1 4 5
116 1 $$ref .= ' ';
117 }
118 5 ▶ if ($arg =~ /[\s!#]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /[\s!#]/) { } 1 4 5
119 1 $$ref .= "'$arg'";
120 }
121 else {
122 4 $$ref .= $arg;
123 }
124
125 5 return;
126 }
127  
128 # -----------------------------------------------------------------------------
129  
130 =head3 addCommand() - Füge Kommando hinzu
131  
132 =head4 Synopsis
133  
134 $cmd->addCommand($command);
135  
136 =head4 Description
137  
138 Ergänze die Kommandozeile am Anfang um das Kommando $command.
139 Die Methode liefert keinen Wert zurück.
140  
141 =head4 Examples
142  
143 Kommando convert:
144  
145 $cmd->addCommand('convert');
146 =>
147 convert
148  
149 =cut
150  
151 # -----------------------------------------------------------------------------
152  
153 0 ▶sub addCommand {
Subroutine addCommand: not called CC=2 SCAR=17.9
Pod documented
154 0 my ($self,$command) = @_;
155
156 0 my $ref = $self->getRef('cmd');
157 0 ▶ $$ref = $$ref? "$command $$ref": $command;
Branch: 1 branch
BranchTrueFalseTotal
$$ref ? : 0 0 0
158  
159 0 return;
160 }
161  
162 # -----------------------------------------------------------------------------
163  
164 =head3 addOption() - Füge Option hinzu
165  
166 =head4 Synopsis
167  
168 $cmd->addOption($opt);
169 $cmd->addOption($opt=>$val);
170  
171 =head4 Description
172  
173 Ergänze die Kommandozeile um die Option $opt und (optional) den
174 Wert $val. Die Methode liefert keinen Wert zurück.
175  
176 =head4 Examples
177  
178 Option ohne Wert:
179  
180 $cmd->addOption('-negate');
181 =>
182 -negate
183  
184 Option mit Wert:
185  
186 $cmd->addOption(-rotate=>90);
187 =>
188 -rotate 90
189  
190 =cut
191  
192 # -----------------------------------------------------------------------------
193  
194 2 ▶sub addOption {
Subroutine addOption: called CC=5 SCAR=16.6
Pod documented
195 2 my $self = shift;
196 2 my $opt = shift;
197 # @_: $val
198  
199 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
200 1 ▶ if (!defined($_[0]) || $_[0] eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $_[0] or $_[0] eq "") 0 1 1
Condition33%1/3
not defined $_[0] or $_[0] eq ""
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $_[0] or $_[0] eq ""
not defined $_[0] $_[0] eq ""
Truth table33%1/3
not(A) or B
A= not defined $_[0]B= $_[0] eq ""
ABresult
1X1
011
000
201 # Option UND Wert weglassen, wenn der Wert undef oder '' ist
202 0 return;
203 }
204 }
205  
206 2 $self->addElement($opt);
207 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 1 1 2
208 # Wert nur hinzufügen, wenn angegeben
209 1 $self->addElement(shift);
210 }
211
212 2 return;
213 }
214  
215 # -----------------------------------------------------------------------------
216  
217 =head2 Kommando
218  
219 =head3 command() - Kommandozeile als Zeichenkette
220  
221 =head4 Synopsis
222  
223 $str = $cmd->command;
224  
225 =head4 Description
226  
227 Liefere das Kommando als Zeichenkette.
228  
229 =cut
230  
231 # -----------------------------------------------------------------------------
232  
233 4 ▶sub command {
Subroutine command: called CC=1 SCAR=0.0
Pod documented
234 4 return shift->{'cmd'};
235 }
236
237  
238 # -----------------------------------------------------------------------------
239  
240 =head2 Kommando-Ausführung
241  
242 =head3 execute() - Führe ImageMagick-Kommandozeile aus
243  
244 =head4 Synopsis
245  
246 $cmd->execute;
247  
248 =head4 Description
249  
250 Führe ImageMagick-Kommando $cmd aus. Im Fehlerfall wird eine
251 Exception geworfen.
252  
253 =cut
254  
255 # -----------------------------------------------------------------------------
256  
257 0 ▶sub execute {
Subroutine execute: not called CC=1 SCAR=6.9
Pod documented
258 0 my $self = shift;
259  
260 0 my $cmd = $self->command;
261 # print "$cmd\n";
262 0 Quiq::Shell->exec($cmd);
263  
264 0 return;
265 }
266  
267 # -----------------------------------------------------------------------------
268  
269 =head2 Klassenmethoden (vollständige Kommandozeilen)
270  
271 =head3 resizeFill() - Generiere Kommando für Operation resizeFill
272  
273 =head4 Synopsis
274  
275 $cmd = $class->resizeFill($input,$output,$size,$background);
276  
277 =head4 Arguments
278  
279 =over 4
280  
281 =item $input
282  
283 Image-Objekt oder Bilddatei-Pfad des Input-Bildes.
284  
285 =item $output
286  
287 Bilddatei-Pfad des Output-Bildes. Das Verzeichnis wird erzeugt
288  
289 =item $size
290  
291 Größe des generierten Output-Bildes.
292  
293 =item $background
294  
295 Farbe des Hintergrunds, wenn das Bild den Bereich $size nicht
296 vollständig ausfüllt.
297  
298 =back
299  
300 =head4 Description
301  
302 Generiere ein convert-Kommando, dass das Input-Bild auf Größe
303 $size bringt.
304  
305 =over 2
306  
307 =item *
308  
309 Ist das Bild in mindestens einer Dimension größer als $size,
310 wird es verkleinert.
311  
312 =item *
313  
314 Andernfalls wird das Bild in seiner Größe nicht verändert.
315  
316 =item *
317  
318 Vom Bild nicht abgedeckte Bereiche werden in Hintergrundfarbe
319 $background dargestellt.
320  
321 =back
322  
323 =cut
324  
325 # -----------------------------------------------------------------------------
326  
327 0 ▶sub resizeFill {
Subroutine resizeFill: not called CC=4 SCAR=30.0
Pod documented
328 0 my $class = shift;
329 0 my $input = shift;
330 0 my $output = shift;
331 0 my $size = shift;
332 0 my $background = shift;
333  
334 # Bild-Objekt der Input-Datei
335 0 ▶ my $img = ref $input? $input: Quiq::File::Image->new($input);
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 0 0 0
336  
337 # Kommando erzeugen
338  
339 0 my $self = $class->new;
340 0 $self->addCommand('convert');
341 0 $self->addElement($img->path);
342 0 my ($width,$height) = split /x/,$size;
343 0 ▶ if ($img->width > $width || $img->height > $height) {
Branch: 1 branch
BranchTrueFalseTotal
if ($img->width > $width or $img->height > $height) 0 0 0
Condition0%0/3
$img->width > $width or $img->height > $height
l!l&&r!l&&!r
000
MC/DC0%0/2
$img->width > $width or $img->height > $height
$img->width > $width $img->height > $height
Truth table0%0/3
A or B
A= $img->width > $widthB= $img->height > $height
ABresult
1X1
011
000
344 # Sonderbehandlung, wenn die Input-Bild verkleinert werden muss
345 0 $self->addOption(-sample=>$size);
346 }
347 0 $self->addOption(-background=>$background);
348 0 $self->addOption(-gravity=>'center');
349 0 $self->addOption(-extent=>$size);
350 0 Quiq::Path->mkdir($output,-createParent=>1); # Erzeuge Verzeichnis
351 0 $self->addElement($output);
352  
353 0 return $self;
354 }
355  
356 # -----------------------------------------------------------------------------
357  
358 =head3 resizeStretch() - Generiere Kommando für Operation resizeStretch
359  
360 =head4 Synopsis
361  
362 $cmd = $class->resizeStretch($input,$output,$size);
363  
364 =head4 Arguments
365  
366 =over 4
367  
368 =item $input
369  
370 Image-Objekt oder Bilddatei-Pfad des Input-Bildes.
371  
372 =item $output
373  
374 Bilddatei-Pfad des Output-Bildes. Das Verzeichnis wird erzeugt
375  
376 =item $size
377  
378 Größe des generierten Output-Bildes.
379  
380 =back
381  
382 =head4 Description
383  
384 Generiere ein convert-Kommando, dass das Input-Bild auf Größe
385 $size bringt.
386  
387 =over 2
388  
389 =item *
390  
391 Weicht das Seitenverhltmis ab, wird das Bild verzerrt.
392  
393 =back
394  
395 =cut
396  
397 # -----------------------------------------------------------------------------
398  
399 0 ▶sub resizeStretch {
Subroutine resizeStretch: not called CC=2 SCAR=17.9
Pod documented
400 0 my $class = shift;
401 0 my $input = shift;
402 0 my $output = shift;
403 0 my $size = shift;
404  
405 # Bild-Objekt der Input-Datei
406 0 ▶ my $img = ref $input? $input: Quiq::File::Image->new($input);
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 0 0 0
407  
408 # Kommando erzeugen
409  
410 0 my $self = $class->new;
411 0 $self->addCommand('convert');
412 0 $self->addElement($img->path);
413 0 $self->addOption(-resize=>"$size!");
414 0 Quiq::Path->mkdir($output,-createParent=>1); # Erzeuge Verzeichnis
415 0 $self->addElement($output);
416  
417 0 return $self;
418 }
419  
420 # -----------------------------------------------------------------------------
421  
422 =head3 negate() - Generiere Kommando für Operation negate
423  
424 =head4 Synopsis
425  
426 $cmd = $class->negate($input,$output);
427  
428 =head4 Arguments
429  
430 =over 4
431  
432 =item $input
433  
434 Image-Objekt oder Bilddatei-Pfad des Input-Bildes.
435  
436 =item $output
437  
438 Bilddatei-Pfad des Output-Bildes. Das Verzeichnis wird erzeugt
439  
440 =back
441  
442 =head4 Description
443  
444 Generiere ein convert-Kommando, dass das Input-Bild negiert.
445  
446 =cut
447  
448 # -----------------------------------------------------------------------------
449  
450 0 ▶sub negate {
Subroutine negate: not called CC=2 SCAR=17.9
Pod documented
451 0 my $class = shift;
452 0 my $input = shift;
453 0 my $output = shift;
454  
455 # Bild-Objekt der Input-Datei
456 0 ▶ my $img = ref $input? $input: Quiq::File::Image->new($input);
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 0 0 0
457  
458 # Kommando erzeugen
459  
460 0 my $self = $class->new;
461 0 $self->addCommand('convert');
462 0 $self->addElement($img->path);
463 0 $self->addOption(-channel=>'green');
464 0 $self->addOption('-negate');
465 0 Quiq::Path->mkdir($output,-createParent=>1); # Erzeuge Verzeichnis
466 0 $self->addElement($output);
467  
468 0 return $self;
469 }
470  
471 # -----------------------------------------------------------------------------
472  
473 =head3 morph() - Generiere Kommando für Operation morph
474  
475 =head4 Synopsis
476  
477 $cmd = $class->morph($input1,$input2,$outPattern,$n);
478  
479 =head4 Arguments
480  
481 =over 4
482  
483 =item $input1
484  
485 Pfad oder Bilddatei-Objekt des ersten Bildes.
486  
487 =item $input2
488  
489 Pfad oder Bilddatei-Objekt des zweiten Bildes.
490  
491 =item $outPattern
492  
493 Pfad-Muster für die erzeugte Bildfolge. Z.B. "$dir/%02d"
494  
495 =item $n
496  
497 Anzahl der erzeugten Zwischenbilder.
498  
499 =back
500  
501 =head4 Description
502  
503 Generiere ein convert-Kommando, das $n Zwischenbilder für die
504 Bilder $input1 und $input2 erzeugt. Unter dem Pfad-Muster
505 $outPattern werden $n+2 Bilder gespeichert: Die $n gemorphten
506 Bilder plus die beiden Ausgangsbilder.
507  
508 Wird als $input1 oder $input2 ein Objekt angegeben, muss dieses
509 eine Methode path() besitzen, die den Pfad zur Bilddatei liefert,
510 wie die Objekte der Klasse Quiq::File::Image.
511  
512 Beim convert-Kommando kann man bei der Morph-Operation zwar auch mehrere
513 Bilddateien angeben, aber dann wird extrem viel Speicher benötigt.
514 Besser ist es, die Bilder paarweise zu morphen und das letzte Bild
515  
516 =head4 Example
517  
518 Erzeuge einen convert-Aufruf für 5 Zwischenbilder (hier an der
519 Kommandozeile):
520  
521 $ perl -MQuiq::ImageMagick -E 'say Quiq::ImageMagick->morph("1.jpg","2.jpg","tmp/%02d.jpg",5)->command'
522  
523 Erzeugtes Kommando:
524  
525 convert 1.jpg 2.jpg -morph 5 tmp/%02d.jpg
526  
527 Wird das Kommando ausgeführt, entstehen in Unterverzeichnis tmp
528 (das vorab existieren muss) sechs Bilder:
529  
530 00.jpg entspricht 1.jpg (die Bilder sind nicht identisch!)
531 01.jpg
532 02.jpg
533 03.jpg
534 04.jpg
535 05.jpg
536 06.jpg entspricht 2.jpg (die Bilder sind nicht identisch!)
537  
538 =cut
539  
540 # -----------------------------------------------------------------------------
541  
542 0 ▶sub morph {
Subroutine morph: not called CC=3 SCAR=24.8
Pod documented
543 0 my $class = shift;
544 0 my $input1 = shift;
545 0 my $input2 = shift;
546 0 my $outPattern = shift;
547 0 my $morph = shift;
548  
549 # Pfade der Bilddateien bestimmen
550  
551 0 ▶ if (ref $input1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input1) 0 0 0
552 0 $input1 = $input1->path;
553 }
554 0 ▶ if (ref $input2) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input2) 0 0 0
555 0 $input2 = $input2->path;
556 }
557  
558 # Kommando erzeugen
559  
560 0 my $self = $class->new;
561 0 $self->addCommand('convert');
562 0 $self->addElement($input1);
563 0 $self->addElement($input2);
564 0 $self->addOption(-morph=>$morph);
565 0 $self->addElement($outPattern);
566  
567 0 return $self;
568 }
569  
570 # -----------------------------------------------------------------------------
571  
572 =head1 VERSION
573  
574 1.240
575  
576 =head1 AUTHOR
577  
578 Frank Seitz, L<http://fseitz.de/>
579  
580 =head1 COPYRIGHT
581  
582 Copyright (C) 2026 Frank Seitz
583  
584 =head1 LICENSE
585  
586 This code is free software; you can redistribute it and/or modify
587 it under the same terms as Perl itself.
588  
589 =cut
590  
591 # -----------------------------------------------------------------------------
592  
593 2 1;
594  
595 # eof
« Quiq/Exit.pm ↑ Summary Quiq/Array.pm »