File: unaryconst.c

package info (click to toggle)
vips 8.14.1-3%2Bdeb12u2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 35,912 kB
  • sloc: ansic: 165,449; cpp: 10,987; python: 4,462; xml: 4,212; sh: 471; perl: 40; makefile: 23
file content (153 lines) | stat: -rw-r--r-- 4,002 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/* an image plus a constant
 *
 * 11/11/11
 * 	- from arith_binary_const
 * 21/8/19
 * 	- revise to fix out of range comparisons
 */

/*

    Copyright (C) 1991-2005 The National Gallery

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA

 */

/*

    These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk

 */

/*
#define DEBUG
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H*/
#include <glib/gi18n-lib.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <vips/vips.h>

#include "unaryconst.h"

G_DEFINE_ABSTRACT_TYPE( VipsUnaryConst, vips_unary_const, VIPS_TYPE_UNARY );

static int
vips_unary_const_build( VipsObject *object )
{
	VipsObjectClass *class = VIPS_OBJECT_GET_CLASS( object );
	VipsArithmetic *arithmetic = VIPS_ARITHMETIC( object );
	VipsUnary *unary = (VipsUnary *) object;
	VipsUnaryConst *uconst = (VipsUnaryConst *) object;

	/* If we have a three-element vector we need to bandup the image to
	 * match.
	 */
	uconst->n = 1;
	if( uconst->c )
		uconst->n = VIPS_MAX( uconst->n, uconst->c->n );
	if( unary->in )
		uconst->n = VIPS_MAX( uconst->n, unary->in->Bands );
	arithmetic->base_bands = uconst->n;

	if( unary->in && 
		uconst->c ) {
		if( vips_check_vector( class->nickname, 
			uconst->c->n, unary->in ) )
		return( -1 );
	}

	/* Some operations need int constants, for example boolean AND, SHIFT
	 * etc.
	 *
	 * Some can use int constants as an optimisation, for example (x <
	 * 12). It depends on the value though: obviously (x < 12.5) should
	 * not use the int form.
	 *
	 * For complex images, we double the vector length and set the
	 * imaginary part to 0.
	 */
	if( uconst->c ) {
		gboolean is_complex = 
			vips_band_format_iscomplex( unary->in->BandFmt );
		int step = is_complex ? 2 : 1;
		int n = step * uconst->n;
		double *c = (double *) uconst->c->data;

		int i;

		uconst->c_int = VIPS_ARRAY( object, n, int );
		uconst->c_double = VIPS_ARRAY( object, n, double );
		if( !uconst->c_int ||
			!uconst->c_double )
			return( -1 );
		memset( uconst->c_int, 0, n * sizeof( int ) );
		memset( uconst->c_double, 0, n * sizeof( double ) );

		for( i = 0; i < n; i += step )
			uconst->c_double[i] = 
				c[VIPS_MIN( i / step, uconst->c->n - 1)];

		for( i = 0; i < n; i += step )
			uconst->c_int[i] = uconst->c_double[i];
		
		uconst->is_int = TRUE;
		for( i = 0; i < n; i += step )
			if( uconst->c_int[i] != uconst->c_double[i] ) {
				uconst->is_int = FALSE;
				break;
			}
	}

	if( VIPS_OBJECT_CLASS( vips_unary_const_parent_class )->
		build( object ) )
		return( -1 );

	return( 0 );
}

static void
vips_unary_const_class_init( VipsUnaryConstClass *class )
{
	GObjectClass *gobject_class = G_OBJECT_CLASS( class );
	VipsObjectClass *object_class = (VipsObjectClass *) class;

	gobject_class->set_property = vips_object_set_property;
	gobject_class->get_property = vips_object_get_property;

	object_class->nickname = "unary_const";
	object_class->description = _( "unary operations with a constant" );
	object_class->build = vips_unary_const_build;

	VIPS_ARG_BOXED( class, "c", 201, 
		_( "c" ), 
		_( "Array of constants" ),
		VIPS_ARGUMENT_REQUIRED_INPUT,
		G_STRUCT_OFFSET( VipsUnaryConst, c ),
		VIPS_TYPE_ARRAY_DOUBLE );
}

static void
vips_unary_const_init( VipsUnaryConst *uconst )
{
}