@@ -161,6 +161,29 @@ define(["require", "exports"], function (require, exports) {
161161 }
162162 return data ;
163163 } ;
164+ Filter . prototype . custom = function ( image , width , height ) {
165+ var data = new Uint8ClampedArray ( width * height * 4 ) ;
166+ for ( var y = 0 ; y < height ; y ++ ) {
167+ for ( var x = 0 ; x < width ; x ++ ) {
168+ var location_5 = y * width * 4 + x * 4 ;
169+ var neighbors = this . getNeighbors ( image , width , height , [ x , y ] ) ;
170+ var total = new RGB ( ) ;
171+ for ( var i = 0 ; i < this . kernel . width * this . kernel . height ; i ++ ) {
172+ total . r += this . kernel . matrix [ i ] * neighbors [ i ] . r ;
173+ total . g += this . kernel . matrix [ i ] * neighbors [ i ] . g ;
174+ total . b += this . kernel . matrix [ i ] * neighbors [ i ] . b ;
175+ }
176+ total . r = this . truncate ( total . r ) ;
177+ total . g = this . truncate ( total . g ) ;
178+ total . b = this . truncate ( total . b ) ;
179+ data [ location_5 ] = total . r ;
180+ data [ location_5 + 1 ] = total . g ;
181+ data [ location_5 + 2 ] = total . b ;
182+ data [ location_5 + 3 ] = 0xFF ;
183+ }
184+ }
185+ return data ;
186+ } ;
164187 Filter . prototype . truncate = function ( value ) {
165188 if ( value < 0 )
166189 value = 0 ;
@@ -177,41 +200,41 @@ define(["require", "exports"], function (require, exports) {
177200 var neighbor = new Array ( 4 ) ;
178201 var pixel = new RGB ( ) ;
179202 if ( x === 0 && y === 0 ) {
180- var location_5 = index [ 1 ] * width * 4 + index [ 0 ] * 4 ;
181- neighbor [ 0 ] = new RGB ( image [ location_5 ] , image [ location_5 + 1 ] , image [ location_5 + 2 ] ) ;
203+ var location_6 = index [ 1 ] * width * 4 + index [ 0 ] * 4 ;
204+ neighbor [ 0 ] = new RGB ( image [ location_6 ] , image [ location_6 + 1 ] , image [ location_6 + 2 ] ) ;
182205 matrix [ halfh * this . kernel . width + halfw ] = neighbor [ 0 ] ;
183206 }
184207 else {
185208 if ( index [ 1 ] - y < 0 || index [ 0 ] - x < 0 ) {
186209 neighbor [ 0 ] = new RGB ( ) ;
187210 }
188211 else {
189- var location_6 = ( index [ 1 ] - y ) * width * 4 + ( index [ 0 ] - x ) * 4 ;
190- neighbor [ 0 ] = new RGB ( image [ location_6 ] , image [ location_6 + 1 ] , image [ location_6 + 2 ] ) ;
212+ var location_7 = ( index [ 1 ] - y ) * width * 4 + ( index [ 0 ] - x ) * 4 ;
213+ neighbor [ 0 ] = new RGB ( image [ location_7 ] , image [ location_7 + 1 ] , image [ location_7 + 2 ] ) ;
191214 }
192215 matrix [ ( halfh - y ) * this . kernel . width + ( halfw - x ) ] = neighbor [ 0 ] ;
193216 if ( index [ 1 ] - y < 0 || index [ 0 ] + x >= width ) {
194217 neighbor [ 1 ] = new RGB ( ) ;
195218 }
196219 else {
197- var location_7 = ( index [ 1 ] - y ) * width * 4 + ( index [ 0 ] + x ) * 4 ;
198- neighbor [ 1 ] = new RGB ( image [ location_7 ] , image [ location_7 + 1 ] , image [ location_7 + 2 ] ) ;
220+ var location_8 = ( index [ 1 ] - y ) * width * 4 + ( index [ 0 ] + x ) * 4 ;
221+ neighbor [ 1 ] = new RGB ( image [ location_8 ] , image [ location_8 + 1 ] , image [ location_8 + 2 ] ) ;
199222 }
200223 matrix [ ( halfh - y ) * this . kernel . width + ( halfw + x ) ] = neighbor [ 1 ] ;
201224 if ( index [ 1 ] + y >= height || index [ 0 ] - x < 0 ) {
202225 neighbor [ 2 ] = new RGB ( ) ;
203226 }
204227 else {
205- var location_8 = ( index [ 1 ] + y ) * width * 4 + ( index [ 0 ] - x ) * 4 ;
206- neighbor [ 2 ] = new RGB ( image [ location_8 ] , image [ location_8 + 1 ] , image [ location_8 + 2 ] ) ;
228+ var location_9 = ( index [ 1 ] + y ) * width * 4 + ( index [ 0 ] - x ) * 4 ;
229+ neighbor [ 2 ] = new RGB ( image [ location_9 ] , image [ location_9 + 1 ] , image [ location_9 + 2 ] ) ;
207230 }
208231 matrix [ ( halfh + y ) * this . kernel . width + ( halfw - x ) ] = neighbor [ 2 ] ;
209232 if ( index [ 1 ] + y >= height || index [ 0 ] + x >= width ) {
210233 neighbor [ 3 ] = new RGB ( ) ;
211234 }
212235 else {
213- var location_9 = ( index [ 1 ] + y ) * width * 4 + ( index [ 0 ] + x ) * 4 ;
214- neighbor [ 3 ] = new RGB ( image [ location_9 ] , image [ location_9 + 1 ] , image [ location_9 + 2 ] ) ;
236+ var location_10 = ( index [ 1 ] + y ) * width * 4 + ( index [ 0 ] + x ) * 4 ;
237+ neighbor [ 3 ] = new RGB ( image [ location_10 ] , image [ location_10 + 1 ] , image [ location_10 + 2 ] ) ;
215238 }
216239 matrix [ ( halfh + y ) * this . kernel . width + ( halfw + x ) ] = neighbor [ 3 ] ;
217240 }
@@ -220,12 +243,11 @@ define(["require", "exports"], function (require, exports) {
220243 return matrix ;
221244 } ;
222245 Filter . prototype . setKernel = function ( width , height , customKernel ) {
246+ this . kernel . height = height ;
247+ this . kernel . width = width ;
248+ this . kernel . matrix = new Array ( height * width ) ;
223249 if ( customKernel ) {
224- }
225- else {
226- this . kernel . height = height ;
227- this . kernel . width = width ;
228- this . kernel . matrix = new Array ( height * width ) ;
250+ this . kernel . matrix = customKernel ;
229251 }
230252 } ;
231253 return Filter ;
0 commit comments