Convolution:

Demo
The convolution demo consists of a source image display, a control panel with ten pre-loaded kernels, a display of kernel coefficients, and a destination image display. Pressing a button loads the appropriate kernel, performs a convolution between the source image and kernel, and displays the the result in the destination image window. The kernel is normalized by the sum of the coefficients, this sum is shown in the center test field. If the sum of the coefficients is zero, the sum is set to 1.0. The kernel matrix text fields are editable and input is restricted to valid integer or floating point numbers. This allows investigation of various kernels and their influence on the convolution operation. The Custom button can be used to perform a convolution with the new kernel. Please note that custom kernels are not saved and pressing another kernel option will erase the current kernel coefficients.

JAI
Pseudocode for the convolution operation on a single sample dst[x][y] follows. Assume the kernel is of size width by height and has already been rotated through 180 degrees. The kernel's Origin element is located at position (xOrigin, yOrigin):



dst[x][y] = 0;
for (int i = -xOrigin; i < -xOrigin + width; i++) {
   for (int j = -yOrigin; j < -yOrigin + height; j++) {
      dst[x][y] += src[x+i][y+j] * kernel[xOrigin+i][yOrigin+j];
   }
}

Convolution, like any neighborhood operation, leaves a band of pixels around the edges undefined. For example, for a 3x3 kernel only four kernel elements and four source pixels contribute to the convolution pixel at the corners of the source image. Pixels that do not allow the full kernel to be applied to the source are not included in the destination image. A "Border" operation may be used to add an appropriate border to the source image in order to avoid shrinkage of the image boundaries. The kernel may not be bigger in any dimension than the image data.

A KernelJAI object is characterized by its width, height, and origin, or key element. The key element is the element which is placed over the current source pixel to perform convolution or error diffusion. In the case of ordered dithering an array of KernelJAI objects is actually required with there being one KernelJAI per band of the image to be dithered. For ordered dithering the location of the key element is irrelevant.

Theory
Convolution is a spatial operation that computes each output sample by multiplying elements of a kernel with the samples surrounding a particular source sample.

For each destination sample, the kernel is rotated 180 degrees and its "key element," or origin, is placed over the source pixel corresponding with the destination pixel. The kernel elements are multiplied with the source pixels beneath them and the resulting products are summed to produce the destination sample value.