HAWKI Pipeline Reference Manual 1.8.12
hawki_mask.c
00001 /* $Id: hawki_mask.c,v 1.3 2010/03/12 12:55:17 cgarcia Exp $
00002  *
00003  * This file is part of the HAWKI Pipeline
00004  * Copyright (C) 2002,2003 European Southern Observatory
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 
00021 /*
00022  * $Author: cgarcia $
00023  * $Date: 2010/03/12 12:55:17 $
00024  * $Revision: 1.3 $
00025  * $Name: hawki-1_8_12 $
00026  */
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031 
00032 /*-----------------------------------------------------------------------------
00033                                 Includes
00034  -----------------------------------------------------------------------------*/
00035 
00036 #include <math.h>
00037 #include <string.h>
00038 #include <cpl.h>
00039 #include <cpl_mask.h>
00040 #include <cpl_matrix.h>
00041 
00042 #include "hawki_mask.h"
00043 
00044 /*----------------------------------------------------------------------------*/
00049 /*----------------------------------------------------------------------------*/
00050 
00056 cpl_error_code hawki_mask_convolve(
00057         cpl_mask            *   in,
00058         const cpl_matrix    *   ker)
00059 {
00060     cpl_mask        *   out;
00061     const double    *   ker_arr;
00062     int                 nc, nr;
00063     int                 hsx, hsy;
00064     int                 curr_pos, im_pos, filt_pos;
00065     int                 i, j, k, l;
00066     double              sum;
00067     int                 nx;
00068     int                 ny;
00069     cpl_binary      *   in_data;
00070     cpl_binary      *   out_data;
00071 
00072     /* Test entries */
00073     cpl_ensure_code(in && ker, CPL_ERROR_NULL_INPUT);
00074 
00075     /* Get kernel informations */
00076     nr = cpl_matrix_get_nrow(ker);
00077     nc = cpl_matrix_get_ncol(ker);
00078     ker_arr = cpl_matrix_get_data_const(ker);
00079 
00080     /* Test the kernel validity */
00081     cpl_ensure_code(nc%2 && nr%2, CPL_ERROR_ILLEGAL_INPUT);
00082     cpl_ensure_code(nc<=31 && nr<=31, CPL_ERROR_ILLEGAL_INPUT);
00083 
00084     /* Initialise */
00085     hsx = (nc-1) / 2;
00086     hsy = (nr-1) / 2;
00087 
00088     /* Create a tmp binary image */
00089     nx = cpl_mask_get_size_x(in);
00090     ny = cpl_mask_get_size_y(in);
00091     out = cpl_mask_new(nx, ny);
00092     in_data = cpl_mask_get_data(in);
00093     out_data = cpl_mask_get_data(out);
00094 
00095     /* Main filter loop */
00096     for (j=0; j<ny; j++) {
00097         for (i=0; i<nx; i++) {
00098             /* Curent pixel position */
00099             curr_pos = i + j*nx;
00100             /* Edges are not computed   */
00101             if ((i<hsx) || (i>=nx-hsx) || (j<hsy) || (j>=ny-hsy)) {
00102                 (out_data)[curr_pos] = CPL_BINARY_0;
00103             } else {
00104                 /* Initialise */
00105                 (out_data)[curr_pos] = CPL_BINARY_0;
00106                 /* Go into upper left corner of current pixel   */
00107                 im_pos = curr_pos - hsx + hsy*nx;
00108                 filt_pos = 0;
00109                 sum      = 0;
00110                 for (k=0; k<nr; k++) {
00111                     for (l=0; l<nc; l++) {
00112                         if (((in_data)[im_pos] == CPL_BINARY_1) &&
00113                                 (fabs(ker_arr[filt_pos]) > FLT_MIN))
00114                             sum+=fabs(ker_arr[filt_pos]);
00115                         /* Next col */
00116                         filt_pos++;
00117                         im_pos++;
00118                     }
00119                     /* Next row */
00120                     im_pos -= nx + nc;
00121                 }
00122                 if(sum>0.5)
00123                     (out_data)[curr_pos] = CPL_BINARY_1;
00124             }
00125         }
00126     }
00127     memcpy(in_data, out_data, nx * ny * sizeof(cpl_binary));
00128     cpl_mask_delete(out);
00129     return CPL_ERROR_NONE;
00130 }
00131