Repository navigation
Expand file tree
/
Copy pathDriver.php
More file actions
156 lines (132 loc) · 3.68 KB
/
Copy pathDriver.php
File metadata and controls
156 lines (132 loc) · 3.68 KB
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
<?php
declare(strict_types = 1);
namespace Darkroom;
use Closure;
/**
* @psalm-consistent-constructor
*/
abstract class Driver
{
/**
* Rec. 601 luma weights, which GD's grayscale filter uses
*/
protected const array LUMA = [0.299, 0.587, 0.114];
/**
* White images of the formats whose decoders libheif loads as plugins
*/
protected const array SAMPLES = [
'Avif' => <<<'BASE64'
AAAAHGZ0eXBhdmlmAAAAAG1pZjFhdmlmbWlhZgAAANZtZXRhAAAAAAAAACFoZGxyAAAAAAAAAABw
aWN0AAAAAAAAAAAAAAAAAAAAACJpbG9jAAAAAERAAAEAAQAAAAAA+gABAAAAAAAAAB8AAAAjaWlu
ZgAAAAAAAQAAABVpbmZlAgAAAAABAABhdjAxAAAAAA5waXRtAAAAAAABAAAAVmlwcnAAAAA4aXBj
bwAAAAxhdjFDgQAMAAAAABRpc3BlAAAAAAAAAAEAAAABAAAAEHBpeGkAAAAAAwgICAAAABZpcG1h
AAAAAAAAAAEAAQOBAgMAAAAnbWRhdBIACggYAAaICGg0IDIRGUeHhiGAAGgAAJBAyRxgimo=
BASE64,
'Heic' => <<<'BASE64'
AAAAHGZ0eXBoZWljAAAAAG1pZjFoZWljbWlhZgAAAUJtZXRhAAAAAAAAACFoZGxyAAAAAAAAAABw
aWN0AAAAAAAAAAAAAAAAAAAAACJpbG9jAAAAAERAAAEAAQAAAAABZgABAAAAAAAAABYAAAAjaWlu
ZgAAAAAAAQAAABVpbmZlAgAAAAABAABodmMxAAAAAA5waXRtAAAAAAABAAAAwmlwcnAAAACkaXBj
bwAAAHhodmNDAQNwAAAAkAAAAAAAHvAA/P34+AAADwNgAAEAGEABDAH//wNwAAADAJAAAAMAAAMA
HroCQGEAAQArQgEBA3AAAAMAkAAAAwAAAwAeoCCBBZbqSSmubgIaDAgAAAMAyAAAAwAIQGIAAQAH
RAHBcrAiQAAAABRpc3BlAAAAAAAAAEAAAABAAAAAEHBpeGkAAAAAAwgICAAAABZpcG1hAAAAAAAA
AAEAAQOBAgMAAAAebWRhdAAAABIoAa8TgObIaFPqH7pl35Rho5g=
BASE64,
];
protected array $cache = [];
protected readonly array $settings;
public function __construct(array $settings = [])
{
$defaults = $this->defaults();
$this->settings = [
...$defaults,
...array_intersect_key($settings, $defaults)
];
}
/**
* Whether the library writes frames of the format and reads them back with their delays
*/
protected function animates(Format $format): bool
{
return false;
}
/**
* @template T
* @param Closure(): T $compute
* @return T
*/
protected function cache(string $key, Closure $compute): mixed
{
if (array_key_exists($key, $this->cache) === false) {
$this->cache[$key] = $compute();
}
/** @var T */
return $this->cache[$key];
}
final public function canAnimate(Format $format): bool
{
return $this->cache(
'animate ' . $format->name,
fn (): bool => $this->animates($format)
);
}
final public function canRead(Format|Source $input): bool
{
$format = $input instanceof Source ? $input->format() : $input;
$decodes = $this->cache(
'read ' . $format->name,
fn (): bool => $this->decodes($format)
);
if ($decodes === false || $input instanceof Format) {
return $decodes;
}
// no library maps PQ or HLG to sRGB
return $input->isHdr() === false && $this->reads($input);
}
final public function canWrite(Format $format): bool
{
return $this->cache(
'write ' . $format->name,
fn (): bool => $this->encodes($format)
);
}
abstract protected function decodes(Format $format): bool;
protected function defaults(): array
{
return [];
}
abstract protected function encodes(Format $format): bool;
/**
* A color profile for the thumb, where the
* library writes none; none to keep the thumb
*/
public function icc(Plan $plan): string|null
{
return null;
}
/**
* @return array{library: string|null, version: string|null}
*/
public function info(): array
{
return [
'library' => null,
'version' => null,
];
}
abstract public function isAvailable(): bool;
/**
* Whether the library applies the source's orientation while loading
*/
public function orients(Source $source): bool
{
return false;
}
abstract public function process(Plan $plan, string $file): void;
/**
* Whether the driver reads a source of a format it decodes
*/
protected function reads(Source $source): bool
{
return true;
}
}