Change Java whitespace handling in .editorconfig (#425)

* Change whitespace handling in .editorconfig

* Reformat code

* fix format error

* Reformat code

---------

Co-authored-by: Zoë Gidiere <duplexsys@protonmail.com>
This commit is contained in:
Astrashh
2023-11-13 11:57:01 +11:00
committed by GitHub
parent a73fda7d04
commit defd775f13
793 changed files with 7579 additions and 7577 deletions
@@ -32,58 +32,58 @@ import com.dfsek.terra.api.util.reflection.TypeKey;
public class ImageLibraryAddon implements AddonInitializer {
public static final TypeKey<Supplier<ObjectTemplate<Image>>> IMAGE_REGISTRY_KEY = new TypeKey<>() {
};
public static final TypeKey<Supplier<ObjectTemplate<ColorSampler>>> COLOR_PICKER_REGISTRY_KEY = new TypeKey<>() {
};
public static final TypeKey<Supplier<ObjectTemplate<NoiseSampler>>> NOISE_SAMPLER_TOKEN = new TypeKey<>() {
};
@Inject
private Platform platform;
@Inject
private BaseAddon addon;
@Override
public void initialize() {
platform.getEventManager()
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.priority(10)
.then(event -> {
ImageLibraryPackConfigTemplate config = event.loadTemplate(new ImageLibraryPackConfigTemplate());
event.getPack().getContext().put(config);
})
.then(event -> {
ConfigPack pack = event.getPack();
CheckedRegistry<Supplier<ObjectTemplate<Image>>> imageRegistry = pack.getOrCreateRegistry(IMAGE_REGISTRY_KEY);
imageRegistry.register(addon.key("BITMAP"), () -> new ImageTemplate(pack.getLoader(), pack));
imageRegistry.register(addon.key("STITCHED_BITMAP"), () -> new StitchedImageTemplate(pack.getLoader(), pack));
})
.then(event -> {
event.getPack()
.applyLoader(DistanceTransform.CostFunction.class,
(type, o, loader, depthTracker) -> DistanceTransform.CostFunction.valueOf((String) o))
.applyLoader(DistanceTransform.Normalization.class,
(type, o, loader, depthTracker) -> DistanceTransform.Normalization.valueOf((String) o))
.applyLoader(ColorString.class, new ColorLoader());
CheckedRegistry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry = event.getPack().getOrCreateRegistry(
NOISE_SAMPLER_TOKEN);
noiseRegistry.register(addon.key("DISTANCE_TRANSFORM"), DistanceTransformNoiseSamplerTemplate::new);
noiseRegistry.register(addon.key("CHANNEL"), ChannelNoiseSamplerTemplate::new);
})
.then(event -> {
CheckedRegistry<Supplier<ObjectTemplate<ColorSampler>>> colorSamplerRegistry = event.getPack().getOrCreateRegistry(
COLOR_PICKER_REGISTRY_KEY);
colorSamplerRegistry.register(addon.key("SINGLE_IMAGE"), SingleImageColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("TILED_IMAGE"), TileImageColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("COLOR"), ConstantColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("ROTATE"), RotateColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("TRANSLATE"), TranslateColorSamplerTemplate::new);
});
.getHandler(FunctionalEventHandler.class)
.register(addon, ConfigPackPreLoadEvent.class)
.priority(10)
.then(event -> {
ImageLibraryPackConfigTemplate config = event.loadTemplate(new ImageLibraryPackConfigTemplate());
event.getPack().getContext().put(config);
})
.then(event -> {
ConfigPack pack = event.getPack();
CheckedRegistry<Supplier<ObjectTemplate<Image>>> imageRegistry = pack.getOrCreateRegistry(IMAGE_REGISTRY_KEY);
imageRegistry.register(addon.key("BITMAP"), () -> new ImageTemplate(pack.getLoader(), pack));
imageRegistry.register(addon.key("STITCHED_BITMAP"), () -> new StitchedImageTemplate(pack.getLoader(), pack));
})
.then(event -> {
event.getPack()
.applyLoader(DistanceTransform.CostFunction.class,
(type, o, loader, depthTracker) -> DistanceTransform.CostFunction.valueOf((String) o))
.applyLoader(DistanceTransform.Normalization.class,
(type, o, loader, depthTracker) -> DistanceTransform.Normalization.valueOf((String) o))
.applyLoader(ColorString.class, new ColorLoader());
CheckedRegistry<Supplier<ObjectTemplate<NoiseSampler>>> noiseRegistry = event.getPack().getOrCreateRegistry(
NOISE_SAMPLER_TOKEN);
noiseRegistry.register(addon.key("DISTANCE_TRANSFORM"), DistanceTransformNoiseSamplerTemplate::new);
noiseRegistry.register(addon.key("CHANNEL"), ChannelNoiseSamplerTemplate::new);
})
.then(event -> {
CheckedRegistry<Supplier<ObjectTemplate<ColorSampler>>> colorSamplerRegistry = event.getPack().getOrCreateRegistry(
COLOR_PICKER_REGISTRY_KEY);
colorSamplerRegistry.register(addon.key("SINGLE_IMAGE"), SingleImageColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("TILED_IMAGE"), TileImageColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("COLOR"), ConstantColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("ROTATE"), RotateColorSamplerTemplate::new);
colorSamplerRegistry.register(addon.key("TRANSLATE"), TranslateColorSamplerTemplate::new);
});
}
}
@@ -2,7 +2,7 @@ package com.dfsek.terra.addons.image.colorsampler;
@FunctionalInterface
public interface ColorSampler {
/**
* @param x World x coordinate
* @param z World z coordinate
@@ -6,19 +6,19 @@ import com.dfsek.terra.addons.image.image.Image;
public class SingleImageColorSampler implements ColorSampler {
private final Image image;
private final ColorSampler fallback;
private final ImageTransformation transformation;
public SingleImageColorSampler(Image image, ColorSampler fallback, ImageTransformation transformation) {
this.image = image;
this.fallback = fallback;
this.transformation = transformation;
}
@Override
public int apply(int x, int z) {
var nx = transformation.transformX(image, x);
@@ -6,16 +6,16 @@ import com.dfsek.terra.addons.image.image.Image;
public class TileImageColorSampler implements ColorSampler {
private final Image image;
private final ImageTransformation transformation;
public TileImageColorSampler(Image image, ImageTransformation transformation) {
this.image = image;
this.transformation = transformation;
}
@Override
public int apply(int x, int z) {
x = transformation.transformX(image, x);
@@ -4,13 +4,13 @@ import com.dfsek.terra.addons.image.image.Image;
public enum Alignment implements ImageTransformation {
NONE() {
@Override
public int transformX(Image image, int x) {
return x;
}
@Override
public int transformZ(Image image, int z) {
return z;
@@ -21,7 +21,7 @@ public enum Alignment implements ImageTransformation {
public int transformX(Image image, int x) {
return x + image.getWidth() / 2;
}
@Override
public int transformZ(Image image, int z) {
return z + image.getHeight() / 2;
@@ -4,8 +4,8 @@ import com.dfsek.terra.addons.image.image.Image;
public interface ImageTransformation {
int transformX(Image image, int x);
int transformZ(Image image, int z);
}
@@ -5,19 +5,19 @@ import com.dfsek.terra.api.util.MathUtil;
public class RotateColorSampler implements ColorSampler {
private final ColorSampler sampler;
private final double radians;
private final RotationMethod rotationMethod;
public RotateColorSampler(ColorSampler sampler, double degrees) {
this.sampler = sampler;
double normalizedDegrees = degrees % 360.0;
if(normalizedDegrees < 0) normalizedDegrees += 360.0;
if(normalizedDegrees == 0.0)
rotationMethod = RotationMethod.DEG_0;
else if(normalizedDegrees == 90.0)
@@ -28,10 +28,10 @@ public class RotateColorSampler implements ColorSampler {
rotationMethod = RotationMethod.DEG_270;
else
rotationMethod = RotationMethod.RAD_ANY;
this.radians = Math.toRadians(degrees);
}
@Override
public int apply(int x, int z) {
int rx = switch(rotationMethod) {
@@ -50,7 +50,7 @@ public class RotateColorSampler implements ColorSampler {
};
return sampler.apply(rx, rz);
}
private enum RotationMethod {
DEG_0,
DEG_90,
@@ -4,16 +4,16 @@ import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
public class TranslateColorSampler implements ColorSampler {
private final ColorSampler sampler;
private final int translateX, translateZ;
public TranslateColorSampler(ColorSampler sampler, int translateX, int translateZ) {
this.sampler = sampler;
this.translateX = translateX;
this.translateZ = translateZ;
}
@Override
public int apply(int x, int z) {
return sampler.apply(x - translateX, z - translateZ);
@@ -13,27 +13,27 @@ import com.dfsek.terra.addons.image.util.ColorUtil;
public class ColorLoader implements TypeLoader<ColorString> {
@Override
public ColorString load(@NotNull AnnotatedType annotatedType, @NotNull Object o, @NotNull ConfigLoader configLoader,
DepthTracker depthTracker) throws LoadException {
return new ColorString((String) o);
}
public static class ColorString {
private final int argb;
public ColorString(String string) throws IllegalArgumentException {
this.argb = parse(string);
}
private static int parse(String string) throws IllegalArgumentException {
if(string.length() == 0)
throw new IllegalArgumentException("Empty string cannot be parsed as a valid color");
String[] split = string.split(",");
if(split.length == 1)
return parseHex(string);
else if(split.length == 3)
@@ -43,48 +43,48 @@ public class ColorLoader implements TypeLoader<ColorString> {
else
throw new IllegalArgumentException("Invalid channels provided, required format RED,GREEN,BLUE or ALPHA,RED,GREEN,BLUE");
}
private static int parseHex(String hex) throws IllegalArgumentException {
if(hex.startsWith("#"))
hex = hex.substring(1);
int alpha = 255;
int red = 0;
int green = 0;
int blue = 0;
try {
if(hex.length() == 8) {
alpha = Integer.parseInt(hex.substring(0, 2), 16);
hex = hex.substring(2);
}
if(hex.length() != 6)
throw new IllegalArgumentException("Invalid color channels, required format AARRGGBB or RRGGBB");
red = Integer.parseInt(hex.substring(0, 2), 16);
green = Integer.parseInt(hex.substring(2, 4), 16);
blue = Integer.parseInt(hex.substring(4, 6), 16);
return ColorUtil.argbValidated(alpha, red, green, blue);
} catch(NumberFormatException e) {
throw new IllegalArgumentException("Failed to parse hex color", e);
}
}
private static int parseChannels(String alpha, String red, String green, String blue) throws IllegalArgumentException {
try {
int a = Integer.decode(alpha);
int r = Integer.decode(red);
int g = Integer.decode(green);
int b = Integer.decode(blue);
return ColorUtil.argbValidated(a, r, g, b);
} catch(NumberFormatException e) {
throw new IllegalArgumentException("Invalid channel value", e);
}
}
public int getColor() {
return argb;
}
@@ -10,12 +10,12 @@ import com.dfsek.terra.api.properties.Properties;
public class ImageLibraryPackConfigTemplate implements ConfigTemplate, Properties {
// TODO - These would be better as plugin wide config parameters in config.yml
@Value("images.cache.load-on-use")
@Description("If set to true, images will load into memory upon use rather than on pack load.")
@Default
private boolean loadOnUse = false;
@Value("images.cache.timeout")
@Description("How many seconds to keep images loaded in the image cache for. " +
"If set to a number greater than 0, images will be removed from memory if not used after the timeout, otherwise images " +
@@ -24,15 +24,15 @@ public class ImageLibraryPackConfigTemplate implements ConfigTemplate, Propertie
" period of time for extra processing time required to perform cache lookups.")
@Default
private int cacheTimeout = 0;
public boolean loadOnUse() {
return loadOnUse;
}
public boolean unloadOnTimeout() {
return cacheTimeout > 0;
}
public int getCacheTimeout() {
return cacheTimeout;
}
@@ -8,10 +8,10 @@ import com.dfsek.terra.addons.image.config.ColorLoader.ColorString;
public class ConstantColorSamplerTemplate implements ObjectTemplate<ColorSampler> {
@Value("color")
private ColorString color;
@Override
public ColorSampler get() {
return ((x, z) -> color.getColor());
@@ -10,12 +10,12 @@ import com.dfsek.terra.addons.image.image.Image;
public abstract class ImageColorSamplerTemplate implements ObjectTemplate<ColorSampler> {
@Value("image")
protected Image image;
@Value("align")
@Default
protected Alignment alignment = Alignment.NONE;
}
@@ -9,7 +9,7 @@ import com.dfsek.terra.addons.image.colorsampler.image.SingleImageColorSampler;
public class SingleImageColorSamplerTemplate extends ImageColorSamplerTemplate {
@Value("outside-sampler")
private ColorSampler fallback;
@Override
public ColorSampler get() {
return new SingleImageColorSampler(image, fallback, alignment);
@@ -5,7 +5,7 @@ import com.dfsek.terra.addons.image.colorsampler.image.TileImageColorSampler;
public class TileImageColorSamplerTemplate extends ImageColorSamplerTemplate {
@Override
public ColorSampler get() {
return new TileImageColorSampler(image, alignment);
@@ -7,7 +7,7 @@ import com.dfsek.terra.addons.image.colorsampler.ColorSampler;
public abstract class MutateColorSamplerTemplate implements ObjectTemplate<ColorSampler> {
@Value("color-sampler")
protected ColorSampler sampler;
}
@@ -7,10 +7,10 @@ import com.dfsek.terra.addons.image.colorsampler.mutate.RotateColorSampler;
public class RotateColorSamplerTemplate extends MutateColorSamplerTemplate {
@Value("angle")
private double degrees;
@Override
public ColorSampler get() {
return new RotateColorSampler(sampler, degrees);
@@ -7,13 +7,13 @@ import com.dfsek.terra.addons.image.colorsampler.mutate.TranslateColorSampler;
public class TranslateColorSamplerTemplate extends MutateColorSamplerTemplate {
@Value("x")
private int translateX;
@Value("z")
private int translateZ;
@Override
public ColorSampler get() {
return new TranslateColorSampler(sampler, translateX, translateZ);
@@ -6,9 +6,9 @@ import com.dfsek.terra.addons.image.converter.mapping.ColorMapping;
public abstract class ClosestColorConverterTemplate<T> implements ColorConverterTemplate<T> {
protected abstract ColorMapping<T> getMapping();
@Override
public ColorConverter<T> get() {
return new ClosestMatchColorConverter<T>(getMapping().get());
@@ -6,13 +6,13 @@ import com.dfsek.terra.addons.image.converter.mapping.ColorMapping;
public abstract class ExactColorConverterTemplate<T> implements ColorConverterTemplate<T> {
protected abstract ColorMapping<T> getMapping();
protected abstract T getFallback();
protected abstract boolean ignoreAlpha();
@Override
public ColorConverter<T> get() {
return new ExactColorConverter<T>(getMapping().get(), getFallback(), ignoreAlpha());
@@ -31,7 +31,7 @@ record ImageCache(LoadingCache<String, Image> cache) implements Properties {
images = new ImageCache(cacheBuilder.build(s -> loadImage(s, files)));
pack.getContext().put(images);
} else images = pack.getContext().get(ImageCache.class);
if(config.loadOnUse()) {
if(config.unloadOnTimeout()) { // Grab directly from cache if images are to unload on timeout
return new SuppliedImage(() -> images.cache.get(path));
@@ -41,10 +41,10 @@ record ImageCache(LoadingCache<String, Image> cache) implements Properties {
return new SuppliedImage(lazyImage::value);
}
}
return images.cache.get(path);
}
private static Image loadImage(String path, Loader files) throws IOException {
try {
return new BufferedImageWrapper(ImageIO.read(files.get(path)));
@@ -11,17 +11,17 @@ import com.dfsek.terra.api.config.Loader;
public class ImageTemplate implements ObjectTemplate<Image> {
private final Loader files;
private final ConfigPack pack;
@Value("path")
private String path;
public ImageTemplate(Loader files, ConfigPack pack) {
this.files = files;
this.pack = pack;
}
@Override
public Image get() {
try {
@@ -15,7 +15,7 @@ import com.dfsek.terra.api.config.Loader;
public class StitchedImageTemplate implements ObjectTemplate<Image>, ValidatedConfigTemplate {
private final Loader files;
private final ConfigPack pack;
@Value("path-format")
@@ -27,12 +27,12 @@ public class StitchedImageTemplate implements ObjectTemplate<Image>, ValidatedCo
@Value("zero-indexed")
@Default
private boolean zeroIndexed = false;
public StitchedImageTemplate(Loader files, ConfigPack pack) {
this.files = files;
this.pack = pack;
}
@Override
public Image get() {
Image[][] grid = new Image[rows][cols];
@@ -47,7 +47,7 @@ public class StitchedImageTemplate implements ObjectTemplate<Image>, ValidatedCo
}
return new StitchedImage(grid, zeroIndexed);
}
private String getFormattedPath(int row, int column) {
if(!zeroIndexed) {
row++;
@@ -55,7 +55,7 @@ public class StitchedImageTemplate implements ObjectTemplate<Image>, ValidatedCo
}
return path.replaceFirst("\\{row}", String.valueOf(row)).replaceFirst("\\{column}", String.valueOf(column));
}
@Override
public boolean validate() throws ValidationException {
if(!path.contains("{row}"))
@@ -11,20 +11,20 @@ import com.dfsek.terra.api.noise.NoiseSampler;
public class ChannelNoiseSamplerTemplate implements ObjectTemplate<NoiseSampler> {
@Value("color-sampler")
private ColorSampler colorSampler;
@Value("channel")
private Channel channel;
/*
* If the channel should be normalized to range [-1, 1] or not
*/
@Value("normalize")
@Default
private boolean normalize = true;
/*
* Whether to multiply color channels by the alpha channel or not. If users
* are expecting pixel transparency to reduce the output value then this should
@@ -33,7 +33,7 @@ public class ChannelNoiseSamplerTemplate implements ObjectTemplate<NoiseSampler>
@Value("premultiply")
@Default
private boolean premultiply = false;
@Override
public NoiseSampler get() {
return new ChannelNoiseSampler(colorSampler, channel, normalize, premultiply);
@@ -13,10 +13,10 @@ import com.dfsek.terra.api.noise.NoiseSampler;
public class DistanceTransformNoiseSamplerTemplate implements ObjectTemplate<NoiseSampler> {
@Value("image")
private Image image;
/**
* The threshold value applied to the channel specified in the 'channel' parameter that splits
* the image into a binary image. This parameter is only used for cost functions that utilize
@@ -25,7 +25,7 @@ public class DistanceTransformNoiseSamplerTemplate implements ObjectTemplate<Noi
@Value("threshold")
@Default
private int threshold = 127;
/**
* If set to true, distances calculated will be clamped to stay above the largest
* distance calculated on the edges of the image. This ensures output values do not
@@ -37,14 +37,14 @@ public class DistanceTransformNoiseSamplerTemplate implements ObjectTemplate<Noi
@Value("clamp-to-max-edge")
@Default
private boolean clampToEdge = false;
/**
* The target channel to run distance calculations on.
*/
@Value("channel")
@Default
private Channel channel = Channel.GRAYSCALE;
/**
* The method of image processing applied to the specified image prior to calculating
* distances.
@@ -52,24 +52,24 @@ public class DistanceTransformNoiseSamplerTemplate implements ObjectTemplate<Noi
@Value("cost-function")
@Default
private CostFunction costFunction = CostFunction.Channel;
/**
* Inverts the resulting binary image that may be used as a cost function.
*/
@Value("invert-threshold")
@Default
private boolean invertThreshold = false;
/**
* How the final distance calculation should be redistributed.
*/
@Value("normalization")
@Default
private Normalization normalization = Normalization.None;
@Override
public NoiseSampler get() {
return new DistanceTransform.Noise(new DistanceTransform(image, channel, threshold, clampToEdge, costFunction, invertThreshold),
normalization);
normalization);
}
}
@@ -6,16 +6,16 @@ import com.dfsek.terra.addons.image.util.ColorUtil;
public class ClosestMatchColorConverter<T> implements ColorConverter<T> {
private final Map<Integer, T> map;
private final Integer[] colors;
public ClosestMatchColorConverter(Map<Integer, T> map) {
this.map = map;
this.colors = map.keySet().toArray(new Integer[0]);
}
@Override
public T apply(int color) {
int closest = 0;
@@ -33,7 +33,7 @@ public class ClosestMatchColorConverter<T> implements ColorConverter<T> {
}
return map.get(closest);
}
@Override
public Iterable<T> getEntries() {
return map.values();
@@ -1,8 +1,8 @@
package com.dfsek.terra.addons.image.converter;
public interface ColorConverter<T> {
T apply(int color);
Iterable<T> getEntries();
}
@@ -10,11 +10,11 @@ import com.dfsek.terra.addons.image.util.MapUtil;
public class ExactColorConverter<T> implements ColorConverter<T> {
private final Map<Integer, T> map;
private final T fallback;
private final boolean ignoreAlpha;
public ExactColorConverter(Map<Integer, T> map, T fallback, boolean ignoreAlpha) {
if(ignoreAlpha) {
map = MapUtil.mapKeys(map, ColorUtil::zeroAlpha);
@@ -23,7 +23,7 @@ public class ExactColorConverter<T> implements ColorConverter<T> {
this.fallback = fallback;
this.ignoreAlpha = ignoreAlpha;
}
@Override
public T apply(int color) {
if(ignoreAlpha) {
@@ -32,7 +32,7 @@ public class ExactColorConverter<T> implements ColorConverter<T> {
T lookup = map.get(color);
return lookup != null ? lookup : fallback;
}
@Override
public Iterable<T> getEntries() {
Set<T> entries = new HashSet<>(map.values());
@@ -12,16 +12,16 @@ import com.dfsek.terra.api.world.biome.Biome;
public class BiomeDefinedColorMapping<T> implements ColorMapping<T> {
Registry<Biome> biomeRegistry;
Function<Biome, T> converter;
public BiomeDefinedColorMapping(Registry<Biome> biomeRegistry, Function<Biome, T> converter) {
this.biomeRegistry = biomeRegistry;
this.converter = converter;
}
@Override
public Map<Integer, T> get() {
Map<Biome, Integer> colorMap = new HashSet<>(biomeRegistry.entries()).stream().collect(Collectors.toMap(b -> b, Biome::getColor));
@@ -31,7 +31,7 @@ public class BiomeDefinedColorMapping<T> implements ColorMapping<T> {
output.put(color, biome);
} else {
throw new IllegalArgumentException(
String.format("Biome %s has same color as %s: %x", biome.getID(), output.get(color).getID(), color));
String.format("Biome %s has same color as %s: %x", biome.getID(), output.get(color).getID(), color));
}
}));
return output.entrySet().stream().collect(Collectors.toMap(Entry::getKey, e -> converter.apply(e.getValue())));
@@ -4,23 +4,23 @@ import java.awt.image.BufferedImage;
public class BufferedImageWrapper implements Image {
private final BufferedImage image;
public BufferedImageWrapper(BufferedImage image) {
this.image = image;
}
@Override
public int getRGB(int x, int y) {
return image.getRGB(x, y);
}
@Override
public int getWidth() {
return image.getWidth();
}
@Override
public int getHeight() {
return image.getHeight();
@@ -2,8 +2,8 @@ package com.dfsek.terra.addons.image.image;
public interface Image {
int getRGB(int x, int y);
int getWidth();
int getHeight();
}
@@ -1,13 +1,13 @@
package com.dfsek.terra.addons.image.image;
public class StitchedImage implements Image {
private final Image[][] images;
private final int[] rowOffsets, columnOffsets;
private final int width, height;
public StitchedImage(Image[][] images, boolean zeroIndexed) throws IllegalArgumentException {
int width = 0;
int height = 0;
@@ -33,12 +33,12 @@ public class StitchedImage implements Image {
throw new IllegalArgumentException("Image widths in column " + (i + (zeroIndexed ? 0 : 1)) + " do not match");
}
}
this.width = width;
this.height = height;
this.images = images;
}
private int getColumn(int x) {
for(int i = columnOffsets.length - 1; i > 0; i--) {
if(x >= columnOffsets[i])
@@ -46,7 +46,7 @@ public class StitchedImage implements Image {
}
return 0;
}
private int getRow(int y) {
for(int i = rowOffsets.length - 1; i > 0; i--) {
if(y >= rowOffsets[i])
@@ -54,19 +54,19 @@ public class StitchedImage implements Image {
}
return 0;
}
@Override
public int getRGB(int x, int y) {
int row = getRow(y);
int column = getColumn(x);
return images[row][column].getRGB(x - columnOffsets[column], y - rowOffsets[row]);
}
@Override
public int getWidth() {
return width;
}
@Override
public int getHeight() {
return height;
@@ -4,23 +4,23 @@ import java.util.function.Supplier;
public class SuppliedImage implements Image {
private final Supplier<Image> imageSupplier;
public SuppliedImage(Supplier<Image> imageSupplier) {
this.imageSupplier = imageSupplier;
}
@Override
public int getRGB(int x, int y) {
return imageSupplier.get().getRGB(x, y);
}
@Override
public int getWidth() {
return imageSupplier.get().getWidth();
}
@Override
public int getHeight() {
return imageSupplier.get().getHeight();
@@ -9,22 +9,22 @@ import static com.dfsek.terra.addons.image.util.MathUtil.lerp;
public class ChannelNoiseSampler implements NoiseSampler {
private final ColorSampler colorSampler;
private final Channel channel;
private final boolean normalize;
private final boolean premultiply;
public ChannelNoiseSampler(ColorSampler colorSampler, Channel channel, boolean normalize, boolean premultiply) {
this.colorSampler = colorSampler;
this.channel = channel;
this.normalize = normalize;
this.premultiply = premultiply;
}
@Override
public double noise(long seed, double x, double y) {
int sample = colorSampler.apply((int) x, (int) y);
@@ -32,7 +32,7 @@ public class ChannelNoiseSampler implements NoiseSampler {
double channelValue = channel.from(premultiplied);
return normalize ? lerp(channelValue, 0, -1, 255, 1) : channelValue;
}
@Override
public double noise(long seed, double x, double y, double z) {
return noise(seed, x, z);
@@ -15,7 +15,7 @@ import static com.dfsek.terra.addons.image.util.MathUtil.lerp;
* by Pedro F. Felzenszwalb and Daniel P. Huttenlocher.
*/
public class DistanceTransform {
private static final double MAX_DISTANCE_CAP = 10_000_000; // Arbitrarily large value, doubtful someone would
private final double[][] distances;
/**
@@ -26,7 +26,7 @@ public class DistanceTransform {
* Min and max distances of the distance computation. These may change after {@link #normalize(Normalization)} calls.
*/
private double minDistance, maxDistance;
// ever use an image large enough to exceed this.
public DistanceTransform(Image image, Channel channel, int threshold, boolean clampToMaxEdgeDistance, CostFunction costFunction,
boolean invertThreshold) {
@@ -37,7 +37,7 @@ public class DistanceTransform {
binaryImage[x][y] = ColorUtil.getChannel(image.getRGB(x, y), channel) > threshold ^ invertThreshold;
}
}
// Get edges of binary image
boolean[][] binaryImageEdge = new boolean[image.getWidth()][image.getHeight()];
for(int x = 0; x < image.getWidth(); x++) {
@@ -52,7 +52,7 @@ public class DistanceTransform {
y < image.getHeight() - 1 && !binaryImage[x][y + 1];
}
}
double[][] function = new double[image.getWidth()][image.getHeight()];
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
@@ -63,9 +63,9 @@ public class DistanceTransform {
};
}
}
distances = calculateDistance2D(function);
if(costFunction == CostFunction.ThresholdEdgeSigned) {
for(int x = 0; x < image.getWidth(); x++) {
for(int y = 0; y < image.getHeight(); y++) {
@@ -73,9 +73,9 @@ public class DistanceTransform {
}
}
}
if(clampToMaxEdgeDistance) {
// Find largest value on the edge of the image
// Find the largest value on the edge of the image
double max = Double.NEGATIVE_INFINITY;
for(int x = 0; x < image.getWidth(); x++) {
max = Math.max(max, distances[x][0]);
@@ -92,13 +92,13 @@ public class DistanceTransform {
}
}
}
this.width = image.getWidth();
this.height = image.getHeight();
setOutputRange();
}
private double[][] calculateDistance2D(double[][] f) {
double[][] d = new double[f.length][f[0].length];
// Distance pass for each column
@@ -117,7 +117,7 @@ public class DistanceTransform {
}
return d;
}
private double[] calculateDistance1D(double[] f) {
double[] d = new double[f.length];
int[] v = new int[f.length];
@@ -128,7 +128,7 @@ public class DistanceTransform {
z[1] = Integer.MAX_VALUE;
for(int q = 1; q <= f.length - 1; q++) {
double fqPlusQ2 = (f[q] + Math.pow(q, 2));
double twoQ = 2*q;
double twoQ = 2 * q;
double s = (fqPlusQ2 - (f[v[k]] + Math.pow(v[k], 2))) / (twoQ - 2 * v[k]);
while(s <= z[k]) {
k--;
@@ -139,7 +139,7 @@ public class DistanceTransform {
z[k] = s;
z[k + 1] = Integer.MAX_VALUE;
}
k = 0;
for(int q = 0; q <= f.length - 1; q++) {
while(z[k + 1] < q)
@@ -148,7 +148,7 @@ public class DistanceTransform {
}
return d;
}
/**
* Redistributes the stored distance computation according to the provided {@link Normalization} method.
*/
@@ -178,7 +178,7 @@ public class DistanceTransform {
}
setOutputRange();
}
private void setOutputRange() {
double minDistance = Double.POSITIVE_INFINITY;
double maxDistance = Double.NEGATIVE_INFINITY;
@@ -191,26 +191,26 @@ public class DistanceTransform {
this.minDistance = minDistance;
this.maxDistance = maxDistance;
}
public enum CostFunction {
Channel,
Threshold,
ThresholdEdge,
ThresholdEdgeSigned,
}
public enum Normalization {
/**
* Return the raw calculated distances.
*/
None,
/**
* Redistribute the output values to fit in the range [-1, 1]
*/
Linear,
/**
* Redistributes smoothly to the range [-1, 1], such that areas where distance = 0 stay 0.
* This is only really applicable to signed distance calculations, and will fall back to linear
@@ -218,23 +218,23 @@ public class DistanceTransform {
*/
SmoothPreserveZero,
}
public static class Noise implements NoiseSampler {
private final DistanceTransform transform;
public Noise(DistanceTransform transform, Normalization normalization) {
this.transform = transform;
transform.normalize(normalization);
}
@Override
public double noise(long seed, double x, double y) {
if(x < 0 || y < 0 || x >= transform.width || y >= transform.height) return transform.minDistance;
return transform.distances[(int) Math.floor(x)][(int) Math.floor(y)];
}
@Override
public double noise(long seed, double x, double y, double z) {
return noise(seed, x, z);
@@ -4,15 +4,15 @@ package com.dfsek.terra.addons.image.util;
* Utility class for manipulating 8 bit ARGB colors
*/
public class ColorUtil {
private ColorUtil() { }
public static int distance(int a, int b) {
return Math.abs(getRed(a) - getRed(b)) +
Math.abs(getGreen(a) - getGreen(b)) +
Math.abs(getBlue(a) - getBlue(b));
}
/**
* Returns the red channel value of a given ARGB color value.
*
@@ -23,7 +23,7 @@ public class ColorUtil {
public static int getRed(int argb) {
return argb >> 16 & 255;
}
/**
* Returns the green channel value of a given ARGB color value.
*
@@ -34,7 +34,7 @@ public class ColorUtil {
public static int getGreen(int argb) {
return argb >> 8 & 255;
}
/**
* Returns the blue channel value of a given ARGB color value.
*
@@ -45,7 +45,7 @@ public class ColorUtil {
public static int getBlue(int argb) {
return argb & 255;
}
/**
* Returns the alpha channel value of a given ARGB color value.
*
@@ -56,7 +56,7 @@ public class ColorUtil {
public static int getAlpha(int argb) {
return argb >> 24 & 255;
}
/**
* Returns the grayscale value of a given ARGB color value.
*
@@ -67,7 +67,7 @@ public class ColorUtil {
public static int getGrayscale(int argb) {
return (getRed(argb) + getGreen(argb) + getBlue(argb)) / 3;
}
/**
* Returns the value of the specified channel for a given ARGB color value.
*
@@ -79,7 +79,7 @@ public class ColorUtil {
public static int getChannel(int argb, Channel channel) {
return channel.from(argb);
}
/**
* Sets the red channel value of a given ARGB color value to zero.
*
@@ -90,7 +90,7 @@ public class ColorUtil {
public static int zeroRed(int argb) {
return argb & ~0x00FF0000;
}
/**
* Sets the green channel value of a given ARGB color value to zero.
*
@@ -101,7 +101,7 @@ public class ColorUtil {
public static int zeroGreen(int argb) {
return argb & ~0x0000FF00;
}
/**
* Sets the blue channel value of a given ARGB color value to zero.
*
@@ -112,7 +112,7 @@ public class ColorUtil {
public static int zeroBlue(int argb) {
return argb & ~0x000000FF;
}
/**
* Sets the alpha channel value of a given ARGB color value to zero.
* This is the same as setting the color to fully transparent.
@@ -124,7 +124,7 @@ public class ColorUtil {
public static int zeroAlpha(int argb) {
return argb & ~0xFF000000;
}
/**
* Sets the color channels of a given ARGB color value to zero.
* This is the same as setting the color to black, while preserving the alpha.
@@ -136,7 +136,7 @@ public class ColorUtil {
public static int zeroGrayscale(int argb) {
return argb & ~0x00FFFFFF;
}
/**
* Sets the specified channel value of a given ARGB color value to zero.
*
@@ -148,7 +148,7 @@ public class ColorUtil {
public static int zeroChannel(int argb, Channel channel) {
return channel.zero(argb);
}
/**
* Multiply the RGB channels of a given ARGB color value by its alpha channel value.
*
@@ -163,7 +163,7 @@ public class ColorUtil {
int blue = (getBlue(argb) * alpha + 127) / 255;
return argb(alpha, red, green, blue);
}
/**
* Returns an ARGB color value with the specified values for alpha, red, green, and blue channels.
*
@@ -177,7 +177,7 @@ public class ColorUtil {
public static int argb(int alpha, int red, int green, int blue) {
return argbAlpha(alpha) | argbRed(red) | argbGreen(green) | argbBlue(blue);
}
/**
* Returns an ARGB color value with the specified values for alpha, red, green, and blue channels,
* after validating that each channel value is in the range 0-255.
@@ -199,7 +199,7 @@ public class ColorUtil {
) throw new IllegalArgumentException("Channel values must be in range 0-255");
return argb(alpha, red, green, blue);
}
/**
* Returns the ARGB color value with the specified alpha channel value and zero
* for the red, green, and blue channels.
@@ -209,7 +209,7 @@ public class ColorUtil {
* @return the resulting ARGB color value
*/
public static int argbAlpha(int alpha) { return alpha << 24; }
/**
* Returns the ARGB color value with the specified red channel value and zero
* for the alpha, green, and blue channels.
@@ -219,7 +219,7 @@ public class ColorUtil {
* @return the resulting ARGB color value
*/
public static int argbRed(int red) { return red << 16; }
/**
* Returns the ARGB color value with the specified red channel value and zero
* for the alpha, red, and blue channels.
@@ -229,7 +229,7 @@ public class ColorUtil {
* @return the resulting ARGB color value
*/
public static int argbGreen(int green) { return green << 8; }
/**
* Returns the ARGB color value with the specified blue channel value and zero
* for the alpha, red, and green channels.
@@ -239,7 +239,7 @@ public class ColorUtil {
* @return the resulting ARGB color value
*/
public static int argbBlue(int blue) { return blue; }
/**
* Returns an ARGB color value with the specified grayscale value for all four channels.
*
@@ -248,19 +248,19 @@ public class ColorUtil {
* @return the resulting ARGB color value with the specified grayscale value for all four channels
*/
public static int argbGrayscale(int value) { return argb(value, value, value, value); }
public enum Channel {
RED {
@Override
public int from(int argb) {
return getRed(argb);
}
@Override
public int zero(int argb) {
return zeroRed(argb);
}
@Override
public int argb(int value) {
return argbRed(value);
@@ -271,12 +271,12 @@ public class ColorUtil {
public int from(int argb) {
return getGreen(argb);
}
@Override
public int zero(int argb) {
return zeroGreen(argb);
}
@Override
public int argb(int value) {
return argbGreen(value);
@@ -287,12 +287,12 @@ public class ColorUtil {
public int from(int argb) {
return getBlue(argb);
}
@Override
public int zero(int argb) {
return zeroBlue(argb);
}
@Override
public int argb(int value) {
return argbBlue(value);
@@ -303,12 +303,12 @@ public class ColorUtil {
public int from(int argb) {
return getGrayscale(argb);
}
@Override
public int zero(int argb) {
return zeroGrayscale(argb);
}
@Override
public int argb(int value) {
return argbAlpha(value);
@@ -319,22 +319,22 @@ public class ColorUtil {
public int from(int argb) {
return getAlpha(argb);
}
@Override
public int zero(int argb) {
return zeroAlpha(argb);
}
@Override
public int argb(int value) {
return argbAlpha(value);
}
};
public abstract int from(int argb);
public abstract int zero(int argb);
public abstract int argb(int value);
}
}
@@ -7,19 +7,19 @@ import java.util.stream.Collectors;
public class MapUtil {
private MapUtil() { }
/**
* Utility method for applying transformations on a map's keys.
*/
public static <O, N, T> Map<N, T> mapKeys(Map<O, T> map, Function<O, N> mappingFunction) {
return map
.entrySet()
.stream()
.collect(Collectors.toMap(
e -> mappingFunction.apply(e.getKey()),
Entry::getValue
));
.entrySet()
.stream()
.collect(Collectors.toMap(
e -> mappingFunction.apply(e.getKey()),
Entry::getValue
));
}
}
@@ -2,7 +2,7 @@ package com.dfsek.terra.addons.image.util;
public class MathUtil {
private MathUtil() { }
public static double lerp(double x, double x1, double y1, double x2, double y2) {
return (((y1 - y2) * (x - x1)) / (x1 - x2)) + y1;
}