Implementation summary
Do all calculations for red, green and blue components of the image separately, that is,new red values are computed from old red values etc.
Represent pixels as floats: the results of filtering can be negative or ouside 0..255 range; truncate only the final result to this range and convert it to integer.
If you need a pixel value that falls outside the image, use zero. Write a function that returns a value for any integer pair of arguments (i,j). If (i,j) is inside the image it returns the image value, otherwise a zero. Always access the image using this function.