1 /* detect zero-crossings
2 *
3 * Copyright: 1990, N. Dessipris.
4 *
5 * Author: Nicos Dessipris
6 * Written on: 12/02/1990
7 * Modified on :
8 * 1/2/95 JC
9 * - rewritten for PIO
10 * - some bugs removed
11 * 11/5/06
12 * - small clean ups
13 * 11/11/10
14 * - small cleanups
15 * - gtkdoc
16 */
17
18 /*
19
20 This file is part of VIPS.
21
22 VIPS is free software; you can redistribute it and/or modify
23 it under the terms of the GNU Lesser General Public License as published by
24 the Free Software Foundation; either version 2 of the License, or
25 (at your option) any later version.
26
27 This program is distributed in the hope that it will be useful,
28 but WITHOUT ANY WARRANTY; without even the implied warranty of
29 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 GNU Lesser General Public License for more details.
31
32 You should have received a copy of the GNU Lesser General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 02110-1301 USA
36
37 */
38
39 /*
40
41 These files are distributed with VIPS - http://www.vips.ecs.soton.ac.uk
42
43 */
44
45 #ifdef HAVE_CONFIG_H
46 #include <config.h>
47 #endif /*HAVE_CONFIG_H*/
48 #include <vips/intl.h>
49
50 #include <stdio.h>
51 #include <stdlib.h>
52
53 #include <vips/vips.h>
54 #include <vips/vips7compat.h>
55
56 #define LOOP( TYPE ) { \
57 for( i = 0; i < ne; i++ ) { \
58 TYPE p1 = ((TYPE *)p)[i]; \
59 TYPE p2 = ((TYPE *)p)[i + ba]; \
60 \
61 if( flag == 1 && p1 > 0 && p2 <= 0 ) \
62 q[i] = 255; \
63 else if( flag == -1 && p1 < 0 && p2 >= 0 ) \
64 q[i] = 255; \
65 else \
66 q[i] = 0; \
67 } \
68 }
69
70 /* Zerox generate function.
71 */
72 static int
zerox_gen(REGION * or,void * seq,void * a,void * b)73 zerox_gen( REGION *or, void *seq, void *a, void *b )
74 {
75 REGION *ir = (REGION *) seq;
76 IMAGE *in = (IMAGE *) a;
77 int flag = GPOINTER_TO_INT( b );
78 Rect irect;
79 Rect *r = &or->valid;
80
81 /* Range of pixels we loop over.
82 */
83 int le = r->left;
84 int to = r->top;
85 int bo = IM_RECT_BOTTOM( r );
86 int ba = in->Bands;
87 int ne = ba * r->width;
88
89 int i, y;
90
91 /* We need to be able to see one pixel to the right.
92 */
93 irect.top = r->top;
94 irect.left = r->left;
95 irect.width = r->width + 1;
96 irect.height = r->height;
97 if( im_prepare( ir, &irect ) )
98 return( -1 );
99
100 for( y = to; y < bo; y++ ) {
101 VipsPel *p = IM_REGION_ADDR( ir, le, y );
102 VipsPel *q = IM_REGION_ADDR( or, le, y );
103
104 switch( in->BandFmt ) {
105 case IM_BANDFMT_CHAR: LOOP( signed char ); break;
106 case IM_BANDFMT_SHORT: LOOP( signed short ); break;
107 case IM_BANDFMT_INT: LOOP( signed int ); break;
108 case IM_BANDFMT_FLOAT: LOOP( float ); break;
109 case IM_BANDFMT_DOUBLE: LOOP( double ); break;
110
111 default:
112 g_assert( 0 );
113 }
114 }
115
116 return( 0 );
117 }
118
119 /**
120 * im_zerox:
121 * @in: input image
122 * @out: output image
123 * @sign: detect positive or negative zero crossings
124 *
125 * im_zerox() detects the positive or negative zero crossings @in,
126 * depending on @sign. If @sign is -1, negative zero crossings are returned,
127 * if @sign is 1, positive zero crossings are returned.
128 *
129 * The output image is byte with zero crossing set to 255 and all other values
130 * set to zero. Input can have any number of channels, and be any non-complex
131 * type.
132 *
133 * See also: im_conv(), im_rot90.
134 *
135 * Returns: 0 on success, -1 on error
136 */
137 int
im_zerox(IMAGE * in,IMAGE * out,int sign)138 im_zerox( IMAGE *in, IMAGE *out, int sign )
139 {
140 IMAGE *t1;
141
142 if( sign != -1 && sign != 1 ) {
143 im_error( "im_zerox", "%s", _( "flag not -1 or 1" ) );
144 return( -1 );
145 }
146 if( in->Xsize < 2 ) {
147 im_error( "im_zerox", "%s", _( "image too narrow" ) );
148 return( -1 );
149 }
150 if( !(t1 = im_open_local( out, "im_zerox" , "p" )) ||
151 im_piocheck( in, t1 ) ||
152 im_check_uncoded( "im_zerox", in ) ||
153 im_check_noncomplex( "im_zerox", in ) )
154 return( -1 );
155 if( vips_bandfmt_isuint( in->BandFmt ) )
156 /* Unsigned type, therefore there will be no zero-crossings.
157 */
158 return( im_black( out, in->Xsize, in->Ysize, in->Bands ) );
159
160 /* Force output to be BYTE. Output is narrower than input by 1 pixel.
161 */
162 if( im_cp_desc( t1, in ) )
163 return( -1 );
164 t1->BandFmt = IM_BANDFMT_UCHAR;
165 t1->Xsize -= 1;
166
167 /* Set hints - THINSTRIP is ok with us.
168 */
169 if( im_demand_hint( t1, IM_THINSTRIP, NULL ) )
170 return( -1 );
171
172 /* Generate image.
173 */
174 if( im_generate( t1, im_start_one, zerox_gen, im_stop_one,
175 in, GINT_TO_POINTER( sign ) ) )
176 return( -1 );
177
178 /* Now embed it in a larger image.
179 */
180 if( im_embed( t1, out, 0, 0, 0, in->Xsize, in->Ysize ) )
181 return( -1 );
182
183 return( 0 );
184 }
185