@@ -31,11 +31,11 @@ class SeamCarver {
31
31
this . energy_matrix [ i ] = new Array ( this . height ) ;
32
32
}
33
33
34
- console . log ( 'calculating energy matrix values ... ') ;
34
+ console . time ( 'createEnergyMatrix ') ;
35
35
36
36
this . createEnergyMatrix ( ) ;
37
37
38
- console . log ( 'done ') ;
38
+ console . timeEnd ( 'createEnergyMatrix ') ;
39
39
}
40
40
41
41
/**
@@ -105,12 +105,12 @@ class SeamCarver {
105
105
var p = this . picture ; // Just to make it more readable ...
106
106
107
107
var score = Math . sqrt (
108
- ( p [ pos_xpost + RED ] - p [ pos_xant + RED ] ) * ( p [ pos_xpost + RED ] - p [ pos_xant + RED ] ) +
108
+ ( p [ pos_xpost + RED ] - p [ pos_xant + RED ] ) * ( p [ pos_xpost + RED ] - p [ pos_xant + RED ] ) +
109
109
( p [ pos_xpost + GREEN ] - p [ pos_xant + GREEN ] ) * ( p [ pos_xpost + GREEN ] - p [ pos_xant + GREEN ] ) +
110
- ( p [ pos_xpost + BLUE ] - p [ pos_xant + BLUE ] ) * ( p [ pos_xpost + BLUE ] - p [ pos_xant + BLUE ] ) +
111
- ( p [ pos_ypost + RED ] - p [ pos_yant + RED ] ) * ( p [ pos_ypost + RED ] - p [ pos_yant + RED ] ) +
110
+ ( p [ pos_xpost + BLUE ] - p [ pos_xant + BLUE ] ) * ( p [ pos_xpost + BLUE ] - p [ pos_xant + BLUE ] ) +
111
+ ( p [ pos_ypost + RED ] - p [ pos_yant + RED ] ) * ( p [ pos_ypost + RED ] - p [ pos_yant + RED ] ) +
112
112
( p [ pos_ypost + GREEN ] - p [ pos_yant + GREEN ] ) * ( p [ pos_ypost + GREEN ] - p [ pos_yant + GREEN ] ) +
113
- ( p [ pos_ypost + BLUE ] - p [ pos_yant + BLUE ] ) * ( p [ pos_ypost + BLUE ] - p [ pos_yant + BLUE ] )
113
+ ( p [ pos_ypost + BLUE ] - p [ pos_yant + BLUE ] ) * ( p [ pos_ypost + BLUE ] - p [ pos_yant + BLUE ] )
114
114
) ;
115
115
return score ;
116
116
}
@@ -176,10 +176,13 @@ class SeamCarver {
176
176
*/
177
177
createEnergyMatrix ( ) {
178
178
// This has to be reverse order (bottom to top)
179
+ this . maxVminsum = 0 ;
179
180
for ( var y = this . height - 1 ; y >= 0 ; y -- ) {
180
181
// This can be in any order ...
181
182
for ( var x = 0 ; x < this . width ; x ++ ) {
182
- this . energy_matrix [ x ] [ y ] = this . recalculate ( x , y ) ;
183
+ var energy = this . recalculate ( x , y ) ;
184
+ this . maxVminsum = Math . max ( energy . vminsum , this . maxVminsum ) ;
185
+ this . energy_matrix [ x ] [ y ] = energy ;
183
186
}
184
187
}
185
188
}
@@ -353,12 +356,48 @@ class SeamCarver {
353
356
this . recalculateVminsumForAffectedPixels ( affectedPixels ) ;
354
357
}
355
358
356
- reDrawImage ( ) {
359
+ /*
360
+ * Takes field as arg to print matrix, default is rgb, accepts energy.
361
+ *
362
+ */
363
+ reDrawImage ( field ) {
357
364
this . context . clearRect ( 0 , 0 , this . canvas . width , this . canvas . height ) ;
358
365
this . canvas . width = this . imageData . width ;
359
366
this . canvas . height = this . imageData . height ;
360
- this . canvas . style . width = this . imageData . width + 'px' ;
361
- this . canvas . style . height = this . imageData . height + 'px' ;
367
+
368
+ if ( field === 'energy' || field === 'vminsum' || ( field !== this . imageData . dataField ) ) {
369
+ this . imageData = this . context . createImageData ( this . width , this . height ) ;
370
+ this . imageData . dataField = field ;
371
+
372
+ for ( var row = 0 ; row < this . height ; row ++ ) {
373
+ for ( var col = 0 ; col < this . width ; col ++ ) {
374
+ var pos = this . pixelToIndex ( col , row ) ;
375
+ var val = this . energy_matrix [ col ] [ row ] [ field ] ;
376
+
377
+ if ( field === 'energy' ) {
378
+ var normalizedVal = Math . min ( 255 , ( ( val / 255 ) * 255 ) ) ;
379
+ } else if ( field === 'vminsum' ) {
380
+ var normalizedVal = ( ( val - 1000 ) / ( this . maxVminsum - 1000 ) ) * 255
381
+ } else {
382
+ // rgb
383
+ for ( var i = 0 ; i < 4 ; i ++ ) {
384
+ this . imageData . data [ pos + i ] = this . picture [ pos + i ] ;
385
+ }
386
+ continue ;
387
+ }
388
+
389
+ for ( var i = 0 ; i < 3 ; i ++ ) {
390
+ this . imageData . data [ pos + i ] = normalizedVal ;
391
+ }
392
+ // make opaque
393
+ this . imageData . data [ pos + 3 ] = 255 ;
394
+
395
+ }
396
+ }
397
+
398
+
399
+ }
400
+
362
401
this . context . putImageData ( this . imageData , 0 , 0 ) ;
363
402
}
364
403
0 commit comments