2020-08-17 15:11:17 +02:00
|
|
|
#!/usr/bin/python3
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-03 14:18:22 +01:00
|
|
|
# This file generates the member definitions and declarations for all vips
|
|
|
|
# operators.
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# this needs pyvips
|
|
|
|
#
|
|
|
|
# pip install --user pyvips
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# Sample member declaration:
|
|
|
|
# VImage invert(VOption *options = 0) const;
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# Sample member definition:
|
|
|
|
# VImage VImage::invert( VOption *options ) const
|
|
|
|
# {
|
|
|
|
# VImage out;
|
|
|
|
#
|
|
|
|
# call( "invert",
|
|
|
|
# (options ? options : VImage::option())->
|
|
|
|
# set( "in", *this )->
|
|
|
|
# set( "out", &out ) );
|
|
|
|
#
|
|
|
|
# return( out );
|
|
|
|
# }
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
import argparse
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-11-29 14:51:03 +01:00
|
|
|
from pyvips import Introspect, Operation, GValue, Error, \
|
2019-02-01 21:45:12 +01:00
|
|
|
ffi, gobject_lib, type_map, type_from_name, nickname_find, type_name
|
2014-10-27 19:17:49 +01:00
|
|
|
|
|
|
|
# turn a GType into a C++ type
|
|
|
|
gtype_to_cpp = {
|
2019-02-01 21:45:12 +01:00
|
|
|
GValue.gbool_type: 'bool',
|
|
|
|
GValue.gint_type: 'int',
|
|
|
|
GValue.gdouble_type: 'double',
|
2019-04-24 16:02:52 +02:00
|
|
|
GValue.gstr_type: 'const char *',
|
2019-02-01 21:45:12 +01:00
|
|
|
GValue.refstr_type: 'char *',
|
|
|
|
GValue.gflags_type: 'int',
|
|
|
|
GValue.image_type: 'VImage',
|
2020-06-18 14:45:14 +02:00
|
|
|
GValue.source_type: 'VSource',
|
|
|
|
GValue.target_type: 'VTarget',
|
2019-02-01 21:45:12 +01:00
|
|
|
GValue.array_int_type: 'std::vector<int>',
|
|
|
|
GValue.array_double_type: 'std::vector<double>',
|
|
|
|
GValue.array_image_type: 'std::vector<VImage>',
|
|
|
|
GValue.blob_type: 'VipsBlob *'
|
2014-10-27 19:17:49 +01:00
|
|
|
}
|
|
|
|
|
2019-11-08 13:47:37 +01:00
|
|
|
cplusplus_suffixes = ('*', '&')
|
|
|
|
cplusplus_keywords = ('case', 'switch')
|
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# values for VipsArgumentFlags
|
|
|
|
_REQUIRED = 1
|
|
|
|
_INPUT = 16
|
|
|
|
_OUTPUT = 32
|
|
|
|
_DEPRECATED = 64
|
|
|
|
_MODIFY = 128
|
|
|
|
|
|
|
|
# for VipsOperationFlags
|
|
|
|
_OPERATION_DEPRECATED = 8
|
|
|
|
|
|
|
|
|
|
|
|
def get_cpp_type(gtype):
|
2020-08-17 11:58:49 +02:00
|
|
|
"""Map a gtype to the C++ type name we use to represent it.
|
2019-02-01 21:45:12 +01:00
|
|
|
"""
|
|
|
|
if gtype in gtype_to_cpp:
|
|
|
|
return gtype_to_cpp[gtype]
|
|
|
|
|
|
|
|
fundamental = gobject_lib.g_type_fundamental(gtype)
|
|
|
|
|
2014-10-27 19:17:49 +01:00
|
|
|
# enum params use the C name as their name
|
2019-02-01 21:45:12 +01:00
|
|
|
if fundamental == GValue.genum_type:
|
|
|
|
return type_name(gtype)
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
if fundamental in gtype_to_cpp:
|
|
|
|
return gtype_to_cpp[fundamental]
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
return '<unknown type>'
|
2014-10-27 19:17:49 +01:00
|
|
|
|
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# swap any '-' for '_'
|
|
|
|
def cppize(name):
|
|
|
|
return name.replace('-', '_')
|
2014-11-16 13:19:04 +01:00
|
|
|
|
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
def generate_operation(operation_name, declaration_only=False):
|
2019-11-29 14:51:03 +01:00
|
|
|
intro = Introspect.get(operation_name)
|
|
|
|
|
2020-08-17 11:58:49 +02:00
|
|
|
required_output = [name
|
|
|
|
for name in intro.required_output if name != intro.member_x]
|
2019-02-01 21:45:12 +01:00
|
|
|
|
|
|
|
has_output = len(required_output) >= 1
|
|
|
|
|
2019-11-29 14:51:03 +01:00
|
|
|
# Add a C++ style comment block with some additional markings (@param,
|
2019-02-03 14:18:22 +01:00
|
|
|
# @return)
|
2019-02-03 12:13:04 +01:00
|
|
|
if declaration_only:
|
2020-08-17 19:07:11 +02:00
|
|
|
result = f'\n/**\n * {intro.description.capitalize()}.'
|
2019-02-03 12:13:04 +01:00
|
|
|
|
2020-08-17 15:11:17 +02:00
|
|
|
if len(intro.optional_input) > 0:
|
|
|
|
result += '\n *\n * **Optional parameters**'
|
|
|
|
for name in intro.optional_input:
|
|
|
|
details = intro.details[name]
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'\n * - **{cppize(name)}** -- '
|
2020-08-17 15:11:17 +02:00
|
|
|
result += f'{details["blurb"]}, '
|
|
|
|
result += f'{get_cpp_type(details["type"])}.'
|
|
|
|
result += '\n *'
|
|
|
|
|
2019-11-29 14:51:03 +01:00
|
|
|
for name in intro.method_args:
|
2020-08-17 19:07:11 +02:00
|
|
|
details = intro.details[name]
|
|
|
|
result += f'\n * @param {cppize(name)} {details["blurb"]}.'
|
2019-02-03 12:13:04 +01:00
|
|
|
|
|
|
|
if has_output:
|
|
|
|
# skip the first element
|
|
|
|
for name in required_output[1:]:
|
2020-08-17 19:07:11 +02:00
|
|
|
details = intro.details[name]
|
|
|
|
result += f'\n * @param {cppize(name)} {details["blurb"]}.'
|
2019-02-03 12:13:04 +01:00
|
|
|
|
2020-08-17 11:58:49 +02:00
|
|
|
result += '\n * @param options Set of options.'
|
2019-02-03 12:13:04 +01:00
|
|
|
|
|
|
|
if has_output:
|
2020-08-17 19:07:11 +02:00
|
|
|
details = intro.details[required_output[0]]
|
|
|
|
result += f'\n * @return {details["blurb"]}.'
|
2019-02-03 12:13:04 +01:00
|
|
|
|
|
|
|
result += '\n */\n'
|
|
|
|
else:
|
|
|
|
result = '\n'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
2019-11-29 14:51:03 +01:00
|
|
|
if intro.member_x is None and declaration_only:
|
2019-02-01 21:45:12 +01:00
|
|
|
result += 'static '
|
|
|
|
if has_output:
|
|
|
|
# the first output arg will be used as the result
|
2019-11-29 14:51:03 +01:00
|
|
|
cpp_type = get_cpp_type(intro.details[required_output[0]]['type'])
|
2019-11-08 13:47:37 +01:00
|
|
|
spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'{cpp_type}{spacing}'
|
2019-02-01 21:45:12 +01:00
|
|
|
else:
|
|
|
|
result += 'void '
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
if not declaration_only:
|
|
|
|
result += 'VImage::'
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-11-08 13:47:37 +01:00
|
|
|
cplusplus_operation = operation_name
|
|
|
|
if operation_name in cplusplus_keywords:
|
|
|
|
cplusplus_operation += '_image'
|
|
|
|
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'{cplusplus_operation}( '
|
2019-11-29 14:51:03 +01:00
|
|
|
for name in intro.method_args:
|
|
|
|
details = intro.details[name]
|
|
|
|
gtype = details['type']
|
2019-02-01 21:45:12 +01:00
|
|
|
cpp_type = get_cpp_type(gtype)
|
2019-11-08 13:47:37 +01:00
|
|
|
spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'{cpp_type}{spacing}{cppize(name)}, '
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# output params are passed by reference
|
|
|
|
if has_output:
|
|
|
|
# skip the first element
|
|
|
|
for name in required_output[1:]:
|
2019-11-29 14:51:03 +01:00
|
|
|
details = intro.details[name]
|
|
|
|
gtype = details['type']
|
2019-02-01 21:45:12 +01:00
|
|
|
cpp_type = get_cpp_type(gtype)
|
2019-11-08 13:47:37 +01:00
|
|
|
spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'{cpp_type}{spacing}*{cppize(name)}, '
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'VOption *options {"= 0 " if declaration_only else ""})'
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# if no 'this' available, it's a class method and they are all const
|
2019-11-29 14:51:03 +01:00
|
|
|
if intro.member_x is not None:
|
2019-02-01 21:45:12 +01:00
|
|
|
result += ' const'
|
|
|
|
|
|
|
|
if declaration_only:
|
|
|
|
result += ';'
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
result += '\n{\n'
|
|
|
|
|
|
|
|
if has_output:
|
|
|
|
# the first output arg will be used as the result
|
|
|
|
name = required_output[0]
|
2019-11-29 14:51:03 +01:00
|
|
|
cpp_type = get_cpp_type(intro.details[name]['type'])
|
2019-11-08 13:47:37 +01:00
|
|
|
spacing = '' if cpp_type.endswith(cplusplus_suffixes) else ' '
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f' {cpp_type}{spacing}{cppize(name)};\n\n'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f' call( "{operation_name}",\n'
|
|
|
|
result += f' (options ? options : VImage::option())'
|
2019-11-29 14:51:03 +01:00
|
|
|
if intro.member_x is not None:
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'->\n'
|
|
|
|
result += f' set( "{intro.member_x}", *this )'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
2019-11-29 14:51:03 +01:00
|
|
|
all_required = intro.method_args
|
2019-02-01 21:45:12 +01:00
|
|
|
|
|
|
|
if has_output:
|
|
|
|
# first element needs to be passed by reference
|
|
|
|
arg = cppize(required_output[0])
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'->\n'
|
|
|
|
result += f' set( "{required_output[0]}", &{arg} )'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
|
|
|
# append the remaining list
|
|
|
|
all_required += required_output[1:]
|
|
|
|
|
|
|
|
for name in all_required:
|
|
|
|
arg = cppize(name)
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'->\n'
|
|
|
|
result += f' set( "{name}", {arg} )'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
|
|
|
result += ' );\n'
|
|
|
|
|
|
|
|
if has_output:
|
2020-08-17 19:07:11 +02:00
|
|
|
result += f'\n'
|
|
|
|
result += f' return( {required_output[0]} );\n'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
result += '}'
|
2019-02-01 21:45:12 +01:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
def generate_operators(declarations_only=False):
|
2019-02-01 21:45:12 +01:00
|
|
|
all_nicknames = []
|
2018-06-11 15:30:17 +02:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
def add_nickname(gtype, a, b):
|
|
|
|
nickname = nickname_find(gtype)
|
|
|
|
try:
|
|
|
|
# can fail for abstract types
|
2019-11-29 14:51:03 +01:00
|
|
|
intro = Introspect.get(nickname)
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# we are only interested in non-deprecated operations
|
2019-11-29 14:51:03 +01:00
|
|
|
if (intro.flags & _OPERATION_DEPRECATED) == 0:
|
2019-02-01 21:45:12 +01:00
|
|
|
all_nicknames.append(nickname)
|
|
|
|
except Error:
|
|
|
|
pass
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
type_map(gtype, add_nickname)
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
return ffi.NULL
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
type_map(type_from_name('VipsOperation'), add_nickname)
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
# add 'missing' synonyms by hand
|
|
|
|
all_nicknames.append('crop')
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-01 21:45:12 +01:00
|
|
|
# make list unique and sort
|
|
|
|
all_nicknames = list(set(all_nicknames))
|
|
|
|
all_nicknames.sort()
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
for nickname in all_nicknames:
|
|
|
|
print(generate_operation(nickname, declarations_only))
|
2014-10-27 19:17:49 +01:00
|
|
|
|
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
parser = argparse.ArgumentParser(description='C++ binding generator')
|
|
|
|
parser.add_argument('--gen', '-g',
|
|
|
|
default='cpp',
|
|
|
|
choices=['h', 'cpp'],
|
2019-02-03 14:18:22 +01:00
|
|
|
help='File to generate: h (headers) or cpp ' + \
|
|
|
|
'(implementations) (default: %(default)s)')
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
args = parser.parse_args()
|
2014-10-27 19:17:49 +01:00
|
|
|
|
2019-02-03 12:13:04 +01:00
|
|
|
generate_operators(args.gen == 'h')
|