src/filter.c

Go to the documentation of this file.
00001 /*
00002    libit - Library for basic source and channel coding functions
00003    Copyright (C) 2005-2005 Vivien Chappelier, Herve Jegou
00004 
00005    This library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Library General Public
00007    License as published by the Free Software Foundation; either
00008    version 2 of the License, or (at your option) any later version.
00009 
00010    This library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Library General Public License for more details.
00014 
00015    You should have received a copy of the GNU Library General Public
00016    License along with this library; if not, write to the Free
00017    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018 */
00019 
00020 /*
00021   Filtering
00022   Copyright (C) 2005 Vivien Chappelier.
00023 */
00024 
00025 #include <it/types.h>
00026 #include <it/filter.h>
00027 
00028 /* X = x + d with proper bound checking and mirroring */
00029 #define mirror_add(X, x, d, min, max) do {  \
00030     (X) = (x) + (d);        \
00031     if((X) < (min) || (X) >= (max))   \
00032       (X) = (x) - (d);        \
00033 } while(0)
00034 
00035 mat mat_filter_fir (mat input, mat filter, int px, int py)
00036 {
00037   idx_t X, Y;
00038   idx_t x, y;
00039   idx_t dx, dy;
00040   idx_t wf, hf;
00041   idx_t w, h;
00042   mat  output;
00043 
00044   w = mat_width (input);
00045   h = mat_height (input);
00046   wf = mat_width (filter);
00047   hf = mat_height (filter);
00048 
00049   assert (px >= 0 && px < wf);
00050   assert (py >= 0 && py < hf);
00051 
00052   output = mat_new_zeros (h, w);
00053 
00054   for (x = 0; x < w; x++) {
00055     for (y = 0; y < h; y++) {
00056 
00057       /* filter around position x,y */
00058       for (dx = -px; dx < wf - px; dx++) {
00059   for (dy = -py; dy < hf - py; dy++) {
00060     /* compute the input sample location */
00061     mirror_add (X, x, dx, 0, w);
00062     mirror_add (Y, y, dy, 0, h);
00063 
00064     output[y][x] += filter[dy + py][dx + px] * input[Y][X];
00065   }
00066       }
00067 
00068     }
00069   }
00070 
00071   return (output);
00072 }

Hosted by
Copyright (C) 2005-2006 Hervé Jégou
Vivien Chappelier
Francois Cayre
libit logo courtesy of Jonathan Delhumeau