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
55 if metadata.has_entry_by_key(MetadataKey.DEVICE_EXPOSURE_TIME):
56 c_capture_information.exposure_time = metadata.get_value_by_key(MetadataKey.DEVICE_EXPOSURE_TIME)
58 c_capture_information.exposure_time = 0
60 return c_capture_information
64 return add_metadata_from_capture_information_to_metadata(Metadata(), capture_information)
68 capture_information: peak_icv_capture_information) -> Metadata:
69 metadata.set_value_by_key(MetadataKey.DEVICE_TIMESTAMP, capture_information.relative_timestamp)
70 metadata.set_value_by_key(MetadataKey.BINNING_HORIZONTAL, capture_information.binning_factor.x)
71 metadata.set_value_by_key(MetadataKey.BINNING_VERTICAL, capture_information.binning_factor.y)
72 metadata.set_value_by_key(MetadataKey.ROI, Rectangle(Point(capture_information.region_of_interest.x,
73 capture_information.region_of_interest.y),
74 Size(capture_information.region_of_interest.width,
75 capture_information.region_of_interest.height)))
76 metadata.set_value_by_key(MetadataKey.DEVICE_EXPOSURE_TIME, capture_information.exposure_time)
81 return ctypes.c_void_p(int(image_handle))
85 if point_type == peak_icv_point_f:
89 "Point type not supported.")
93 if point_type == peak_icv_point_f:
94 return peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY
97 "Point type not supported.")
101 if point_enum == peak_icv_point_type.PEAK_ICV_POINT_TYPE_XY:
102 return peak_icv_point_f
105 "point_enum not supported " + str(point_enum.value))
108T = TypeVar(
'T', bound=Any)
109InputType = Union[Sequence[T], NDArray[T], ctypes.Array[Any]]
110OutputType = Union[MutableSequence[T], NDArray[T], ctypes.Array[Any]]
113def transform(input_: InputType, output: OutputType, func: Callable) ->
None:
114 for i
in range(len(input_)):
115 output[i] = func(input_[i])
119 def execute_and_map_return_codes_light(func_light: Callable, *args_light: Any, **kwargs_light: Any) ->
None:
120 ec = func_light(*args_light, **kwargs_light)
123 if ec != peak_icv_status.PEAK_ICV_STATUS_SUCCESS:
124 raise ICVException(
"Could not query the last error!", ec)
126 error_code = func(*args, **kwargs)
134 if error_code != peak_icv_status.PEAK_ICV_STATUS_SUCCESS:
136 c_count = ctypes.c_size_t()
137 execute_and_map_return_codes_light(
138 Loader().dll().peak_icv_GetLastErrorMessage_GetCount,
139 ctypes.pointer(c_count))
141 if c_count.value == 0:
144 c_array = create_array(ctypes.c_char, c_count.value)
145 execute_and_map_return_codes_light(
Loader().dll().peak_icv_GetLastErrorMessage,
147 exception_msg = c_array.value
150 peak_icv_status.PEAK_ICV_STATUS_LIBRARY_NOT_INITIALIZED,
151 peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING,
155 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NULL_POINTER:
158 elif error_code == peak_icv_status.PEAK_ICV_STATUS_MISMATCH:
161 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NOT_SUPPORTED:
164 elif error_code == peak_icv_status.PEAK_ICV_STATUS_NOT_POSSIBLE:
167 elif error_code == peak_icv_status.PEAK_ICV_STATUS_OUT_OF_RANGE:
170 elif error_code == peak_icv_status.PEAK_ICV_STATUS_MATH_ERROR:
173 elif error_code == peak_icv_status.PEAK_ICV_STATUS_TARGET_NOT_FOUND:
176 elif error_code == peak_icv_status.PEAK_ICV_STATUS_CORRUPTED:
179 elif error_code == peak_icv_status.PEAK_ICV_STATUS_IO_ERROR:
183 peak_icv_status.PEAK_ICV_STATUS_INTERNAL_ERROR,
184 peak_icv_status.PEAK_ICV_STATUS_INVALID_BUFFER_SIZE,
185 peak_icv_status.PEAK_ICV_STATUS_INVALID_HANDLE,
193 for path
in site.getsitepackages():
194 package_path = os.path.join(path,
"ids_peak_ipl")
195 if os.path.isdir(package_path):
198 peak_icv_status.PEAK_ICV_STATUS_DYNAMIC_DEPENDENCY_MISSING)
203 _dll: ctypes.CDLL |
None =
None
208 file_dir = os.path.dirname(os.path.realpath(__file__))
210 if sys.platform.startswith(
'win'):
211 lib_name =
"ids_peak_icv.dll"
212 dll_path = os.path.join(file_dir,
"..")
213 os.add_dll_directory(dll_path)
215 os.add_dll_directory(ids_peak_ipl_dll_path)
217 lib_name =
"libids_peak_icv.so"
219 lib_path = os.path.abspath(os.path.join(file_dir,
"..", lib_name))
221 cls.
_instance._dll = ctypes.cdll.LoadLibrary(lib_path)
223 execute_and_map_return_codes(cls.
_instance._dll.peak_icv_Init)
225 atexit.register(Loader._library_exit)
229 def dll(self) -> ctypes.CDLL:
230 if self.
_dll is None:
238 execute_and_map_return_codes(
Loader().
dll().peak_icv_Exit)
241ReturnType = TypeVar(
'ReturnType', bound=ctypes.c_void_p)
245 *args: Any, **kwargs: Any) -> ReturnType:
247 handle = kwargs[
"_handle"]
248 if type(handle)
is not return_type:
254 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)
Metadata add_metadata_from_capture_information_to_metadata(Metadata metadata, peak_icv_capture_information capture_information)
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)