Code cleaning and warning removal

This commit is contained in:
Klemek
2018-04-21 15:20:11 +02:00
parent 088054817a
commit a351ba6d8b
8 changed files with 76 additions and 156 deletions
+3 -15
View File
@@ -7,7 +7,7 @@ class Color {
final byte g;
final byte b;
Color(byte r, byte g, byte b) {
private Color(byte r, byte g, byte b) {
this.r = r;
this.g = g;
this.b = b;
@@ -19,7 +19,7 @@ class Color {
this.b = (byte) b;
}
static Color fromRgb(int r, int g, int b){
static Color fromRgb(int r, int g, int b) {
return new Color(r + Byte.MIN_VALUE, g + Byte.MIN_VALUE, b + Byte.MIN_VALUE);
}
@@ -30,18 +30,6 @@ class Color {
Utils.randomByte(rand));
}
Color plus(Color other) {
return new Color(Utils.bound((short) r + other.r),
Utils.bound((short) g + other.g),
Utils.bound((short) b + other.b));
}
Color times(float factor) {
return new Color((byte) (r * factor),
(byte) (g * factor),
(byte) (b * factor));
}
static Color add(float[] factors, Color[] colors) {
float r = 0f;
float g = 0f;
@@ -51,7 +39,7 @@ class Color {
g += factors[i] * colors[i].g;
b += factors[i] * colors[i].b;
}
return new Color(Utils.bound(Math.round(r)),Utils.bound(Math.round(g)), Utils.bound(Math.round(b)));
return new Color(Utils.bound(Math.round(r)), Utils.bound(Math.round(g)), Utils.bound(Math.round(b)));
}
static Color add(Color... colors) {
+4 -65
View File
@@ -11,9 +11,9 @@ class Generator {
private final int height2;
private final int size;
private float slope;
private Color source;
private Color divergence;
private final float slope;
private final Color source;
private final Color divergence;
private long seed = 0L;
@@ -34,7 +34,7 @@ class Generator {
Utils.randInt(0, 30)));
}
Generator(int width, int height, int size, float slope, Color divergence){
Generator(int width, int height, int size, float slope, Color divergence) {
this(width, height, size, slope, divergence, null);
}
@@ -88,17 +88,6 @@ class Generator {
generateLine(y);
}
private static Color getDivergence(int width, int height, int size) {
int size2 = (int) Math.min(Math.max(width / size, height / size), Byte.MAX_VALUE * 1.5f);
Color c;
do {
c = new Color(Utils.randInt(0, Byte.MAX_VALUE),
Utils.randInt(0, Byte.MAX_VALUE),
Utils.randInt(0, Byte.MAX_VALUE));
} while (c.sum() * 2 < size2 || c.sum() > Byte.MAX_VALUE * 2);
return c;
}
private void generateLine(int y) {
for (int x = 0; x < width2; x++) {
Color div = divergence.diverge(rand);
@@ -112,56 +101,6 @@ class Generator {
}
}
void inspect(int x, int y, int size, boolean unsigned) {
System.out.println(String.format("Inspect area : %d-%d x %d-%d", x, x + size, y, y + size));
int sumr = 0;
int sumg = 0;
int sumb = 0;
for (int i = x; i < x + size; i++) {
for (int j = y; j < y + size; j++) {
System.out.print(String.format("%1$-12s %2$-4d ", table[i][j].toString(unsigned), table[i][j].sum()));
sumr += unsigned ? table[i][j].r - Byte.MIN_VALUE : table[i][j].r;
sumg += unsigned ? table[i][j].g - Byte.MIN_VALUE : table[i][j].g;
sumb += unsigned ? table[i][j].b - Byte.MIN_VALUE : table[i][j].b;
}
System.out.println();
}
System.out.println(String.format("mean : (%d,%d,%d) %d", sumr / (size * size), sumg / (size * size), sumb / (size * size), (sumr + sumg + sumb) / (size * size)));
}
void inspectDivergence(int x0, int y0, int size) {
System.out.println(String.format("Inspect divergence in area : %d-%d x %d-%d", x0, x0 + size, y0, y0 + size));
int sumr = 0;
int sumg = 0;
int sumb = 0;
for (int x = x0; x < x0 + size; x++) {
for (int y = y0; y < y0 + size; y++) {
Color div = table[x][y];
if (x > 0 && y == 0) {
div = Color.add(new float[]{1f,-1f},new Color[]{table[x][y], table[x - 1][y]});
} else if (x == 0 && y > 0) {
div = Color.add(new float[]{1f,-1f},new Color[]{table[x][y], table[x][y-1]});
} else if (x > 0 && y > 0) {
div = Color.add(new float[]{1f,-slope,slope-1f},new Color[]{table[x][y], table[x][y - 1], table[x - 1][y]});
}
System.out.print(String.format("%1$-12s %2$-4d ", div, div.sum()));
sumr += div.r;
sumg += div.g;
sumb += div.b;
}
System.out.println();
}
System.out.println(String.format("mean : (%d,%d,%d) %d", sumr / (size * size), sumg / (size * size), sumb / (size * size), (sumr + sumg + sumb) / (size * size)));
}
byte[] getData() {
byte[] data = new byte[width * height * 3];
int k = 0;
+4 -4
View File
@@ -35,8 +35,8 @@ final class ImageUtils {
int linePadding = (width * 3) % 4;
if (linePadding > 0) {
byte[] tail = new byte[4-linePadding];
tail[tail.length-1] = (byte) 255;
byte[] tail = new byte[4 - linePadding];
tail[tail.length - 1] = (byte) 255;
data = Utils.interlaceArrays(data, tail, width * 3, height);
}
return data;
@@ -64,7 +64,7 @@ final class ImageUtils {
return header;
}
static boolean saveBmpFile(byte[] data, File bmpFile){
static boolean saveBmpFile(byte[] data, File bmpFile) {
try (FileOutputStream fos = new FileOutputStream(bmpFile.getPath())) {
fos.write(data);
return true;
@@ -74,7 +74,7 @@ final class ImageUtils {
}
}
static boolean convertBmpToJpg(File bmpFile, File jpgFile){
static boolean convertBmpToJpg(File bmpFile, File jpgFile) {
try {
BufferedImage inputImage = ImageIO.read(bmpFile);
ImageIO.write(inputImage, "JPG", jpgFile);
+1 -1
View File
@@ -1,6 +1,6 @@
package fr.klemek.marble;
public class LocalTests {
class LocalTests {
public static void main(String[] args) {
Generator gen = new Generator(800, 800, 2);
+10 -10
View File
@@ -3,10 +3,10 @@ package fr.klemek.marble;
import javax.swing.*;
import java.awt.*;
public class MarbleViewer extends JFrame {
class MarbleViewer extends JFrame {
public MarbleViewer(Generator generator){
this.setLocation(0,0);
public MarbleViewer(Generator generator) {
this.setLocation(0, 0);
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setResizable(false);
this.add(new Panel(generator));
@@ -14,13 +14,13 @@ public class MarbleViewer extends JFrame {
this.setVisible(true);
}
private class Panel extends JPanel{
private class Panel extends JPanel {
private Generator generator;
private final Generator generator;
Panel(Generator generator){
Panel(Generator generator) {
this.generator = generator;
Dimension size= new Dimension(generator.getWidth(), generator.getHeight());
Dimension size = new Dimension(generator.getWidth(), generator.getHeight());
this.setMinimumSize(size);
this.setPreferredSize(size);
this.setMaximumSize(size);
@@ -32,10 +32,10 @@ public class MarbleViewer extends JFrame {
int size = generator.getSize();
for(int x = 0; x < generator.getWidth2(); x++){
for(int y = 0; y < generator.getHeight2(); y++){
for (int x = 0; x < generator.getWidth2(); x++) {
for (int y = 0; y < generator.getHeight2(); y++) {
g.setColor(generator.getTable()[x][y].toColor());
g.fillRect(x*size, y*size, size, size);
g.fillRect(x * size, y * size, size, size);
}
}
}
+24 -24
View File
@@ -37,55 +37,55 @@ final class Utils {
return out;
}
static void translateUnsigned(byte[] data){
for(int i = 0; i < data.length; i++){
static void translateUnsigned(byte[] data) {
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (data[i] - Byte.MIN_VALUE);
}
}
static byte[] interlaceArrays(byte[] data, byte[] added, int size, int times){
byte[] out = new byte[data.length + added.length*times];
int size2 = size+added.length;
for(int i = 0; i < times; i++){
writeArray(out, data, size2*i, size2*i+size, size*i);
writeArray(out, added, size2*i+size, size2*(i+1), 0);
static byte[] interlaceArrays(byte[] data, byte[] added, int size, int times) {
byte[] out = new byte[data.length + added.length * times];
int size2 = size + added.length;
for (int i = 0; i < times; i++) {
writeArray(out, data, size2 * i, size2 * i + size, size * i);
writeArray(out, added, size2 * i + size, size2 * (i + 1), 0);
}
writeArray(out, data, size2*times, out.length-size2*times, size*times);
writeArray(out, data, size2 * times, out.length - size2 * times, size * times);
return out;
}
static void writeArray(byte[] out, byte[] data, int start, int stop, int padding){
for(int i = 0; i < Math.min(stop, out.length)-start; i++){
out[i+start] = data[i+padding];
static void writeArray(byte[] out, byte[] data, int start, int stop, int padding) {
for (int i = 0; i < Math.min(stop, out.length) - start; i++) {
out[i + start] = data[i + padding];
}
}
static void writeArray(byte[] out, byte[] data, int start, int padding){
writeArray(out, data, start, start+data.length-padding, padding);
static void writeArray(byte[] out, byte[] data, int start, int padding) {
writeArray(out, data, start, start + data.length - padding, padding);
}
static void writeArray(byte[] out, byte[] data, int start){
static void writeArray(byte[] out, byte[] data, int start) {
writeArray(out, data, start, 0);
}
static byte[] num2bytes(int number, int nbyte){
static byte[] num2bytes(int number, int nbyte) {
byte[] b = new byte[nbyte];
for(int i = 0; i < nbyte; i++){
b[i] = (byte)(number%256);
number = number/256;
for (int i = 0; i < nbyte; i++) {
b[i] = (byte) (number % 256);
number = number / 256;
}
return b;
}
static byte[] num2bytes(int number){
static byte[] num2bytes(int number) {
return num2bytes(number, 4);
}
static int randInt(int min, int max){
return ThreadLocalRandom.current().nextInt(max-min)+min;
static int randInt(int min, int max) {
return ThreadLocalRandom.current().nextInt(max - min) + min;
}
static int div(Random rand, byte src){
return Math.round(rand.nextFloat()*2*src-src);
static int div(Random rand, byte src) {
return Math.round(rand.nextFloat() * 2 * src - src);
}
}
+4 -4
View File
@@ -29,7 +29,7 @@ class WallpaperGenerator {
makeWallpaper(file, width, height, size);
}
static Dimension getScreenSizes() {
private static Dimension getScreenSizes() {
Dimension dim = new Dimension(0, 0);
for (GraphicsDevice gd : GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices()) {
dim.width = Math.max(dim.width, gd.getDisplayMode().getWidth());
@@ -38,7 +38,7 @@ class WallpaperGenerator {
return dim;
}
static void makeWallpaper(String name, int width, int height, int size) {
private static void makeWallpaper(String name, int width, int height, int size) {
System.out.println("Making wallpaper '" + name + "' " + width + "x" + height + "px");
File bmpFile = new File(name + ".bmp");
@@ -56,12 +56,12 @@ class WallpaperGenerator {
System.out.println("\tData writing done in " + (System.currentTimeMillis() - t1) + " ms");
t1 = System.currentTimeMillis();
if(!ImageUtils.saveBmpFile(file, bmpFile))
if (!ImageUtils.saveBmpFile(file, bmpFile))
return;
System.out.println("\tFile writing done in " + (System.currentTimeMillis() - t1) + " ms");
t1 = System.currentTimeMillis();
if(!ImageUtils.convertBmpToJpg(bmpFile, outputFile))
if (!ImageUtils.convertBmpToJpg(bmpFile, outputFile))
return;
System.out.println("\tFile converting done in " + (System.currentTimeMillis() - t1) + " ms");
+26 -33
View File
@@ -1,72 +1,65 @@
package fr.klemek.marble;
import static org.junit.Assert.*;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
public class UtilsTest {
@org.junit.Test
public void bound() {
assertEquals((byte)-128, Utils.bound(-200));
assertEquals((byte)-128, Utils.bound(-129));
assertEquals((byte)-128, Utils.bound(-128));
assertEquals((byte)-64, Utils.bound(-64));
assertEquals((byte)0, Utils.bound(0));
assertEquals((byte)64, Utils.bound(64));
assertEquals((byte)127, Utils.bound(127));
assertEquals((byte)127, Utils.bound(128));
assertEquals((byte)127, Utils.bound(200));
assertEquals((byte) -128, Utils.bound(-200));
assertEquals((byte) -128, Utils.bound(-129));
assertEquals((byte) -128, Utils.bound(-128));
assertEquals((byte) -64, Utils.bound(-64));
assertEquals((byte) 0, Utils.bound(0));
assertEquals((byte) 64, Utils.bound(64));
assertEquals((byte) 127, Utils.bound(127));
assertEquals((byte) 127, Utils.bound(128));
assertEquals((byte) 127, Utils.bound(200));
}
@org.junit.Test
public void subArray() {
byte[] array0 = new byte[]{0,1,2,3,4,5,6,7,8,9};
assertArrayEquals(new byte[]{2,3,4}, Utils.subArray(array0, 2, 5));
byte[] array0 = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
assertArrayEquals(new byte[]{2, 3, 4}, Utils.subArray(array0, 2, 5));
}
@org.junit.Test
public void translateUnsigned() {
byte[] array0 = new byte[]{-128,-64,0,64,127};
byte[] array0 = new byte[]{-128, -64, 0, 64, 127};
Utils.translateUnsigned(array0);
assertArrayEquals(new byte[]{0,64,(byte)128,(byte)192,(byte)255}, array0);
assertArrayEquals(new byte[]{0, 64, (byte) 128, (byte) 192, (byte) 255}, array0);
}
@org.junit.Test
public void interlaceArrays() {
byte[] array0 = new byte[10];
byte[] array1 = new byte[]{1,1};
assertArrayEquals(new byte[]{0,0,0,1,1,0,0,0,1,1,0,0,0,0}, Utils.interlaceArrays(array0, array1, 3,2));
byte[] array1 = new byte[]{1, 1};
assertArrayEquals(new byte[]{0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0}, Utils.interlaceArrays(array0, array1, 3, 2));
}
@org.junit.Test
public void writeArray() {
byte[] array0 = new byte[10];
byte[] array1 = new byte[]{0,1,2,3,4,5,6,7,8,9};
Utils.writeArray(array0,array1,2,5,3);
assertArrayEquals(new byte[]{0,0,3,4,5,0,0,0,0,0}, array0);
byte[] array1 = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Utils.writeArray(array0, array1, 2, 5, 3);
assertArrayEquals(new byte[]{0, 0, 3, 4, 5, 0, 0, 0, 0, 0}, array0);
}
@org.junit.Test
public void writeArray1() {
byte[] array0 = new byte[10];
byte[] array1 = new byte[]{0,1,2,3,4,5,6,7,8,9};
Utils.writeArray(array0,array1,2,3);
assertArrayEquals(new byte[]{0,0,3,4,5,6,7,8,9,0}, array0);
byte[] array1 = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Utils.writeArray(array0, array1, 2, 3);
assertArrayEquals(new byte[]{0, 0, 3, 4, 5, 6, 7, 8, 9, 0}, array0);
}
@org.junit.Test
public void writeArray2() {
byte[] array0 = new byte[10];
byte[] array1 = new byte[]{0,1,2,3,4,5,6,7,8,9};
Utils.writeArray(array0,array1,2);
assertArrayEquals(new byte[]{0,0,0,1,2,3,4,5,6,7}, array0);
}
@org.junit.Test
public void num2bytes() {
}
@org.junit.Test
public void num2bytes1() {
byte[] array1 = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
Utils.writeArray(array0, array1, 2);
assertArrayEquals(new byte[]{0, 0, 0, 1, 2, 3, 4, 5, 6, 7}, array0);
}
}