Skip to content

Commit 435e8f8

Browse files
committed
use copyWithin in removeSeam
pretty nice speed-up: image | before | after -------|--------|------ castle | 13.4s | 8.5s dog | 8.6s | 5.1s
1 parent 51201de commit 435e8f8

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
build/index.js -diff

build/index.js

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,12 @@ function removeSeam(data, width, height, remove) {
2626
let offset = 0;
2727
for (let y = 0; y < height; y++) {
2828
let removex = remove[y];
29-
for (let x = 0; x < width; x++) {
30-
if (x === removex) {
31-
// pixels been removed, skip over it
32-
offset++;
33-
}
34-
if (x < width && offset > 0) {
35-
data32[y * (width - 1) + x] = data32[y * (width - 1) + x + offset];
36-
}
37-
}
29+
// copy left half
30+
data32.copyWithin(y * (width - 1), y * (width - 1) + offset, y * (width - 1) + offset + removex);
31+
// skip removex
32+
offset++;
33+
// copy right half
34+
data32.copyWithin(y * (width - 1) + removex, y * (width - 1) + offset + removex, y * (width - 1) + (width - 1) + offset);
3835
}
3936
return data;
4037
}
@@ -119,7 +116,7 @@ function recalcEnergy(data, width, height, energy, removed) {
119116
}
120117
function calcEnergySum(energySum, energy, width, height) {
121118
// populate the first row in energySum
122-
for (let j = 0; j < energy.length; j++) {
119+
for (let j = 0; j < width; j++) {
123120
energySum[j] = energy[j];
124121
}
125122
// populate the rest of the rows

index.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@ function removeSeam(
3939
let offset = 0;
4040
for (let y = 0; y < height; y++) {
4141
let removex = remove[y];
42-
for (let x = 0; x < width; x++) {
43-
if (x === removex) {
44-
// pixels been removed, skip over it
45-
offset++;
46-
}
47-
if (x < width && offset > 0) {
48-
data32[y * (width - 1) + x] = data32[y * (width - 1) + x + offset];
49-
}
50-
}
42+
// copy left half
43+
data32.copyWithin(
44+
y * (width - 1),
45+
y * (width - 1) + offset,
46+
y * (width - 1) + offset + removex,
47+
);
48+
// skip removex
49+
offset++;
50+
// copy right half
51+
data32.copyWithin(
52+
y * (width - 1) + removex,
53+
y * (width - 1) + offset + removex,
54+
y * (width - 1) + (width - 1) + offset,
55+
);
5156
}
5257

5358
return data;
@@ -158,7 +163,7 @@ function calcEnergySum(
158163
height: number,
159164
) {
160165
// populate the first row in energySum
161-
for (let j = 0; j < energy.length; j++) {
166+
for (let j = 0; j < width; j++) {
162167
energySum[j] = energy[j];
163168
}
164169

@@ -252,4 +257,4 @@ function dbgEnergySum(energySum: Uint32Array) {
252257
}
253258

254259
return newData;
255-
}
260+
}

0 commit comments

Comments
 (0)