Mainly formatting and some work on schematic commands

This commit is contained in:
MattBDev
2019-07-22 22:22:32 -04:00
parent 46f5b12b36
commit b230999ca0
28 changed files with 796 additions and 656 deletions

View File

@ -5,6 +5,7 @@ import java.lang.reflect.Field;
import java.util.Locale;
public class ColorUtil {
private static final int PARSE_COMPONENT = 0; // percent, or clamped to [0,255] => [0,1]
private static final int PARSE_PERCENT = 1; // clamped to [0,100]% => [0,1]
private static final int PARSE_ANGLE = 2; // clamped to [0,360]
@ -17,57 +18,57 @@ public class ColorUtil {
throw new IllegalArgumentException("Invalid color specification");
}
type = PARSE_PERCENT;
color = color.substring(0, color.length()-1).trim();
color = color.substring(0, color.length() - 1).trim();
} else if (type == PARSE_PERCENT) {
throw new IllegalArgumentException("Invalid color specification");
}
float c = ((type == PARSE_COMPONENT)
? Integer.parseInt(color)
: Float.parseFloat(color));
float c = type == PARSE_COMPONENT
? Integer.parseInt(color)
: Float.parseFloat(color);
switch (type) {
case PARSE_ALPHA:
return (c < 0f) ? 0f : (Math.min(c, 1f));
return c < 0f ? 0f : Math.min(c, 1f);
case PARSE_PERCENT:
return (c <= 0f) ? 0f : ((c >= 100f) ? 1f : (c / 100f));
return c <= 0f ? 0f : c >= 100f ? 1f : c / 100f;
case PARSE_COMPONENT:
return (c <= 0f) ? 0f : ((c >= 255f) ? 1f : (c / 255f));
return c <= 0f ? 0f : c >= 255f ? 1f : c / 255f;
case PARSE_ANGLE:
return ((c < 0f)
? ((c % 360f) + 360f)
: ((c > 360f)
? (c % 360f)
: c));
return c < 0f
? c % 360f + 360f
: c > 360f
? c % 360f
: c;
}
throw new IllegalArgumentException("Invalid color specification");
}
private static Color parseRGBColor(String color, int roff)
{
private static Color parseRGBColor(String color, int roff) {
try {
int rend = color.indexOf(',', roff);
int gend = rend < 0 ? -1 : color.indexOf(',', rend+1);
int bend = gend < 0 ? -1 : color.indexOf(gend+1);
int gend = rend < 0 ? -1 : color.indexOf(',', rend + 1);
int bend = gend < 0 ? -1 : color.indexOf(gend + 1);
float r = parseComponent(color, roff, rend, PARSE_COMPONENT);
float g = parseComponent(color, rend+1, gend, PARSE_COMPONENT);
float b = parseComponent(color, gend+1, bend, PARSE_COMPONENT);
float g = parseComponent(color, rend + 1, gend, PARSE_COMPONENT);
float b = parseComponent(color, gend + 1, bend, PARSE_COMPONENT);
return new Color(r, g, b);
} catch (NumberFormatException ignored) {}
} catch (NumberFormatException ignored) {
}
throw new IllegalArgumentException("Invalid color specification");
}
private static Color parseHSLColor(String color, int hoff)
{
private static Color parseHSLColor(String color, int hoff) {
try {
int hend = color.indexOf(',', hoff);
int send = hend < 0 ? -1 : color.indexOf(',', hend+1);
int lend = send < 0 ? -1 : color.indexOf(send+1);
int send = hend < 0 ? -1 : color.indexOf(',', hend + 1);
int lend = send < 0 ? -1 : color.indexOf(send + 1);
float h = parseComponent(color, hoff, hend, PARSE_ANGLE);
float s = parseComponent(color, hend+1, send, PARSE_PERCENT);
float l = parseComponent(color, send+1, lend, PARSE_PERCENT);
float s = parseComponent(color, hend + 1, send, PARSE_PERCENT);
float l = parseComponent(color, send + 1, lend, PARSE_PERCENT);
return Color.getHSBColor(h, s, l);
} catch (NumberFormatException ignored) {}
} catch (NumberFormatException ignored) {
}
throw new IllegalArgumentException("Invalid color specification");
}
@ -75,7 +76,7 @@ public class ColorUtil {
public static Color parseColor(String colorString) {
if (colorString == null) {
throw new NullPointerException(
"The color components or name must be specified");
"The color components or name must be specified");
}
if (colorString.isEmpty()) {
throw new IllegalArgumentException("Invalid color specification");
@ -104,7 +105,8 @@ public class ColorUtil {
try {
Field field = Color.class.getField(color.toLowerCase());
col = (Color) field.get(null);
} catch (Throwable ignore) {}
} catch (Throwable ignore) {
}
if (col != null) {
return col;
}
@ -138,7 +140,8 @@ public class ColorUtil {
b = Integer.parseInt(color.substring(4, 6), 16);
return new Color(r, g, b);
}
} catch (NumberFormatException ignored) {}
} catch (NumberFormatException ignored) {
}
throw new IllegalArgumentException("Invalid color specification");
}

View File

@ -30,14 +30,14 @@ public class MaskTraverser {
Field field = current.getDeclaredField("extent");
field.setAccessible(true);
field.set(mask, newExtent);
} catch (NoSuchFieldException | IllegalAccessException ignore) {
} catch (NoSuchFieldException | IllegalAccessException ignored) {
}
try {
Field field = current.getDeclaredField("mask");
field.setAccessible(true);
Mask next = (Mask) field.get(mask);
reset(next, newExtent);
} catch (NoSuchFieldException | IllegalAccessException ignore) {
} catch (NoSuchFieldException | IllegalAccessException ignored) {
}
try {
Field field = current.getDeclaredField("masks");
@ -46,7 +46,7 @@ public class MaskTraverser {
for (Mask next : masks) {
reset(next, newExtent);
}
} catch (NoSuchFieldException | IllegalAccessException ignore) {
} catch (NoSuchFieldException | IllegalAccessException ignored) {
}
current = current.getSuperclass();
}

View File

@ -1,6 +1,7 @@
package com.boydti.fawe.util.image;
import com.boydti.fawe.Fawe;
import com.boydti.fawe.config.Settings;
import com.boydti.fawe.util.MainUtil;
import com.boydti.fawe.util.MathMan;
import com.sk89q.worldedit.extension.input.InputParseException;
@ -21,17 +22,17 @@ import java.net.URISyntaxException;
import java.net.URL;
public class ImageUtil {
public static BufferedImage getScaledInstance(BufferedImage img,
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality)
{
int targetWidth,
int targetHeight,
Object hint,
boolean higherQuality) {
if (img.getHeight() == targetHeight && img.getWidth() == targetWidth) {
return img;
}
int type = (img.getTransparency() == Transparency.OPAQUE) ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
int type = img.getTransparency() == Transparency.OPAQUE ?
BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
BufferedImage ret = img;
int w, h;
if (higherQuality) {
@ -53,21 +54,27 @@ public class ImageUtil {
if (w < targetWidth) {
w = targetWidth;
}
} else if (w < targetWidth) w = targetWidth;
} else if (w < targetWidth) {
w = targetWidth;
}
if (higherQuality && h > targetHeight) {
h /= 2;
if (h < targetHeight) {
h = targetHeight;
}
} else if (h < targetHeight) h = targetHeight;
} else if (h < targetHeight) {
h = targetHeight;
}
BufferedImage tmp = new BufferedImage(w, h, type);
Graphics2D g2 = tmp.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_SPEED);
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION, RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
RenderingHints.VALUE_COLOR_RENDER_SPEED);
g2.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
RenderingHints.VALUE_ALPHA_INTERPOLATION_SPEED);
g2.drawImage(ret, 0, 0, w, h, null);
g2.dispose();
@ -103,12 +110,13 @@ public class ImageUtil {
float dz2 = sqrZ[z];
for (int x = 0; x < width; x++, index++) {
int color = raw[index];
int alpha = (color >> 24) & 0xFF;
int alpha = color >> 24 & 0xFF;
if (alpha != 0) {
float dx2 = sqrX[x];
float distSqr = dz2 + dx2;
if (distSqr > 1) raw[index] = 0;
else {
if (distSqr > 1) {
raw[index] = 0;
} else {
alpha = (int) (alpha * (1 - distSqr));
raw[index] = (color & 0x00FFFFFF) + (alpha << 24);
}
@ -119,10 +127,10 @@ public class ImageUtil {
public static void scaleAlpha(BufferedImage image, double alphaScale) {
int[] raw = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
int defined = (MathMan.clamp((int) (255 * alphaScale), 0, 255)) << 24;
int defined = MathMan.clamp((int) (255 * alphaScale), 0, 255) << 24;
for (int i = 0; i < raw.length; i++) {
int color = raw[i];
int alpha = ((color >> 24) & 0xFF);
int alpha = color >> 24 & 0xFF;
switch (alpha) {
case 0:
continue;
@ -147,10 +155,10 @@ public class ImageUtil {
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int color = image.getRGB(x, y);
totalRed += (color >> 16) & 0xFF;
totalGreen += (color >> 8) & 0xFF;
totalBlue += (color >> 0) & 0xFF;
totalAlpha += (color >> 24) & 0xFF;
totalRed += color >> 16 & 0xFF;
totalGreen += color >> 8 & 0xFF;
totalBlue += color >> 0 & 0xFF;
totalAlpha += color >> 24 & 0xFF;
}
}
int a = width * height;
@ -161,7 +169,8 @@ public class ImageUtil {
return (alpha << 24) + (red << 16) + (green << 8) + (blue << 0);
}
public static BufferedImage load(@Nullable ProvideBindings.ImageUri uri) throws InputParseException {
public static BufferedImage load(@Nullable ProvideBindings.ImageUri uri)
throws InputParseException {
return uri == null ? null : uri.load();
}
@ -200,7 +209,8 @@ public class ImageUtil {
return img;
} else if (arg.startsWith("file:/")) {
arg = arg.replaceFirst("file:/+", "");
File file = MainUtil.getFile(MainUtil.getFile(Fawe.imp().getDirectory(), com.boydti.fawe.config.Settings.IMP.PATHS.HEIGHTMAP), arg);
File file = MainUtil.getFile(MainUtil.getFile(Fawe.imp().getDirectory(),
Settings.IMP.PATHS.HEIGHTMAP), arg);
return MainUtil.readImage(file);
} else {
throw new InputParseException("Invalid image " + arg);
@ -219,7 +229,8 @@ public class ImageUtil {
return new URL(arg).toURI();
} else if (arg.startsWith("file:/")) {
arg = arg.replaceFirst("file:/+", "");
File file = MainUtil.getFile(MainUtil.getFile(Fawe.imp().getDirectory(), com.boydti.fawe.config.Settings.IMP.PATHS.HEIGHTMAP), arg);
File file = MainUtil.getFile(MainUtil.getFile(Fawe.imp().getDirectory(),
Settings.IMP.PATHS.HEIGHTMAP), arg);
if (!file.exists()) {
throw new InputParseException("File not found " + file);
}