micro-reader 0.4.1__tar.gz → 0.4.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.
Files changed (74) hide show
  1. {micro_reader-0.4.1 → micro_reader-0.4.2}/PKG-INFO +7 -7
  2. {micro_reader-0.4.1 → micro_reader-0.4.2}/README.md +5 -5
  3. {micro_reader-0.4.1 → micro_reader-0.4.2}/pyproject.toml +2 -2
  4. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_codecs.py +33 -13
  5. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_mosaic.py +2 -2
  6. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_version.py +1 -1
  7. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_czi_metadata.py +1 -1
  8. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/czi.py +24 -10
  9. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/oir.py +15 -1
  10. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/tiff.py +12 -2
  11. {micro_reader-0.4.1 → micro_reader-0.4.2}/.gitignore +0 -0
  12. {micro_reader-0.4.1 → micro_reader-0.4.2}/LICENSE +0 -0
  13. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/__init__.py +0 -0
  14. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_axes.py +0 -0
  15. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_base.py +0 -0
  16. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_cfb.py +0 -0
  17. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_errors.py +0 -0
  18. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_eubi_guard.py +0 -0
  19. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_fileset.py +0 -0
  20. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_groups.py +0 -0
  21. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_index.py +0 -0
  22. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_io.py +0 -0
  23. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_jpeg_restarts.py +0 -0
  24. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_lambda.py +0 -0
  25. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_memory.py +0 -0
  26. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_metadata.py +0 -0
  27. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_naming.py +0 -0
  28. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_outline.py +0 -0
  29. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_parts.py +0 -0
  30. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_plate.py +0 -0
  31. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_pool.py +0 -0
  32. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_registry.py +0 -0
  33. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_spatial.py +0 -0
  34. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_stack.py +0 -0
  35. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/_stream.py +0 -0
  36. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/__init__.py +0 -0
  37. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_ets.py +0 -0
  38. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_hdf5.py +0 -0
  39. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_lif_metadata.py +0 -0
  40. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_nd2_legacy.py +0 -0
  41. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_nd2_metadata.py +0 -0
  42. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_nd2_run.py +0 -0
  43. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_nd2_structure.py +0 -0
  44. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_tiff_metadata.py +0 -0
  45. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_tiff_variants.py +0 -0
  46. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/_vsi_tree.py +0 -0
  47. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/bdv.py +0 -0
  48. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/cellomics.py +0 -0
  49. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/columbus.py +0 -0
  50. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/companion.py +0 -0
  51. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/cv7000.py +0 -0
  52. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/dv.py +0 -0
  53. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/flex.py +0 -0
  54. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/harmony.py +0 -0
  55. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/hdf5.py +0 -0
  56. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/ics.py +0 -0
  57. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/ilastik.py +0 -0
  58. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/ims.py +0 -0
  59. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/incell.py +0 -0
  60. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/jdce.py +0 -0
  61. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/klb.py +0 -0
  62. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/lif.py +0 -0
  63. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/luxendo.py +0 -0
  64. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/metamorph.py +0 -0
  65. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/metaxpress.py +0 -0
  66. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/micromanager.py +0 -0
  67. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/mrc.py +0 -0
  68. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/nd2.py +0 -0
  69. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/ndpis.py +0 -0
  70. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/oif.py +0 -0
  71. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/omp2info.py +0 -0
  72. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/raster.py +0 -0
  73. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/scanr.py +0 -0
  74. {micro_reader-0.4.1 → micro_reader-0.4.2}/src/micro_reader/backends/vsi.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: micro-reader
3
- Version: 0.4.1
3
+ Version: 0.4.2
4
4
  Summary: Low-level, memory-bounded pixel reading for microscopy formats: read exactly the rectangle you ask for.
5
5
  Project-URL: Homepage, https://github.com/bugraoezdemir/micro-reader
6
6
  Project-URL: Source, https://github.com/bugraoezdemir/micro-reader
@@ -18,7 +18,7 @@ Classifier: Programming Language :: Python :: 3.13
18
18
  Classifier: Programming Language :: Python :: 3.14
19
19
  Classifier: Topic :: Scientific/Engineering :: Image Processing
20
20
  Requires-Python: >=3.11
21
- Requires-Dist: micro-reader-codecs<0.1,>=0.0.2
21
+ Requires-Dist: micro-reader-codecs<0.1,>=0.0.3
22
22
  Requires-Dist: numpy>=1.24
23
23
  Requires-Dist: tifffile>=2025.5.21
24
24
  Provides-Extra: all
@@ -73,8 +73,8 @@ pip install micro-reader
73
73
  Optional support:
74
74
 
75
75
  ```bash
76
- pip install "micro-reader[ims]" # Imaris (.ims)
77
- pip install "micro-reader[codecs]" # additional codecs
76
+ pip install "micro-reader[hdf5]" # Imaris (.ims), ilastik, BigDataViewer, Luxendo, Fiji HDF5
77
+ pip install "micro-reader[codecs]" # LERC, CCITT fax and other rare TIFF codecs
78
78
  ```
79
79
 
80
80
  ## Quick start
@@ -657,7 +657,7 @@ Supported codecs include:
657
657
  * PNG
658
658
  * EER
659
659
 
660
- Additional codecs such as JPEG XR and LERC are available with:
660
+ JPEG (NDPI's included) and JPEG XR decode with libjpeg-turbo and jxrlib, built into micro-reader-codecs: the decoders imagecodecs uses, with its pixels and speed. LERC, CCITT fax, float24 and a few rarer codecs need imagecodecs:
661
661
 
662
662
  ```bash
663
663
  pip install "micro-reader[codecs]"
@@ -704,7 +704,7 @@ CZI files support:
704
704
  CZI-specific operations include:
705
705
 
706
706
  ```python
707
- image.read_region(x, y, w, h)
707
+ image.read_region(y=y, x=x, height=h, width=w)
708
708
  ```
709
709
 
710
710
  and:
@@ -713,7 +713,7 @@ and:
713
713
  f.canvas()
714
714
  ```
715
715
 
716
- `image.origin` gives the image corner in the file's pixel coordinates.
716
+ `image.origin` gives the image corner `(y, x)` in the file's pixel coordinates.
717
717
 
718
718
  Damaged subblock directories can be recovered with a `DamagedFileWarning`. Details are available through:
719
719
 
@@ -19,8 +19,8 @@ pip install micro-reader
19
19
  Optional support:
20
20
 
21
21
  ```bash
22
- pip install "micro-reader[ims]" # Imaris (.ims)
23
- pip install "micro-reader[codecs]" # additional codecs
22
+ pip install "micro-reader[hdf5]" # Imaris (.ims), ilastik, BigDataViewer, Luxendo, Fiji HDF5
23
+ pip install "micro-reader[codecs]" # LERC, CCITT fax and other rare TIFF codecs
24
24
  ```
25
25
 
26
26
  ## Quick start
@@ -603,7 +603,7 @@ Supported codecs include:
603
603
  * PNG
604
604
  * EER
605
605
 
606
- Additional codecs such as JPEG XR and LERC are available with:
606
+ JPEG (NDPI's included) and JPEG XR decode with libjpeg-turbo and jxrlib, built into micro-reader-codecs: the decoders imagecodecs uses, with its pixels and speed. LERC, CCITT fax, float24 and a few rarer codecs need imagecodecs:
607
607
 
608
608
  ```bash
609
609
  pip install "micro-reader[codecs]"
@@ -650,7 +650,7 @@ CZI files support:
650
650
  CZI-specific operations include:
651
651
 
652
652
  ```python
653
- image.read_region(x, y, w, h)
653
+ image.read_region(y=y, x=x, height=h, width=w)
654
654
  ```
655
655
 
656
656
  and:
@@ -659,7 +659,7 @@ and:
659
659
  f.canvas()
660
660
  ```
661
661
 
662
- `image.origin` gives the image corner in the file's pixel coordinates.
662
+ `image.origin` gives the image corner `(y, x)` in the file's pixel coordinates.
663
663
 
664
664
  Damaged subblock directories can be recovered with a `DamagedFileWarning`. Details are available through:
665
665
 
@@ -6,7 +6,7 @@ build-backend = "hatchling.build"
6
6
 
7
7
  [project]
8
8
  name = "micro-reader"
9
- version = "0.4.1"
9
+ version = "0.4.2"
10
10
  description = "Low-level, memory-bounded pixel reading for microscopy formats: read exactly the rectangle you ask for."
11
11
  readme = "README.md"
12
12
  requires-python = ">=3.11"
@@ -29,7 +29,7 @@ classifiers = [
29
29
  # TIFF covers most microscopy images, so it is not an extra (Bugra,
30
30
  # 2026-10-01) until micro-reader parses TIFF itself (post-0.0.1). The floor
31
31
  # matches eubi-bridge's (tifffile>=2025.5.21), so both install together.
32
- dependencies = ["numpy>=1.24", "micro-reader-codecs>=0.0.2,<0.1", "tifffile>=2025.5.21"]
32
+ dependencies = ["numpy>=1.24", "micro-reader-codecs>=0.0.3,<0.1", "tifffile>=2025.5.21"]
33
33
 
34
34
  [project.urls]
35
35
  Homepage = "https://github.com/bugraoezdemir/micro-reader"
@@ -76,16 +76,26 @@ def zstd_decompress_into(data, out: np.ndarray, *, native: bool | None = None) -
76
76
  raise CorruptFileError(f"zstd data decodes to {written} bytes, expected {len(view)}")
77
77
 
78
78
 
79
- def _imagecodecs(what: str):
79
+ def _imagecodecs(what: str, native_from: str | None = None):
80
+ """imagecodecs, or an error naming the way out: *native_from*, the
81
+ micro-reader-codecs release whose native decoder reads it, when there is one."""
80
82
  try:
81
83
  import imagecodecs
82
84
  except ImportError as exc:
83
- raise MicroReaderError(
84
- f"{what} needs micro-reader's native module or the [codecs] extra (imagecodecs)"
85
- ) from exc
85
+ fix = 'pip install "micro-reader[codecs]"'
86
+ if native_from:
87
+ fix = (f'pip install -U "micro-reader-codecs>={native_from}" (its own decoder), '
88
+ f"or {fix}")
89
+ raise MicroReaderError(f"{what} needs a decoder that is not installed: {fix}") from exc
86
90
  return imagecodecs
87
91
 
88
92
 
93
+ def has_libjpeg_turbo() -> bool:
94
+ """micro-reader-codecs links libjpeg-turbo (0.0.3 on): every JPEG micro-reader
95
+ decodes, NDPI's and 12-bit ones included, at its speed and with its pixels."""
96
+ return bool(getattr(_native, "HAS_LIBJPEG_TURBO", False))
97
+
98
+
89
99
  def _use_native(native, name: str) -> bool:
90
100
  if native is None:
91
101
  return _native is not None and hasattr(_native, name)
@@ -360,11 +370,14 @@ def jpeg_decode_into(data, out: np.ndarray, *, colour: str = "auto",
360
370
  samples]) little-endian, colour in R, G, B order. Returns (width,
361
371
  height, samples, bits). *colour*: "auto" (the JPEG's own markers),
362
372
  "ycbcr", or "rgb" (stored without a colour transform: TIFF Photometric
363
- RGB). Baseline and extended 8-bit JPEG decode to libjpeg-turbo's pixels
364
- (imagecodecs, tifffile, Pillow), bit for bit; progressive JPEG may differ
365
- from them by a few grey levels (allowed by the JPEG standard); lossless is
366
- exact. 12-bit lossy JPEG goes to imagecodecs."""
367
- if _use_native(native, "jpeg_decode_into") and not _is_12bit_lossy_jpeg(data):
373
+ RGB). With micro-reader-codecs 0.0.3 on, libjpeg-turbo itself decodes
374
+ (the decoder of imagecodecs, tifffile and Pillow): its pixels and speed,
375
+ for 8-bit, 12-bit and lossless JPEG. Before, baseline and extended 8-bit
376
+ JPEG decoded to libjpeg-turbo's pixels, bit for bit, progressive JPEG
377
+ could differ by a few grey levels, and 12-bit lossy JPEG went to
378
+ imagecodecs."""
379
+ if _use_native(native, "jpeg_decode_into") and (
380
+ has_libjpeg_turbo() or not _is_12bit_lossy_jpeg(data)):
368
381
  try:
369
382
  return _native.jpeg_decode_into(data, memoryview(out).cast("B"), colour)
370
383
  except ValueError as exc:
@@ -402,10 +415,17 @@ def jxl_decode_into(data, out: np.ndarray, *, native: bool | None = None):
402
415
  image.dtype.itemsize * 8)
403
416
 
404
417
 
405
- def jpegxr_decode_into(data, out: np.ndarray):
406
- """A JPEG XR codestream -> *out* (colour R, G, B). Through imagecodecs
407
- until the native jxrlib binding is decided (it needs libclang to build)."""
408
- codecs = _imagecodecs("JPEG XR data")
418
+ def jpegxr_decode_into(data, out: np.ndarray, *, native: bool | None = None):
419
+ """A JPEG XR codestream -> *out* (colour R, G, B; fixed point as float32).
420
+ micro-reader-codecs 0.0.3 on decodes it with jxrlib, as imagecodecs does and
421
+ with the same pixel-format conversions; imagecodecs otherwise."""
422
+ if _use_native(native, "jpegxr_decode_into"):
423
+ try:
424
+ _native.jpegxr_decode_into(data, memoryview(out).cast("B"))
425
+ return
426
+ except ValueError as exc:
427
+ raise CorruptFileError(f"corrupt JPEG XR data: {exc}") from exc
428
+ codecs = _imagecodecs("JPEG XR data", native_from="0.0.3")
409
429
  try:
410
430
  image = codecs.jpegxr_decode(bytes(data))
411
431
  except Exception as exc: # noqa: BLE001
@@ -19,7 +19,7 @@ class Mosaic(Image):
19
19
  """*tiles*: ``[(image, y, x)]`` in drawing order (later on top), with each
20
20
  tile's corner in canvas pixels. The tiles share axes, pixel type and
21
21
  every size but ``y`` / ``x``. The canvas starts at the smallest corner
22
- (``origin``: that corner, in the tiles' pixel grid)."""
22
+ (``origin``: that corner as ``(y, x)`` in the tiles' pixel grid)."""
23
23
 
24
24
  def __init__(self, position: int, tiles: list, name: str, index: dict, fill=0):
25
25
  first = tiles[0][0]
@@ -41,7 +41,7 @@ class Mosaic(Image):
41
41
  super().__init__(position, first.axes, shape, first.dtype, name=name, index=index)
42
42
  self._yi, self._xi = yi, xi
43
43
  self._tiles = [(t, y - y0, x - x0) for t, y, x in tiles]
44
- #: the canvas corner in the tiles' pixel grid
44
+ #: the canvas corner in the tiles' pixel grid, (y, x) as on every image
45
45
  self.origin = (y0, x0)
46
46
  self._fill = fill
47
47
  self._closed = False
@@ -1,2 +1,2 @@
1
1
  """micro-reader's version, in one place (the package, the import guard)."""
2
- __version__ = "0.4.1"
2
+ __version__ = "0.4.2"
@@ -221,7 +221,7 @@ def first_pixel_position(image, centre: dict, s, sizes: dict) -> dict:
221
221
  the whole file, so the offset between the two frames found at *s* holds
222
222
  for the image's corner too."""
223
223
  out = {}
224
- origin = dict(zip("xy", image.origin))
224
+ origin = dict(zip("yx", image.origin))
225
225
  extent = {"x": s.width, "y": s.height}
226
226
  start = {"x": s.x, "y": s.y}
227
227
  for axis, q in centre.items():
@@ -395,7 +395,7 @@ class _CziImage(Image):
395
395
  y0 = min(s.y for s in subblocks)
396
396
  x1 = max(s.x + s.width for s in subblocks)
397
397
  y1 = max(s.y + s.height for s in subblocks)
398
- self.origin = (x0, y0) # series box in image pixel coordinates
398
+ self._xy = (x0, y0) # series box corner, file pixel coordinates (x, y)
399
399
  # plane -> subblocks in drawing order: series, mosaic index, directory
400
400
  # order (libCZI's order; the series only matters on the whole-file canvas)
401
401
  by_plane: dict = {}
@@ -406,7 +406,7 @@ class _CziImage(Image):
406
406
  s.plane.get(d, czi._dim_starts.get(d, 0)) - czi._dim_starts.get(d, 0)
407
407
  for d in axes)
408
408
  by_plane.setdefault(key, []).append(s)
409
- self._planes = {key: _PlaneTiles(tiles, self.origin) for key, tiles in by_plane.items()}
409
+ self._planes = {key: _PlaneTiles(tiles, self._xy) for key, tiles in by_plane.items()}
410
410
  shape = [len(ids) if d == "C" and ids is not None else czi._dim_sizes.get(d, 1)
411
411
  for d in axes] + [y1 - y0, x1 - x0]
412
412
  letters = "".join(d.lower() for d in axes) + "yx"
@@ -564,7 +564,7 @@ class _CziImage(Image):
564
564
  def _draw(self, s: Subblock, piece, band_y0, xs, dst) -> int:
565
565
  """Draw *piece* (y0, y1, x0, x1 in series coordinates, inside subblock
566
566
  *s* and inside the band) onto *dst* (the band: rows, cols[, samples])."""
567
- ox, oy = self.origin
567
+ ox, oy = self._xy
568
568
  sy0, sx0 = s.y - oy, s.x - ox # tile's place in the series
569
569
  y0, y1, x0, x1 = piece
570
570
  item = self._file_dtype.itemsize * self._samples
@@ -643,7 +643,7 @@ class _CziImage(Image):
643
643
  """*piece* of subblock *s* from its rows alone, decoded as a stream in
644
644
  pieces of rows, each held within the memory budget."""
645
645
  codec, offset, length = source
646
- ox, oy = self.origin
646
+ ox, oy = self._xy
647
647
  sy0, sx0 = s.y - oy, s.x - ox
648
648
  y0, y1, x0, x1 = piece
649
649
  row = s.width * self._samples
@@ -668,7 +668,7 @@ class _CziImage(Image):
668
668
  covering it) or of a JPEG subblock with restart markers (cut out of
669
669
  it, ``_jpeg_restarts``), in bands of rows within the budget; None
670
670
  for a JPEG that cannot be cut."""
671
- ox, oy = self.origin
671
+ ox, oy = self._xy
672
672
  sy0, sx0 = s.y - oy, s.x - ox
673
673
  y0, y1, x0, x1 = piece
674
674
  base, size = self._data_offset(s), s.data_size
@@ -733,7 +733,7 @@ class _CziImage(Image):
733
733
  def _draw_span(self, s: Subblock, pieces, band_y0, xs, dst) -> int:
734
734
  """Uncompressed *pieces* of subblock *s* from ONE contiguous read: from
735
735
  the first piece pixel to the last, the bytes between them included."""
736
- ox, oy = self.origin
736
+ ox, oy = self._xy
737
737
  sy0, sx0 = s.y - oy, s.x - ox
738
738
  by0, by1 = min(p[0] for p in pieces), max(p[1] for p in pieces)
739
739
  bx0, bx1 = min(p[2] for p in pieces), max(p[3] for p in pieces)
@@ -808,14 +808,28 @@ class _CziImage(Image):
808
808
  out = out[..., ss]
809
809
  return out, read
810
810
 
811
- def read_region(self, x: int, y: int, width: int, height: int, **planes) -> np.ndarray:
811
+ @property
812
+ def origin(self) -> tuple:
813
+ """``(y, x)`` of the series' corner in the file's own pixel coordinates
814
+ (libCZI's bounding box; libCZI writes it x first)."""
815
+ x, y = self._xy
816
+ return (y, x)
817
+
818
+ def read_region(self, *args, y: int = None, x: int = None, height: int = None,
819
+ width: int = None, **planes) -> np.ndarray:
812
820
  """A rectangle in the file's own pixel coordinates (libCZI's ``roi``,
813
821
  e.g. from ``origin`` or the XML metadata), composed from this series'
814
822
  tiles; parts outside the series' box are background (0), as libCZI
815
- returns them. *planes* select the other axes like ``read``."""
823
+ returns them. *planes* select the other axes like ``read``. Keywords
824
+ only: before 0.4.2 it took ``(x, y, width, height)`` by position, so a
825
+ positional call is refused rather than read with y and x swapped."""
826
+ if args or None in (y, x, height, width):
827
+ raise TypeError("read_region takes keywords only: read_region(y=, x=, height=, "
828
+ "width=, **planes) (micro-reader 0.4.2; it took (x, y, width, "
829
+ "height) by position before)")
816
830
  if width < 0 or height < 0:
817
831
  raise ValueError(f"negative region size {width} x {height}")
818
- ox, oy = self.origin
832
+ ox, oy = self._xy
819
833
  ny, nx = self.shape[self.axes.index("y")], self.shape[self.axes.index("x")]
820
834
  y0, y1 = min(max(y - oy, 0), ny), min(max(y + height - oy, 0), ny)
821
835
  x0, x1 = min(max(x - ox, 0), nx), min(max(x + width - ox, 0), nx)
@@ -1171,7 +1185,7 @@ class _CziFile(File):
1171
1185
  mine = [e for e in pyramid if self._key_of(e) == values]
1172
1186
  if not mine:
1173
1187
  return []
1174
- x0, y0 = image.origin
1188
+ x0, y0 = image._xy
1175
1189
  ny, nx = image.shape[image.axes.index("y")], image.shape[image.axes.index("x")]
1176
1190
  by_zoom: dict = {}
1177
1191
  for e in mine:
@@ -69,6 +69,7 @@ from .._io import ThreadHandles
69
69
  NAME = "oir"
70
70
  MAGIC = b"OLYMPUSRAWFORMAT"
71
71
  RAW_TASK_BYTES = 8 << 20 # planes are read in row pieces of this size
72
+ SEPARATE_READ_BYTES = 32 << 10 # bytes worth skipping rather than issuing one more read
72
73
  BLOCK_METADATA, BLOCK_FRAMEPROPERTIES, BLOCK_UID, BLOCK_PIXEL = 0, 1, 3, 4
73
74
  UID_PATTERN = re.compile(
74
75
  r"(?:l(\d+))?(?:z(\d+))?(?:t(\d+))?(?:_(\d+)_(\d+))?_([0-9a-f-]+)_(\d+)$")
@@ -477,7 +478,9 @@ class _OirImage(Image):
477
478
  return end - start
478
479
 
479
480
  def _read_rows(self, plane, y0, y1, xs, dst) -> int:
480
- """Rows y0:y1, columns xs of a plane into *dst* (rows, columns)."""
481
+ """Rows y0:y1, columns xs of a plane into *dst* (rows, columns).
482
+ Returns the values read (more than *dst* holds when skipped columns
483
+ were read along)."""
481
484
  blocks, starts = plane
482
485
  item, row = self._file_dtype.itemsize, self._row_bytes
483
486
  direct = NATIVE_LITTLE or item == 1
@@ -489,6 +492,17 @@ class _OirImage(Image):
489
492
  self._read_range(blocks, starts, y0 * row, memoryview(buf).cast("B"))
490
493
  dst[...] = buf
491
494
  return dst.size
495
+ rows, width = y1 - y0, (xs.stop - xs.start) * item
496
+ if rows > 1 and row - width <= SEPARATE_READ_BYTES:
497
+ # a few skipped bytes per row cost less than one more read per row (a
498
+ # 512-column block of a 3554-column frame was 512 reads): the span from
499
+ # the first row's columns to the last row's, in one read (as CZI, LIF, MRC)
500
+ span = (rows - 1) * row + width
501
+ flat = np.empty(rows * row, np.uint8)
502
+ self._read_range(blocks, starts, y0 * row + xs.start * item,
503
+ memoryview(flat)[:span])
504
+ dst[...] = flat.reshape(rows, row)[:, :width].view(self._file_dtype)
505
+ return span // item
492
506
  buf = None if direct else np.empty(xs.stop - xs.start, self._file_dtype)
493
507
  for i, y in enumerate(range(y0, y1)):
494
508
  target = dst[i] if direct else buf
@@ -142,7 +142,11 @@ def own_codec(key):
142
142
  return None
143
143
  codec = IMAGE_CODECS[c]
144
144
  if codec == "jpeg" and getattr(key, "jpegheader", None):
145
- return None # NDPI: header outside the segments
145
+ # NDPI: header outside the segments; _decode_segment joins it to each
146
+ # stripe. Native when micro-reader-codecs links libjpeg-turbo (0.0.3 on):
147
+ # its speed and pixels, no imagecodecs. Before that tifffile + imagecodecs
148
+ # (micro-reader's own JPEG decoder was 1.5-1.9x slower per stripe)
149
+ return "jpeg" if _codecs.has_libjpeg_turbo() else None
146
150
  if codec != "jpeg" and key.is_subsampled:
147
151
  return None
148
152
  return codec
@@ -1055,7 +1059,13 @@ class _TiffImage(Image):
1055
1059
  _codecs.eer_decode_into(data, seg.reshape(shape[:2]), *eer_parameters(self._key))
1056
1060
  return seg[None]
1057
1061
  if codec == "jpeg":
1058
- if jpegtables: # abbreviated stream: tables from JPEGTables
1062
+ header = getattr(self._key, "jpegheader", None)
1063
+ if header:
1064
+ # NDPI: tifffile cuts the one big JPEG at its restart markers (McuStarts)
1065
+ # into tiles of one restart interval; each is the shared header, its size
1066
+ # patched to the tile (tifffile.ndpi_jpeg_tile), + its bytes + EOI
1067
+ data = b"".join((header, data, b"\xff\xd9"))
1068
+ elif jpegtables: # abbreviated stream: tables from JPEGTables
1059
1069
  data = bytes(jpegtables[:-2]) + bytes(data[2:])
1060
1070
  got = _codecs.jpeg_decode_into(data, seg, colour=self._jpeg_colour)
1061
1071
  else:
File without changes
File without changes