File: im_rightshift_size.c

package info (click to toggle)
vips 7.28.5-1%2Bdeb7u1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,372 kB
  • sloc: ansic: 95,587; cpp: 50,751; sh: 11,548; python: 1,290; makefile: 916; perl: 40
file content (291 lines) | stat: -rw-r--r-- 13,207 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/* fast shrink by a power of two 
 *
 * Copyright: 2006, Tom Vajzovic
 *
 * Author: Tom Vajzovic
 *
 * Written on: 2006-07-26
 *
 * 2006-12-17 tcv: 
 *  - rewrite generator function macros to produce many smaller functions
 *  - remove post-shift-up functionality
 * 2007-02-02 jc
 *  - added return 0; on success
 *  - FUNCTION_NAME updated
 * 2/2/11
 * 	- gtk-doc
 */

/*

    This file is part of VIPS.

    VIPS 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 of the License, or
    (at your option) any later version.

    This program 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 program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

 */

/*

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

 */


/** HEADERS **/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif /*HAVE_CONFIG_H */
#include <vips/intl.h>

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

#include <vips/vips.h>


/** LOCAL FUNCTION DECLARATIONS **/

/* function prototype macro */
#define GEN_FUNC( SHIFT_MACRO, FROM_T, TO_T, SUM_T ) \
static int gen_ ## SHIFT_MACRO ## _ ## FROM_T ## _to_ ## TO_T ## _with_ ## SUM_T( \
          REGION *to_make, void *seq, void *a, void *b );

/* macros to call function prototype macro for all possible combinations of types and macros */
#define GEN_FUNCS_TO( SIGN, FROM_T, TO_T )    \
  GEN_FUNC( PRE_POST_SHIFT, FROM_T, TO_T, SIGN ## 64 ) \
  GEN_FUNC( POST_SHIFT, FROM_T, TO_T, SIGN ## 64 ) \
  GEN_FUNC( POST_SHIFT, FROM_T, TO_T, SIGN ## 32 ) \
  GEN_FUNC( NO_SHIFT, FROM_T, TO_T, SIGN ## 32 )

#define GEN_FUNCS_FROM( SIGN, FROM_T )      \
  GEN_FUNCS_TO( SIGN, FROM_T, SIGN ## 32 )  \
  GEN_FUNCS_TO( SIGN, FROM_T, SIGN ## 16 )  \
  GEN_FUNCS_TO( SIGN, FROM_T, SIGN ## 8 )

#define GEN_FUNCS_SIGN( SIGN )          \
  GEN_FUNCS_FROM( SIGN, SIGN ## 32 )    \
  GEN_FUNCS_FROM( SIGN, SIGN ## 16 )    \
  GEN_FUNCS_FROM( SIGN, SIGN ## 8 )

GEN_FUNCS_SIGN( gint )
GEN_FUNCS_SIGN( guint )


/** FUNCTION DEFINITIONS **/


/**
 * im_rightshift_size:
 * @in: input image
 * @out: output image
 * @xshift: horizontal shrink
 * @yshift: vertical shrink
 * @band_fmt: output format
 *
 * Shrink @in by a pair of power-of-two factors, shifting to give 
 * output of the specified band format. This is faster than im_shrink(). 
 *
 * See also: im_shrink(), im_affinei().
 *
 * Returns: 0 on success, -1 on error
 */
int
im_rightshift_size( IMAGE *in, IMAGE *out, int xshift, int yshift, int band_fmt ){
#define FUNCTION_NAME "im_rightshift_size"
  int *params;
  int needbits;
  int outbits;

  if( im_piocheck( in, out ) )
    return -1;

  if( xshift < 0 || yshift < 0 ){
    im_error( FUNCTION_NAME, "%s", _( "bad arguments" ) );
    return -1;
  }
  if( ! xshift && ! yshift ){
    im_warn( FUNCTION_NAME, "%s", _( "shift by zero: falling back to im_copy" ) );
    return im_copy( in, out );
  }
  if( ! in-> Xsize >> xshift || ! in-> Ysize >> yshift ){
    im_error( FUNCTION_NAME, "%s", _( "would result in zero size output image" ) );
    return -1;
  }
  if( im_check_int( FUNCTION_NAME, in ) || 
	  im_check_uncoded( FUNCTION_NAME, in ) ) 
    return -1;
  if( vips_bandfmt_isuint( in->BandFmt ) != vips_bandfmt_isuint( band_fmt ) ) {
      im_error( FUNCTION_NAME, "%s", 
	_( "image and band_fmt must match in sign" ) );
      return -1;
  }
  outbits= im_bits_of_fmt( band_fmt );

  if( -1 == outbits )
    return -1;

  needbits= im_bits_of_fmt( in-> BandFmt ) + xshift + yshift;

  params= IM_ARRAY( out, 4, int );

  if( ! params )
    return -1;

  params[ 0 ]= xshift;                      /* xshift */
  params[ 1 ]= yshift;                      /* yshift */

  if( im_cp_desc( out, in ) )
    return -1;

  out-> Xsize >>= xshift;
  out-> Ysize >>= yshift;
  out-> Xres/= ( 1 << xshift ); /* x scale */
  out-> Yres/= ( 1 << yshift ); /* y scale */
  out-> BandFmt= band_fmt;

  if( im_demand_hint( out, IM_THINSTRIP, in, NULL ) )
    return -1;

#define RETURN_MACRO( MACRO, FROM_T, TO_T, SUM_T ) \
  return im_generate( out, im_start_one, gen_ ## MACRO ## _ ## FROM_T ## _to_ ## TO_T ## _with_ ## SUM_T, im_stop_one, in, params );

#define RETURN_MACRO_OUTS( MACRO, SUM_SIZE, OUT_SIZE )   switch( in-> BandFmt ){  \
                                                                                  \
  case IM_BANDFMT_CHAR:                                                           \
    RETURN_MACRO( MACRO, gint8, gint ## OUT_SIZE, gint ## SUM_SIZE )              \
                                                                                  \
  case IM_BANDFMT_UCHAR:                                                          \
    RETURN_MACRO( MACRO, guint8, guint ## OUT_SIZE, guint ## SUM_SIZE )           \
                                                                                  \
  case IM_BANDFMT_SHORT:                                                          \
    RETURN_MACRO( MACRO, gint16, gint ## OUT_SIZE, gint ## SUM_SIZE )             \
                                                                                  \
  case IM_BANDFMT_USHORT:                                                         \
    RETURN_MACRO( MACRO, guint16, guint ## OUT_SIZE, guint ## SUM_SIZE )          \
                                                                                  \
  case IM_BANDFMT_INT:                                                            \
    RETURN_MACRO( MACRO, gint32, gint ## OUT_SIZE, gint ## SUM_SIZE )             \
                                                                                  \
  case IM_BANDFMT_UINT:                                                           \
    RETURN_MACRO( MACRO, guint32, guint ## OUT_SIZE, guint ## SUM_SIZE )          \
  default: \
	  g_assert( 0 ); \
}

#define RETURN_MACRO_SUMSS( MACRO, SUM_SIZE )   switch( im_bits_of_fmt( out->BandFmt ) ){  \
  case 32:                                                              \
    RETURN_MACRO_OUTS( MACRO, SUM_SIZE, 32 )                            \
  case 16:                                                              \
    RETURN_MACRO_OUTS( MACRO, SUM_SIZE, 16 )                            \
  case 8:                                                               \
    RETURN_MACRO_OUTS( MACRO, SUM_SIZE, 8 )                             \
}

  if( needbits > 64 ){
    params[ 2 ]= needbits - 64;
    params[ 3 ]= 64 - outbits;
    RETURN_MACRO_SUMSS( PRE_POST_SHIFT, 64 )
  }
  if( needbits > 32 ){
    params[ 3 ]= needbits - outbits;
    RETURN_MACRO_SUMSS( POST_SHIFT, 64 )
  }
  if( needbits > outbits ){
    params[ 3 ]= needbits - outbits;
    RETURN_MACRO_SUMSS( POST_SHIFT, 32 )
  }

  RETURN_MACRO_SUMSS( NO_SHIFT, 32 )

  return 0;

#undef FUNCTION_NAME
}

/** LOCAL FUNCTION DEFINITIONS **/

/* macros used in the function creating macro */
#define PRE_POST_SHIFT()    pixel+= from[ fx ] >> preshift; \
                          to[ tx ]= pixel >> postshift;

#define POST_SHIFT()   pixel+= from[ fx ];                  \
                          to[ tx ]= pixel >> postshift;

#define NO_SHIFT()          pixel+= from[ fx ];             \
                          to[ tx ]= pixel;

/* function creating macro */
#undef GEN_FUNC
#define GEN_FUNC( SHIFT_MACRO, FROM_T, TO_T, SUM_T ) \
static int gen_ ## SHIFT_MACRO ## _ ## FROM_T ## _to_ ## TO_T ## _with_ ## SUM_T(                 \
          REGION *to_make, void *seq, void *a, void *b ){                    \
                                                                                                  \
  REGION *make_from = (REGION *) seq;                                                             \
  int *params = (int *) b;                                                                        \
  int xshift= params[0];                                                                          \
  int yshift= params[1];                                                                          \
  int G_GNUC_UNUSED preshift= params[2];                                                          \
  int G_GNUC_UNUSED postshift= params[3];                                                         \
  Rect need= {                                                                                    \
    to_make-> valid. left << xshift,                                                              \
    to_make-> valid. top << yshift,                                                               \
    to_make-> valid. width << xshift,                                                             \
    to_make-> valid. height << yshift                                                             \
  };                                                                                              \
  TO_T *to_start= (TO_T*) IM_REGION_ADDR( to_make, to_make-> valid. left, to_make-> valid. top ); \
  TO_T *to, *ty_stop;                                                                             \
  SUM_T pixel;                                                                                    \
  FROM_T *from_start, *from_row, *from, *fy_stop;                                                 \
                                                                                                  \
  int band, tx, fx;                                                                               \
  int tx_max= to_make-> valid. width * to_make-> im-> Bands;                                      \
  int fx_max= to_make-> im-> Bands << xshift;                                                     \
                                                                                                  \
  size_t t_skip= IM_REGION_LSKIP( to_make ) / sizeof( TO_T );                                     \
  size_t f_skip;                                                                                  \
                                                                                                  \
  if( im_prepare( make_from, &need ) || ! im_rect_includesrect( &make_from-> valid, &need ) )     \
    return -1;                                                                                    \
                                                                                                  \
  from_start= (FROM_T*) IM_REGION_ADDR( make_from, need. left, need. top );                       \
  f_skip= IM_REGION_LSKIP( make_from ) / sizeof( FROM_T );                                        \
                                                                                                  \
  for( band= 0; band < make_from-> im-> Bands; ++band ){                                          \
                                                                                                  \
    to= to_start + band;                                                                          \
    ty_stop= to + t_skip * to_make-> valid. height;                                               \
                                                                                                  \
    from_row= from_start + band;                                                                  \
                                                                                                  \
    for( ; to < ty_stop; to+= t_skip, from_row+= f_skip << yshift )                               \
      for( tx= 0; tx < tx_max; tx+= to_make-> im-> Bands ){                                       \
                                                                                                  \
        from= from_row + ( tx << xshift );                                                        \
        fy_stop= from + ( f_skip << yshift );                                                     \
                                                                                                  \
        pixel= 0;                                                                                 \
                                                                                                  \
        for( ; from < fy_stop; from+= f_skip )                                                    \
          for( fx= 0; fx < fx_max; fx+= to_make-> im-> Bands )                                    \
            SHIFT_MACRO()                                                                         \
      }                                                                                           \
  }                                                                                               \
  return 0;                                                                                       \
}

/* create functions for all possible combinations of types and macros */
GEN_FUNCS_SIGN( gint )
GEN_FUNCS_SIGN( guint )