function [outm]=imconv(inm, mask, form);
//
// This function space-domain-convolves 2D matrix "inm" with nxn "mask",
// n odd, yielding "outm".
// Output isn't normalized.
//
// AUTHOR
//    Ricardo Fabbri  <rfabbri@if.sc.usp.br>
//    Cybernetic Vision Research Group
//    Luciano da Fontoura Costa, supervisor.
//    http://cyvision.if.sc.usp.br
//
// REMARKS
//    The implementation incorporates suggestions from Scilab group.
//
// $Revision: 1.5 $ $Date: 2002/02/02 13:11:25 $

if argn(2)==2 then 
   form = 'same'
elseif argn(2) <> 3 then 
   error('Invalid number of arguments.')
end

[mm,nm]=size(mask)
r=(mm-1); 
if mm<>nm | mm==2*int(mm/2) then 
  error('mask must be a square nxn matrix, n odd.'),end

[m,n]=size(inm)
select form
case 'same' then    // pad image and return the original(same) size
   m = m+r, n = n+r
   aux = zeros(m ,n) 
   aux((1+r/2):(m-r/2), (1+r/2):(n-r/2)) = inm
   inm = aux
   clear aux
case 'full' then    // pad image and outm has same size as padded image
   m = m+2*r, n = n+2*r
   aux = zeros(m ,n) 
   aux(r+1:m-r,r+1:n-r) = inm
   inm = aux
   clear aux
case 'valid' then    // don't padd and return normally 
else
   error('Invalid format specification.')
end

inm=double(inm)
outm=zeros(inm);     //final image gets a black border

M=matrix(mask,1,-1)  // reshapes mask into a vector
nelem=size(M,'*');

// J --> column numbers of sliding window
J=ones(1,n-r).*.(0:r)+(1:n-r).*.ones(1,mm) 

for i=(1+r/2):(m-r/2);
  outm(i,(1+r/2):(n-r/2))=M*matrix( inm(i-r/2:i+r/2,J), nelem, n-r)
end

outm = outm((1+r/2):(m-r/2), (1+r/2):(n-r/2))

//
// Documentation of the above procedure:
//
// inm(i-r/2:i+r/2, J) contains all the neighborhoods of
// elements in row i of inm, side-by-side, so this is a 
// mm x((n-r/2)*3) matrix. 
//
// matrix( inm(i-r/2:i+r/2,J), nelem, n-r) is the same
// matrix, but now it is mm x(n-r/2), that is, each
// neighborhood is a mm x 1 vector, and they are side-by-side.
//
// Now, by multiplying the original mask and that matrix,
// we have row i of the convolved matrix. 
//

//
// -------------------------------------------------------------------------
// SIP - Scilab Image Processing toolbox
// Copyright (C) 2002  Ricardo Fabbri
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// -------------------------------------------------------------------------
//
