// This tutorial is part of a series that explains the basic concepts of the APRON simulation system.
// Please ensure you have read and understood any previous tutorial examples.
//
// © David R W Barr 2009
//
// Name: 		KERNELS, CONVOLUTIONS AND FILTERS
// Description:	This tutorial will show how various filtering strategies
//				are provide intrinsically by APRON
// Usage:		Please STEP-THROUGH this algorithm in the simulator.


// Always include this line for an APRON simulation. This file includes many pre-defined
// types and functions that are used in a standard APRON simulation.
!include('apron.aps')

// Create the reources used in this tutorial
!define(RW, 128)
!define(RH, 128)
!create_reg('a', RW, RH) 
!create_reg('b', RW, RH) 
!create_reg('c', RW, RH) 
!create_reg('d', RW, RH)

showreg(r[a])
showreg(r[b])
showreg(r[c])
showreg(r[d])
tile

// KERNELS
// ~~~~~~~
// Kernels are small arrays of data that are used during an image-convolution operation.
// A kernel is defined as:
!create_kernel('edge_detect', 3, 0, -1, 0, -1, 4, -1, 0, -1, 0)

// The first argument is the name. The second is the dimension, in this case a 3x3 kernel
// is required. APRON supports 3x3, 5x5, 7x7. 
// The formation of the kernel is then specified in row order
//  0  -1   0
// -1   4  -1
//  0  -1   0
// This kernel will perform a crude edge detection when applied


// We shall use an image as the source data
r[a] = input(IO_IMAGE, 'tut_resources\testcard', UNUSED, 'png')

// Convolve the image, using the % operator. To specify a kernel we use 'k' or 'kernel' as a prefix and the name
// as an index
r[b] = r[a] % k[edge_detect]

// We can perform an absolute of the edges to tidy it up a bit
r[b] = abs(r[b])


// Using the convolve operator '%' assumes that the boundary condition is 0
// We can specify a boundary condition using the 'filterb' function
r[c] = filterb(r[a], k[edge_detect], 100)

// Notice how a boundary box has appeard, simply because of the convolution assuming
// that elements beyound the boundary of the register are 100.

// APRON also provides a Gaussian blur function, for rapid diffusions. This implementation
// is particularly fast as it is a variation of a FIR filter
r[b] = 0
sigma = 3
//Notice the order of the arguments Source Register, Destination Register, Temporary Register, SD of Gaussian Function
gaussianblur(r[a], r[d], r[b], sigma)

// The temporary register will be filled with vertically filtered data
	
	
end