1 /*--------------------------------------------------------------
2 	Signal processing function library    libsgp
3 	  ( Convolution  Rev.940809)
4 		Written by H.Goto , Aug.1994
5 --------------------------------------------------------------*/
6 
7 /*--------------------------------------------------------------------
8   Copyright (C) 1994  Hideaki Goto
9 
10         All Rights Reserved
11 
12   Permission to use, copy, modify, and distribute this software and
13   its documentation for any purpose is hereby granted without fee,
14   provided that (i) the above copyright notice and this permission
15   notice appear in all copies and in supporting documentation, (ii)
16   the name of the author, Hideaki Goto, may not be used in any
17   advertising or otherwise to promote the sale, use or other
18   dealings in this software without prior written authorization
19   from the author, (iii) this software may not be used for
20   commercial products without prior written permission from the
21   author, and (iv) the notice of the modification is specified in
22   case of that the modified copies of this software are distributed.
23 
24   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND.
25   THE AUTHOR WILL NOT BE RESPONSIBLE FOR ANY DAMAGE CAUSED BY THIS
26   SOFTWARE.
27 --------------------------------------------------------------------*/
28 
29 
30 #include	<stdio.h>
31 #include	<stdlib.h>
32 #include	<memory.h>
33 
34 #include	"sgplib.h"
35 
36 
37 /*------------------------------------------------------
38 	Convolution
39 ------------------------------------------------------*/
40 
sgp_convolutec(char * src,char * dst,int len,char sdata,char * mask,char div,int masklen)41 int sgp_convolutec(char *src,char *dst,int len,char sdata, \
42 				char *mask,char div,int masklen){
43 	char	*buf,sum;
44 	int	i,j,off;
45 	off = (masklen -1) /2;
46 	if ( NULL == (buf = (char *)malloc(len + masklen)) )  return(-1);
47 	for ( i=0 ; i<off ; i++ )  buf[i] = sdata;
48 	for ( i=(off + masklen) ; i<(len + masklen) ; i++ )  buf[i] = sdata;
49 	memcpy((char *)&buf[off],(char *)src,len * sizeof(char));
50 	if ( div == 1 ){
51 		for ( i=0 ; i<len ; i++ ){
52 			sum = 0;
53 			for ( j=0 ; j<masklen ; j++ )  sum += mask[j] * buf[i + j];
54 			*dst++ = sum;
55 		}
56 	}
57 	else{	for ( i=0 ; i<len ; i++ ){
58 			sum = 0;
59 			for ( j=0 ; j<masklen ; j++ )  sum += mask[j] * buf[i + j];
60 			*dst++ = sum / div;
61 		}
62 	}
63 	free(buf);
64 	return(0);
65 }
66 
67 
sgp_convolutei(int * src,int * dst,int len,int sdata,int * mask,int div,int masklen)68 int sgp_convolutei(int *src,int *dst,int len,int sdata, \
69 				int *mask,int div,int masklen){
70 	int	*buf,sum;
71 	int	i,j,off;
72 	off = (masklen -1) /2;
73 	if ( NULL == (buf = (int *)malloc((len + masklen) * sizeof(int))) )  return(-1);
74 	for ( i=0 ; i<off ; i++ )  buf[i] = sdata;
75 	for ( i=(off + masklen) ; i<(len + masklen) ; i++ )  buf[i] = sdata;
76 	memcpy((char *)&buf[off],(char *)src,len * sizeof(int));
77 	if ( div == 1 ){
78 		for ( i=0 ; i<len ; i++ ){
79 			sum = 0;
80 			for ( j=0 ; j<masklen ; j++ )  sum += mask[j] * buf[i + j];
81 			*dst++ = sum;
82 		}
83 	}
84 	else{	for ( i=0 ; i<len ; i++ ){
85 			sum = 0;
86 			for ( j=0 ; j<masklen ; j++ )  sum += mask[j] * buf[i + j];
87 			*dst++ = sum / div;
88 		}
89 	}
90 	free(buf);
91 	return(0);
92 }
93 
94 
95