HAWKI Pipeline Reference Manual  1.8.12
hawki_mask.c
1 /* $Id: hawki_mask.c,v 1.3 2010/03/12 12:55:17 cgarcia Exp $
2  *
3  * This file is part of the HAWKI Pipeline
4  * Copyright (C) 2002,2003 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 
21 /*
22  * $Author: cgarcia $
23  * $Date: 2010/03/12 12:55:17 $
24  * $Revision: 1.3 $
25  * $Name: hawki-1_8_12 $
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 #include <config.h>
30 #endif
31 
32 /*-----------------------------------------------------------------------------
33  Includes
34  -----------------------------------------------------------------------------*/
35 
36 #include <math.h>
37 #include <string.h>
38 #include <cpl.h>
39 #include <cpl_mask.h>
40 #include <cpl_matrix.h>
41 
42 #include "hawki_mask.h"
43 
44 /*----------------------------------------------------------------------------*/
49 /*----------------------------------------------------------------------------*/
50 
56 cpl_error_code hawki_mask_convolve(
57  cpl_mask * in,
58  const cpl_matrix * ker)
59 {
60  cpl_mask * out;
61  const double * ker_arr;
62  int nc, nr;
63  int hsx, hsy;
64  int curr_pos, im_pos, filt_pos;
65  int i, j, k, l;
66  double sum;
67  int nx;
68  int ny;
69  cpl_binary * in_data;
70  cpl_binary * out_data;
71 
72  /* Test entries */
73  cpl_ensure_code(in && ker, CPL_ERROR_NULL_INPUT);
74 
75  /* Get kernel informations */
76  nr = cpl_matrix_get_nrow(ker);
77  nc = cpl_matrix_get_ncol(ker);
78  ker_arr = cpl_matrix_get_data_const(ker);
79 
80  /* Test the kernel validity */
81  cpl_ensure_code(nc%2 && nr%2, CPL_ERROR_ILLEGAL_INPUT);
82  cpl_ensure_code(nc<=31 && nr<=31, CPL_ERROR_ILLEGAL_INPUT);
83 
84  /* Initialise */
85  hsx = (nc-1) / 2;
86  hsy = (nr-1) / 2;
87 
88  /* Create a tmp binary image */
89  nx = cpl_mask_get_size_x(in);
90  ny = cpl_mask_get_size_y(in);
91  out = cpl_mask_new(nx, ny);
92  in_data = cpl_mask_get_data(in);
93  out_data = cpl_mask_get_data(out);
94 
95  /* Main filter loop */
96  for (j=0; j<ny; j++) {
97  for (i=0; i<nx; i++) {
98  /* Curent pixel position */
99  curr_pos = i + j*nx;
100  /* Edges are not computed */
101  if ((i<hsx) || (i>=nx-hsx) || (j<hsy) || (j>=ny-hsy)) {
102  (out_data)[curr_pos] = CPL_BINARY_0;
103  } else {
104  /* Initialise */
105  (out_data)[curr_pos] = CPL_BINARY_0;
106  /* Go into upper left corner of current pixel */
107  im_pos = curr_pos - hsx + hsy*nx;
108  filt_pos = 0;
109  sum = 0;
110  for (k=0; k<nr; k++) {
111  for (l=0; l<nc; l++) {
112  if (((in_data)[im_pos] == CPL_BINARY_1) &&
113  (fabs(ker_arr[filt_pos]) > FLT_MIN))
114  sum+=fabs(ker_arr[filt_pos]);
115  /* Next col */
116  filt_pos++;
117  im_pos++;
118  }
119  /* Next row */
120  im_pos -= nx + nc;
121  }
122  if(sum>0.5)
123  (out_data)[curr_pos] = CPL_BINARY_1;
124  }
125  }
126  }
127  memcpy(in_data, out_data, nx * ny * sizeof(cpl_binary));
128  cpl_mask_delete(out);
129  return CPL_ERROR_NONE;
130 }
131