interpolate.c 2.71 KB
Newer Older
Emmanuel Bertin's avatar
Emmanuel Bertin committed
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
 /*
 				interpolate.c

*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*
*	Part of:	SExtractor
*
*	Author:		E.BERTIN (IAP)
*
*	Contents:	Interpolation of input data.
*
*	Last modify:	26/11/2003
*
*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
*/

#ifdef HAVE_CONFIG_H
#include        "config.h"
#endif

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

#include	"define.h"
#include	"globals.h"
#include	"field.h"
#include	"interpolate.h"


/****************************** init_interpolate ****************************/
/*
Init resources required for data interpolation.
*/
void    init_interpolate(picstruct *field, int xtimeout, int ytimeout)

  {
  QMALLOC(field->interp_backup, PIXTYPE, field->width);
/* ytimeout < 0 means we don't need a timeout buffer (it won't be used) */
  if (ytimeout>=0)
    {
    QMALLOC(field->interp_ytimeoutbuf, int, field->width);
    memset(field->interp_ytimeoutbuf, 0, (size_t)(field->width*sizeof(int)));
    }

  field->interp_xtimeout = xtimeout;
  field->interp_ytimeout = ytimeout;

  field->interp_flag = 1;

  return;
  }


/******************************** interpolate *******************************/
/*
Interpolate (crudely) input data.
*/
void    interpolate(picstruct *field, picstruct *wfield,
		PIXTYPE *data, PIXTYPE *wdata)

  {
   PIXTYPE	*backup,*wbackup,
		thresh;
   int		*ytimeout,
		xtimeout,xtimeout0,ytimeout0, allflag, i;

  thresh = wfield->weight_thresh;
  backup = field->interp_backup;
  wbackup = wfield->interp_backup;
  ytimeout = wfield->interp_ytimeoutbuf;
  xtimeout0 = wfield->interp_xtimeout;
  ytimeout0 = wfield->interp_ytimeout;
  xtimeout = 0;	/* Start as if the previous pixel was already interpolated */
  allflag = field->interp_flag;
  for (i=field->width; i--; ytimeout++)
    {
/*-- Check if interpolation is needed */
    if (*wdata>=thresh)	/* It's a variance map: the bigger the worse */
      {
/*---- Check if the previous pixel was already interpolated */
      if (!xtimeout)
        {
        if (*ytimeout)
          {
          (*ytimeout)--;
          *wdata = *wbackup;
          if (allflag)
            *data = *backup;
          }
        }
      else
        {
        xtimeout--;
        *wdata = *(wdata-1);
        if (allflag)
          *data = *(data-1);
        }
      }
    else
      {
      xtimeout = xtimeout0;
     *ytimeout = ytimeout0;
      }
    *(wbackup++) = *(wdata++);
    if (allflag)
      *(backup++) = *(data++);
    }

  return;
  }


/******************************* end_interpolate ****************************/
/*
Free memory allocated for data interpolation.
*/
void    end_interpolate(picstruct *field)

  {
  free(field->interp_backup);
  free(field->interp_ytimeoutbuf);

  return;
  }