Add image utility class

This commit is contained in:
Jordan Atwood
2018-07-29 19:37:37 -07:00
parent f0799ffe79
commit 06cd5cd7d0
8 changed files with 369 additions and 86 deletions

View File

@@ -29,7 +29,6 @@ import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.SystemTray;
@@ -41,8 +40,6 @@ import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.concurrent.Callable;
import java.util.function.BiConsumer;
@@ -105,27 +102,6 @@ public class SwingUtil
System.setProperty("sun.awt.noerasebackground", "true");
}
/**
* Offsets an image in the grayscale (darkens/brightens) by an offset
*/
public static BufferedImage grayscaleOffset(BufferedImage image, int offset)
{
final float offsetFloat = (float) offset;
final int numComponents = image.getColorModel().getNumComponents();
final float[] scales = new float[numComponents];
final float[] offsets = new float[numComponents];
Arrays.fill(scales, 1f);
for (int i = 0; i < numComponents; i++)
{
offsets[i] = offsetFloat;
}
// Set alpha to not offset
offsets[numComponents - 1] = 0f;
return offset(image, scales, offsets);
}
/**
* Safely sets Swing theme
*
@@ -266,25 +242,6 @@ public class SwingUtil
});
}
/**
* Re-size a BufferedImage to the given dimensions.
*
* @param image the BufferedImage.
* @param newWidth The width to set the BufferedImage to.
* @param newHeight The height to set the BufferedImage to.
* @return The BufferedImage with the specified dimensions
*/
private static BufferedImage resizeImage(BufferedImage image, int newWidth, int newHeight)
{
final Image tmp = image.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
final BufferedImage dimg = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_ARGB);
final Graphics2D g2d = dimg.createGraphics();
g2d.drawImage(tmp, 0, 0, null);
g2d.dispose();
return dimg;
}
/**
* Create swing button from navigation button.
*
@@ -300,7 +257,7 @@ public class SwingUtil
{
final BufferedImage scaledImage = iconSize > 0
? resizeImage(navigationButton.getIcon(), iconSize, iconSize)
? ImageUtil.resizeImage(navigationButton.getIcon(), iconSize, iconSize)
: navigationButton.getIcon();
final JButton button = new JButton();
@@ -350,17 +307,4 @@ public class SwingUtil
{
return SubstanceCoreUtilities.getTitlePaneComponent(frame) != null;
}
/**
* Performs a rescale operation on the image's color components.
*
* @param image The image to be adjusted.
* @param scales An array of scale operations to be performed on the image's color components.
* @param offsets An array of offset operations to be performed on the image's color components.
* @return The modified image after applying the given adjustments.
*/
private static BufferedImage offset(final BufferedImage image, final float[] scales, final float[] offsets)
{
return new RescaleOp(scales, offsets, null).filter(image, null);
}
}