summaryrefslogtreecommitdiff
path: root/tools/roi-visualize.php
diff options
context:
space:
mode:
authorEvgeny Zinoviev <me@ch1p.io>2022-06-07 19:57:12 +0300
committerEvgeny Zinoviev <me@ch1p.io>2022-06-07 19:57:12 +0300
commit70a622e04bb1479865f7383abda308a101ef2675 (patch)
tree1cf7cd731b4a0968eacba25c3ddf7483dcdb3b74 /tools/roi-visualize.php
parent49106dd29d2c974a7373a3cf4230f733feecbb02 (diff)
tools/roi-visualize: upd
Diffstat (limited to 'tools/roi-visualize.php')
-rwxr-xr-xtools/roi-visualize.php94
1 files changed, 49 insertions, 45 deletions
diff --git a/tools/roi-visualize.php b/tools/roi-visualize.php
index 9677303..6506730 100755
--- a/tools/roi-visualize.php
+++ b/tools/roi-visualize.php
@@ -16,7 +16,7 @@ function parse_roi_input(string $file): array {
$lines = array_map(fn($line) => array_map('intval', explode(' ', $line)), $lines);
foreach ($lines as $points) {
if (count($points) != 4)
- throw new Exception("invalid line: ".implode(' ', $points));
+ throw new Exception(__METHOD__.": invalid line: ".implode(' ', $points));
}
return $lines;
@@ -57,58 +57,62 @@ $colors = [
if ($argc < 2)
fatal("usage: {$argv[0]} --roi FILE --input PATH --output PATH\n");
-array_shift($argv);
-while (count($argv) > 0) {
- switch ($argv[0]) {
- case '--roi':
- array_shift($argv);
- $roi_file = array_shift($argv);
- break;
-
- case '--input':
- array_shift($argv);
- $input = array_shift($argv);
- break;
-
- case '--output':
- array_shift($argv);
- $output = array_shift($argv);
- break;
-
- default:
- fatal('unsupported argument: '.$argv[0]);
+try {
+ array_shift($argv);
+ while (count($argv) > 0) {
+ switch ($argv[0]) {
+ case '--roi':
+ array_shift($argv);
+ $roi_file = array_shift($argv);
+ break;
+
+ case '--input':
+ array_shift($argv);
+ $input = array_shift($argv);
+ break;
+
+ case '--output':
+ array_shift($argv);
+ $output = array_shift($argv);
+ break;
+
+ default:
+ throw new Exception("unsupported argument: {$argv[0]}");
+ }
}
-}
-if (!$roi_file)
- throw new Exception("--roi is not specified");
+ if (!$roi_file)
+ throw new Exception("--roi is not specified");
-if (!$input)
- throw new Exception('--input is not specified');
+ if (!$input)
+ throw new Exception('--input is not specified');
-if (!$output)
- throw new Exception('--output is not specified');
+ if (!$output)
+ throw new Exception('--output is not specified');
-$regions = parse_roi_input($roi_file);
-$img = imageopen($input);
-if (!$img)
- throw new Exception("failed to open image");
+ $regions = parse_roi_input($roi_file);
+ $img = imageopen($input);
+ if (!$img)
+ throw new Exception("failed to open image");
-$imgw = imagesx($img);
-$imgh = imagesy($img);
+ $imgw = imagesx($img);
+ $imgh = imagesy($img);
-foreach ($regions as $i => $region) {
- list($r, $g, $b) = hex2rgb($colors[$i]);
+ foreach ($regions as $i => $region) {
+ list($r, $g, $b) = hex2rgb($colors[$i]);
- if ($region[0]+$region[2] > $imgw || $region[1]+$region[3] > $imgh)
- throw new Exception('error: invalid region (line '.($i+1).')');
+ if ($region[0]+$region[2] > $imgw || $region[1]+$region[3] > $imgh)
+ throw new Exception('error: invalid region (line '.($i+1).')');
- $col = imagecolorallocatealpha($img, $r, $g, $b, 50);
- imagerectangle($img, $region[0], $region[1], $region[0]+$region[2], $region[1]+$region[3], $col);
+ $col = imagecolorallocatealpha($img, $r, $g, $b, 50);
+ imagerectangle($img, $region[0], $region[1], $region[0]+$region[2], $region[1]+$region[3], $col);
- $col = imagecolorallocatealpha($img, $r, $g, $b, 90);
- imagefilledrectangle($img, $region[0]+1, $region[1]+1, $region[0]+$region[2]-2, $region[1]+$region[3]-2, $col);
-}
+ $col = imagecolorallocatealpha($img, $r, $g, $b, 90);
+ imagefilledrectangle($img, $region[0]+1, $region[1]+1, $region[0]+$region[2]-2, $region[1]+$region[3]-2, $col);
+ }
-imagejpeg($img, $output, 97);
-echo "saved to $output\n";
+ imagejpeg($img, $output, 97);
+ echo "saved to $output\n";
+} catch (Exception $e) {
+ fatal($e->getMessage()."\n");
+} \ No newline at end of file