modisp 0.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
modisp/__init__.py ADDED
@@ -0,0 +1,13 @@
1
+ from ._reader import RawImage, RawMetadata, read_metadata, read_raw
2
+ from ._tiff import RawFormatError
3
+
4
+ __version__ = "0.0.2"
5
+
6
+ __all__ = [
7
+ "read_raw",
8
+ "read_metadata",
9
+ "RawImage",
10
+ "RawMetadata",
11
+ "RawFormatError",
12
+ "__version__",
13
+ ]
modisp/_ljpeg.py ADDED
@@ -0,0 +1,284 @@
1
+ import re
2
+ from typing import List, Tuple
3
+
4
+ import numpy as np
5
+
6
+ from ._tiff import RawFormatError
7
+
8
+ _MARKER = re.compile(b"\xff[^\x00\xff]")
9
+
10
+
11
+ _TABLE_CACHE: dict = {}
12
+
13
+
14
+ def _build_table(counts: List[int], symbols: List[int]):
15
+ key = bytes(counts) + bytes(symbols)
16
+ if key not in _TABLE_CACHE:
17
+ if len(_TABLE_CACHE) > 64:
18
+ _TABLE_CACHE.clear()
19
+ _TABLE_CACHE[key] = _make_table(counts, symbols)
20
+ return _TABLE_CACHE[key]
21
+
22
+
23
+ def _make_table(counts: List[int], symbols: List[int]):
24
+ lens = np.zeros(65536, dtype=np.int64)
25
+ syms = np.zeros(65536, dtype=np.int64)
26
+ code = 0
27
+ k = 0
28
+ for length in range(1, 17):
29
+ for _ in range(counts[length - 1]):
30
+ lo = code << (16 - length)
31
+ hi = (code + 1) << (16 - length)
32
+ if hi > 65536:
33
+ raise RawFormatError("invalid Huffman table")
34
+ lens[lo:hi] = length
35
+ syms[lo:hi] = symbols[k]
36
+ code += 1
37
+ k += 1
38
+ code <<= 1
39
+ peek = np.arange(65536, dtype=np.int64)
40
+ cat = syms
41
+ tot = np.where(cat == 16, lens, lens + cat)
42
+ valid = (lens > 0) & (tot <= 16)
43
+ shift = np.clip(16 - tot, 0, 16)
44
+ safe = np.maximum(cat, 1)
45
+ extra = (peek >> shift) & ((1 << safe) - 1)
46
+ diff = np.where(extra >= (1 << (safe - 1)), extra, extra - (1 << safe) + 1)
47
+ diff = np.where(cat == 0, 0, np.where(cat == 16, 32768, diff))
48
+ total_lut = np.where(valid, tot, 0).tolist()
49
+ diff_lut = np.where(valid, diff, 0).tolist()
50
+ return total_lut, diff_lut, lens.tolist(), syms.tolist()
51
+
52
+
53
+ def _slow(buf: int, nbits: int, p: int, lens: list, syms: list) -> Tuple[int, int]:
54
+ length = lens[p]
55
+ if length == 0:
56
+ raise RawFormatError("invalid Huffman code in scan data")
57
+ cat = syms[p]
58
+ if cat == 16:
59
+ return 32768, length
60
+ if cat == 0:
61
+ return 0, length
62
+ extra = (buf >> (nbits - length - cat)) & ((1 << cat) - 1)
63
+ if extra < (1 << (cat - 1)):
64
+ extra = extra - (1 << cat) + 1
65
+ return extra, length + cat
66
+
67
+
68
+ def _words(seg: bytes) -> Tuple[List[int], int]:
69
+ clean = seg.replace(b"\xff\x00", b"\xff")
70
+ pad = (-len(clean)) % 4 + 16
71
+ return np.frombuffer(clean + b"\0" * pad, dtype=">u4").tolist(), len(clean) * 8
72
+
73
+
74
+ def _decode_single(seg: bytes, count: int, table) -> List[int]:
75
+ words, nbytes = _words(seg)
76
+ tl, dl, ll, sl = table
77
+ out: List[int] = []
78
+ ap = out.append
79
+ buf = 0
80
+ nbits = 0
81
+ wi = 0
82
+ for _ in range(count):
83
+ if nbits < 32:
84
+ buf = ((buf & ((1 << nbits) - 1)) << 32) | words[wi]
85
+ wi += 1
86
+ nbits += 32
87
+ p = (buf >> (nbits - 16)) & 0xFFFF
88
+ n = tl[p]
89
+ if n:
90
+ nbits -= n
91
+ ap(dl[p])
92
+ else:
93
+ d, n = _slow(buf, nbits, p, ll, sl)
94
+ nbits -= n
95
+ ap(d)
96
+ if wi * 32 - nbits > nbytes:
97
+ raise RawFormatError("scan data ended early")
98
+ return out
99
+
100
+
101
+ def _decode_multi(seg: bytes, nmcu: int, tables: list) -> List[int]:
102
+ words, nbytes = _words(seg)
103
+ out: List[int] = []
104
+ ap = out.append
105
+ buf = 0
106
+ nbits = 0
107
+ wi = 0
108
+ for _ in range(nmcu):
109
+ for tl, dl, ll, sl in tables:
110
+ if nbits < 32:
111
+ buf = ((buf & ((1 << nbits) - 1)) << 32) | words[wi]
112
+ wi += 1
113
+ nbits += 32
114
+ p = (buf >> (nbits - 16)) & 0xFFFF
115
+ n = tl[p]
116
+ if n:
117
+ nbits -= n
118
+ ap(dl[p])
119
+ else:
120
+ d, n = _slow(buf, nbits, p, ll, sl)
121
+ nbits -= n
122
+ ap(d)
123
+ if wi * 32 - nbits > nbytes:
124
+ raise RawFormatError("scan data ended early")
125
+ return out
126
+
127
+
128
+ def _recon_p1(diff: np.ndarray, init: int) -> np.ndarray:
129
+ e = diff.astype(np.int64)
130
+ e[0, 0, :] += init
131
+ e[:, 0, :] = np.cumsum(e[:, 0, :], axis=0)
132
+ return np.cumsum(e, axis=1) & 0xFFFF
133
+
134
+
135
+ def _recon_generic(flat: np.ndarray, width: int, nc: int, pred: int, init: int, starts: List[int]):
136
+ d = flat.tolist()
137
+ n = len(d) // nc
138
+ x = [0] * len(d)
139
+ bounds = starts + [n]
140
+ row = width * nc
141
+ for s, e in zip(bounds[:-1], bounds[1:]):
142
+ for idx in range(s, e):
143
+ col = idx % width
144
+ base = idx * nc
145
+ for k in range(nc):
146
+ i = base + k
147
+ if idx == s:
148
+ p = init
149
+ elif idx - width < s:
150
+ p = x[i - nc]
151
+ elif col == 0 or pred == 2:
152
+ p = x[i - row]
153
+ elif pred == 1:
154
+ p = x[i - nc]
155
+ else:
156
+ ra = x[i - nc]
157
+ rb = x[i - row]
158
+ rc = x[i - row - nc]
159
+ if pred == 3:
160
+ p = rc
161
+ elif pred == 4:
162
+ p = ra + rb - rc
163
+ elif pred == 5:
164
+ p = ra + ((rb - rc) >> 1)
165
+ elif pred == 6:
166
+ p = rb + ((ra - rc) >> 1)
167
+ else:
168
+ p = (ra + rb) >> 1
169
+ x[i] = (p + d[i]) & 0xFFFF
170
+ return np.array(x, dtype=np.int64)
171
+
172
+
173
+ def decode_ljpeg(data: bytes) -> np.ndarray:
174
+ """Decode a lossless JPEG stream to a uint16 array of shape (height, width, components)."""
175
+ if data[:2] != b"\xff\xd8":
176
+ raise RawFormatError("missing JPEG start marker")
177
+ pos = 2
178
+ tables = {}
179
+ restart = 0
180
+ frame = None
181
+ scan = None
182
+ n = len(data)
183
+ while pos + 4 <= n:
184
+ if data[pos] != 0xFF:
185
+ raise RawFormatError("bad JPEG marker")
186
+ marker = data[pos + 1]
187
+ if marker == 0xFF:
188
+ pos += 1
189
+ continue
190
+ length = int.from_bytes(data[pos + 2 : pos + 4], "big")
191
+ body = data[pos + 4 : pos + 2 + length]
192
+ pos += 2 + length
193
+ if marker == 0xC4:
194
+ i = 0
195
+ while i + 17 <= len(body):
196
+ tid = body[i] & 0x0F
197
+ counts = list(body[i + 1 : i + 17])
198
+ total = sum(counts)
199
+ syms = list(body[i + 17 : i + 17 + total])
200
+ tables[tid] = _build_table(counts, syms)
201
+ i += 17 + total
202
+ elif marker == 0xDD:
203
+ restart = int.from_bytes(body[:2], "big")
204
+ elif marker == 0xC3:
205
+ prec = body[0]
206
+ height = int.from_bytes(body[1:3], "big")
207
+ width = int.from_bytes(body[3:5], "big")
208
+ nc = body[5]
209
+ for c in range(nc):
210
+ if body[7 + 3 * c] != 0x11:
211
+ raise NotImplementedError("subsampled lossless JPEG is not supported")
212
+ frame = (prec, height, width, nc)
213
+ elif marker in (0xC0, 0xC1, 0xC2, 0xC5, 0xC6, 0xC7, 0xC9, 0xCA, 0xCB, 0xCD, 0xCE, 0xCF):
214
+ raise NotImplementedError("JPEG process is not lossless Huffman (SOF3)")
215
+ elif marker == 0xDA:
216
+ ns = body[0]
217
+ sel = [body[2 + 2 * c] >> 4 for c in range(ns)]
218
+ scan = (sel, body[1 + 2 * ns], body[3 + 2 * ns] & 0x0F)
219
+ break
220
+ if frame is None or scan is None:
221
+ raise RawFormatError("JPEG frame or scan header missing")
222
+ prec, height, width, nc = frame
223
+ sel, pred, pt = scan
224
+ if len(sel) != nc:
225
+ raise NotImplementedError("non-interleaved lossless JPEG scans are not supported")
226
+ if not 1 <= pred <= 7:
227
+ raise RawFormatError("invalid predictor")
228
+ try:
229
+ tabs = [tables[s] for s in sel]
230
+ except KeyError:
231
+ raise RawFormatError("missing Huffman table") from None
232
+
233
+ nmcu = width * height
234
+ segments: List[bytes] = []
235
+ start = pos
236
+ for m in _MARKER.finditer(data, pos):
237
+ code = data[m.start() + 1]
238
+ if 0xD0 <= code <= 0xD7:
239
+ segments.append(data[start : m.start()])
240
+ start = m.end()
241
+ else:
242
+ segments.append(data[start : m.start()])
243
+ start = -1
244
+ break
245
+ if start >= 0:
246
+ segments.append(data[start:])
247
+
248
+ interval = restart if restart else nmcu
249
+ starts: List[int] = []
250
+ flat: List[int] = []
251
+ done = 0
252
+ for seg in segments:
253
+ if done >= nmcu:
254
+ break
255
+ cnt = min(interval, nmcu - done)
256
+ starts.append(done)
257
+ try:
258
+ if all(t is tabs[0] for t in tabs):
259
+ flat.extend(_decode_single(seg, cnt * nc, tabs[0]))
260
+ else:
261
+ flat.extend(_decode_multi(seg, cnt, tabs))
262
+ except IndexError:
263
+ raise RawFormatError("scan data ended early") from None
264
+ done += cnt
265
+ if done < nmcu:
266
+ raise RawFormatError("scan data ended early")
267
+
268
+ init = 1 << (prec - pt - 1)
269
+ diff = np.array(flat, dtype=np.int64)
270
+ if pred == 1 and (not restart or restart % width == 0):
271
+ diff = diff.reshape(height, width, nc)
272
+ if restart:
273
+ rows = restart // width
274
+ parts = [
275
+ _recon_p1(diff[r : r + rows], init) for r in range(0, height, rows)
276
+ ]
277
+ out = np.concatenate(parts, axis=0)
278
+ else:
279
+ out = _recon_p1(diff, init)
280
+ else:
281
+ out = _recon_generic(diff, width, nc, pred, init, starts).reshape(height, width, nc)
282
+ if pt:
283
+ out = (out << pt) & 0xFFFF
284
+ return out.astype(np.uint16)
modisp/_reader.py ADDED
@@ -0,0 +1,346 @@
1
+ import os
2
+ import zlib
3
+ from dataclasses import dataclass, fields
4
+ from typing import Any, Optional, Tuple, Union
5
+
6
+ import numpy as np
7
+
8
+ from ._ljpeg import decode_ljpeg
9
+ from ._tiff import Ifd, RawFormatError, TiffFile, find_raw_ifd
10
+
11
+ PathLike = Union[str, "os.PathLike[str]"]
12
+ _PLANE_LETTERS = "RGBCMYW"
13
+
14
+
15
+ @dataclass(frozen=True, eq=False)
16
+ class RawMetadata:
17
+ """Metadata of the raw image; width and height refer to the returned (cropped) array."""
18
+
19
+ width: int
20
+ height: int
21
+ raw_width: int
22
+ raw_height: int
23
+ bits_per_sample: int
24
+ samples_per_pixel: int
25
+ compression: int
26
+ photometric: int
27
+ make: str
28
+ model: str
29
+ unique_camera_model: Optional[str]
30
+ dng_version: Optional[Tuple[int, ...]]
31
+ cfa_pattern: Optional[np.ndarray]
32
+ cfa_colors: Optional[str]
33
+ black_level: np.ndarray
34
+ white_level: Tuple[int, ...]
35
+ as_shot_neutral: Optional[Tuple[float, ...]]
36
+ as_shot_white_xy: Optional[Tuple[float, ...]]
37
+ baseline_exposure: Optional[float]
38
+ orientation: int
39
+ active_area: Optional[Tuple[int, int, int, int]]
40
+ default_crop_origin: Optional[Tuple[float, ...]]
41
+ default_crop_size: Optional[Tuple[float, ...]]
42
+ color_matrix_1: Optional[np.ndarray]
43
+ color_matrix_2: Optional[np.ndarray]
44
+ calibration_illuminant_1: Optional[int]
45
+ calibration_illuminant_2: Optional[int]
46
+ iso: Optional[int]
47
+ exposure_time: Optional[float]
48
+ f_number: Optional[float]
49
+
50
+ def to_dict(self) -> dict:
51
+ """Return the metadata as plain Python values (arrays become nested lists)."""
52
+ out = {}
53
+ for f in fields(self):
54
+ v = getattr(self, f.name)
55
+ if isinstance(v, np.ndarray):
56
+ v = v.tolist()
57
+ elif isinstance(v, tuple):
58
+ v = list(v)
59
+ out[f.name] = v
60
+ return out
61
+
62
+
63
+ @dataclass(frozen=True, eq=False)
64
+ class RawImage:
65
+ """Decoded raw pixels together with their metadata."""
66
+
67
+ data: np.ndarray
68
+ metadata: RawMetadata
69
+
70
+ @property
71
+ def cfa_pattern(self) -> Optional[np.ndarray]:
72
+ return self.metadata.cfa_pattern
73
+
74
+ @property
75
+ def black_level(self) -> np.ndarray:
76
+ return self.metadata.black_level
77
+
78
+ @property
79
+ def white_level(self) -> Tuple[int, ...]:
80
+ return self.metadata.white_level
81
+
82
+
83
+ def _load(path: PathLike) -> TiffFile:
84
+ with open(os.fspath(path), "rb") as fh:
85
+ return TiffFile(fh.read())
86
+
87
+
88
+ def _lookup(tag: int, *ifds: Optional[Ifd]) -> Any:
89
+ for ifd in ifds:
90
+ if ifd is not None and tag in ifd:
91
+ return ifd.get(tag)
92
+ return None
93
+
94
+
95
+ def _opt_tuple(v: Any, cast: Any) -> Optional[tuple]:
96
+ if v is None or isinstance(v, str):
97
+ return None
98
+ return tuple(cast(x) for x in v)
99
+
100
+
101
+ def _opt_scalar(v: Any, cast: Any) -> Any:
102
+ if v is None or isinstance(v, str) or len(v) == 0:
103
+ return None
104
+ return cast(v[0])
105
+
106
+
107
+ def _build_metadata(tiff: TiffFile, raw: Ifd) -> RawMetadata:
108
+ ifd0 = tiff.ifds[0]
109
+ raw_w = int(raw.first(256, 0))
110
+ raw_h = int(raw.first(257, 0))
111
+ spp = int(raw.first(277, 1))
112
+ bps_all = raw.get(258, (16,))
113
+ bps = int(bps_all[0])
114
+ photometric = int(raw.first(262))
115
+ if raw_w <= 0 or raw_h <= 0:
116
+ raise RawFormatError("invalid image dimensions")
117
+
118
+ exif = [ifd0.exif] + [i.exif for i in tiff.ifds[1:]]
119
+ exif_ifds = [e for e in exif if e is not None]
120
+
121
+ dims = _lookup(33421, raw, ifd0)
122
+ pattern = _lookup(33422, raw, ifd0)
123
+ cfa = None
124
+ colors = None
125
+ if dims is not None and pattern is not None and len(dims) == 2:
126
+ if dims[0] * dims[1] == len(pattern) and len(pattern) > 0:
127
+ cfa = np.array(pattern, dtype=np.uint8).reshape(dims[0], dims[1])
128
+ planes = _lookup(50710, raw, ifd0) or (0, 1, 2)
129
+ letters = []
130
+ for v in cfa.ravel():
131
+ code = planes[v] if v < len(planes) else -1
132
+ letters.append(_PLANE_LETTERS[code] if 0 <= code < len(_PLANE_LETTERS) else "?")
133
+ colors = "".join(letters)
134
+
135
+ bdim = _lookup(50713, raw, ifd0)
136
+ bl = _lookup(50714, raw, ifd0)
137
+ if bl is None:
138
+ black = np.zeros((1, 1, spp) if spp > 1 else (1, 1), dtype=np.float64)
139
+ else:
140
+ vals = np.array(bl, dtype=np.float64)
141
+ br, bc = (int(bdim[0]), int(bdim[1])) if bdim is not None and len(bdim) == 2 else (1, 1)
142
+ if vals.size == br * bc * spp:
143
+ shape = (br, bc, spp) if spp > 1 else (br, bc)
144
+ elif vals.size == spp and bdim is None:
145
+ shape = (1, 1, spp) if spp > 1 else (1, 1)
146
+ elif vals.size == br * bc:
147
+ vals = np.repeat(vals, spp)
148
+ shape = (br, bc, spp) if spp > 1 else (br, bc)
149
+ else:
150
+ raise RawFormatError("BlackLevel does not match BlackLevelRepeatDim")
151
+ black = vals.reshape(shape)
152
+
153
+ wl = _lookup(50717, raw, ifd0)
154
+ if wl is None:
155
+ white = (2 ** bps - 1,)
156
+ else:
157
+ white = tuple(int(round(x)) for x in wl)
158
+ if len(white) == 1 and spp > 1:
159
+ white = white * spp
160
+
161
+ area = _lookup(50829, raw, ifd0)
162
+ active = None
163
+ width, height = raw_w, raw_h
164
+ if area is not None and len(area) == 4:
165
+ t, l, b, r = (int(x) for x in area)
166
+ if not (0 <= t < b <= raw_h and 0 <= l < r <= raw_w):
167
+ raise RawFormatError("ActiveArea lies outside the image")
168
+ active = (t, l, b, r)
169
+ width, height = r - l, b - t
170
+
171
+ cm1 = _lookup(50721, ifd0, raw)
172
+ cm2 = _lookup(50722, ifd0, raw)
173
+
174
+ def matrix(v: Any) -> Optional[np.ndarray]:
175
+ if v is None or len(v) % 3:
176
+ return None
177
+ return np.array(v, dtype=np.float64).reshape(-1, 3)
178
+
179
+ def exif_get(tag: int) -> Any:
180
+ v = _lookup(tag, *exif_ifds)
181
+ return v if v is not None else _lookup(tag, ifd0)
182
+
183
+ ver = _lookup(50706, ifd0, raw)
184
+ uniq = _lookup(50708, ifd0, raw)
185
+ return RawMetadata(
186
+ width=width,
187
+ height=height,
188
+ raw_width=raw_w,
189
+ raw_height=raw_h,
190
+ bits_per_sample=bps,
191
+ samples_per_pixel=spp,
192
+ compression=int(raw.first(259, 1)),
193
+ photometric=photometric,
194
+ make=str(_lookup(271, ifd0, raw) or ""),
195
+ model=str(_lookup(272, ifd0, raw) or ""),
196
+ unique_camera_model=str(uniq) if uniq is not None else None,
197
+ dng_version=tuple(int(x) for x in ver) if ver is not None else None,
198
+ cfa_pattern=cfa,
199
+ cfa_colors=colors,
200
+ black_level=black,
201
+ white_level=white,
202
+ as_shot_neutral=_opt_tuple(_lookup(50728, ifd0, raw), float),
203
+ as_shot_white_xy=_opt_tuple(_lookup(50729, ifd0, raw), float),
204
+ baseline_exposure=_opt_scalar(_lookup(50730, ifd0, raw), float),
205
+ orientation=int(_opt_scalar(_lookup(274, ifd0, raw), int) or 1),
206
+ active_area=active,
207
+ default_crop_origin=_opt_tuple(_lookup(50719, raw, ifd0), float),
208
+ default_crop_size=_opt_tuple(_lookup(50720, raw, ifd0), float),
209
+ color_matrix_1=matrix(cm1),
210
+ color_matrix_2=matrix(cm2),
211
+ calibration_illuminant_1=_opt_scalar(_lookup(50778, ifd0, raw), int),
212
+ calibration_illuminant_2=_opt_scalar(_lookup(50779, ifd0, raw), int),
213
+ iso=_opt_scalar(exif_get(34855), int),
214
+ exposure_time=_opt_scalar(exif_get(33434), float),
215
+ f_number=_opt_scalar(exif_get(33437), float),
216
+ )
217
+
218
+
219
+ def _unpack_bits(data: bytes, rows: int, n: int, bps: int) -> np.ndarray:
220
+ row_bytes = (n * bps + 7) // 8
221
+ if len(data) < rows * row_bytes:
222
+ raise RawFormatError("uncompressed block is shorter than expected")
223
+ src = np.frombuffer(data, dtype=np.uint8, count=rows * row_bytes).reshape(rows, row_bytes)
224
+ out = np.empty((rows, n), dtype=np.uint16)
225
+ weights = (1 << np.arange(bps - 1, -1, -1)).astype(np.uint16)
226
+ step = max(1, (1 << 22) // (n * bps))
227
+ for r in range(0, rows, step):
228
+ bits = np.unpackbits(src[r : r + step], axis=1)[:, : n * bps]
229
+ bits = bits.reshape(-1, n, bps)
230
+ out[r : r + step] = (bits * weights).sum(axis=2, dtype=np.uint16)
231
+ return out
232
+
233
+
234
+ def _from_bytes(data: bytes, rows: int, n: int, bps: int, bo: str) -> np.ndarray:
235
+ if bps == 8:
236
+ if len(data) < rows * n:
237
+ raise RawFormatError("block is shorter than expected")
238
+ return np.frombuffer(data, dtype=np.uint8, count=rows * n).reshape(rows, n)
239
+ if bps == 16:
240
+ if len(data) < rows * n * 2:
241
+ raise RawFormatError("block is shorter than expected")
242
+ arr = np.frombuffer(data, dtype=bo + "u2", count=rows * n)
243
+ return arr.astype(np.uint16).reshape(rows, n)
244
+ raise NotImplementedError(
245
+ "compression 8 with %d bits per sample is not supported" % bps
246
+ )
247
+
248
+
249
+ def _decode_block(
250
+ raw: bytes, comp: int, rows: int, cols: int, spp: int, bps: int, bo: str, predictor: int
251
+ ) -> np.ndarray:
252
+ n = cols * spp
253
+ if comp == 1:
254
+ if bps in (8, 16):
255
+ block = _from_bytes(raw, rows, n, bps, bo)
256
+ elif 1 <= bps < 16:
257
+ block = _unpack_bits(raw, rows, n, bps)
258
+ else:
259
+ raise NotImplementedError("%d bits per sample is not supported" % bps)
260
+ return block.reshape(rows, cols, spp)
261
+ if comp == 7:
262
+ img = decode_ljpeg(raw)
263
+ flat = img.reshape(-1)
264
+ if flat.size < rows * n:
265
+ raise RawFormatError("lossless JPEG block is smaller than the tile")
266
+ return flat[: rows * n].reshape(rows, cols, spp)
267
+ if comp in (8, 32946):
268
+ try:
269
+ payload = zlib.decompress(raw)
270
+ except zlib.error as exc:
271
+ raise RawFormatError("corrupt deflate block") from exc
272
+ block = _from_bytes(payload, rows, n, bps, bo).reshape(rows, cols, spp)
273
+ if predictor == 2:
274
+ block = np.cumsum(block, axis=1, dtype=block.dtype)
275
+ return block
276
+ raise NotImplementedError("compression %d is not supported" % comp)
277
+
278
+
279
+ def _decode_pixels(tiff: TiffFile, raw: Ifd, meta: RawMetadata) -> np.ndarray:
280
+ comp = meta.compression
281
+ if comp not in (1, 7, 8, 32946):
282
+ raise NotImplementedError("compression %d is not supported" % comp)
283
+ spp = meta.samples_per_pixel
284
+ bps_all = raw.get(258, (16,))
285
+ if len(set(bps_all)) != 1:
286
+ raise NotImplementedError("mixed bits per sample are not supported")
287
+ bps = meta.bits_per_sample
288
+ if bps > 16 or bps < 1:
289
+ raise NotImplementedError("%d bits per sample is not supported" % bps)
290
+ fmt = raw.get(339, (1,))
291
+ if any(f != 1 for f in fmt):
292
+ raise NotImplementedError("only unsigned integer samples are supported")
293
+ if spp > 1 and raw.first(284, 1) != 1:
294
+ raise NotImplementedError("planar configuration 2 is not supported")
295
+ predictor = int(raw.first(317, 1))
296
+ if comp in (8, 32946) and predictor not in (1, 2):
297
+ raise NotImplementedError("predictor %d is not supported" % predictor)
298
+
299
+ W, H = meta.raw_width, meta.raw_height
300
+ if 322 in raw:
301
+ tw, tl = int(raw.first(322)), int(raw.first(323, 0))
302
+ offsets, counts = raw.get(324), raw.get(325)
303
+ if tw <= 0 or tl <= 0:
304
+ raise RawFormatError("invalid tile size")
305
+ across = -(-W // tw)
306
+ down = -(-H // tl)
307
+ tiles = [(y * tl, x * tw, tl, tw) for y in range(down) for x in range(across)]
308
+ else:
309
+ offsets, counts = raw.get(273), raw.get(279)
310
+ rps = int(raw.first(278, H))
311
+ rps = min(max(rps, 1), H)
312
+ tiles = [(y, 0, min(rps, H - y), W) for y in range(0, H, rps)]
313
+ if offsets is None or counts is None or len(offsets) < len(tiles) or len(counts) < len(tiles):
314
+ raise RawFormatError("strip or tile offsets are missing")
315
+
316
+ dtype = np.uint8 if bps <= 8 else np.uint16
317
+ out = np.zeros((H, W, spp), dtype=dtype)
318
+ data = tiff.data
319
+ for (y0, x0, bh, bw), off, cnt in zip(tiles, offsets, counts):
320
+ if off + cnt > len(data):
321
+ raise RawFormatError("strip or tile data lies outside the file")
322
+ block = _decode_block(data[off : off + cnt], comp, bh, bw, spp, bps, tiff.bo, predictor)
323
+ h = min(bh, H - y0)
324
+ w = min(bw, W - x0)
325
+ out[y0 : y0 + h, x0 : x0 + w] = block[:h, :w]
326
+ return out
327
+
328
+
329
+ def read_metadata(path: PathLike) -> RawMetadata:
330
+ """Read the metadata of the main raw image without decoding pixels."""
331
+ tiff = _load(path)
332
+ return _build_metadata(tiff, find_raw_ifd(tiff))
333
+
334
+
335
+ def read_raw(path: PathLike) -> RawImage:
336
+ """Decode the main raw image, cropped to the ActiveArea when one is present."""
337
+ tiff = _load(path)
338
+ raw = find_raw_ifd(tiff)
339
+ meta = _build_metadata(tiff, raw)
340
+ pixels = _decode_pixels(tiff, raw, meta)
341
+ if meta.active_area is not None:
342
+ t, l, b, r = meta.active_area
343
+ pixels = pixels[t:b, l:r]
344
+ if meta.samples_per_pixel == 1:
345
+ pixels = pixels[:, :, 0]
346
+ return RawImage(data=np.ascontiguousarray(pixels), metadata=meta)
modisp/_tiff.py ADDED
@@ -0,0 +1,159 @@
1
+ import struct
2
+ from typing import Any, Dict, List, Optional, Tuple
3
+
4
+
5
+ class RawFormatError(ValueError):
6
+ """Raised when a file is malformed or is not a supported raw image."""
7
+
8
+
9
+ _TYPE_FMT = {
10
+ 1: ("B", 1),
11
+ 2: ("B", 1),
12
+ 3: ("H", 2),
13
+ 4: ("I", 4),
14
+ 5: ("I", 4),
15
+ 6: ("b", 1),
16
+ 7: ("B", 1),
17
+ 8: ("h", 2),
18
+ 9: ("i", 4),
19
+ 10: ("i", 4),
20
+ 11: ("f", 4),
21
+ 12: ("d", 8),
22
+ 13: ("I", 4),
23
+ }
24
+ _TYPE_SIZE = {1: 1, 2: 1, 3: 2, 4: 4, 5: 8, 6: 1, 7: 1, 8: 2, 9: 4, 10: 8, 11: 4, 12: 8, 13: 4}
25
+
26
+ SUB_IFDS = 330
27
+ EXIF_IFD = 34665
28
+
29
+
30
+ class Ifd:
31
+ """One image file directory with lazily decoded tag values."""
32
+
33
+ def __init__(self, tiff: "TiffFile") -> None:
34
+ self._tiff = tiff
35
+ self._entries: Dict[int, Tuple[int, int, int]] = {}
36
+ self._cache: Dict[int, Any] = {}
37
+ self.exif: Optional["Ifd"] = None
38
+
39
+ def __contains__(self, tag: int) -> bool:
40
+ return tag in self._entries
41
+
42
+ def get(self, tag: int, default: Any = None) -> Any:
43
+ if tag not in self._entries:
44
+ return default
45
+ if tag not in self._cache:
46
+ self._cache[tag] = self._tiff.decode(*self._entries[tag])
47
+ return self._cache[tag]
48
+
49
+ def first(self, tag: int, default: Any = None) -> Any:
50
+ value = self.get(tag)
51
+ if value is None or isinstance(value, str):
52
+ return default if value is None else value
53
+ return value[0] if len(value) else default
54
+
55
+
56
+ class TiffFile:
57
+ """Parsed classic TIFF container; collects all IFDs reachable from IFD0."""
58
+
59
+ def __init__(self, data: bytes) -> None:
60
+ if len(data) < 8:
61
+ raise RawFormatError("file too small to be a TIFF")
62
+ if data[:2] == b"II":
63
+ self.bo = "<"
64
+ elif data[:2] == b"MM":
65
+ self.bo = ">"
66
+ else:
67
+ raise RawFormatError("not a TIFF file")
68
+ self.data = data
69
+ magic = struct.unpack(self.bo + "H", data[2:4])[0]
70
+ if magic == 43:
71
+ raise RawFormatError("BigTIFF is not supported")
72
+ if magic != 42:
73
+ raise RawFormatError("not a TIFF file")
74
+ self.ifds: List[Ifd] = []
75
+ self._seen: set = set()
76
+ first = struct.unpack(self.bo + "I", data[4:8])[0]
77
+ self._walk(first)
78
+ if not self.ifds:
79
+ raise RawFormatError("no image directory found")
80
+
81
+ def decode(self, typ: int, count: int, pos: int) -> Any:
82
+ raw = self.data[pos : pos + _TYPE_SIZE[typ] * count]
83
+ if typ == 2:
84
+ return raw.split(b"\0", 1)[0].decode("utf-8", "replace")
85
+ fmt, size = _TYPE_FMT[typ]
86
+ if typ in (5, 10):
87
+ vals = struct.unpack(self.bo + "%d%s" % (2 * count, fmt), raw)
88
+ return tuple(
89
+ (vals[i] / vals[i + 1]) if vals[i + 1] else 0.0 for i in range(0, 2 * count, 2)
90
+ )
91
+ return struct.unpack(self.bo + "%d%s" % (count, fmt), raw)
92
+
93
+ def _read_ifd(self, offset: int) -> Tuple[Optional[Ifd], int]:
94
+ data = self.data
95
+ if offset < 8 or offset + 2 > len(data):
96
+ raise RawFormatError("IFD offset out of range")
97
+ n = struct.unpack(self.bo + "H", data[offset : offset + 2])[0]
98
+ end = offset + 2 + 12 * n
99
+ if end + 4 > len(data):
100
+ raise RawFormatError("IFD extends past end of file")
101
+ ifd = Ifd(self)
102
+ for i in range(n):
103
+ p = offset + 2 + 12 * i
104
+ tag, typ, count = struct.unpack(self.bo + "HHI", data[p : p + 8])
105
+ if typ not in _TYPE_SIZE:
106
+ continue
107
+ size = _TYPE_SIZE[typ] * count
108
+ if size <= 4:
109
+ pos = p + 8
110
+ else:
111
+ pos = struct.unpack(self.bo + "I", data[p + 8 : p + 12])[0]
112
+ if pos + size > len(data):
113
+ continue
114
+ ifd._entries[tag] = (typ, count, pos)
115
+ nxt = struct.unpack(self.bo + "I", data[end : end + 4])[0]
116
+ return ifd, nxt
117
+
118
+ def _walk(self, offset: int) -> None:
119
+ while offset and offset not in self._seen:
120
+ self._seen.add(offset)
121
+ try:
122
+ ifd, nxt = self._read_ifd(offset)
123
+ except RawFormatError:
124
+ if not self.ifds:
125
+ raise
126
+ return
127
+ assert ifd is not None
128
+ self.ifds.append(ifd)
129
+ exif_off = ifd.first(EXIF_IFD)
130
+ if isinstance(exif_off, int) and exif_off not in self._seen:
131
+ self._seen.add(exif_off)
132
+ try:
133
+ ifd.exif, _ = self._read_ifd(exif_off)
134
+ except RawFormatError:
135
+ ifd.exif = None
136
+ for sub in ifd.get(SUB_IFDS, ()):
137
+ if sub not in self._seen:
138
+ try:
139
+ self._walk(sub)
140
+ except RawFormatError:
141
+ pass
142
+ offset = nxt
143
+
144
+
145
+ def find_raw_ifd(tiff: TiffFile) -> Ifd:
146
+ """Pick the largest full-resolution IFD with a CFA or LinearRaw photometric."""
147
+ best: Optional[Ifd] = None
148
+ best_area = -1
149
+ for ifd in tiff.ifds:
150
+ if ifd.first(262) not in (32803, 34892):
151
+ continue
152
+ if ifd.first(254, 0) != 0:
153
+ continue
154
+ area = ifd.first(256, 0) * ifd.first(257, 0)
155
+ if area > best_area:
156
+ best, best_area = ifd, area
157
+ if best is None:
158
+ raise RawFormatError("no raw image found")
159
+ return best
modisp/py.typed ADDED
File without changes
@@ -0,0 +1,43 @@
1
+ Metadata-Version: 2.5
2
+ Name: modisp
3
+ Version: 0.0.2
4
+ Summary: A small reader for raw camera images (DNG).
5
+ Classifier: Operating System :: OS Independent
6
+ Classifier: Programming Language :: Python :: 3
7
+ Classifier: Programming Language :: Python :: 3.9
8
+ Classifier: Programming Language :: Python :: 3.10
9
+ Classifier: Programming Language :: Python :: 3.11
10
+ Classifier: Programming Language :: Python :: 3.12
11
+ Classifier: Programming Language :: Python :: 3.13
12
+ Requires-Python: >=3.9
13
+ Requires-Dist: numpy>=1.20
14
+ Description-Content-Type: text/markdown
15
+
16
+ # modisp
17
+
18
+ ## Install
19
+
20
+ pip install modisp
21
+
22
+ ## Usage
23
+
24
+ import modisp
25
+ img = modisp.read_raw("photo.dng")
26
+ print(img.data.shape, img.data.dtype)
27
+ print(img.cfa_pattern, img.black_level, img.white_level)
28
+ meta = modisp.read_metadata("photo.dng")
29
+ print(meta.make, meta.model, meta.cfa_colors)
30
+
31
+ ## Supported compressions
32
+
33
+ - 1: uncompressed, 1 to 16 bits per sample, packed or byte aligned
34
+ - 7: lossless JPEG (ITU-T T.81 process 14), predictors 1 to 7
35
+ - 8 and 32946: deflate, with predictor 1 or 2
36
+
37
+ ## Raises NotImplementedError
38
+
39
+ - 34892: lossy JPEG
40
+ - 52546: JPEG XL
41
+ - 34713 and any other compression code
42
+ - floating point and signed sample formats, floating point predictors
43
+ - separate planar configuration with more than one sample per pixel
@@ -0,0 +1,8 @@
1
+ modisp/__init__.py,sha256=QYTa66QaH-vcGYoaWLSpAErfTq-HBKXollieRsru7ZI,253
2
+ modisp/_ljpeg.py,sha256=1NKgNBFg91rMbo3An47yMMvNpnkTwwxXa5VFq90jus8,9272
3
+ modisp/_reader.py,sha256=hveL4bky3fe3C7ZdQ3JfZcTxODKRHtvvxE51UMX7v1s,12767
4
+ modisp/_tiff.py,sha256=9nMKbxKuro6ZYB2xh5Z9MrVmpXETdn2gujWjyt41opc,5409
5
+ modisp/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
+ modisp-0.0.2.dist-info/METADATA,sha256=B_AVkCDlegQjiBWP8uH5Ru2WerWTKNHPG934QMCiKS4,1303
7
+ modisp-0.0.2.dist-info/WHEEL,sha256=2iZrJkDIkeDh0k9SikKMqFuRP-ua6mb4Ir1AsER4BEw,87
8
+ modisp-0.0.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.33.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any