1 /* read a single getpoint
2 *
3 * Copyright: J. Cupitt
4 * Written: 15/06/1992
5 * 22/7/93 JC
6 * - im_incheck() added
7 * 16/8/94 JC
8 * - im_incheck() changed to im_makerw()
9 * 5/12/06
10 * - im_invalidate() after paint
11 * 6/3/10
12 * - don't im_invalidate() after paint, this now needs to be at a higher
13 * level
14 * 29/9/10
15 * - gtk-doc
16 * - use Draw base class
17 * - read_getpoint partial-ised
18 * 10/2/14
19 * - redo as a class
20 * 16/12/14
21 * - free the input region much earlier
22 * 14/10/16
23 * - crop to a memory image rather than using a region ... this means we
24 * use workers to calculate pixels and therefore use per-thread caching
25 * in the revised buffer system
26 */
27
28 /*
29
30 This file is part of VIPS.
31
32 VIPS is free software; you can redistribute it and/or modify
33 it under the terms of the GNU Lesser General Public License as published by
34 the Free Software Foundation; either version 2 of the License, or
35 (at your option) any later version.
36
37 This program is distributed in the hope that it will be useful,
38 but WITHOUT ANY WARRANTY; without even the implied warranty of
39 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
40 GNU Lesser General Public License for more details.
41
42 You should have received a copy of the GNU Lesser General Public License
43 along with this program; if not, write to the Free Software
44 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
45 02110-1301 USA
46
47 */
48
49 /*
50
51 These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
52
53 */
54
55 /*
56 #define VIPS_DEBUG
57 */
58
59 #ifdef HAVE_CONFIG_H
60 #include <config.h>
61 #endif /*HAVE_CONFIG_H*/
62 #include <vips/intl.h>
63
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include <math.h>
68
69 #include <vips/vips.h>
70 #include <vips/internal.h>
71
72 #include "statistic.h"
73
74 typedef struct _VipsGetpoint {
75 VipsOperation parent_instance;
76
77 VipsImage *in;
78 int x;
79 int y;
80 VipsArrayDouble *out_array;
81
82 } VipsGetpoint;
83
84 typedef VipsOperationClass VipsGetpointClass;
85
86 G_DEFINE_TYPE( VipsGetpoint, vips_getpoint, VIPS_TYPE_OPERATION );
87
88 static int
vips_getpoint_build(VipsObject * object)89 vips_getpoint_build( VipsObject *object )
90 {
91 VipsGetpoint *getpoint = (VipsGetpoint *) object;
92 VipsImage **t = (VipsImage **) vips_object_local_array( object, 4 );
93
94 double *vector;
95 VipsArrayDouble *out_array;
96
97 if( VIPS_OBJECT_CLASS( vips_getpoint_parent_class )->build( object ) )
98 return( -1 );
99
100 /* Crop, decode and unpack to double.
101 */
102 if( vips_crop( getpoint->in, &t[0],
103 getpoint->x, getpoint->y, 1, 1, NULL ) ||
104 vips_image_decode( t[0], &t[1] ) ||
105 vips_cast( t[1], &t[2], VIPS_FORMAT_DOUBLE, NULL ) )
106 return( -1 );
107
108 /* To a mem buffer, then copy to out.
109 */
110 vips_image_set_int( t[2], "hide-progress", 1 );
111 if( !(t[3] = vips_image_new_memory()) ||
112 vips_image_write( t[2], t[3] ) )
113 return( -1 );
114
115 if( !(vector = VIPS_ARRAY( getpoint->in, t[3]->Bands, double )) )
116 return( -1 );
117 memcpy( vector, t[3]->data, VIPS_IMAGE_SIZEOF_PEL( t[3] ) );
118
119 out_array = vips_array_double_new( vector, t[3]->Bands );
120 g_object_set( object,
121 "out_array", out_array,
122 NULL );
123 vips_area_unref( VIPS_AREA( out_array ) );
124
125 return( 0 );
126 }
127
128 static void
vips_getpoint_class_init(VipsGetpointClass * class)129 vips_getpoint_class_init( VipsGetpointClass *class )
130 {
131 GObjectClass *gobject_class = (GObjectClass *) class;
132 VipsObjectClass *object_class = (VipsObjectClass *) class;
133
134 gobject_class->set_property = vips_object_set_property;
135 gobject_class->get_property = vips_object_get_property;
136
137 object_class->nickname = "getpoint";
138 object_class->description = _( "read a point from an image" );
139 object_class->build = vips_getpoint_build;
140
141 VIPS_ARG_IMAGE( class, "in", 1,
142 _( "in" ),
143 _( "Input image" ),
144 VIPS_ARGUMENT_REQUIRED_INPUT,
145 G_STRUCT_OFFSET( VipsGetpoint, in ) );
146
147 VIPS_ARG_BOXED( class, "out_array", 2,
148 _( "Output array" ),
149 _( "Array of output values" ),
150 VIPS_ARGUMENT_REQUIRED_OUTPUT,
151 G_STRUCT_OFFSET( VipsGetpoint, out_array ),
152 VIPS_TYPE_ARRAY_DOUBLE );
153
154 VIPS_ARG_INT( class, "x", 5,
155 _( "x" ),
156 _( "Point to read" ),
157 VIPS_ARGUMENT_REQUIRED_INPUT,
158 G_STRUCT_OFFSET( VipsGetpoint, x ),
159 0, VIPS_MAX_COORD, 0 );
160
161 VIPS_ARG_INT( class, "y", 6,
162 _( "y" ),
163 _( "Point to read" ),
164 VIPS_ARGUMENT_REQUIRED_INPUT,
165 G_STRUCT_OFFSET( VipsGetpoint, y ),
166 0, VIPS_MAX_COORD, 0 );
167
168 }
169
170 static void
vips_getpoint_init(VipsGetpoint * getpoint)171 vips_getpoint_init( VipsGetpoint *getpoint )
172 {
173 }
174
175 /**
176 * vips_getpoint: (method)
177 * @in: image to read from
178 * @vector: (out)(array length=n): output pixel value here
179 * @n: length of output vector
180 * @x: position to read
181 * @y: position to read
182 * @...: %NULL-terminated list of optional named arguments
183 *
184 * Reads a single pixel on an image.
185 *
186 * The pixel values are returned in @vector, the length of the
187 * array in @n. You must free the array with g_free() when you are done with
188 * it.
189 *
190 * See also: vips_draw_point().
191 *
192 * Returns: 0 on success, or -1 on error.
193 */
194 int
vips_getpoint(VipsImage * in,double ** vector,int * n,int x,int y,...)195 vips_getpoint( VipsImage *in, double **vector, int *n, int x, int y, ... )
196 {
197 va_list ap;
198 VipsArrayDouble *out_array;
199 VipsArea *area;
200 int result;
201
202 va_start( ap, y );
203 result = vips_call_split( "getpoint", ap, in, &out_array, x, y );
204 va_end( ap );
205
206 if( result )
207 return( -1 );
208
209 area = VIPS_AREA( out_array );
210 *vector = VIPS_ARRAY( NULL, area->n, double );
211 if( !*vector ) {
212 vips_area_unref( area );
213 return( -1 );
214 }
215 memcpy( *vector, area->data, area->n * area->sizeof_type );
216 *n = area->n;
217 vips_area_unref( area );
218
219 return( 0 );
220 }
221