opentile-jpeg 0.1.0__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 (31) hide show
  1. opentile_jpeg-0.1.0/CHANGELOG.md +28 -0
  2. opentile_jpeg-0.1.0/CMakeLists.txt +65 -0
  3. opentile_jpeg-0.1.0/LICENSE +176 -0
  4. opentile_jpeg-0.1.0/PKG-INFO +113 -0
  5. opentile_jpeg-0.1.0/README.md +93 -0
  6. opentile_jpeg-0.1.0/licenses/libjpeg-turbo-LICENSE.md +135 -0
  7. opentile_jpeg-0.1.0/licenses/libjpeg-turbo-README.ijg +260 -0
  8. opentile_jpeg-0.1.0/pyproject.toml +84 -0
  9. opentile_jpeg-0.1.0/src/binding.cpp +388 -0
  10. opentile_jpeg-0.1.0/src/engine.c +875 -0
  11. opentile_jpeg-0.1.0/src/engine.h +153 -0
  12. opentile_jpeg-0.1.0/src/opentile_jpeg/__init__.py +80 -0
  13. opentile_jpeg-0.1.0/src/opentile_jpeg/_binding.pyi +73 -0
  14. opentile_jpeg-0.1.0/src/opentile_jpeg/core.py +450 -0
  15. opentile_jpeg-0.1.0/src/opentile_jpeg/errors.py +36 -0
  16. opentile_jpeg-0.1.0/src/opentile_jpeg/models.py +87 -0
  17. opentile_jpeg-0.1.0/src/opentile_jpeg/py.typed +0 -0
  18. opentile_jpeg-0.1.0/tests/conftest.py +115 -0
  19. opentile_jpeg-0.1.0/tests/test_coefficients.py +113 -0
  20. opentile_jpeg-0.1.0/tests/test_compose.py +300 -0
  21. opentile_jpeg-0.1.0/tests/test_corrupt_input.py +152 -0
  22. opentile_jpeg-0.1.0/tests/test_crop.py +263 -0
  23. opentile_jpeg-0.1.0/tests/test_encodings.py +206 -0
  24. opentile_jpeg-0.1.0/tests/test_fill.py +104 -0
  25. opentile_jpeg-0.1.0/tests/test_header.py +34 -0
  26. opentile_jpeg-0.1.0/tests/test_metadata.py +134 -0
  27. opentile_jpeg-0.1.0/tests/test_optimize.py +37 -0
  28. opentile_jpeg-0.1.0/tests/test_package.py +151 -0
  29. opentile_jpeg-0.1.0/tests/test_properties.py +421 -0
  30. opentile_jpeg-0.1.0/tests/test_threads.py +67 -0
  31. opentile_jpeg-0.1.0/tools/build_turbo.py +145 -0
@@ -0,0 +1,28 @@
1
+ # opentile-jpeg changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ ## [0.1.0] - 2026-11-09
11
+
12
+ ### Added
13
+
14
+ - Initial version: lossless JPEG operations on the DCT coefficients, built on
15
+ libjpeg-turbo.
16
+ - `crop` and `crop_multiple`, optionally extending past the image edges with a
17
+ `background` color.
18
+ - `compose`, to re-tile sources placed on a grid into output tiles.
19
+ - `optimize`, rebuilding with optimal Huffman tables, optionally progressive.
20
+ - `fill`, a solid-color tile matching a template's size and encoding.
21
+ - `header`, reading dimensions and sampling without decoding.
22
+ - `decode_coefficients` and `encode_coefficients`, DCT coefficients as numpy
23
+ arrays.
24
+ - Metadata (ICC, EXIF, XMP, comments) kept by default, removable with `strip`.
25
+ - A per-call `max_pixels` limit, by default `DEFAULT_MAX_PIXELS` (Pillow's),
26
+ refusing larger images with `JpegTooLargeError` before allocating for them.
27
+ - Wheels with libjpeg-turbo included, for Linux (x86_64 and aarch64), Windows
28
+ (x64 and ARM64) and macOS (Apple Silicon and Intel), Python 3.11 or later.
@@ -0,0 +1,65 @@
1
+ # nanobind extension build: statically links the pinned libjpeg-turbo (built on
2
+ # demand by tools/build_turbo.py, cached under .turbo/<version>-<build hash>).
3
+ cmake_minimum_required(VERSION 3.18)
4
+ project(opentile_jpeg LANGUAGES C CXX)
5
+
6
+ # Development.SABIModule is required for STABLE_ABI to actually take effect —
7
+ # without it nanobind silently falls back to a per-version (full API) build.
8
+ find_package(Python 3.11 COMPONENTS Interpreter Development.Module
9
+ Development.SABIModule REQUIRED)
10
+
11
+ execute_process(
12
+ COMMAND "${Python_EXECUTABLE}" -m nanobind --cmake_dir
13
+ OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE nanobind_ROOT)
14
+ find_package(nanobind CONFIG REQUIRED)
15
+
16
+ # Ensure the pinned libjpeg-turbo static lib exists (fetch+build if missing;
17
+ # OPENTILE_JPEG_TURBO_PREFIX overrides — see tools/build_turbo.py).
18
+ execute_process(
19
+ COMMAND "${Python_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/tools/build_turbo.py"
20
+ --cmake "${CMAKE_COMMAND}"
21
+ OUTPUT_STRIP_TRAILING_WHITESPACE OUTPUT_VARIABLE TURBO_PREFIX
22
+ RESULT_VARIABLE _turbo_rc)
23
+ if(_turbo_rc)
24
+ message(FATAL_ERROR "tools/build_turbo.py failed (rc=${_turbo_rc})")
25
+ endif()
26
+ # The script logs to stderr and prints the prefix last; keep only that line.
27
+ string(REGEX REPLACE ".*\n" "" TURBO_PREFIX "${TURBO_PREFIX}")
28
+ find_library(TURBO_STATIC NAMES jpeg-static jpeg
29
+ PATHS "${TURBO_PREFIX}/lib" "${TURBO_PREFIX}/lib64" NO_DEFAULT_PATH)
30
+ if(NOT TURBO_STATIC)
31
+ message(FATAL_ERROR "no libjpeg-turbo static lib under ${TURBO_PREFIX}")
32
+ endif()
33
+
34
+ # STABLE_ABI: abi3 wheel when building on Python >= 3.12, per-version below.
35
+ nanobind_add_module(_binding STABLE_ABI
36
+ src/binding.cpp
37
+ src/engine.c)
38
+ target_include_directories(_binding PRIVATE "${TURBO_PREFIX}/include")
39
+ target_link_libraries(_binding PRIVATE "${TURBO_STATIC}")
40
+ # ELF exports a static library's symbols and binds calls to them at load time,
41
+ # so a libjpeg already loaded globally (the host program's, an RTLD_GLOBAL or
42
+ # LD_PRELOAD one) would take over our jpeg_* calls. Keep them local instead.
43
+ # (Mach-O binds to the defining library and Windows exports nothing unasked.)
44
+ if(UNIX AND NOT APPLE)
45
+ target_link_options(_binding PRIVATE "LINKER:--exclude-libs,ALL")
46
+ endif()
47
+
48
+ nanobind_add_stub(_binding_stub
49
+ MODULE _binding
50
+ OUTPUT _binding.pyi
51
+ PYTHON_PATH $<TARGET_FILE_DIR:_binding>
52
+ DEPENDS _binding)
53
+
54
+ # Mirror the stub into the source tree so type checkers resolve
55
+ # opentile_jpeg._binding without a build product on the import path (the editable
56
+ # install keeps the extension out of src/). Committed, build-refreshed.
57
+ add_custom_target(_binding_stub_src ALL
58
+ COMMAND "${CMAKE_COMMAND}" -E copy_if_different
59
+ "${CMAKE_CURRENT_BINARY_DIR}/_binding.pyi"
60
+ "${CMAKE_CURRENT_SOURCE_DIR}/src/opentile_jpeg/_binding.pyi"
61
+ DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/_binding.pyi")
62
+
63
+ # The stub reaches wheels via its src/ mirror (wheel.packages); installing it
64
+ # here too would collide with that copy.
65
+ install(TARGETS _binding LIBRARY DESTINATION opentile_jpeg)
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,113 @@
1
+ Metadata-Version: 2.4
2
+ Name: opentile-jpeg
3
+ Version: 0.1.0
4
+ Summary: Lossless JPEG operations in the DCT-coefficient domain: crop, compose, optimize, and fill without re-encoding.
5
+ Keywords: jpeg,lossless,dct,coefficient,crop,jpegtran,whole slide image
6
+ Author-Email: Erik O Gabrielsson <erik.o.gabrielsson@sectra.com>
7
+ License-Expression: Apache-2.0
8
+ License-File: LICENSE
9
+ License-File: licenses/libjpeg-turbo-LICENSE.md
10
+ License-File: licenses/libjpeg-turbo-README.ijg
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Operating System :: OS Independent
14
+ Classifier: Topic :: Multimedia :: Graphics
15
+ Classifier: Programming Language :: Python :: 3
16
+ Project-URL: Repository, https://github.com/imi-bigpicture/opentile
17
+ Requires-Python: >=3.11
18
+ Requires-Dist: numpy>=1.22
19
+ Description-Content-Type: text/markdown
20
+
21
+ # opentile-jpeg
22
+
23
+ Lossless JPEG operations that work directly on the compressed DCT coefficients:
24
+ crop, compose (re-tile), optimize and fill, plus coefficient access as numpy
25
+ arrays. Pixels are never decoded or re-encoded, so there is no quality loss.
26
+
27
+ Built on libjpeg-turbo, which is bundled in the wheels: no system install needed.
28
+
29
+ ## Install
30
+
31
+ ```sh
32
+ pip install opentile-jpeg
33
+ ```
34
+
35
+ ## Use
36
+
37
+ ```python
38
+ import opentile_jpeg
39
+
40
+ # Crop a region (x, y, width, height).
41
+ tile = opentile_jpeg.crop(jpeg, 512, 0, 512, 512)
42
+
43
+ # Crop many regions from one source, decoded once; crops are produced lazily.
44
+ for tile in opentile_jpeg.crop_multiple(jpeg, [(0, 0, 512, 512), (512, 0, 512, 512)]):
45
+ ...
46
+
47
+ # Crop past the right/bottom edge, filling outside the image with a color.
48
+ edge_tile = opentile_jpeg.crop(jpeg, 1536, 0, 512, 512, background=255)
49
+
50
+ # Shrink with optimal Huffman tables.
51
+ smaller = opentile_jpeg.optimize(jpeg)
52
+
53
+ # A solid-color tile with the template's size and encoding.
54
+ blank = opentile_jpeg.fill(template, (255, 255, 255))
55
+
56
+ # Re-tile sources placed on a grid at (gx, gy), e.g. overlapping scanner tiles,
57
+ # into output tiles at the given positions.
58
+ for tile in opentile_jpeg.compose(
59
+ [(left, 0, 0), (right, 1, 0)],
60
+ content_size=(448, 448), # source size minus overlap
61
+ tile_size=(512, 512),
62
+ positions=[(0, 0), (512, 0)],
63
+ ):
64
+ ...
65
+
66
+ # Edit DCT coefficients and encode them back.
67
+ coeffs, info = opentile_jpeg.decode_coefficients(jpeg)
68
+ coeffs[0][:, :, 1:, :] = 0 # one (rows, cols, 8, 8) int16 array per component
69
+ edited = opentile_jpeg.encode_coefficients(coeffs, info)
70
+ ```
71
+
72
+ Positions and sizes must follow the source's MCU grid, given by its subsampling
73
+ (see `opentile_jpeg.header`):
74
+
75
+ | Subsampling | MCU (width × height) |
76
+ |-------------|----------------------|
77
+ | 4:4:4 | 8 × 8 |
78
+ | 4:2:2 | 16 × 8 |
79
+ | 4:4:0 | 8 × 16 |
80
+ | 4:2:0 | 16 × 16 |
81
+ | 4:1:1 | 32 × 8 |
82
+
83
+ ## Metadata
84
+
85
+ Outputs keep the source's metadata (ICC profile, EXIF, XMP, comments) by
86
+ default. Remove any of it with `strip`:
87
+
88
+ ```python
89
+ opentile_jpeg.crop(jpeg, 0, 0, 512, 512, strip=opentile_jpeg.Metadata.EXIF)
90
+ ```
91
+
92
+ EXIF is copied as is, so after a crop or compose its dimensions and thumbnail
93
+ still describe the source.
94
+
95
+ ## Errors and warnings
96
+
97
+ - Invalid arguments, such as a region off the MCU grid, raise `ValueError`.
98
+ - JPEG data libjpeg-turbo cannot read raises `opentile_jpeg.JpegError`.
99
+ - Problems libjpeg-turbo recovers from, such as a truncated file, issue a
100
+ `opentile_jpeg.JpegWarning`.
101
+ - A JPEG claiming more pixels than `max_pixels` raises
102
+ `opentile_jpeg.JpegTooLargeError` (a `JpegError`) before anything is allocated
103
+ for it. Every operation that reads an image takes `max_pixels`, by default
104
+ `opentile_jpeg.DEFAULT_MAX_PIXELS` (about 179 million pixels, Pillow's
105
+ limit). Pass a larger value, or `None`, to the call that reads a legitimately
106
+ larger image; the limit applies to that call only.
107
+
108
+ ## License
109
+
110
+ opentile-jpeg is licensed under Apache-2.0. The wheels include libjpeg-turbo, under
111
+ the IJG and BSD-3-Clause licenses, whose texts are in `licenses/`.
112
+
113
+ This software is based in part on the work of the Independent JPEG Group.
@@ -0,0 +1,93 @@
1
+ # opentile-jpeg
2
+
3
+ Lossless JPEG operations that work directly on the compressed DCT coefficients:
4
+ crop, compose (re-tile), optimize and fill, plus coefficient access as numpy
5
+ arrays. Pixels are never decoded or re-encoded, so there is no quality loss.
6
+
7
+ Built on libjpeg-turbo, which is bundled in the wheels: no system install needed.
8
+
9
+ ## Install
10
+
11
+ ```sh
12
+ pip install opentile-jpeg
13
+ ```
14
+
15
+ ## Use
16
+
17
+ ```python
18
+ import opentile_jpeg
19
+
20
+ # Crop a region (x, y, width, height).
21
+ tile = opentile_jpeg.crop(jpeg, 512, 0, 512, 512)
22
+
23
+ # Crop many regions from one source, decoded once; crops are produced lazily.
24
+ for tile in opentile_jpeg.crop_multiple(jpeg, [(0, 0, 512, 512), (512, 0, 512, 512)]):
25
+ ...
26
+
27
+ # Crop past the right/bottom edge, filling outside the image with a color.
28
+ edge_tile = opentile_jpeg.crop(jpeg, 1536, 0, 512, 512, background=255)
29
+
30
+ # Shrink with optimal Huffman tables.
31
+ smaller = opentile_jpeg.optimize(jpeg)
32
+
33
+ # A solid-color tile with the template's size and encoding.
34
+ blank = opentile_jpeg.fill(template, (255, 255, 255))
35
+
36
+ # Re-tile sources placed on a grid at (gx, gy), e.g. overlapping scanner tiles,
37
+ # into output tiles at the given positions.
38
+ for tile in opentile_jpeg.compose(
39
+ [(left, 0, 0), (right, 1, 0)],
40
+ content_size=(448, 448), # source size minus overlap
41
+ tile_size=(512, 512),
42
+ positions=[(0, 0), (512, 0)],
43
+ ):
44
+ ...
45
+
46
+ # Edit DCT coefficients and encode them back.
47
+ coeffs, info = opentile_jpeg.decode_coefficients(jpeg)
48
+ coeffs[0][:, :, 1:, :] = 0 # one (rows, cols, 8, 8) int16 array per component
49
+ edited = opentile_jpeg.encode_coefficients(coeffs, info)
50
+ ```
51
+
52
+ Positions and sizes must follow the source's MCU grid, given by its subsampling
53
+ (see `opentile_jpeg.header`):
54
+
55
+ | Subsampling | MCU (width × height) |
56
+ |-------------|----------------------|
57
+ | 4:4:4 | 8 × 8 |
58
+ | 4:2:2 | 16 × 8 |
59
+ | 4:4:0 | 8 × 16 |
60
+ | 4:2:0 | 16 × 16 |
61
+ | 4:1:1 | 32 × 8 |
62
+
63
+ ## Metadata
64
+
65
+ Outputs keep the source's metadata (ICC profile, EXIF, XMP, comments) by
66
+ default. Remove any of it with `strip`:
67
+
68
+ ```python
69
+ opentile_jpeg.crop(jpeg, 0, 0, 512, 512, strip=opentile_jpeg.Metadata.EXIF)
70
+ ```
71
+
72
+ EXIF is copied as is, so after a crop or compose its dimensions and thumbnail
73
+ still describe the source.
74
+
75
+ ## Errors and warnings
76
+
77
+ - Invalid arguments, such as a region off the MCU grid, raise `ValueError`.
78
+ - JPEG data libjpeg-turbo cannot read raises `opentile_jpeg.JpegError`.
79
+ - Problems libjpeg-turbo recovers from, such as a truncated file, issue a
80
+ `opentile_jpeg.JpegWarning`.
81
+ - A JPEG claiming more pixels than `max_pixels` raises
82
+ `opentile_jpeg.JpegTooLargeError` (a `JpegError`) before anything is allocated
83
+ for it. Every operation that reads an image takes `max_pixels`, by default
84
+ `opentile_jpeg.DEFAULT_MAX_PIXELS` (about 179 million pixels, Pillow's
85
+ limit). Pass a larger value, or `None`, to the call that reads a legitimately
86
+ larger image; the limit applies to that call only.
87
+
88
+ ## License
89
+
90
+ opentile-jpeg is licensed under Apache-2.0. The wheels include libjpeg-turbo, under
91
+ the IJG and BSD-3-Clause licenses, whose texts are in `licenses/`.
92
+
93
+ This software is based in part on the work of the Independent JPEG Group.
@@ -0,0 +1,135 @@
1
+ libjpeg-turbo Licenses
2
+ ======================
3
+
4
+ libjpeg-turbo is covered by two compatible BSD-style open source licenses:
5
+
6
+ - The IJG (Independent JPEG Group) License, which is listed in
7
+ [README.ijg](README.ijg)
8
+
9
+ This license applies to the libjpeg API library and associated programs,
10
+ including any code inherited from libjpeg and any modifications to that
11
+ code. Note that the libjpeg-turbo SIMD source code bears the
12
+ [zlib License](https://opensource.org/licenses/Zlib), but in the context of
13
+ the overall libjpeg API library, the terms of the zlib License are subsumed
14
+ by the terms of the IJG License.
15
+
16
+ - The Modified (3-clause) BSD License, which is listed below
17
+
18
+ This license applies to the TurboJPEG API library and associated programs, as
19
+ well as the build system. Note that the TurboJPEG API library wraps the
20
+ libjpeg API library, so in the context of the overall TurboJPEG API library,
21
+ both the terms of the IJG License and the terms of the Modified (3-clause)
22
+ BSD License apply.
23
+
24
+
25
+ Complying with the libjpeg-turbo Licenses
26
+ =========================================
27
+
28
+ This section provides a roll-up of the libjpeg-turbo licensing terms, to the
29
+ best of our understanding. This is not a license in and of itself. It is
30
+ intended solely for clarification.
31
+
32
+ 1. If you are distributing a modified version of the libjpeg-turbo source,
33
+ then:
34
+
35
+ 1. You cannot alter or remove any existing copyright or license notices
36
+ from the source.
37
+
38
+ **Origin**
39
+ - Clause 1 of the IJG License
40
+ - Clause 1 of the Modified BSD License
41
+ - Clauses 1 and 3 of the zlib License
42
+
43
+ 2. You must add your own copyright notice to the header of each source
44
+ file you modified, so others can tell that you modified that file. (If
45
+ there is not an existing copyright header in that file, then you can
46
+ simply add a notice stating that you modified the file.)
47
+
48
+ **Origin**
49
+ - Clause 1 of the IJG License
50
+ - Clause 2 of the zlib License
51
+
52
+ 3. You must include the IJG README file, and you must not alter any of the
53
+ copyright or license text in that file.
54
+
55
+ **Origin**
56
+ - Clause 1 of the IJG License
57
+
58
+ 2. If you are distributing only libjpeg-turbo binaries without the source, or
59
+ if you are distributing an application that statically links with
60
+ libjpeg-turbo, then:
61
+
62
+ 1. Your product documentation must include a message stating:
63
+
64
+ This software is based in part on the work of the Independent JPEG
65
+ Group.
66
+
67
+ **Origin**
68
+ - Clause 2 of the IJG license
69
+
70
+ 2. If your binary distribution includes or uses the TurboJPEG API, then
71
+ your product documentation must include the text of the Modified BSD
72
+ License (see below.)
73
+
74
+ **Origin**
75
+ - Clause 2 of the Modified BSD License
76
+
77
+ 3. You cannot use the name of the IJG or The libjpeg-turbo Project or the
78
+ contributors thereof in advertising, publicity, etc.
79
+
80
+ **Origin**
81
+ - IJG License
82
+ - Clause 3 of the Modified BSD License
83
+
84
+ 4. The IJG and The libjpeg-turbo Project do not warrant libjpeg-turbo to be
85
+ free of defects, nor do we accept any liability for undesirable
86
+ consequences resulting from your use of the software.
87
+
88
+ **Origin**
89
+ - IJG License
90
+ - Modified BSD License
91
+ - zlib License
92
+
93
+
94
+ The Modified (3-clause) BSD License
95
+ ===================================
96
+
97
+ Copyright (C)2009-2023 D. R. Commander. All Rights Reserved.<br>
98
+ Copyright (C)2015 Viktor Szathmáry. All Rights Reserved.
99
+
100
+ Redistribution and use in source and binary forms, with or without
101
+ modification, are permitted provided that the following conditions are met:
102
+
103
+ - Redistributions of source code must retain the above copyright notice,
104
+ this list of conditions and the following disclaimer.
105
+ - Redistributions in binary form must reproduce the above copyright notice,
106
+ this list of conditions and the following disclaimer in the documentation
107
+ and/or other materials provided with the distribution.
108
+ - Neither the name of the libjpeg-turbo Project nor the names of its
109
+ contributors may be used to endorse or promote products derived from this
110
+ software without specific prior written permission.
111
+
112
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS",
113
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
114
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
115
+ ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
116
+ LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
117
+ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
118
+ SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
119
+ INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
120
+ CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
121
+ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
122
+ POSSIBILITY OF SUCH DAMAGE.
123
+
124
+
125
+ Why Two Licenses?
126
+ =================
127
+
128
+ The zlib License could have been used instead of the Modified (3-clause) BSD
129
+ License, and since the IJG License effectively subsumes the distribution
130
+ conditions of the zlib License, this would have effectively placed
131
+ libjpeg-turbo binary distributions under the IJG License. However, the IJG
132
+ License specifically refers to the Independent JPEG Group and does not extend
133
+ attribution and endorsement protections to other entities. Thus, it was
134
+ desirable to choose a license that granted us the same protections for new code
135
+ that were granted to the IJG for code derived from their software.