Skip to content

Commit

Permalink
Autocrop not working when no changes #234
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Nov 15, 2021
1 parent 2635afb commit 59fbb9c
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
7 changes: 4 additions & 3 deletions buildSrc/src/main/kotlin/Libs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ object Libs {
const val kotlinVersion = "1.5.20"

const val org = "com.sksamuel.scrimage"
const val CommonsIoVersion = "2.11.0"
const val CommonsLangVersion = "3.11"


object TwelveMonkeys {
private const val Version = "3.7.0"
Expand All @@ -24,7 +23,7 @@ object Libs {
}

object Kotest {
private const val version = "4.6.1"
private const val version = "4.6.3"
const val assertions = "io.kotest:kotest-assertions-core-jvm:$version"
const val api = "io.kotest:kotest-framework-api:$version"
const val junit5 = "io.kotest:kotest-runner-junit5-jvm:$version"
Expand All @@ -41,6 +40,8 @@ object Libs {
}

object Commons {
private const val CommonsIoVersion = "2.11.0"
private const val CommonsLangVersion = "3.11"
const val io = "commons-io:commons-io:$CommonsIoVersion"
const val lang = "org.apache.commons:commons-lang3:$CommonsLangVersion"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public static int scanright(Color color, int height, int width, int col, PixelsE
return scanright(color, height, width, col + 1, f, tolerance);
}

public static int scanleft(Color color, int height, int width, int col, PixelsExtractor f, int tolerance) {
public static int scanleft(Color color, int height, int col, PixelsExtractor f, int tolerance) {
if (col == 0 || !PixelTools.colorMatches(color, tolerance, f.apply(new Rectangle(col, 0, 1, height))))
return col;
else
return scanleft(color, height, width, col - 1, f, tolerance);
return scanleft(color, height, col - 1, f, tolerance);
}

public static int scandown(Color color, int height, int width, int row, PixelsExtractor f, int tolerance) {
Expand All @@ -32,10 +32,10 @@ public static int scandown(Color color, int height, int width, int row, PixelsEx
return scandown(color, height, width, row + 1, f, tolerance);
}

public static int scanup(Color color, int height, int width, int row, PixelsExtractor f, int tolerance) {
public static int scanup(Color color, int width, int row, PixelsExtractor f, int tolerance) {
if (row == 0 || !PixelTools.colorMatches(color, tolerance, f.apply(new Rectangle(0, row, width, 1))))
return row;
else
return scanup(color, height, width, row - 1, f, tolerance);
return scanup(color, width, row - 1, f, tolerance);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@
import org.apache.commons.io.IOUtils;
import thirdparty.colorthief.ColorThief;
import thirdparty.colorthief.MMCQ;
import thirdparty.mortennobel.*;
import thirdparty.mortennobel.BSplineFilter;
import thirdparty.mortennobel.BiCubicFilter;
import thirdparty.mortennobel.Lanczos3Filter;
import thirdparty.mortennobel.ResampleFilters;
import thirdparty.mortennobel.ResampleOp;
import thirdparty.mortennobel.TriangleFilter;

import javax.imageio.ImageIO;
import java.awt.*;
Expand Down Expand Up @@ -410,9 +415,10 @@ public ImmutableImage autocrop(Color color) {
*/
public ImmutableImage autocrop(Color color, int colorTolerance) {
int x1 = AutocropOps.scanright(color, height, width, 0, pixelExtractor(), colorTolerance);
int x2 = AutocropOps.scanleft(color, height, width, width - 1, pixelExtractor(), colorTolerance);
int x2 = AutocropOps.scanleft(color, height, width - 1, pixelExtractor(), colorTolerance);
int y1 = AutocropOps.scandown(color, height, width, 0, pixelExtractor(), colorTolerance);
int y2 = AutocropOps.scanup(color, height, width, height - 1, pixelExtractor(), colorTolerance);
int y2 = AutocropOps.scanup(color, width, height - 1, pixelExtractor(), colorTolerance);
if (x1 == 0 && y1 == 0 && x2 == width - 1 && y2 == height - 1) return this;
return subimage(x1, y1, x2 - x1, y2 - y1);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ import com.sksamuel.scrimage.Dimension
import com.sksamuel.scrimage.nio.ImmutableImageLoader
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import io.kotest.matchers.types.shouldBeSameInstanceAs
import java.awt.Color

class AutoCropTest : FunSpec() {
init {

test("autocrop should work with transparent backgrounds") {
val image = ImmutableImageLoader.create()
.fromResource("/com/sksamuel/scrimage/core/autocrop/1b64a74b-ae2b-417e-a7db-b238e8ec5555.png")
image.autocrop().dimensions() shouldBe Dimension(744, 1868)
}

test("autocrop with no matching content should return the same image") {
val image = ImmutableImageLoader.create()
.fromResource("/com/sksamuel/scrimage/core/autocrop/1b64a74b-ae2b-417e-a7db-b238e8ec5555.png")
image.autocrop(Color.ORANGE).shouldBeSameInstanceAs(image)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sksamuel.scrimage.core.autocrop

import com.sksamuel.scrimage.ImmutableImage
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import java.awt.Color

class Issue234Test : FunSpec() {
init {
test("Autocrop not working properly when on small images #234") {
val input = ImmutableImage.loader().fromResource("/issue234.png")
input.autocrop(Color.green).dimensions() shouldBe input.dimensions()
}
}
}
Binary file added scrimage-tests/src/test/resources/issue234.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

1 comment on commit 59fbb9c

@csaltos
Copy link

@csaltos csaltos commented on 59fbb9c Nov 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍😎

Please sign in to comment.