Skip to content

Commit

Permalink
Fix autocrop mixing up east and west (#1227)
Browse files Browse the repository at this point in the history
* Make it easier to see why a JGD string doesn't match

* Fix east/west mixup in autocrop

* Fix error message when JDG objects aren't equal
  • Loading branch information
sjoerd108 authored May 11, 2023
1 parent e04dd91 commit 2948127
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
24 changes: 12 additions & 12 deletions packages/plugin-crop/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,22 @@ export default function pluginCrop(event) {
}
}

// east side (scan columns from east to west)
// west side (scan columns from west to east)
colorTarget = this.getPixelColor(w, 0);
if (!ignoreSides.east) {
east: for (let x = 0; x < w - minPixelsPerSide; x++) {
if (!ignoreSides.west) {
west: for (let x = 0; x < w - minPixelsPerSide; x++) {
for (let y = 0 + northPixelsToCrop; y < h; y++) {
const colorXY = this.getPixelColor(x, y);
const rgba2 = this.constructor.intToRGBA(colorXY);

if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) {
// this pixel is too distant from the first one: abort this side scan
break east;
break west;
}
}

// this column contains all pixels with the same color: increment this side pixels to crop
eastPixelsToCrop++;
westPixelsToCrop++;
}
}

Expand Down Expand Up @@ -203,12 +203,12 @@ export default function pluginCrop(event) {
}
}

// west side (scan columns from west to east)
// east side (scan columns from east to west)
colorTarget = this.getPixelColor(w, h);
if (!ignoreSides.west) {
west: for (
if (!ignoreSides.east) {
east: for (
let x = w - 1;
x >= 0 + eastPixelsToCrop + minPixelsPerSide;
x >= 0 + westPixelsToCrop + minPixelsPerSide;
x--
) {
for (let y = h - 1; y >= 0 + northPixelsToCrop; y--) {
Expand All @@ -217,12 +217,12 @@ export default function pluginCrop(event) {

if (this.constructor.colorDiff(rgba1, rgba2) > tolerance) {
// this pixel is too distant from the first one: abort this side scan
break west;
break east;
}
}

// this column contains all pixels with the same color: increment this side pixels to crop
westPixelsToCrop++;
eastPixelsToCrop++;
}
}

Expand Down Expand Up @@ -275,7 +275,7 @@ export default function pluginCrop(event) {
if (doCrop) {
// do the real crop
this.crop(
eastPixelsToCrop,
westPixelsToCrop,
northPixelsToCrop,
widthOfRemainingPixels,
heightOfRemainingPixels
Expand Down
12 changes: 6 additions & 6 deletions packages/plugin-crop/test/autocrop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,11 +375,11 @@ describe("Autocrop", () => {
})
.getJGDSync(),
mkJGD(
" ◆◆ ",
" ◆▦▦◆ ",
"◆▦▦▦▦◆ ",
" ◆▦▦◆ ",
" ◆◆ ",
" ◆◆ ",
" ◆▦▦◆ ",
" ◆▦▦▦▦◆",
" ◆▦▦◆ ",
" ◆◆ ",
" "
)
);
Expand All @@ -402,7 +402,7 @@ describe("Autocrop", () => {
imgSrc
.autocrop({ cropOnlyFrames: false, ignoreSides: { east: true } })
.getJGDSync(),
mkJGD(" ◆◆ ", " ◆▦▦◆ ", " ◆▦▦▦▦◆", " ◆▦▦◆ ", " ◆◆ ")
mkJGD(" ◆◆ ", " ◆▦▦◆ ", "◆▦▦▦▦◆ ", " ◆▦▦◆ ", " ◆◆ ")
);
});
});
4 changes: 3 additions & 1 deletion packages/test-utils/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ function determineJGDError(testJGD, targetJGD) {
if (!equal(jgdReadableMatrix(testJGD), jgdReadableMatrix(targetJGD))) {
return {
pass: false,
message: "Expected testJGD to be equal to targetJGD",
message: `Expected testJGD:\n${jgdToStr(
testJGD
)}\n to be equal to targetJGD:\n${jgdToStr(targetJGD)}`,
};
}

Expand Down

0 comments on commit 2948127

Please sign in to comment.