Skip to content

Commit d2b385e

Browse files
committed
merged with master
2 parents 21fa173 + 882feb2 commit d2b385e

File tree

6 files changed

+652
-117
lines changed

6 files changed

+652
-117
lines changed

SeamCarver.js

+49-10
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ class SeamCarver {
3131
this.energy_matrix[i] = new Array(this.height);
3232
}
3333

34-
console.log('calculating energy matrix values ...');
34+
console.time('createEnergyMatrix');
3535

3636
this.createEnergyMatrix();
3737

38-
console.log('done');
38+
console.timeEnd('createEnergyMatrix');
3939
}
4040

4141
/**
@@ -105,12 +105,12 @@ class SeamCarver {
105105
var p = this.picture; // Just to make it more readable ...
106106

107107
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]) +
109109
(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]) +
112112
(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])
114114
);
115115
return score;
116116
}
@@ -176,10 +176,13 @@ class SeamCarver {
176176
*/
177177
createEnergyMatrix() {
178178
// This has to be reverse order (bottom to top)
179+
this.maxVminsum = 0;
179180
for (var y = this.height - 1; y >= 0; y--) {
180181
// This can be in any order ...
181182
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;
183186
}
184187
}
185188
}
@@ -353,12 +356,48 @@ class SeamCarver {
353356
this.recalculateVminsumForAffectedPixels(affectedPixels);
354357
}
355358

356-
reDrawImage() {
359+
/*
360+
* Takes field as arg to print matrix, default is rgb, accepts energy.
361+
*
362+
*/
363+
reDrawImage(field) {
357364
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
358365
this.canvas.width = this.imageData.width;
359366
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+
362401
this.context.putImageData(this.imageData, 0, 0);
363402
}
364403

0 commit comments

Comments
 (0)