Skip to content

Commit

Permalink
fix issue where crops were not postscaled properly
Browse files Browse the repository at this point in the history
  • Loading branch information
sigpwned committed Feb 29, 2024
1 parent c2ed248 commit 940bab0
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,19 @@ public DefaultCropResult crop(BufferedImage originalImage, int aspectWidth, int
getOptions().getSaturationBrightnessMin(), getOptions().getSaturationBrightnessMax());
Boosting.applyBoosts(output, boosts);

final float finalPrescale = prescale;
ScoredCrop topCrop = scoreCrops(output,
Composition.generateCandidateCrops(input.width, input.height, cropWidth, cropHeight,
getOptions().getMinScale(), getOptions().getMaxScale(), getOptions().getScaleStep(),
getOptions().getCropSearchStep()), getOptions().getScoreDownSample()).stream()
.max(Comparator.comparing(ScoredCrop::getScore)).orElseThrow();
.max(Comparator.comparing(ScoredCrop::getScore))
.map(c -> new ScoredCrop(
(int) (c.getX() / finalPrescale),
(int) (c.getY() / finalPrescale),
(int) (c.getWidth() / finalPrescale),
(int) (c.getHeight() / finalPrescale),
c.getScore()))
.orElseThrow();

BufferedImage debugImage;
if (getOptions().isDebug()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Expand All @@ -31,6 +31,7 @@

import com.sigpwned.smartcrop4j.CropBoost;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.junit.BeforeClass;
Expand All @@ -40,21 +41,36 @@ public class DefaultSmartCropperTest {

public static BufferedImage testImage1;

public static BufferedImage testImage2;

public static BufferedImage testImage3;

@BeforeClass
public static void setupDefaultSmartCropperTestClass() throws IOException {
testImage1 = ImageIO.read(DefaultSmartCropper.class.getResource("test.jpeg"));
testImage1 = ImageIO.read(DefaultSmartCropper.class.getResource("test1.jpg"));

testImage2 = ImageIO.read(DefaultSmartCropper.class.getResource("test2.jpg"));

testImage3 = ImageIO.read(DefaultSmartCropper.class.getResource("test3.jpg"));
}

@Test
public void givenTestImage1AndSquareAspectRatioAndDefaultConfig_whenSmartCropImage_thenReceiveExpectedCrop()
throws IOException {
DefaultSmartCropper unit = new DefaultSmartCropper();
DefaultSmartCropper unit = new DefaultSmartCropper(
DefaultSmartCropperOptions.builder()
.setDebug(true)
.build());

DefaultCropResult crop = unit.crop(testImage1, 100, 100);

boolean written = ImageIO.write(crop.getDebugImage(), "png",
new File("/Users/aboothe/Downloads/debug1.png"));
assertThat(written, is(true));

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{40, 0, 229, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{66, 0, 381, 381}));
}

@Test
Expand All @@ -66,7 +82,7 @@ public void givenTestImage1AndLandscapeAspectRatioAndDefaultConfig_whenSmartCrop

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{0, 32, 344, 171}));
crop.getTopCrop().getHeight()}, is(new int[]{0, 53, 573, 285}));
}

@Test
Expand All @@ -78,7 +94,7 @@ public void givenTestImage1AndPortraitAspectRatioAndDefaultConfig_whenSmartCropI

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{56, 0, 114, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{93, 0, 190, 381}));
}

@Test
Expand All @@ -91,7 +107,7 @@ public void givenTestImage1AndSquareAspectRatioAndDefaultConfigAndBoosts_whenSma

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{152, 0, 229, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{253, 0, 381, 381}));
}

@Test
Expand All @@ -104,7 +120,7 @@ public void givenTestImage1AndSquareAspectRatioAndLargePositiveSkinWeight_whenSm

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{40, 0, 229, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{66, 0, 381, 381}));
}

@Test
Expand All @@ -117,7 +133,7 @@ public void givenTestImage1AndSquareAspectRatioAndLargeNegativeSkinWeight_whenSm

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{152, 0, 229, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{253, 0, 381, 381}));
}

@Test
Expand All @@ -130,7 +146,7 @@ public void givenTestImage1AndSquareAspectRatioAndLargePositiveSaturationWeight_

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{48, 24, 229, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{80, 40, 381, 381}));
}

@Test
Expand All @@ -143,7 +159,7 @@ public void givenTestImage1AndSquareAspectRatioAndLargeNegativeSaturationWeight_

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{152, 0, 229, 229}));
crop.getTopCrop().getHeight()}, is(new int[]{253, 0, 381, 381}));
}

@Test(expected = IllegalArgumentException.class)
Expand All @@ -153,4 +169,32 @@ public void givenAnyImageAndZeroAspectRatioAndAnyConfig_whenSmartCropImage_thenT

unit.crop(testImage1, 0, 0);
}

@Test
public void givenTestImage2AndSquareAspectRatioAndDefaultConfig_whenSmartCropImage_thenReceiveExpectedCrop()
throws IOException {
DefaultSmartCropper unit = new DefaultSmartCropper(DefaultSmartCropperOptions.builder()
.setDebug(true)
.build());

DefaultCropResult crop = unit.crop(testImage2, 100, 100);

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{42, 0, 675, 675}));
}

@Test
public void givenTestImage3AndSquareAspectRatioAndDefaultConfig_whenSmartCropImage_thenReceiveExpectedCrop()
throws IOException {
DefaultSmartCropper unit = new DefaultSmartCropper(DefaultSmartCropperOptions.builder()
.setDebug(true)
.build());

DefaultCropResult crop = unit.crop(testImage3, 100, 100);

assertThat(
new int[]{crop.getTopCrop().getX(), crop.getTopCrop().getY(), crop.getTopCrop().getWidth(),
crop.getTopCrop().getHeight()}, is(new int[]{464, 0, 742, 742}));
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 940bab0

Please sign in to comment.