Skip to content

Commit 29d6296

Browse files
committed
feature: Update UI fix small issues
1 parent 6efd60c commit 29d6296

32 files changed

+309
-373
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ project.ext {
1818
lwjglVersion = '3.2.3'
1919
log4jVersion = '2.17.1'
2020
majorVersion = '0'
21-
minorVersion = '3'
21+
minorVersion = '4'
2222
}
2323

2424
version = "${majorVersion}.${minorVersion}.${getBuildId()}"

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

Lines changed: 61 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,33 @@
11
package com.bobrust.generator;
22

33
class BorstCore {
4-
static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[] lines, int alpha) {
4+
static BorstColor computeColor(BorstImage target, BorstImage current, int alpha, int size, int x_offset, int y_offset) {
55
long rsum_1 = 0;
66
long gsum_1 = 0;
77
long bsum_1 = 0;
8-
8+
99
long rsum_2 = 0;
1010
long gsum_2 = 0;
1111
long bsum_2 = 0;
12-
12+
1313
int count = 0;
1414
int w = target.width;
15+
int h = target.height;
1516

17+
final Scanline[] lines = CircleCache.CIRCLE_CACHE[size];
1618
final int len = lines.length;
17-
for(int i = 0; i < len; i++) {
19+
for (int i = 0; i < len; i++) {
1820
Scanline line = lines[i];
19-
int idx = line.y * w;
20-
21-
for(int x = line.x1; x <= line.x2; x++) {
21+
int y = line.y + y_offset;
22+
if (y < 0 || y >= h) {
23+
continue;
24+
}
25+
26+
int xs = Math.max(line.x1 + x_offset, 0);
27+
int xe = Math.min(line.x2 + x_offset, w - 1);
28+
int idx = y * w;
29+
30+
for (int x = xs; x <= xe; x++) {
2231
int tt = target.pixels[idx + x];
2332
int cc = current.pixels[idx + x];
2433

@@ -30,7 +39,7 @@ static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[]
3039
gsum_2 += (cc >>> 8) & 0xff;
3140
bsum_2 += (cc ) & 0xff;
3241
}
33-
42+
3443
count += (line.x2 - line.x1 + 1);
3544
}
3645

@@ -48,30 +57,30 @@ static BorstColor computeColor(BorstImage target, BorstImage current, Scanline[]
4857

4958
return BorstUtils.getClosestColor((alpha << 24) | (r << 16) | (g << 8) | (b));
5059
}
51-
52-
static void copyLinesReplaceRegion(BorstImage dst, BorstImage src, Scanline[] lines) {
53-
int w = dst.width;
54-
int len = lines.length;
55-
for(int i = 0; i < len; i++) {
56-
Scanline line = lines[i];
57-
int idx = line.x1 + line.y * w;
58-
59-
System.arraycopy(src.pixels, idx, dst.pixels, idx, line.x2 - line.x1 + 1);
60-
}
61-
}
62-
63-
static void drawLines(BorstImage im, BorstColor c, Scanline[] lines, int alpha) {
60+
61+
static void drawLines(BorstImage im, BorstColor c, int alpha, int size, int x_offset, int y_offset) {
6462
int cr = c.r * alpha;
6563
int cg = c.g * alpha;
6664
int cb = c.b * alpha;
6765
int pa = 255 - alpha;
6866
int w = im.width;
67+
int h = im.height;
68+
69+
final Scanline[] lines = CircleCache.CIRCLE_CACHE[size];
6970
final int len = lines.length;
70-
for(int i = 0; i < len; i++) {
71+
for (int i = 0; i < len; i++) {
7172
Scanline line = lines[i];
73+
int y = line.y + y_offset;
74+
if (y < 0 || y >= h) {
75+
continue;
76+
}
77+
78+
int xs = Math.max(line.x1 + x_offset, 0);
79+
int xe = Math.min(line.x2 + x_offset, w - 1);
80+
int idx = y * w;
7281

73-
for(int x = line.x1; x <= line.x2; x++) {
74-
int a = im.pixels[line.y * w + x];
82+
for (int x = xs; x <= xe; x++) {
83+
int a = im.pixels[idx + x];
7584
int a_a = (a >>> 24) & 0xff;
7685
int a_r = (a >>> 16) & 0xff;
7786
int a_g = (a >>> 8) & 0xff;
@@ -82,7 +91,7 @@ static void drawLines(BorstImage im, BorstColor c, Scanline[] lines, int alpha)
8291
int ab = (cb + (a_b * pa)) >>> 8;
8392
int aa = 255 - (((255 - a_a) * pa) >>> 8);
8493

85-
im.pixels[line.y * w + x] = (aa << 24) | (ar << 16) | (ag << 8) | (ab);
94+
im.pixels[idx + x] = (aa << 24) | (ar << 16) | (ag << 8) | (ab);
8695
}
8796
}
8897
}
@@ -119,18 +128,26 @@ static float differenceFull(BorstImage a, BorstImage b) {
119128
return (float)(Math.sqrt(total / (w * h * 4.0)) / 255.0);
120129
}
121130

122-
static float differencePartial(BorstImage target, BorstImage before, BorstImage after, float score, Scanline[] lines) {
131+
static float differencePartial(BorstImage target, BorstImage before, BorstImage after, float score, int size, int x_offset, int y_offset) {
123132
int w = target.width;
124133
int h = target.height;
125134
double denom = (w * h * 4.0);
126135
long total = (long)(Math.pow(score * 255, 2) * denom);
127136

137+
final Scanline[] lines = CircleCache.CIRCLE_CACHE[size];
128138
final int len = lines.length;
129-
for(int i = 0; i < len; i++) {
139+
for (int i = 0; i < len; i++) {
130140
Scanline line = lines[i];
131-
int idx = line.y * w;
141+
int y = line.y + y_offset;
142+
if (y < 0 || y >= h) {
143+
continue;
144+
}
145+
146+
int xs = Math.max(line.x1 + x_offset, 0);
147+
int xe = Math.min(line.x2 + x_offset, w - 1);
148+
int idx = y * w;
132149

133-
for(int x = line.x1; x <= line.x2; x++) {
150+
for (int x = xs; x <= xe; x++) {
134151
int tt = target.pixels[idx + x];
135152
int bb = before.pixels[idx + x];
136153
int aa = after.pixels[idx + x];
@@ -168,10 +185,9 @@ static float differencePartial(BorstImage target, BorstImage before, BorstImage
168185
return (float)(Math.sqrt(total / denom) / 255.0);
169186
}
170187

171-
static float differencePartialThread(BorstImage target, BorstImage before, float score, int alpha, Scanline[] lines) {
172-
BorstColor color = BorstCore.computeColor(target, before, lines, alpha);
188+
static float differencePartialThread(BorstImage target, BorstImage before, float score, int alpha, int size, int x_offset, int y_offset) {
189+
BorstColor color = BorstCore.computeColor(target, before, alpha, size, x_offset, y_offset);
173190

174-
final int len = lines.length;
175191
final int h = target.height;
176192
final int w = target.width;
177193

@@ -183,11 +199,21 @@ static float differencePartialThread(BorstImage target, BorstImage before, float
183199
final int cb = color.b * alpha;
184200
final int pa = 255 - alpha;
185201

186-
for(int i = 0; i < len; i++) {
202+
final Scanline[] lines = CircleCache.CIRCLE_CACHE[size];
203+
final int len = lines.length;
204+
205+
for (int i = 0; i < len; i++) {
187206
Scanline line = lines[i];
188-
int idx = line.y * w;
207+
int y = line.y + y_offset;
208+
if (y < 0 || y >= h) {
209+
continue;
210+
}
211+
212+
int xs = Math.max(line.x1 + x_offset, 0);
213+
int xe = Math.min(line.x2 + x_offset, w - 1);
214+
int idx = y * w;
189215

190-
for(int x = line.x1; x <= line.x2; x++) {
216+
for (int x = xs; x <= xe; x++) {
191217
int tt = target.pixels[idx + x];
192218
int bb = before.pixels[idx + x];
193219

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.ArrayList;
55
import java.util.List;
66
import java.util.Objects;
7+
import java.util.concurrent.ExecutorService;
78
import java.util.function.Consumer;
89

910
import com.bobrust.generator.sorter.Blob;
@@ -19,7 +20,7 @@ public class BorstGenerator {
1920
private volatile Model model;
2021

2122
// Sent as a callback
22-
private final BorstData data = new BorstData();
23+
public final BorstData data = new BorstData();
2324

2425
public BorstGenerator(Consumer<BorstData> callback) {
2526
this.callback = Objects.requireNonNull(callback);
@@ -72,7 +73,7 @@ public synchronized boolean start(Model previous, BufferedImage inputImage, int
7273
int i = model.shapes.size();
7374
try {
7475
long begin = System.nanoTime();
75-
for (; i <= maxShapes; i++) {
76+
for (; i < maxShapes; i++) {
7677
int n = model.processStep();
7778
long end = System.nanoTime();
7879

@@ -82,7 +83,7 @@ public synchronized boolean start(Model previous, BufferedImage inputImage, int
8283
return;
8384
}
8485

85-
if ((i == maxShapes) || (i % callbackInterval) == 0) {
86+
if ((i == maxShapes - 1) || (i % callbackInterval) == 0) {
8687
synchronized (data) {
8788
data.update(model, i);
8889
callback.accept(data);
@@ -125,13 +126,12 @@ public synchronized Model stop() {
125126
var localThread = thread;
126127
if (localThread != null) {
127128
try {
128-
// Interrupt the thread and join it to wait for it to close.
129+
// Interrupt the thread and join it to wait for it to close
129130
localThread.interrupt();
130131
localThread.join();
131132
} catch (InterruptedException ignored) {
132133
Thread.currentThread().interrupt();
133134
} finally {
134-
// Make sure that the thread keeps it's interrupted state.
135135
thread = null;
136136
}
137137
}
@@ -205,11 +205,7 @@ private List<Blob> generateDebugDrawList() {
205205
/**
206206
* When accessing this object you must first synchronize with it
207207
*
208-
* <pre>
209-
* synchronized (data) {
210-
*
211-
* }
212-
* </pre>
208+
* <pre>synchronized (data);</pre>
213209
*/
214210
public static class BorstData {
215211
private final List<Blob> blobs;
@@ -241,21 +237,21 @@ private synchronized void update(Model model, int index) {
241237
}
242238
}
243239

244-
private synchronized void clear() {
240+
private void clear() {
245241
this.alpha = 0;
246242
this.index = 0;
247243
this.blobs.clear();
248244
}
249245

250-
public synchronized List<Blob> getBlobs() {
246+
public List<Blob> getBlobs() {
251247
return blobs;
252248
}
253249

254-
public synchronized int getAlpha() {
250+
public int getAlpha() {
255251
return alpha;
256252
}
257253

258-
public synchronized int getIndex() {
254+
public int getIndex() {
259255
return index;
260256
}
261257
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class BorstUtils {
55
// 23, 48, 153, 214, 250, 255
66
// public static final int[] ALPHAS = { 9, 18, 72, 136, 218, 255 };
77
// public static final int[] ALPHAS = { 23, 48, 153, 214, 250, 255 };
8-
public static final int[] ALPHAS = { 23, 48, 140, 190, 230, 255 };
8+
public static final int[] ALPHAS = { 23, 48, 100, 190, 230, 255 };
99
public static final int[] SIZES = CircleCache.CIRCLE_CACHE_LENGTH; // { 1, 2, 4, 6, 10, 13 };
1010

1111
public static final BorstColor[] COLORS = {
Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.bobrust.generator;
22

3-
import java.util.ArrayList;
4-
import java.util.List;
53
import java.util.Random;
64

75
public class Circle {
@@ -16,10 +14,7 @@ public class Circle {
1614

1715
public Circle(Worker worker) {
1816
this.worker = worker;
19-
Random rnd = worker.rnd;
20-
this.x = rnd.nextInt(worker.w);
21-
this.y = rnd.nextInt(worker.h);
22-
this.r = BorstUtils.SIZES[rnd.nextInt(BorstUtils.SIZES.length)];
17+
this.randomize();
2318
}
2419

2520
public Circle(Worker worker, int x, int y, int r) {
@@ -40,39 +35,20 @@ public void mutateShape() {
4035
x = BorstUtils.clampInt(a, 0, w);
4136
y = BorstUtils.clampInt(b, 0, h);
4237
} else {
43-
int c = BorstUtils.getClosestSize(r + (int)(rnd.nextGaussian() * 32));
38+
int c = BorstUtils.getClosestSize(r + (int)(rnd.nextGaussian() * 16));
4439
r = BorstUtils.clampInt(c, 1, w);
4540
}
4641
}
4742

48-
49-
public Scanline[] getScanlines() {
50-
int w = worker.w;
51-
int h = worker.h;
52-
53-
int cache_index = BorstUtils.getClosestSizeIndex(r);
54-
Scanline[] LINES = CircleCache.CIRCLE_CACHE[cache_index];
55-
int LENGTH = CircleCache.CIRCLE_CACHE_LENGTH[cache_index];
56-
57-
List<Scanline> list = new ArrayList<>(LENGTH);
58-
for (int i = 0; i < LENGTH; i++) {
59-
Scanline line = LINES[i];
60-
int yy = line.y + y;
61-
if (yy < 0) {
62-
continue;
63-
}
64-
65-
if (yy >= h) {
66-
break;
67-
}
68-
69-
int x1 = line.x1 + x;
70-
int x2 = line.x2 + x;
71-
x1 = (x1 < 0) ? 0 : x1;
72-
x2 = (x2 >= w) ? (w - 1):x2;
73-
list.add(new Scanline(yy, x1, x2));
74-
}
75-
76-
return list.toArray(Scanline[]::new);
43+
public void randomize() {
44+
this.x = worker.rnd.nextInt(worker.w);
45+
this.y = worker.rnd.nextInt(worker.h);
46+
this.r = BorstUtils.SIZES[worker.rnd.nextInt(BorstUtils.SIZES.length)];
47+
}
48+
49+
public void fromValues(Circle shape) {
50+
this.r = shape.r;
51+
this.x = shape.x;
52+
this.y = shape.y;
7753
}
7854
}

0 commit comments

Comments
 (0)