@@ -16,25 +16,40 @@ CV_MAT_DEPTH(flags) = ((flags) & CV_MAT_DEPTH_MASK)
1616CV_MAKETYPE (depth,cn) = (CV_MAT_DEPTH (depth) + (((cn)- 1 ) << CV_CN_SHIFT))
1717CV_MAKE_TYPE = CV_MAKETYPE
1818
19+ # Bijective map between Julia element types and OpenCV depth constants. The two
20+ # conversion directions below both look this up rather than repeating a 7-branch
21+ # `if`/`elseif` ladder; adding a supported dtype is a single entry here.
22+ const _CV_DEPTH = (
23+ (UInt8, CV_8U),
24+ (Int8, CV_8S),
25+ (UInt16, CV_16U),
26+ (Int16, CV_16S),
27+ (Int32, CV_32S),
28+ (Float32, CV_32F),
29+ (Float64, CV_64F),
30+ )
31+
32+ # OpenCV depth constant for a Julia element type (throws on unsupported types).
33+ function _cv_depth (:: Type{T} ) where {T}
34+ for (jt, depth) in _CV_DEPTH
35+ jt === T && return depth
36+ end
37+ throw (OpenCVError (" unsupported array element type $T for cv::Mat; expected one " *
38+ " of UInt8, Int8, UInt16, Int16, Int32, Float32, Float64" ))
39+ end
40+
41+ # Julia element type for an OpenCV type flag + channel count (throws on unknown flag).
42+ function _julia_eltype (cvtype, channels)
43+ for (jt, depth) in _CV_DEPTH
44+ cvtype == CV_MAKE_TYPE (depth, channels) && return jt
45+ end
46+ throw (OpenCVError (" unsupported cv::Mat type returned from OpenCV " *
47+ " (type flag $cvtype , $channels channel(s))" ))
48+ end
49+
1950function cpp_to_julia (mat:: CxxMat )
2051 rets = jlopencv_core_Mat_mutable_data (mat)
21- if rets[2 ] == CV_MAKE_TYPE (CV_8U, rets[3 ])
22- dtype = UInt8
23- elseif rets[2 ]== CV_MAKE_TYPE (CV_8S, rets[3 ])
24- dtype = Int8
25- elseif rets[2 ]== CV_MAKE_TYPE (CV_16U, rets[3 ])
26- dtype = UInt16
27- elseif rets[2 ]== CV_MAKE_TYPE (CV_16S, rets[3 ])
28- dtype = Int16
29- elseif rets[2 ]== CV_MAKE_TYPE (CV_32S, rets[3 ])
30- dtype = Int32
31- elseif rets[2 ]== CV_MAKE_TYPE (CV_32F, rets[3 ])
32- dtype = Float32
33- elseif rets[2 ]== CV_MAKE_TYPE (CV_64F, rets[3 ])
34- dtype = Float64
35- else
36- error (" Bad type returned from OpenCV" )
37- end
52+ dtype = _julia_eltype (rets[2 ], rets[3 ])
3853 steps = [rets[6 ]/ sizeof (dtype), rets[7 ]/ sizeof (dtype)]
3954 # println(steps[1]/rets[3], steps[2]/rets[3]/rets[4])
4055 # TODO : Implement views when steps do not result in continous memory
@@ -83,21 +98,8 @@ function julia_to_cpp(img::InputArray)
8398
8499 push! (ndims_a, Int32 (size (img)[3 ]))
85100 push! (ndims_a, Int32 (size (img)[2 ]))
86- if eltype (img) == UInt8
87- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_8U, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
88- elseif eltype (img) == UInt16
89- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_16U, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
90- elseif eltype (img) == Int8
91- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_8S, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
92- elseif eltype (img) == Int16
93- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_16S, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
94- elseif eltype (img) == Int32
95- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_32S, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
96- elseif eltype (img) == Float32
97- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_32F, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
98- elseif eltype (img) == Float64
99- return CxxMat (2 , pointer (ndims_a), CV_MAKE_TYPE (CV_64F, size (img)[1 ]), Ptr {Nothing} (pointer (img)), pointer (steps_a))
100- end
101+ cvtype = CV_MAKE_TYPE (_cv_depth (eltype (img)), size (img)[1 ])
102+ return CxxMat (2 , pointer (ndims_a), cvtype, Ptr {Nothing} (pointer (img)), pointer (steps_a))
101103 else
102104 # Copy array, invalid config
103105 return julia_to_cpp (img[:, :, :])
0 commit comments