modisp 0.0.1__tar.gz → 0.0.2__tar.gz

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-0.0.2/PKG-INFO ADDED
@@ -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
modisp-0.0.2/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # modisp
2
+
3
+ ## Install
4
+
5
+ pip install modisp
6
+
7
+ ## Usage
8
+
9
+ import modisp
10
+ img = modisp.read_raw("photo.dng")
11
+ print(img.data.shape, img.data.dtype)
12
+ print(img.cfa_pattern, img.black_level, img.white_level)
13
+ meta = modisp.read_metadata("photo.dng")
14
+ print(meta.make, meta.model, meta.cfa_colors)
15
+
16
+ ## Supported compressions
17
+
18
+ - 1: uncompressed, 1 to 16 bits per sample, packed or byte aligned
19
+ - 7: lossless JPEG (ITU-T T.81 process 14), predictors 1 to 7
20
+ - 8 and 32946: deflate, with predictor 1 or 2
21
+
22
+ ## Raises NotImplementedError
23
+
24
+ - 34892: lossy JPEG
25
+ - 52546: JPEG XL
26
+ - 34713 and any other compression code
27
+ - floating point and signed sample formats, floating point predictors
28
+ - separate planar configuration with more than one sample per pixel
@@ -0,0 +1,30 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "modisp"
7
+ version = "0.0.2"
8
+ description = "A small reader for raw camera images (DNG)."
9
+ readme = "README.md"
10
+ requires-python = ">=3.9"
11
+ dependencies = ["numpy>=1.20"]
12
+ classifiers = [
13
+ "Programming Language :: Python :: 3",
14
+ "Programming Language :: Python :: 3.9",
15
+ "Programming Language :: Python :: 3.10",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: 3.13",
19
+ "Operating System :: OS Independent",
20
+ ]
21
+
22
+ [tool.hatch.build.targets.sdist]
23
+ include = ["src/modisp", "tests", "README.md", "pyproject.toml"]
24
+
25
+ [tool.hatch.build.targets.wheel]
26
+ packages = ["src/modisp"]
27
+
28
+ [tool.pytest.ini_options]
29
+ pythonpath = ["src"]
30
+ testpaths = ["tests"]
@@ -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
+ ]
@@ -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)