Skip to content

Commit 02001c0

Browse files
committed
use copyWithin in recalcEnergy
another decent speedup: image | before | after -------|--------|------ castle | 8.5s | 6.1s dog | 5.1s | 3.6s
1 parent 435e8f8 commit 02001c0

2 files changed

Lines changed: 40 additions & 37 deletions

File tree

‎build/index.js‎

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,26 @@ function recalcEnergy(data, width, height, energy, removed) {
8888
let offset = 0;
8989
for (let y = 0; y < height; y++) {
9090
let removex = removed[y];
91-
for (let x = 0; x < width; x++) {
92-
if (x === removex) {
93-
// pixels been removed, skip over it
94-
offset++;
95-
}
91+
// copy left half
92+
energy.copyWithin(y * width, y * width + offset, y * width + offset + removex - 1);
93+
// skip removex
94+
offset++;
95+
// copy right half
96+
energy.copyWithin(y * width + removex + 1, y * width + offset + removex + 1, y * width + offset + width);
97+
// recalc pixels around seam
98+
for (let x = removex - 1; x <= removex; x++) {
9699
let nrg = energy[y * width + x + offset];
97-
if (x >= removex - 1 && x <= removex) {
98-
if (x === 0 || x === width - 1 || y === 0) {
99-
// border pixels
100-
nrg = nrg;
101-
}
102-
else {
103-
const hdiff = diff(buf32[y * width + x - 1], buf32[y * width + x + 1]);
104-
const vdiff = diff(buf32[(y - 1) * width + x], buf32[(y + 1) * width + x]);
105-
nrg = hdiff + vdiff;
106-
}
100+
if (x === 0 || x === width - 1 || y === 0) {
101+
// border pixels
102+
nrg = nrg;
103+
}
104+
else {
105+
const hdiff = diff(buf32[y * width + x - 1], buf32[y * width + x + 1]);
106+
const vdiff = diff(buf32[(y - 1) * width + x], buf32[(y + 1) * width + x]);
107+
nrg = hdiff + vdiff;
107108
}
108109
energy[y * width + x] = nrg;
109110
}
110-
if (removex >= width) {
111-
// case where a pixel on the removed edge is gone
112-
offset++;
113-
}
114111
}
115112
return energy;
116113
}

‎index.ts‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -127,30 +127,36 @@ function recalcEnergy(
127127
let offset = 0;
128128
for (let y = 0; y < height; y++) {
129129
let removex = removed[y];
130-
for (let x = 0; x < width; x++) {
131-
if (x === removex) {
132-
// pixels been removed, skip over it
133-
offset++;
134-
}
135130

131+
// copy left half
132+
energy.copyWithin(
133+
y * width,
134+
y * width + offset,
135+
y * width + offset + removex - 1,
136+
);
137+
// skip removex
138+
offset++;
139+
// copy right half
140+
energy.copyWithin(
141+
y * width + removex + 1,
142+
y * width + offset + removex + 1,
143+
y * width + offset + width,
144+
);
145+
146+
// recalc pixels around seam
147+
for (let x = removex - 1; x <= removex; x++) {
136148
let nrg = energy[y * width + x + offset];
137-
if (x >= removex - 1 && x <= removex) {
138-
if (x === 0 || x === width - 1 || y === 0) {
139-
// border pixels
140-
nrg = nrg;
141-
} else {
142-
const hdiff = diff(buf32[y * width + x - 1], buf32[y * width + x + 1]);
143-
const vdiff = diff(buf32[(y - 1) * width + x], buf32[(y + 1) * width + x]);
144-
nrg = hdiff + vdiff;
145-
}
149+
if (x === 0 || x === width - 1 || y === 0) {
150+
// border pixels
151+
nrg = nrg;
152+
} else {
153+
const hdiff = diff(buf32[y * width + x - 1], buf32[y * width + x + 1]);
154+
const vdiff = diff(buf32[(y - 1) * width + x], buf32[(y + 1) * width + x]);
155+
nrg = hdiff + vdiff;
146156
}
147157
energy[y * width + x] = nrg;
148158
}
149159

150-
if (removex >= width) {
151-
// case where a pixel on the removed edge is gone
152-
offset++;
153-
}
154160
}
155161

156162
return energy;

0 commit comments

Comments
 (0)