You can try loading images for this filter from the local file system, but if you do you will discover that it doesn't work. You cannot access the pixels of an image that has been loaded from a different URL and accessing pixels of a local file is strictly forbidden. If you download the file from a server with the same URL as the script then there's no problem.
To allow for testing, Chrome has a command line switch that turns off the security check - no doubt other browsers have similar features. All you have to do is locate the shortcut that you use to launch Chrome and change the target to read:
Alternatively simply use the command from the command prompt or add:
--allow-file-access-from-files
to whatever command initiates Chrome. With this command line switch it all works and you can see the result of the effect.
Sections in book but not in this extract:
The Filter API
Convolution Filter
Custom Convolution
Reading a PCX File
Processing the PCX File
Listing - Read a PCX File
Summary
The ImageData object allows direct access to the pixel data within a bitmap.
The pixel data is stored in the data array property which is a Uint8ClampedArray in RGBA order.
With direct access to the pixel data you can write filters which change the bitmap in controlled ways using functions formed from the surrounding pixels.
The new Filter API provides a range of predefined filters that you can use.
You can also use any of the SVG filters via the url filter.
The SVG filters include a general convolution filter which can be used to implement many standard and custom linear filters.
A convolution filter replaces the current pixel value with a weighted average of it and the pixels that surround it. The weighted average is specified as a convolution matrix or mask.
It is also fairly easy to implement a convolution filter directly using ImageData and direct pixel manipulation.
You can also arrange to read and process any graphics file format that you have the specification for using the standard techniques of reading files, bit manipulation and ImageData.
Now available as a paperback or ebook from Amazon.