-
Notifications
You must be signed in to change notification settings - Fork 1
/
px.py
531 lines (491 loc) · 16.2 KB
/
px.py
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
"""
Radiance picture processing utilities.
"""
from pathlib import Path
import subprocess as sp
from typing import List, Optional, Union
from .anci import BINPATH, handle_called_process_error
@handle_called_process_error
def pcond(
hdr: Path,
human: bool = False,
acuity: bool = False,
veiling: bool = False,
sense: bool = False,
closs: bool = False,
center_weighted: bool = False,
linear: bool = False,
fixfrac: float = 0.0,
fixpoints: Optional[List[tuple]] = None,
histo: str = "",
expval: str = "",
ldmax: float = 100.0,
lddyn: float = 100.0,
primaries: Optional[List[float]] = None,
macbeth: str = "",
mapfile: str = "",
) -> bytes:
"""
Condition a Radiance picture.
Args:
hdr: input hdr
human: Mimic human visual response. This is the same as turning on all acuity,
veiling, sensitivity, and color loss.
acuity: Defocus darker region.
veiling: Add veiling glare.
sense: Use human contrast sensitivity, simulating eye internal scattering.
center_weighted: Use center-weighted average for acuity and veiling.
linear: Use a linear reponse function instead of the standard dynamic range
compression. This preseves the extremas.
fixfrac: Fixation fraction for acuity and veiling.
histo: Histogram file for acuity and veiling.
expval: Exposure value for acuity and veiling.
ldmax: Maximum luminance for acuity and veiling.
lddyn: Luminance dynamic range for acuity and veiling.
primaries: Color primaries for color loss.
macbeth: Macbeth chart file for color loss.
mapfile: Color map file for color loss.
Returns:
bytes: output of pcond
"""
stdin = None
cmd = [str(BINPATH / "pcond")]
if human:
cmd.append("-h")
else:
if acuity:
cmd.append("-a")
if veiling:
cmd.append("-v")
if sense:
cmd.append("-s")
if closs:
cmd.append("-c")
if linear:
cmd.append("-l")
if center_weighted:
cmd.append("-w")
if fixfrac > 0:
if fixpoints is None:
raise ValueError("fixfrac is set but fixpoints is not provided.")
stdin = str(fixpoints).encode()
cmd.extend(["-i", str(fixfrac)])
elif histo:
stdin = histo.encode()
cmd.append("-I")
if expval != "":
cmd.extend(["-e", expval])
if ldmax != 100:
cmd.extend(["-u", str(ldmax)])
if lddyn != 100:
cmd.extend(["-d", str(lddyn)])
if primaries is not None:
cmd.extend(["-p", *(str(p) for p in primaries)])
elif macbeth:
cmd.extend(["-f", macbeth])
if mapfile != "":
cmd.extend(["-x", mapfile])
if not isinstance(hdr, (str, Path)):
raise TypeError("hdr should be a string or a Path.")
cmd.append(str(hdr))
return sp.run(cmd, input=stdin, stdout=sp.PIPE, check=True).stdout
@handle_called_process_error
def pfilt(
image: Union[str, Path, bytes],
xres: Optional[str] = None,
yres: Optional[str] = None,
pixel_aspect: float = 0,
pa_correct: bool = False,
exposure: Optional[float] = 0,
lamp: Optional[str] = None,
lampdat: Optional[str] = None,
one_pass: bool = False,
gaussian_filter_radius: Optional[float] = None,
limitfrac: Optional[float] = None,
hot_threshold: float = 100,
star_points: int = 0,
star_spread: float = 0.0001,
average_hot: bool = False,
) -> bytes:
"""filter a Radiance picture.
By default, it uses two passes on the input, using a box filter.
Args:
image: input image
xres: horizontal resolution
yres: vertical resolution
pixel_aspect: pixel aspect ratio
exposure: exposure value
lamp: lamp file
lampdat: lamp data file
one_pass: use one pass filter
gaussian_filter_radius: gaussian filter radius
limitfrac: limit fraction
hot_threshold: Set intensity considered 'hot', default 100 watts/sr/m2
star_points: Number of points on a start pattern.
star_spread: star pattern spread
average_hot: average hot spots
Returns:
bytes: output of pfilt
"""
stdin = None
cmd = [str(BINPATH / "pfilt")]
if xres:
cmd.extend(["-x", xres])
if yres:
cmd.extend(["-y", yres])
if pixel_aspect:
cmd.extend(["-p", str(pixel_aspect)])
if pa_correct:
cmd.append("-c")
if exposure:
cmd.extend(["-e", str(exposure)])
if lamp:
cmd.extend(["-t", lamp])
if lampdat:
cmd.extend(["-f", lampdat])
if one_pass:
cmd.append("-1")
if gaussian_filter_radius:
cmd.extend(["-r", str(gaussian_filter_radius)])
if limitfrac:
cmd.extend(["-m", str(limitfrac)])
if hot_threshold != 100:
cmd.extend(["-h", str(hot_threshold)])
if star_points:
cmd.extend(["-n", str(star_points)])
if star_spread != 0.0001:
cmd.extend(["-s", str(star_spread)])
if average_hot:
cmd.append("-a")
if isinstance(image, bytes):
stdin = image
elif isinstance(image, (str, Path)):
cmd.append(str(image))
else:
raise TypeError("image should be a string, Path, or bytes.")
return sp.run(cmd, input=stdin, stdout=sp.PIPE, check=True).stdout
@handle_called_process_error
def pvalue(
pic: Union[Path, str, bytes],
unique: bool = False,
original: bool = False,
header: bool = True,
resstr: bool = True,
skip: int = 0,
exposure: int = 0,
gamma: float = 1.0,
dataonly: bool = False,
outform: str = "",
reverse_rgb: bool = False,
interleaving: bool = True,
brightness: bool = False,
outprimary: Optional[str] = None,
) -> bytes:
"""convert RADIANCE picture to/from alternate formats
Pvalue converts the pixels of a RADIANCE picture to or from another format.
In the default mode, pixels are sent to the standard output, one per line,
in the following ascii format: xpos ypos red green blue
Args:
pic: hdr file path. Either path or stdin is used, path takes precedence.
unique: if True, only unique values will be returned
original: if True, print original values, before exposure compensation
header: if True, header will be returned
resstr: if True, resolution string will be returned
skip: number of bytes to skip
exposure: exposure value
gamma: gamma value
dataonly: if True, only data will be returned
outform: output data format
reverse_rgb: if True, RGB values will be reversed
interleaving: if True, interleaving will be used
brightness: if True, only brightness will be returned
outprimary: output color primaries
Returns:
bytes: output of pvalue
"""
cmd = [str(BINPATH / "pvalue")]
if unique:
cmd.append("-u")
if original:
cmd.append("-o")
if not header:
cmd.append("-h")
if not resstr:
cmd.append("-H")
if skip:
cmd.extend(["-s", str(skip)])
if exposure:
cmd.extend(["-e", str(exposure)])
if gamma:
cmd.extend(["-g", str(gamma)])
if dataonly:
cmd.append("-d")
if outform != "":
cmd.append(f"-d{outform}")
if reverse_rgb:
cmd.append("-R")
if not interleaving:
cmd.append("-n")
if brightness:
cmd.append("-b")
if outprimary:
cmd.append("-p{outprimary}")
if isinstance(pic, (Path, str)):
cmd.append(str(pic))
proc = sp.run(cmd, check=True, input=None, stdout=sp.PIPE)
elif isinstance(pic, bytes):
proc = sp.run(cmd, check=True, input=pic, stdout=sp.PIPE)
else:
raise ValueError("pic should be either a file path or bytes.")
return proc.stdout
@handle_called_process_error
def pvaluer(
pic: Union[Path, str, bytes],
xres: Optional[int] = None,
yres: Optional[int] = None,
inform: str = "a",
resstr: bool = True,
dataonly: bool = False,
header: bool = False,
primaries: Optional[List[float]] = None,
pxyz: bool = False,
) -> bytes:
"""
Run Radiance pvalue tools reverse mode:
constructing a image from pixel values.
Args:
pic: Path or bytes of the input pixel data.
xres: X resolution.
yres: Y resolution.
inform: input data format. Default is "a" for ascii.
header: Set to True if the picture file has a header. Default is False.
primaries: List of primaries for XYZ calculation. Default is None.
pxyz: Set to True to calculate XYZ values. Default is False.
Returns:
Bytes of the pvalue output
"""
stdin = None
cmd = ["pvalue", "-r"]
if not header:
cmd.append("-h")
if not resstr:
cmd.append("-H")
if dataonly:
cmd.append("-d")
if inform != "a":
cmd.append(f"-d{inform}")
if yres:
sign = "-" if yres > 0 else "+"
cmd.extend([f"{sign}y", str(abs(yres))])
if xres:
sign = "+" if xres > 0 else "-"
cmd.extend([f"{sign}x", str(abs(xres))])
if primaries:
cmd.extend(["-p", *map(str, primaries)])
if pxyz:
cmd.append("-pXYZ")
if isinstance(pic, (Path, str)):
cmd.append(str(pic))
elif isinstance(pic, bytes):
stdin = pic
return sp.run(cmd, check=True, stdout=sp.PIPE, input=stdin).stdout
@handle_called_process_error
def ra_tiff(
inp,
out: Optional[str] = None,
gamma: float = 2.2,
greyscale: bool = False,
lzw: bool = False,
sgilog: bool = False,
sgilog24: bool = False,
ieee32: bool = False,
primary: bool = False,
exposure: int = 0,
reverse: bool = False,
xyze: bool = False,
) -> Optional[bytes]:
"""ra_tiff - convert RADIANCE picture to/from a TIFF color
or greyscale image
Args:
inp: Path or bytes to input picture file.
out: Path to output file, required when output is a TIFF file.
gamma: Gamma value for the output image. Default is 2.2.
greyscale: Set to True to convert to greyscale. Default is False.
lzw: Set to True to use LZW compression. Default is False.
sgilog: Set to True to use SGI log compression. Default is False.
sgilog24: Set to True to use SGI log 24 compression. Default is False.
ieee32: Set to True to use IEEE 32-bit floating point compression.
primary: Set to True to use 16-bit/primary output. Default is False.
reverse: Set to True to invoke a reverse conversion, from a TIFF
to a RADIANCE picture. Default is False.
xyze: Set to True to use XYZE output when invoking a reverse
conversion. Default is False.
Returns:
bytes: output of ra_tiff
"""
cmd = [str(BINPATH / "ra_tiff")]
if reverse:
if isinstance(inp, bytes):
raise ValueError("Input should be a file when input is TIFF.")
cmd.append("-r")
if xyze:
cmd.append("-e")
if gamma:
cmd.extend(["-g", str(gamma)])
if exposure:
cmd.extend(["-e", str(exposure)])
else:
if out is None:
raise ValueError(
"Output should be specified when input is a RADIANCE picture."
)
if lzw:
cmd.append("-z")
elif sgilog:
cmd.append("-L")
elif sgilog24:
cmd.append("-l")
elif ieee32:
cmd.append("-f")
elif primary:
cmd.append("-w")
if greyscale:
cmd.append("-b")
if gamma:
cmd.extend(["-g", str(gamma)])
if exposure:
cmd.extend(["-e", str(exposure)])
stdin = None
if isinstance(inp, (Path, str)):
cmd.append(str(inp))
elif isinstance(inp, bytes):
stdin = inp
cmd.append("-")
cmd.append(str(out))
pout = sp.run(cmd, check=True, input=stdin, stdout=sp.PIPE).stdout
if out is None:
return pout
return
@handle_called_process_error
def ra_ppm(
inp,
gamma: float = 2.2,
greyscale: bool = False,
reverse: bool = False,
exposure: int = 0,
ascii: bool = False,
outscale: int = 255,
) -> bytes:
"""convert RADIANCE picture to/from a Poskanzer Portable Pixmap
Args:
inp: Path or bytes to input picture file.
gamma: Gamma value for the output image. Default is 2.2.
reverse: Set to True to invoke a reverse conversion, from a PPM
to a RADIANCE picture. Default is False.
exposure: Exposure value for the output image. Default is 0.
ascii: Set to True to use ASCII Pixmap output. Default is False.
outscale: Output scale value. Default is 255.
Returns:
bytes: output of ra_ppm
"""
cmd = [str(BINPATH / "ra_ppm")]
if reverse:
cmd.append("-r")
if gamma:
cmd.extend(["-g", str(gamma)])
if greyscale:
cmd.append("-b")
if exposure:
cmd.extend(["-e", str(exposure)])
if ascii:
cmd.append("-a")
if outscale != 255:
cmd.extend(["-s", str(outscale)])
stdin = None
if isinstance(inp, (Path, str)):
cmd.append(str(inp))
elif isinstance(inp, bytes):
stdin = inp
return sp.run(cmd, check=True, input=stdin, stdout=sp.PIPE).stdout
@handle_called_process_error
def falsecolor(
inp: [Union[str, Path, bytes]],
pic_overlay: Optional[str] = None,
contour: Optional[str] = None,
extrema: bool = False,
scale: Optional[str] = None,
digits: Optional[int] = None,
label: Optional[str] = None,
ndivs: Optional[int] = None,
lwidth: Optional[int] = None,
lheight: Optional[int] = None,
decades: Optional[int] = None,
multiplier: Optional[float] = None,
palette: Optional[str] = None,
redv: Optional[str] = None,
grnv: Optional[str] = None,
bluv: Optional[str] = None,
) -> bytes:
"""
Generate a falsecolor Radiance picture.
Args:
inp: Path or bytes to input picture file.
pic_overlay: Path to another picture to overlay with contours.
contour: Type of contour ('b' for bands, 'l' for lines, 'p' for posterization).
extrema: Flag to print extrema points on the brightest and darkest pixels.
scale: Scale for the false color (e.g., 'auto' or specific scale).
digits: Max number of decimal places for legend entries.
label: Label for the new image.
ndivs: Number of contours and corresponding legend entries.
lwidth: Width of the legend.
lheight: Height of the legend.
decades: Number of decades below the maximum scale for logarithmic mapping.
multiplier: Multiplier for the scale (e.g., to convert units).
palette: Color palette to use for false color.
redv: Expression for mapping values to red.
grnv: Expression for mapping values to green.
bluv: Expression for mapping values to blue.
Returns:
bytes: Output of falsecolor.
"""
cmd = [str(BINPATH / "falsecolor")]
# In the man-page, it is mentioned that stdin is used if no input file is specified.
if isinstance(inp, str):
cmd.extend(["-i", inp])
stdin = None
else:
stdin = inp
if pic_overlay:
cmd.extend(["-p", pic_overlay])
if contour:
if contour in ['b', 'l', 'p']:
cmd.append(f"-c{contour}")
else:
raise ValueError("Invalid value for contour. Allowed values are 'b', 'l', or 'p'.")
if extrema:
cmd.append("-e")
if scale:
cmd.extend(["-s", scale])
if digits is not None:
cmd.extend(["-d", str(digits)])
if label:
cmd.extend(["-l", label])
if ndivs is not None:
cmd.extend(["-n", str(ndivs)])
if lwidth is not None:
cmd.extend(["-lw", str(lwidth)])
if lheight is not None:
cmd.extend(["-lh", str(lheight)])
if decades is not None:
cmd.extend(["-log", str(decades)])
if multiplier is not None:
cmd.extend(["-m", str(multiplier)])
if palette:
cmd.extend(["-pal", palette])
if redv:
cmd.extend(["-r", redv])
if grnv:
cmd.extend(["-g", grnv])
if bluv:
cmd.extend(["-b", bluv])
return sp.run(cmd, stdout=sp.PIPE, check=True, input=stdin).stdout