1from __future__
import annotations
7from typing
import Callable, Type, TypeVar, Sequence, Any, MutableSequence, Union
9from ids_peak_common
import Metadata, MetadataKey, Rectangle, Point, Size
11 peak_icv_capture_information)
13 NotPossibleException, OutOfRangeException, MathErrorException,
14 TargetNotFoundException, CorruptedException, IOException, InternalErrorException,
15 ICVException, NullPointerException)
16from numpy.typing
import NDArray
20 array_class: Type[ctypes.Array] = c_type * size
27 if metadata.has_entry_by_key(MetadataKey.DEVICE_TIMESTAMP):
28 c_capture_information.relative_timestamp = metadata.get_value_by_key(MetadataKey.DEVICE_TIMESTAMP)
30 c_capture_information.relative_timestamp = 0
31 if metadata.has_entry_by_key(MetadataKey.BINNING_HORIZONTAL):
32 c_capture_information.binning_factor.x = metadata.get_value_by_key(MetadataKey.BINNING_HORIZONTAL)
34 c_capture_information.binning_factor.x = 1
35 if metadata.has_entry_by_key(MetadataKey.BINNING_VERTICAL):
36 c_capture_information.binning_factor.y = metadata.get_value_by_key(MetadataKey.BINNING_VERTICAL)
38 c_capture_information.binning_factor.y = 1
39 if metadata.has_entry_by_key(MetadataKey.ROI):
40 roi = metadata.get_value_by_key(MetadataKey.ROI)
41 c_capture_information.region_of_interest.x = ctypes.c_uint32(roi.x)
42 c_capture_information.region_of_interest.y = ctypes.c_uint32(roi.y)
43 c_capture_information.region_of_interest.width = ctypes.c_uint32(roi.width)
44 c_capture_information.region_of_interest.height = ctypes.c_uint32(roi.height)
46 c_capture_information.region_of_interest.x = 0
47 c_capture_information.region_of_interest.y = 0
50 c_capture_information.region_of_interest.width = size.width
51 c_capture_information.region_of_interest.height = size.height
53 c_capture_information.region_of_interest.width = 0
54 c_capture_information.region_of_interest.height = 0
56 return c_capture_information
61 metadata.set_value_by_key(MetadataKey.DEVICE_TIMESTAMP, capture_information.relative_timestamp)
62 metadata.set_value_by_key(MetadataKey.BINNING_HORIZONTAL, capture_information.binning_factor.x)
63 metadata.set_value_by_key(MetadataKey.BINNING_VERTICAL, capture_information.binning_factor.y)
64 metadata.set_value_by_key(MetadataKey.ROI, Rectangle(Point(capture_information.region_of_interest.x,
65 capture_information.region_of_interest.y),
66 Size(capture_information.region_of_interest.width,
67 capture_information.region_of_interest.height)))
72 return ctypes.c_void_p(int(image_handle))
76 if point_type == peak_icv_point_f:
80 "Point type not supported.")
84 if point_type == peak_icv_point_f:
85 return peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY
88 "Point type not supported.")
92 if point_enum == peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY:
93 return peak_icv_point_f
96 "point_enum not supported " + str(point_enum.value))
99T = TypeVar(
'T', bound=Any)
100InputType = Union[Sequence[T], NDArray[T], ctypes.Array[Any]]
101OutputType = Union[MutableSequence[T], NDArray[T], ctypes.Array[Any]]
104def transform(input_: InputType, output: OutputType, func: Callable) ->
None:
105 for i
in range(len(input_)):
106 output[i] = func(input_[i])
110 def execute_and_map_return_codes_light(func_light: Callable, *args_light: Any, **kwargs_light: Any) ->
None:
111 ec = func_light(*args_light, **kwargs_light)
114 if ec != peak_icv_status.PEAK_ICV_STATUS_SUCCESS:
115 raise ICVException(
"Could not query the last error!", ec)
117 error_code = func(*args, **kwargs)
125 if error_code != peak_icv_status.PEAK_ICV_STATUS_SUCCESS:
127 c_count = ctypes.c_size_t()
128 execute_and_map_return_codes_light(
129 Loader().dll().peak_icv_GetLastErrorMessage_GetCount,
130 ctypes.pointer(c_count))
132 if c_count.value == 0:
135 c_array = create_array(ctypes.c_char, c_count.value)
136 execute_and_map_return_codes_light(
Loader().dll().peak_icv_GetLastErrorMessage,
138 exception_msg = c_array.value
141 peak_icv_status.PEAK_ICV_STATUS_LIBRARY_NOT_INITIALIZED,
142 peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING,
146 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NULL_POINTER:
149 elif error_code == peak_icv_status.PEAK_ICV_STATUS_MISMATCH:
152 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NOT_SUPPORTED:
155 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NOT_POSSIBLE:
158 elif error_code == peak_icv_status.PEAK_ICV_STATUS_OUT_OF_RANGE:
161 elif error_code == peak_icv_status.PEAK_ICV_STATUS_MATH_ERROR:
164 elif error_code == peak_icv_status.PEAK_ICV_STATUS_TARGET_NOT_FOUND:
167 elif error_code == peak_icv_status.PEAK_ICV_STATUS_CORRUPTED:
170 elif error_code == peak_icv_status.PEAK_ICV_STATUS_IO_ERROR:
174 peak_icv_status.PEAK_ICV_STATUS_INTERNAL_ERROR,
175 peak_icv_status.PEAK_ICV_STATUS_INVALID_BUFFER_SIZE,
176 peak_icv_status.PEAK_ICV_STATUS_INVALID_HANDLE,
184 for path
in site.getsitepackages():
185 package_path = os.path.join(path,
"ids_peak_ipl")
186 if os.path.isdir(package_path):
189 peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING)
194 _dll: ctypes.CDLL |
None =
None
199 file_dir = os.path.dirname(os.path.realpath(__file__))
201 if sys.platform.startswith(
'win'):
202 lib_name =
"ids_peak_icv.dll"
203 dll_path = os.path.join(file_dir,
"..")
204 os.add_dll_directory(dll_path)
206 os.add_dll_directory(ids_peak_ipl_dll_path)
208 lib_name =
"libids_peak_icv.so"
210 lib_path = os.path.abspath(os.path.join(file_dir,
"..", lib_name))
212 cls.
_instance._dll = ctypes.cdll.LoadLibrary(lib_path)
214 execute_and_map_return_codes(cls.
_instance._dll.peak_icv_Init)
216 atexit.register(Loader._library_exit)
220 def dll(self) -> ctypes.CDLL:
221 if self.
_dll is None:
229 execute_and_map_return_codes(
Loader().
dll().peak_icv_Exit)
232ReturnType = TypeVar(
'ReturnType', bound=ctypes.c_void_p)
236 *args: Any, **kwargs: Any) -> ReturnType:
238 handle = kwargs[
"_handle"]
239 if type(handle)
is not return_type:
245 f
"Cannot initialize {class_name} with __init__. Please use a classmethod.")
Thrown when input data or a file is malformed, unreadable, or otherwise corrupted.
Base class for all exceptions thrown by the IDS peak ICV library.
Thrown when a file input/output operation fails.
Thrown on unexpected internal system errors.
Thrown when the library configuration is invalid.
Thrown when a mathematical computation fails.
Thrown when input parameters do not match the expected conditions.
Thrown when an operation cannot be performed due to logical constraints.
Thrown when a requested operation is not implemented or supported.
Thrown when attempting to access an object or value that has not been initialized.
Thrown when a value is outside the allowed range.
Thrown when an expected target object is not detected in the input image.
ctypes.Array create_array(Any c_type, int size)
str find_ids_peak_ipl_dll()
Metadata metadata_from_capture_information(peak_icv_capture_information capture_information)
Type map_enum_to_point_type(peak_icv_point_type point_enum)
ctypes.c_void_p image_handle_to_ctypes(int image_handle)
ReturnType check_init_for_classes_with_classmethods_only(str class_name, Type[ReturnType] return_type, *Any args, **Any kwargs)
Type map_point_type_to_native_type(Type point_type)
None transform(InputType input_, OutputType output, Callable func)
None execute_and_map_return_codes(Callable func, *Any args, **Any kwargs)
peak_icv_point_type map_point_type_to_enum(Type point_type)
peak_icv_capture_information metadata_to_capture_information(Metadata metadata, Size|None size=None)