Example #1
0
def bandreduction(bands: List[VIPSImage],
                  reduction: ChannelReduction) -> VIPSImage:
    if reduction == ChannelReduction.ADD:
        return VIPSImage.sum(bands).cast(bands[0].format)
    elif reduction == ChannelReduction.MAX:
        return Operation.call('bandrank', bands, index=len(bands) - 1)
    elif reduction == ChannelReduction.MIN:
        return Operation.call('bandrank', bands, index=0)
    elif reduction == ChannelReduction.MED:
        return Operation.call('bandrank', bands, index=-1)
    else:
        raise ValueError(f"{reduction} not implemented")
Example #2
0
def gen_function(operation_name):
    op = Operation.new_from_name(operation_name)

    print('<row>')
    print('  <entry>{}</entry>'.format(operation_name))
    print('  <entry>{}</entry>'.format(op.get_description().capitalize()))
    print('  <entry>vips_{}()</entry>'.format(operation_name))
    print('</row>')
Example #3
0
    def add_nickname(gtype, a, b):
        nickname = nickname_find(gtype)
        try:
            # can fail for abstract types
            op = Operation.new_from_name(nickname)

            # we are only interested in non-deprecated operations
            if (op.get_flags() & _OPERATION_DEPRECATED) == 0:
                all_nicknames.append(nickname)
        except Error:
            pass

        type_map(gtype, add_nickname)

        return ffi.NULL
Example #4
0
    def add_nickname(gtype, a, b):
        nickname = nickname_find(gtype)
        try:
            # can fail for abstract types
            op = Operation.new_from_name(nickname)

            # we are only interested in non-deprecated operations
            if (op.get_flags() & _OPERATION_DEPRECATED) == 0:
                all_nicknames.append(nickname)
        except Error:
            pass

        type_map(gtype, add_nickname)

        return ffi.NULL
Example #5
0
def generate_operation(operation_name):
    op = Operation.new_from_name(operation_name)

    # we are only interested in non-deprecated args
    args = [[name, flags] for name, flags in op.get_args()
            if not flags & _DEPRECATED]

    # find the first required input image arg, if any ... that will be self
    member_x = None
    for name, flags in args:
        if ((flags & _INPUT) != 0 and (flags & _REQUIRED) != 0
                and op.get_typeof(name) == GValue.image_type):
            member_x = name
            break

    required_input = [
        name for name, flags in args
        if (flags & _INPUT) != 0 and (flags
                                      & _REQUIRED) != 0 and name != member_x
    ]

    required_output = [
        name for name, flags in args
        if ((flags & _OUTPUT) != 0 and (flags & _REQUIRED) != 0) or (
            (flags & _INPUT) != 0 and (flags & _REQUIRED) != 0 and
            (flags & _MODIFY) != 0)
    ]

    result = ' * @method '
    if member_x is None:
        result += 'static '
    if len(required_output) == 0:
        result += 'void '
    elif len(required_output) == 1:
        result += '{0} '.format(
            gtype_to_php(op.get_typeof(required_output[0]), True))
    else:
        # we generate a Returns: block for this case, see below
        result += 'array '

    result += '{0}('.format(operation_name)
    for name in required_input:
        gtype = op.get_typeof(name)
        result += '{0} ${1}, '.format(gtype_to_php(gtype), name)

    result += 'array $options = []) '

    description = op.get_description()
    result += description[0].upper() + description[1:] + '.\n'

    # find any Enums we've referenced and output @see lines for them
    for name in required_output + required_input:
        gtype = op.get_typeof(name)
        fundamental = gobject_lib.g_type_fundamental(gtype)

        if fundamental != GValue.genum_type:
            continue

        result += ' *     @see {0} for possible values for ${1}\n'.format(
            remove_prefix(type_name(gtype)), name)

    if len(required_output) > 1:
        result += ' *     Return array with: [\n'
        for name in required_output:
            gtype = op.get_typeof(name)
            blurb = op.get_blurb(name)
            result += ' *         \'{0}\' => @type {1} {2}\n'.format(
                name, gtype_to_php(gtype), blurb[0].upper() + blurb[1:])
        result += ' *     ];\n'

    result += ' *     @throws Exception\n'

    return result
Example #6
0
def generate_operation(operation_name, declaration_only=False):
    op = Operation.new_from_name(operation_name)

    # we are only interested in non-deprecated args
    args = [[name, flags] for name, flags in op.get_args()
            if not flags & _DEPRECATED]

    # find the first required input image arg, if any ... that will be self
    member_x = None
    for name, flags in args:
        if ((flags & _INPUT) != 0 and (flags & _REQUIRED) != 0
                and op.get_typeof(name) == GValue.image_type):
            member_x = name
            break

    required_input = [
        name for name, flags in args
        if (flags & _INPUT) != 0 and (flags
                                      & _REQUIRED) != 0 and name != member_x
    ]

    required_output = [
        name for name, flags in args
        if ((flags & _OUTPUT) != 0 and (flags & _REQUIRED) != 0) or (
            (flags & _INPUT) != 0 and (flags & _REQUIRED) != 0 and
            (flags & _MODIFY) != 0) and name != member_x
    ]

    has_output = len(required_output) >= 1

    # Add a C++ style comment block with some additional markings (@param,
    # @return)
    if declaration_only:
        result = '\n/**\n * {}.'.format(op.get_description().capitalize())

        for name in required_input:
            result += '\n * @param {} {}.' \
                      .format(cppize(name), op.get_blurb(name))

        if has_output:
            # skip the first element
            for name in required_output[1:]:
                result += '\n * @param {} {}.' \
                          .format(cppize(name), op.get_blurb(name))

        result += '\n * @param options Optional options.'

        if has_output:
            result += '\n * @return {}.' \
                      .format(op.get_blurb(required_output[0]))

        result += '\n */\n'
    else:
        result = '\n'

    if member_x is None and declaration_only:
        result += 'static '
    if has_output:
        # the first output arg will be used as the result
        cpp_type = get_cpp_type(op.get_typeof(required_output[0]))
        spacing = '' if cpp_type.endswith('*') else ' '
        result += '{0}{1}'.format(cpp_type, spacing)
    else:
        result += 'void '

    if not declaration_only:
        result += 'VImage::'

    result += '{0}( '.format(operation_name)
    for name in required_input:
        gtype = op.get_typeof(name)
        cpp_type = get_cpp_type(gtype)
        spacing = '' if cpp_type.endswith('*') else ' '
        result += '{0}{1}{2}, '.format(cpp_type, spacing, cppize(name))

    # output params are passed by reference
    if has_output:
        # skip the first element
        for name in required_output[1:]:
            gtype = op.get_typeof(name)
            cpp_type = get_cpp_type(gtype)
            spacing = '' if cpp_type.endswith('*') else ' '
            result += '{0}{1}*{2}, '.format(cpp_type, spacing, cppize(name))

    result += 'VOption *options {0})'.format(
        '= 0 ' if declaration_only else '')

    # if no 'this' available, it's a class method and they are all const
    if member_x is not None:
        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]
        cpp_type = get_cpp_type(op.get_typeof(name))
        spacing = '' if cpp_type.endswith('*') else ' '
        result += '    {0}{1}{2};\n\n'.format(cpp_type, spacing, cppize(name))

    result += '    call( "{0}",\n'.format(operation_name)
    result += '        (options ? options : VImage::option())'
    if member_x is not None:
        result += '->\n'
        result += '            set( "{0}", *this )'.format(member_x)

    all_required = required_input

    if has_output:
        # first element needs to be passed by reference
        arg = cppize(required_output[0])
        result += '->\n'
        result += '            set( "{0}", &{1} )' \
                  .format(required_output[0], arg)

        # append the remaining list
        all_required += required_output[1:]

    for name in all_required:
        arg = cppize(name)
        result += '->\n'
        result += '            set( "{0}", {1} )'.format(name, arg)

    result += ' );\n'

    if has_output:
        result += '\n'
        result += '    return( {0} );\n'.format(required_output[0])

    result += '}'

    return result
Example #7
0
def generate_operation(operation_name, declaration_only=False):
    op = Operation.new_from_name(operation_name)

    # we are only interested in non-deprecated args
    args = [[name, flags] for name, flags in op.get_args()
            if not flags & _DEPRECATED]

    # find the first required input image arg, if any ... that will be self
    member_x = None
    for name, flags in args:
        if ((flags & _INPUT) != 0 and
                (flags & _REQUIRED) != 0 and
                op.get_typeof(name) == GValue.image_type):
            member_x = name
            break

    required_input = [name for name, flags in args
                      if (flags & _INPUT) != 0 and
                      (flags & _REQUIRED) != 0 and
                      name != member_x]

    required_output = [name for name, flags in args
                       if ((flags & _OUTPUT) != 0 and
                           (flags & _REQUIRED) != 0) or
                       ((flags & _INPUT) != 0 and
                        (flags & _REQUIRED) != 0 and
                        (flags & _MODIFY) != 0) and
                       name != member_x]

    has_output = len(required_output) >= 1

    # Add a C++ style comment block with some additional markings (@param, 
    # @return)
    if declaration_only:
        result = '\n/**\n * {}.'.format(op.get_description().capitalize())

        for name in required_input:
            result += '\n * @param {} {}.' \
                      .format(cppize(name), op.get_blurb(name))

        if has_output:
            # skip the first element
            for name in required_output[1:]:
                result += '\n * @param {} {}.' \
                          .format(cppize(name), op.get_blurb(name))

        result += '\n * @param options Optional options.'

        if has_output:
            result += '\n * @return {}.' \
                      .format(op.get_blurb(required_output[0]))

        result += '\n */\n'
    else:
        result = '\n'

    if member_x is None and declaration_only:
        result += 'static '
    if has_output:
        # the first output arg will be used as the result
        cpp_type = get_cpp_type(op.get_typeof(required_output[0]))
        spacing = '' if cpp_type.endswith('*') else ' '
        result += '{0}{1}'.format(cpp_type, spacing)
    else:
        result += 'void '

    if not declaration_only:
        result += 'VImage::'

    result += '{0}( '.format(operation_name)
    for name in required_input:
        gtype = op.get_typeof(name)
        cpp_type = get_cpp_type(gtype)
        spacing = '' if cpp_type.endswith('*') else ' '
        result += '{0}{1}{2}, '.format(cpp_type, spacing, cppize(name))

    # output params are passed by reference
    if has_output:
        # skip the first element
        for name in required_output[1:]:
            gtype = op.get_typeof(name)
            cpp_type = get_cpp_type(gtype)
            spacing = '' if cpp_type.endswith('*') else ' '
            result += '{0}{1}*{2}, '.format(cpp_type, spacing, cppize(name))

    result += 'VOption *options {0})'.format('= 0 ' if declaration_only else '')

    # if no 'this' available, it's a class method and they are all const
    if member_x is not None:
        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]
        cpp_type = get_cpp_type(op.get_typeof(name))
        spacing = '' if cpp_type.endswith('*') else ' '
        result += '    {0}{1}{2};\n\n'.format(cpp_type, spacing, cppize(name))

    result += '    call( "{0}",\n'.format(operation_name)
    result += '        (options ? options : VImage::option())'
    if member_x is not None:
        result += '->\n'
        result += '            set( "{0}", *this )'.format(member_x)

    all_required = required_input

    if has_output:
        # first element needs to be passed by reference
        arg = cppize(required_output[0])
        result += '->\n'
        result += '            set( "{0}", &{1} )' \
                  .format(required_output[0], arg)

        # append the remaining list
        all_required += required_output[1:]

    for name in all_required:
        arg = cppize(name)
        result += '->\n'
        result += '            set( "{0}", {1} )'.format(name, arg)

    result += ' );\n'

    if has_output:
        result += '\n'
        result += '    return( {0} );\n'.format(required_output[0])

    result += '}'

    return result
Example #8
0
def bandjoin(bands: List[VIPSImage]) -> VIPSImage:
    if len(bands) == 1:
        return bands[0]

    return Operation.call('bandjoin', bands)