00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 #include <config.h>
00030 #endif
00031
00032
00033
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
00073 cpl_ensure_code(in && ker, CPL_ERROR_NULL_INPUT);
00074
00075
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
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
00085 hsx = (nc-1) / 2;
00086 hsy = (nr-1) / 2;
00087
00088
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
00096 for (j=0; j<ny; j++) {
00097 for (i=0; i<nx; i++) {
00098
00099 curr_pos = i + j*nx;
00100
00101 if ((i<hsx) || (i>=nx-hsx) || (j<hsy) || (j>=ny-hsy)) {
00102 (out_data)[curr_pos] = CPL_BINARY_0;
00103 } else {
00104
00105 (out_data)[curr_pos] = CPL_BINARY_0;
00106
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
00116 filt_pos++;
00117 im_pos++;
00118 }
00119
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