mirror of
https://github.com/opencv/opencv.git
synced 2025-07-25 22:57:53 +08:00
feat: add matrix type stubs generation
Adds missing typing stubs: - Matrix depths: `CV_8U`, `CV_8S` and etc. - Matrix type constants: `CV_8UC1`, `CV_32FC3` and etc. - Matrix type factory functions: `CV_*(channels) -> int` and `CV_MAKETYPE`
This commit is contained in:
parent
fdc0c12b7f
commit
986e379f28
@ -4,11 +4,13 @@ __all__ = [
|
|||||||
|
|
||||||
from typing import Sequence, Callable
|
from typing import Sequence, Callable
|
||||||
|
|
||||||
from .nodes import NamespaceNode, FunctionNode, OptionalTypeNode, ClassProperty, PrimitiveTypeNode
|
from .nodes import (NamespaceNode, FunctionNode, OptionalTypeNode,
|
||||||
|
ClassProperty, PrimitiveTypeNode)
|
||||||
from .ast_utils import find_function_node, SymbolName
|
from .ast_utils import find_function_node, SymbolName
|
||||||
|
|
||||||
|
|
||||||
def apply_manual_api_refinement(root: NamespaceNode) -> None:
|
def apply_manual_api_refinement(root: NamespaceNode) -> None:
|
||||||
|
export_matrix_type_constants(root)
|
||||||
# Export OpenCV exception class
|
# Export OpenCV exception class
|
||||||
builtin_exception = root.add_class("Exception")
|
builtin_exception = root.add_class("Exception")
|
||||||
builtin_exception.is_exported = False
|
builtin_exception.is_exported = False
|
||||||
@ -17,6 +19,33 @@ def apply_manual_api_refinement(root: NamespaceNode) -> None:
|
|||||||
refine_symbol(root, symbol_name)
|
refine_symbol(root, symbol_name)
|
||||||
|
|
||||||
|
|
||||||
|
def export_matrix_type_constants(root: NamespaceNode) -> None:
|
||||||
|
MAX_PREDEFINED_CHANNELS = 4
|
||||||
|
|
||||||
|
depth_names = ("CV_8U", "CV_8S", "CV_16U", "CV_16S", "CV_32S",
|
||||||
|
"CV_32F", "CV_64F", "CV_16F")
|
||||||
|
for depth_value, depth_name in enumerate(depth_names):
|
||||||
|
# Export depth constants
|
||||||
|
root.add_constant(depth_name, str(depth_value))
|
||||||
|
# Export predefined types
|
||||||
|
for c in range(MAX_PREDEFINED_CHANNELS):
|
||||||
|
root.add_constant(f"{depth_name}C{c + 1}",
|
||||||
|
f"{depth_value + 8 * c}")
|
||||||
|
# Export type creation function
|
||||||
|
root.add_function(
|
||||||
|
f"{depth_name}C",
|
||||||
|
(FunctionNode.Arg("channels", PrimitiveTypeNode.int_()), ),
|
||||||
|
FunctionNode.RetType(PrimitiveTypeNode.int_())
|
||||||
|
)
|
||||||
|
# Export CV_MAKETYPE
|
||||||
|
root.add_function(
|
||||||
|
"CV_MAKETYPE",
|
||||||
|
(FunctionNode.Arg("depth", PrimitiveTypeNode.int_()),
|
||||||
|
FunctionNode.Arg("channels", PrimitiveTypeNode.int_())),
|
||||||
|
FunctionNode.RetType(PrimitiveTypeNode.int_())
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def make_optional_arg(arg_name: str) -> Callable[[NamespaceNode, SymbolName], None]:
|
def make_optional_arg(arg_name: str) -> Callable[[NamespaceNode, SymbolName], None]:
|
||||||
def _make_optional_arg(root_node: NamespaceNode,
|
def _make_optional_arg(root_node: NamespaceNode,
|
||||||
function_symbol_name: SymbolName) -> None:
|
function_symbol_name: SymbolName) -> None:
|
||||||
|
@ -103,9 +103,6 @@ def _generate_typing_stubs(root: NamespaceNode, output_path: Path) -> None:
|
|||||||
|
|
||||||
_write_reexported_symbols_section(root, output_stream)
|
_write_reexported_symbols_section(root, output_stream)
|
||||||
|
|
||||||
# Write constants section, because constants don't impose any dependencies
|
|
||||||
_generate_section_stub(StubSection("# Constants", ConstantNode), root,
|
|
||||||
output_stream, 0)
|
|
||||||
# NOTE: Enumerations require special handling, because all enumeration
|
# NOTE: Enumerations require special handling, because all enumeration
|
||||||
# constants are exposed as module attributes
|
# constants are exposed as module attributes
|
||||||
has_enums = _generate_section_stub(StubSection("# Enumerations", EnumerationNode),
|
has_enums = _generate_section_stub(StubSection("# Enumerations", EnumerationNode),
|
||||||
|
Loading…
Reference in New Issue
Block a user