Skip to content

Commit 200899f

Browse files
authored
Merge pull request #14 from Bob-Rust/update_project
Update project
2 parents 9ccdb9c + 051c706 commit 200899f

40 files changed

+2360
-1634
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ target/
2424
bin/
2525
.classpath
2626
.project
27+
28+
# project
29+
logs/
30+
bobrust.properties

build.gradle

Lines changed: 91 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,87 @@ plugins {
1111

1212
repositories {
1313
mavenLocal()
14-
maven {
15-
url = uri('https://repo.maven.apache.org/maven2/')
16-
}
14+
maven { url 'https://repo.maven.apache.org/maven2' }
1715
}
1816

19-
group = 'Bob-Rust-Java'
20-
version = '0.0.2'
21-
description = 'Bob-Rust-Java'
17+
project.ext {
18+
lwjglVersion = '3.2.3'
19+
log4jVersion = '2.17.1'
20+
majorVersion = '0'
21+
minorVersion = '1'
22+
}
23+
version = "${majorVersion}.${minorVersion}.${getBuildId()}"
24+
group = "Bob-Rust-Java"
25+
description = "Bob-Rust-Java"
2226
java.sourceCompatibility = JavaVersion.VERSION_16
2327

2428
application {
2529
mainClass = 'com.bobrust.main.Main'
2630
}
2731

32+
import org.gradle.internal.os.OperatingSystem
33+
34+
switch (OperatingSystem.current()) {
35+
case OperatingSystem.LINUX:
36+
def osArch = System.getProperty("os.arch")
37+
project.ext.lwjglNatives = osArch.startsWith("arm") || osArch.startsWith("aarch64")
38+
? "natives-linux-${osArch.contains("64") || osArch.startsWith("armv8") ? "arm64" : "arm32"}"
39+
: "natives-linux"
40+
break
41+
case OperatingSystem.MAC_OS:
42+
project.ext.lwjglNatives = natives-macos
43+
break
44+
case OperatingSystem.WINDOWS:
45+
def osArch = System.getProperty("os.arch")
46+
project.ext.lwjglNatives = osArch.contains("64")
47+
? "natives-windows${osArch.startsWith("aarch64") ? "-arm64" : ""}"
48+
: "natives-windows-x86"
49+
break
50+
}
51+
52+
configurations {
53+
packImpl {
54+
canBeResolved = true
55+
transitive = false
56+
}
57+
58+
implementation.extendsFrom(packImpl)
59+
}
60+
61+
dependencies {
62+
packImpl "org.lwjgl:lwjgl:$lwjglVersion"
63+
packImpl "org.lwjgl:lwjgl-tinyfd:$lwjglVersion"
64+
packImpl "org.lwjgl:lwjgl:$lwjglVersion:$lwjglNatives"
65+
packImpl "org.lwjgl:lwjgl-tinyfd:$lwjglVersion:$lwjglNatives"
66+
67+
packImpl "org.apache.logging.log4j:log4j-api:$log4jVersion"
68+
packImpl "org.apache.logging.log4j:log4j-core:$log4jVersion"
69+
}
70+
71+
72+
def incrementBuildId() {
73+
def versionPropsFile = file('version.properties')
74+
if (versionPropsFile.canRead()) {
75+
def Properties versionProps = new Properties()
76+
77+
versionProps.load(new FileInputStream(versionPropsFile))
78+
def code = versionProps['build_id'].toInteger() + 1
79+
versionProps['build_id'] = code.toString()
80+
versionProps.store(versionPropsFile.newWriter(), null)
81+
}
82+
}
83+
84+
def getBuildId() {
85+
def versionPropsFile = file('version.properties')
86+
if (versionPropsFile.canRead()) {
87+
def Properties versionProps = new Properties()
88+
89+
versionProps.load(new FileInputStream(versionPropsFile))
90+
return versionProps['build_id'].toInteger()
91+
}
92+
return 0
93+
}
94+
2895
task clearDist {
2996
doLast {
3097
println "Cleaning dist"
@@ -35,18 +102,34 @@ task clearDist {
35102
}
36103
}
37104

105+
task assembleRelease {
106+
doFirst {
107+
println "AutoIncrement"
108+
incrementBuildId()
109+
}
110+
}
111+
112+
task copyRuntimeLibs(type: Copy) {
113+
into "${buildDir}/$libsDirName"
114+
from configurations.packImpl
115+
}
116+
38117
/**
39118
* https://github.com/petr-panteleyev/jpackage-gradle-plugin
40119
* https://docs.oracle.com/en/java/javase/16/jpackage/override-jpackage-resources.html#GUID-1B718F8B-B68D-4D46-B1DB-003D7729AAB6
41120
*/
42121
tasks.jpackage {
122+
dependsOn("copyRuntimeLibs")
43123
dependsOn("clearDist")
44124
dependsOn("build")
125+
126+
// autoincrement version
127+
dependsOn("assembleRelease")
45128

46129
appName = "Bob Rust"
47-
temp="${buildDir}/jpackagetemp"
130+
temp = "${buildDir}/jpackagetemp"
48131
type = "MSI"
49-
input = "$buildDir/$libsDirName"
132+
input = "$buildDir/$libsDirName"
50133
destination = "$buildDir/$distsDirName"
51134
mainClass = "com.bobrust.main.Main"
52135
verbose = true

deploy.cmd

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/main/java/com/bobrust/generator/BorstCore.java

Lines changed: 65 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.bobrust.generator;
22

33
class BorstCore {
4-
public static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[] lines, int alpha) {
4+
static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[] lines, int alpha) {
55
long rsum_1 = 0;
66
long gsum_1 = 0;
77
long bsum_1 = 0;
@@ -49,7 +49,7 @@ public static BorstColor computeColor(BorstImage target, BorstImage current, Sca
4949
return BorstUtils.getClosestColor((alpha << 24) | (r << 16) | (g << 8) | (b));
5050
}
5151

52-
public static void copyLines_replaceRegion(BorstImage dst, BorstImage src, Scanline[] lines) {
52+
static void copyLinesReplaceRegion(BorstImage dst, BorstImage src, Scanline[] lines) {
5353
int w = dst.width;
5454
int len = lines.length;
5555
for(int i = 0; i < len; i++) {
@@ -60,7 +60,7 @@ public static void copyLines_replaceRegion(BorstImage dst, BorstImage src, Scanl
6060
}
6161
}
6262

63-
public static void drawLines(BorstImage im, BorstColor c, Scanline[] lines, int alpha) {
63+
static void drawLines(BorstImage im, BorstColor c, Scanline[] lines, int alpha) {
6464
int cr = c.r * alpha;
6565
int cg = c.g * alpha;
6666
int cb = c.b * alpha;
@@ -86,11 +86,10 @@ public static void drawLines(BorstImage im, BorstColor c, Scanline[] lines, int
8686
}
8787
}
8888
}
89-
90-
// [Only used once]
91-
public static float differenceFull(BorstImage a, BorstImage b) {
92-
int w = a.width;
93-
int h = a.height;
89+
90+
static float differenceFull(BorstImage a, BorstImage b) {
91+
final int w = a.width;
92+
final int h = a.height;
9493

9594
long total = 0;
9695

@@ -120,7 +119,7 @@ public static float differenceFull(BorstImage a, BorstImage b) {
120119
return (float)(Math.sqrt(total / (w * h * 4.0)) / 255.0);
121120
}
122121

123-
public static float differencePartial(BorstImage target, BorstImage before, BorstImage after, float score, Scanline[] lines) {
122+
static float differencePartial(BorstImage target, BorstImage before, BorstImage after, float score, Scanline[] lines) {
124123
int w = target.width;
125124
int h = target.height;
126125
double denom = (w * h * 4.0);
@@ -166,6 +165,62 @@ public static float differencePartial(BorstImage target, BorstImage before, Bors
166165
}
167166
}
168167

169-
return (float)(Math.sqrt(total / denom) * 0.003921568627451);
168+
return (float)(Math.sqrt(total / denom) / 255.0);
169+
}
170+
171+
static float differencePartialThread(BorstImage target, BorstImage before, float score, int alpha, Scanline[] lines) {
172+
BorstColor color = BorstCore.computeColor(target, before, lines, alpha);
173+
174+
final int len = lines.length;
175+
final int h = target.height;
176+
final int w = target.width;
177+
178+
final double denom = (w * h * 4.0);
179+
long total = (long)(Math.pow(score * 255, 2) * denom);
180+
181+
final int cr = color.r * alpha;
182+
final int cg = color.g * alpha;
183+
final int cb = color.b * alpha;
184+
final int pa = 255 - alpha;
185+
186+
for(int i = 0; i < len; i++) {
187+
Scanline line = lines[i];
188+
int idx = line.y * w;
189+
190+
for(int x = line.x1; x <= line.x2; x++) {
191+
int tt = target.pixels[idx + x];
192+
int bb = before.pixels[idx + x];
193+
194+
int bb_a = (bb >>> 24) & 0xff;
195+
int bb_r = (bb >>> 16) & 0xff;
196+
int bb_g = (bb >>> 8) & 0xff;
197+
int bb_b = (bb ) & 0xff;
198+
199+
int aa_r = (cr + (bb_r * pa)) >>> 8;
200+
int aa_g = (cg + (bb_g * pa)) >>> 8;
201+
int aa_b = (cb + (bb_b * pa)) >>> 8;
202+
int aa_a = 255 - (((255 - bb_a) * pa) >>> 8);
203+
204+
int tt_a = (tt >>> 24) & 0xff;
205+
int tt_r = (tt >>> 16) & 0xff;
206+
int tt_g = (tt >>> 8) & 0xff;
207+
int tt_b = (tt ) & 0xff;
208+
209+
int da1 = tt_a - bb_a;
210+
int dr1 = tt_r - bb_r;
211+
int dg1 = tt_g - bb_g;
212+
int db1 = tt_b - bb_b;
213+
214+
int da2 = tt_a - aa_a;
215+
int dr2 = tt_r - aa_r;
216+
int dg2 = tt_g - aa_g;
217+
int db2 = tt_b - aa_b;
218+
219+
total -= (long)(dr1*dr1 + dg1*dg1 + db1*db1 + da1*da1);
220+
total += (long)(dr2*dr2 + dg2*dg2 + db2*db2 + da2*da2);
221+
}
222+
}
223+
224+
return (float)(Math.sqrt(total / denom) / 255.0);
170225
}
171226
}

src/main/java/com/bobrust/generator/BorstGenerator.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import java.util.Objects;
44
import java.util.function.Consumer;
55

6-
import com.bobrust.logging.LogUtils;
6+
import org.apache.logging.log4j.LogManager;
7+
import org.apache.logging.log4j.Logger;
8+
9+
import com.bobrust.util.RustConstants;
710

811
public class BorstGenerator {
12+
private static final Logger LOGGER = LogManager.getLogger(BorstGenerator.class);
913
private final Consumer<BorstData> callback;
1014
private final BorstSettings settings;
1115
private volatile Thread thread;
@@ -30,7 +34,7 @@ public synchronized boolean isPaused() {
3034
*/
3135
public synchronized boolean start() {
3236
if(thread != null) {
33-
LogUtils.warn("BorstGenerator has already been started! Restarting generator");
37+
LOGGER.warn("BorstGenerator has already been started! Restarting generator");
3438
try {
3539
stop();
3640
} catch(InterruptedException e) {
@@ -39,7 +43,7 @@ public synchronized boolean start() {
3943
}
4044

4145
if(settings.DirectImage == null) {
42-
LogUtils.error("Failed to load borst image");
46+
LOGGER.error("Failed to load borst image");
4347
return false;
4448
}
4549

@@ -52,13 +56,12 @@ public synchronized boolean start() {
5256
try {
5357
alpha = BorstUtils.ALPHAS[settings.Alpha];
5458
} catch(Exception e) {
55-
LogUtils.error("Failed to set alpha value. '%s' is not a valid alpha index.", settings.Alpha);
59+
LOGGER.error("Failed to set alpha value. '%s' is not a valid alpha index.", settings.Alpha);
5660
return false;
5761
}
5862

5963
isPaused = false;
6064

61-
@SuppressWarnings("unused")
6265
Thread thread = new Thread(() -> {
6366
this.index = 0;
6467

@@ -96,7 +99,15 @@ public synchronized boolean start() {
9699

97100
double time = (end - begin) / 1000000000.0;
98101
double sps = i / time;
99-
//LogUtils.info("%5d: t=%.3f s, score=%.6f, n=%d, s/s=%.2f", i, time, model.score, n, sps);
102+
if(RustConstants.DEBUG_GENERATOR) {
103+
LOGGER.debug("{}: t={} s, score={}, n={}, s/s={}",
104+
"%5d".formatted(i),
105+
"%.3f".formatted(time),
106+
"%.6f".formatted(model.score),
107+
n,
108+
"%.2f".formatted(sps)
109+
);
110+
}
100111
}
101112
}
102113
} finally {

src/main/java/com/bobrust/generator/BorstImage.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ public BorstImage(int width, int height) {
2727
this.width = width;
2828
this.height = height;
2929
}
30+
31+
/**
32+
* Creates a new BorstImage that has no internal bufferedImage.
33+
* @param pixels the pixel buffer
34+
*/
35+
public BorstImage(int[] pixels, int width) {
36+
this.image = null;
37+
this.pixels = pixels;
38+
this.width = width;
39+
this.height = pixels.length / width;
40+
}
3041

3142
public BorstImage createCopy() {
3243
BorstImage copy = new BorstImage(width, height);

src/main/java/com/bobrust/generator/Circle.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
public class Circle {
88
private final Worker worker;
99

10-
// position
10+
// Position
1111
public int x;
1212
public int y;
1313

14-
// radius
14+
// Radius
1515
public int r;
1616

1717
public Circle(Worker worker) {
@@ -29,24 +29,24 @@ public Circle(Worker worker, int x, int y, int r) {
2929
this.r = r;
3030
}
3131

32-
public void Mutate() {
32+
public void mutateShape() {
3333
int w = worker.w - 1;
3434
int h = worker.h - 1;
3535
Random rnd = worker.rnd;
3636

3737
if(rnd.nextInt(3) == 0) {
38-
int a = x + (int)(rnd.nextGaussian() * 16); // NormFloat64
39-
int b = y + (int)(rnd.nextGaussian() * 16); // NormFloat64
38+
int a = x + (int)(rnd.nextGaussian() * 16);
39+
int b = y + (int)(rnd.nextGaussian() * 16);
4040
x = BorstUtils.clampInt(a, 0, w);
4141
y = BorstUtils.clampInt(b, 0, h);
4242
} else {
43-
int c = BorstUtils.getClosestSize(r + (int)(rnd.nextGaussian() * 16)); // NormFloat64
43+
int c = BorstUtils.getClosestSize(r + (int)(rnd.nextGaussian() * 16));
4444
r = BorstUtils.clampInt(c, 1, w);
4545
}
4646
}
4747

4848

49-
public Scanline[] Rasterize() {
49+
public Scanline[] getScanlines() {
5050
int w = worker.w;
5151
int h = worker.h;
5252

0 commit comments

Comments
 (0)