correctness of in-place convolution filter?
<p>I have a simple box blur function that takes an <code>ImageData</code> object, returning it when done. However, I have just realised that this implementation may be incorrect because the <code>ImageData</code> object is edited in place and convolution filters depend on surrounding pixels. Should I be reading from the original <code>ImageData</code> and writing to a new one so that each pixel doesn't depend on already-changed surrounding pixels? If so, I'll have to rework my web worker manager to supply a new <code>ImageData</code> for the convolution functions to write to.</p>

<pre><code>expressive.boxBlur = function(data, options) {
    var w = data.width, h = data.height, dataReal = data.data;
    for (var i = 0; i &lt; w; i++)
        for (var j = 0; j &lt; h; j++)
            for (var k = 0; k &lt; 4; k++) {
                var total = 0, values = 0, temp = 0;
                if (!(i == 0 &amp;&amp; j == 0)) {
                    temp = dataReal[4 * w * (j - 1) + 4 * (i - 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == w - 1 &amp;&amp; j == 0)) {
                    temp = dataReal[4 * w * (j - 1) + 4 * (i + 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == 0 &amp;&amp; j == h - 1)) {
                    temp = dataReal[4 * w * (j + 1) + 4 * (i - 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == w - 1 &amp;&amp; j == h - 1)) {
                    temp = dataReal[4 * w * (j + 1) + 4 * (i + 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(j == 0)) {
                    temp = dataReal[4 * w * (j - 1) + 4 * (i + 0) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(j == h - 1)) {
                    temp = dataReal[4 * w * (j + 1) + 4 * (i + 0) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == 0)) {
                    temp = dataReal[4 * w * (j + 0) + 4 * (i - 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                if (!(i == w - 1)) {
                    temp = dataReal[4 * w * (j + 0) + 4 * (i + 1) + k];
                    if (temp !== undefined) values++, total += temp;
                }
                values++, total += dataReal[4 * w * j + 4 * i + k];
                total /= values;
                dataReal[4 * w * j + 4 * i + k] = total;
            }
    return data;
};
</code></pre>
