|
1 | 1 | import numpy as np |
2 | | -import ctypes |
3 | 2 | import inspect |
4 | 3 | from collections.abc import Buffer |
5 | 4 |
|
@@ -39,8 +38,8 @@ def imwrite_to_memory( |
39 | 38 | qstep=None, |
40 | 39 | progression_order=None, |
41 | 40 | tlm_marker=True, |
42 | | - tileparts_at_resolutions=True, |
43 | | - tileparts_at_components=False, |
| 41 | + tileparts_at_resolutions=None, |
| 42 | + tileparts_at_components=None, |
44 | 43 | ): |
45 | 44 | mem_outfile = MemOutfile() |
46 | 45 | mem_outfile.open(65536, False) |
@@ -72,8 +71,8 @@ def imwrite( |
72 | 71 | qstep=None, |
73 | 72 | progression_order=None, |
74 | 73 | tlm_marker=True, |
75 | | - tileparts_at_resolutions=True, |
76 | | - tileparts_at_components=False, |
| 74 | + tileparts_at_resolutions=None, |
| 75 | + tileparts_at_components=None, |
77 | 76 | ): |
78 | 77 | # Auto-detect channel order if not provided |
79 | 78 | if channel_order is None: |
@@ -149,46 +148,16 @@ def imwrite( |
149 | 148 | if not reversible and qstep is not None: |
150 | 149 | codestream.access_qcd().set_irrev_quant(qstep) |
151 | 150 | codestream.set_planar(num_components > 1) |
152 | | - # Set tile parts for resolution, but not for channels |
153 | | - codestream.set_tilepart_divisions(True, False) |
| 151 | + if tileparts_at_resolutions is None: |
| 152 | + tileparts_at_resolutions = progression_order == "RLCP" |
| 153 | + if tileparts_at_components is None: |
| 154 | + tileparts_at_components = False |
| 155 | + codestream.set_tilepart_divisions(tileparts_at_resolutions, tileparts_at_components) |
154 | 156 | codestream.request_tlm_marker(tlm_marker) |
155 | 157 |
|
156 | 158 | codestream.write_headers(ojph_file, None, 0) |
157 | 159 |
|
158 | | - line = codestream.exchange(None, 0) |
159 | | - if channel_order == "HW": |
160 | | - # Single component - simple case |
161 | | - for i in range(height): |
162 | | - i32_ptr = ctypes.cast(line.i32_address, ctypes.POINTER(ctypes.c_uint32)) |
163 | | - line_array = np.ctypeslib.as_array( |
164 | | - ctypes.cast(i32_ptr, ctypes.POINTER(ctypes.c_uint32)), |
165 | | - shape=(line.size,) |
166 | | - ) |
167 | | - line_array[...] = image[i, :] |
168 | | - line = codestream.exchange(line, 0) |
169 | | - elif channel_order == 'HWC': |
170 | | - # Multi-component - use planar mode for efficiency |
171 | | - # HWC format: image[height, width, channel] |
172 | | - for c in range(num_components): |
173 | | - for i in range(height): |
174 | | - i32_ptr = ctypes.cast(line.i32_address, ctypes.POINTER(ctypes.c_uint32)) |
175 | | - line_array = np.ctypeslib.as_array( |
176 | | - ctypes.cast(i32_ptr, ctypes.POINTER(ctypes.c_uint32)), |
177 | | - shape=(line.size,) |
178 | | - ) |
179 | | - line_array[...] = image[i, :, c] |
180 | | - line = codestream.exchange(line, c) |
181 | | - elif channel_order == 'CHW': |
182 | | - # CHW format: image[channel, height, width] |
183 | | - for c in range(num_components): |
184 | | - for i in range(height): |
185 | | - i32_ptr = ctypes.cast(line.i32_address, ctypes.POINTER(ctypes.c_uint32)) |
186 | | - line_array = np.ctypeslib.as_array( |
187 | | - ctypes.cast(i32_ptr, ctypes.POINTER(ctypes.c_uint32)), |
188 | | - shape=(line.size,) |
189 | | - ) |
190 | | - line_array[...] = image[c, i, :] |
191 | | - line = codestream.exchange(line, c) |
| 160 | + codestream.push_all_components(image, num_components, channel_order) |
192 | 161 |
|
193 | 162 | codestream.flush() |
194 | 163 | if close_codestream: |
|
0 commit comments