Skip to content

Commit bd18639

Browse files
committed
Reduce memory garbage created by MaxiCode symbols
1 parent 45d2531 commit bd18639

File tree

6 files changed

+37
-22
lines changed

6 files changed

+37
-22
lines changed

src/main/java/uk/org/okapibarcode/backend/MaxiCode.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -936,10 +936,14 @@ protected void plotSymbol() {
936936
}
937937

938938
// circles
939-
double[] radii = { 10.85, 8.97, 7.10, 5.22, 3.31, 1.43 };
940-
for (int i = 0; i < radii.length; i++) {
941-
target.add(new Circle(35.76 * m, 35.60 * m, radii[i] * m));
942-
}
939+
double x = 35.76 * m;
940+
double y = 35.60 * m;
941+
target.add(new Circle(x, y, 10.85 * m));
942+
target.add(new Circle(x, y, 8.97 * m));
943+
target.add(new Circle(x, y, 7.10 * m));
944+
target.add(new Circle(x, y, 5.22 * m));
945+
target.add(new Circle(x, y, 3.31 * m));
946+
target.add(new Circle(x, y, 1.43 * m));
943947
}
944948

945949
/** {@inheritDoc} */

src/main/java/uk/org/okapibarcode/graphics/Hexagon.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,26 @@ public final class Hexagon {
3232

3333
public final double centreX;
3434
public final double centreY;
35-
public final double[] pointX = new double[6];
36-
public final double[] pointY = new double[6];
35+
public final int factor;
3736

3837
public Hexagon(double centreX, double centreY, int factor) {
3938
this.centreX = centreX;
4039
this.centreY = centreY;
41-
for (int i = 0; i < 6; i++) {
42-
pointX[i] = centreX + (OFFSET_X[i] * INK_SPREAD * factor);
43-
pointY[i] = centreY + (OFFSET_Y[i] * INK_SPREAD * factor);
40+
this.factor = factor;
41+
}
42+
43+
public double getX(int vertex) {
44+
if (vertex < 0 || vertex > 5) {
45+
throw new IllegalArgumentException("Vertex must be between 0 and 5");
46+
}
47+
return centreX + (OFFSET_X[vertex] * INK_SPREAD * factor);
48+
}
49+
50+
public double getY(int vertex) {
51+
if (vertex < 0 || vertex > 5) {
52+
throw new IllegalArgumentException("Vertex must be between 0 and 5");
4453
}
54+
return centreY + (OFFSET_Y[vertex] * INK_SPREAD * factor);
4555
}
4656

4757
/** {@inheritDoc} */
@@ -51,18 +61,18 @@ public boolean equals(Object other) {
5161
return false;
5262
}
5363
Hexagon h = (Hexagon) other;
54-
return centreX == h.centreX && centreY == h.centreY;
64+
return centreX == h.centreX && centreY == h.centreY && factor == h.factor;
5565
}
5666

5767
/** {@inheritDoc} */
5868
@Override
5969
public int hashCode() {
60-
return Objects.hash(centreX, centreY);
70+
return Objects.hash(centreX, centreY, factor);
6171
}
6272

6373
/** {@inheritDoc} */
6474
@Override
6575
public String toString() {
66-
return "Hexagon[centreX=" + centreX + ", centreY=" + centreY + "]";
76+
return "Hexagon[centreX=" + centreX + ", centreY=" + centreY + ", factor=" + factor + "]";
6777
}
6878
}

src/main/java/uk/org/okapibarcode/output/Java2DRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public void render(Symbol symbol) {
135135
Polygon polygon = new Polygon();
136136
for (Hexagon hexagon : symbol.getHexagons()) {
137137
for (int j = 0; j < 6; j++) {
138-
polygon.addPoint((int) ((hexagon.pointX[j] * magnification) + marginX),
139-
(int) ((hexagon.pointY[j] * magnification) + marginY));
138+
polygon.addPoint((int) ((hexagon.getX(j) * magnification) + marginX),
139+
(int) ((hexagon.getY(j) * magnification) + marginY));
140140
}
141141
g2d.fill(polygon);
142142
polygon.reset();

src/main/java/uk/org/okapibarcode/output/PostScriptRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,8 @@ public void render(Symbol symbol) throws IOException {
229229
// Because MaxiCode size is fixed, this ignores magnification
230230
for (Hexagon hexagon : symbol.getHexagons()) {
231231
for (int j = 0; j < 6; j++) {
232-
writer.append(hexagon.pointX[j] + marginX).append(" ")
233-
.append((height - hexagon.pointY[j]) - marginY).append(" ");
232+
writer.append(hexagon.getX(j) + marginX).append(" ")
233+
.append((height - hexagon.getY(j)) - marginY).append(" ");
234234
}
235235
writer.append(" TH\n");
236236
}

src/main/java/uk/org/okapibarcode/output/SvgRenderer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,8 @@ public void render(Symbol symbol) throws IOException {
201201
} else {
202202
writer.append("L ");
203203
}
204-
writer.append((hexagon.pointX[j] * magnification) + marginX).append(" ")
205-
.append((hexagon.pointY[j] * magnification) + marginY).append(" ");
204+
writer.append((hexagon.getX(j) * magnification) + marginX).append(" ")
205+
.append((hexagon.getY(j) * magnification) + marginY).append(" ");
206206
}
207207
writer.append("Z\" />\n");
208208
}

src/test/java/uk/org/okapibarcode/graphics/HexagonTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package uk.org.okapibarcode.graphics;
1818

19-
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
2019
import static org.junit.jupiter.api.Assertions.assertEquals;
2120
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2221

@@ -33,7 +32,7 @@ public void testHexagon() {
3332
Hexagon hex1 = new Hexagon(1, 2, 1);
3433
assertEquals(1, hex1.centreX, 0.001);
3534
assertEquals(2, hex1.centreY, 0.001);
36-
assertEquals("Hexagon[centreX=1.0, centreY=2.0]", hex1.toString());
35+
assertEquals("Hexagon[centreX=1.0, centreY=2.0, factor=1]", hex1.toString());
3736

3837
Hexagon hex2 = new Hexagon(1, 2, 1);
3938
assertEqual(hex1, hex2);
@@ -49,11 +48,13 @@ public void testHexagon() {
4948
private static void assertEqual(Hexagon hex1, Hexagon hex2) {
5049
assertEquals(hex1.centreX, hex2.centreX);
5150
assertEquals(hex1.centreY, hex2.centreY);
52-
assertArrayEquals(hex1.pointX, hex2.pointX);
53-
assertArrayEquals(hex1.pointY, hex2.pointY);
5451
assertEquals(hex1, hex2);
5552
assertEquals(hex1.hashCode(), hex2.hashCode());
5653
assertEquals(hex1.toString(), hex2.toString());
54+
for (int i = 0; i < 6; i++) {
55+
assertEquals(hex1.getX(i), hex2.getX(i));
56+
assertEquals(hex1.getY(i), hex2.getY(i));
57+
}
5758
}
5859

5960
private static void assertNotEqual(Hexagon hex1, Hexagon hex2) {

0 commit comments

Comments
 (0)