Skip to content

Commit

Permalink
add more BufferedImages scale overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
sigpwned committed Feb 28, 2024
1 parent cca5779 commit 1918557
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,10 @@ public DefaultCropResult crop(BufferedImage originalImage, int aspectWidth, int
// Therefore, if prescale is less than 1, it indicates that scaling down is necessary for at
// least one dimension of the image to fit within a 256-pixel size constraint, implying that
// either imageWidth or imageHeight (or both) is larger than 256 pixels.
// TODO Add prescaleRenderingStyle?
analyzeImage = BufferedImages.scaled(originalImage,
(int) (originalImage.getWidth() * prescale),
(int) (originalImage.getHeight() * prescale), BufferedImage.TYPE_INT_ARGB,
(int) (originalImage.getHeight() * prescale), BufferedImage.TYPE_INT_ARGB, null, null,
getOptions().getPrescaleAlgorithm());

cropWidth = (int) (cropWidth * prescale);
Expand Down Expand Up @@ -165,15 +166,13 @@ public DefaultCropResult crop(BufferedImage originalImage, int aspectWidth, int
getOptions().getSkinThreshold(), getOptions().getSkinBrightnessMin(),
getOptions().getSkinBrightnessMax());
Saturation.saturationDetect(input, output, getOptions().getSaturationThreshold(),
getOptions().getSaturationBrightnessMin(),
getOptions().getSaturationBrightnessMax());
getOptions().getSaturationBrightnessMin(), getOptions().getSaturationBrightnessMax());
Boosting.applyBoosts(output, boosts);

ScoredCrop topCrop = scoreCrops(output,
Composition.generateCandidateCrops(input.width, input.height, cropWidth, cropHeight,
getOptions().getMinScale(), getOptions().getMaxScale(),
getOptions().getScaleStep(), getOptions().getCropSearchStep()),
getOptions().getScoreDownSample()).stream()
getOptions().getMinScale(), getOptions().getMaxScale(), getOptions().getScaleStep(),
getOptions().getCropSearchStep()), getOptions().getScoreDownSample()).stream()
.max(Comparator.comparing(ScoredCrop::getScore)).orElseThrow();

BufferedImage debugImage;
Expand Down Expand Up @@ -223,12 +222,11 @@ public List<ScoredCrop> scoreCrops(ImageData output, List<Crop> crops, int downs

final float dspDetail = od[dsp + GO] / 255.0f;

skin += (od[dsp + RO] / 255.0f) * (dspDetail + getOptions().getSkinBias())
* ospImportance;
skin +=
(od[dsp + RO] / 255.0f) * (dspDetail + getOptions().getSkinBias()) * ospImportance;
detail += dspDetail * ospImportance;
saturation +=
(od[dsp + BO] / 255.0f) * (dspDetail + getOptions().getSaturationBias())
* ospImportance;
saturation += (od[dsp + BO] / 255.0f) * (dspDetail + getOptions().getSaturationBias())
* ospImportance;
boost += (od[dsp + AO] / 255.0f) * ospImportance;
}
}
Expand Down
59 changes: 40 additions & 19 deletions src/main/java/com/sigpwned/smartcrop4j/util/BufferedImages.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.util.Optional;

public final class BufferedImages {

Expand Down Expand Up @@ -62,12 +61,26 @@ public static BufferedImage scaled(BufferedImage image, int newWidth, int newHei

/**
* Scales the given image to the given dimensions using the given type. Equivalent to calling
* {@code scaled(image, newWidth, newHeight, type, null)}.
* {@code scaled(image, newWidth, newHeight, newType, null)}.
*
* @see #scaled(BufferedImage, int, int, int, Object)
* @see #scaled(BufferedImage, int, int, int, Color)
*/
public static BufferedImage scaled(BufferedImage image, int newWidth, int newHeight, int type) {
return scaled(image, newWidth, newHeight, type, null);
public static BufferedImage scaled(BufferedImage image, int newWidth, int newHeight,
int newType) {
return scaled(image, newWidth, newHeight, newType, null);
}

/**
* Scales the given image to the given dimensions using the given type. Equivalent to calling
* {@code scaled(image, newWidth, newHeight, newType, backgroundColor, null, null)}.
*
* @see #scaled(BufferedImage, int, int, int, Color, Object, Object)
* @see #DEFAULT_RENDERING_STYLE
* @see #DEFAULT_INTERPOLATION_STYLE
*/
public static BufferedImage scaled(BufferedImage image, int newWidth, int newHeight, int newType,
Color backgroundColor) {
return scaled(image, newWidth, newHeight, newType, backgroundColor, null, null);
}

/**
Expand All @@ -76,19 +89,34 @@ public static BufferedImage scaled(BufferedImage image, int newWidth, int newHei
* @param image the image to scale
* @param newWidth the new width
* @param newHeight the new height
* @param type the type
* @param newType the new image type
* @param backgroundColor the background color
* @param renderingStyle the rendering style
* @param interpolationStyle the interpolation style
* @return the scaled image
*/
public static BufferedImage scaled(BufferedImage image, int newWidth, int newHeight, int type,
Object interpolationStyle) {
final BufferedImage result = new BufferedImage(newWidth, newHeight, type);
public static BufferedImage scaled(BufferedImage image, int newWidth, int newHeight, int newType,
Color backgroundColor, Object renderingStyle, Object interpolationStyle) {
if (renderingStyle == null) {
renderingStyle = DEFAULT_RENDERING_STYLE;
}
if (interpolationStyle == null) {
interpolationStyle = DEFAULT_INTERPOLATION_STYLE;
}

final BufferedImage result = new BufferedImage(newWidth, newHeight, newType);

final Graphics2D g = result.createGraphics();
try {
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
Optional.ofNullable(interpolationStyle).orElse(DEFAULT_INTERPOLATION_STYLE));
g.drawImage(image, 0, 0, newWidth, newHeight, null);
g.setRenderingHint(RenderingHints.KEY_RENDERING, renderingStyle);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, interpolationStyle);
if (backgroundColor != null) {
g.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, image.getWidth(), image.getHeight(),
backgroundColor, null);
} else {
g.drawImage(image, 0, 0, newWidth, newHeight, 0, 0, image.getWidth(), image.getHeight(),
null);
}
} finally {
g.dispose();
}
Expand Down Expand Up @@ -137,13 +165,6 @@ public static BufferedImage cropped(BufferedImage image, Crop region, int newWid
* Equivalent to calling
* {@code cropped(image, region, newWidth, newHeight, newType, backgroundColor, null, null)}.
*
* @param image the image to crop
* @param region the region to crop
* @param newWidth the new width
* @param newHeight the new height
* @param newType the new type
* @param backgroundColor the background color
* @return the cropped and scaled image
* @see #cropped(BufferedImage, Crop, int, int, int, Color, Object, Object)
* @see #DEFAULT_RENDERING_STYLE
* @see #DEFAULT_INTERPOLATION_STYLE
Expand Down

0 comments on commit 1918557

Please sign in to comment.