Adding generate subcommand. #16.

This commit is contained in:
Rob Greene
2018-06-19 20:09:39 -05:00
parent 590314877b
commit 707b567b96
6 changed files with 156 additions and 9 deletions
@@ -12,6 +12,7 @@ import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import io.github.applecommander.bastools.api.utils.Streams;
@@ -59,16 +60,32 @@ public class ShapeTable {
public void write(OutputStream outputStream) throws IOException {
Objects.requireNonNull(outputStream);
// TODO
// Header
outputStream.write(shapes.size());
outputStream.write(0);
// Collect each shape
List<byte[]> data = this.shapes.stream()
.map(Shape::toVector)
.map(VectorShape::toBytes)
.collect(Collectors.toList());
// Build offset table
int offset = 2 + 2*data.size();
for (byte[] d : data) {
outputStream.write(offset & 0xff);
outputStream.write(offset >> 8);
offset += d.length;
}
// Write shape data
for (byte[] d : data) {
outputStream.write(d);
}
}
public void write(File file) throws IOException {
Objects.requireNonNull(file);
try (OutputStream outputStream = new FileOutputStream(file)) {
write(file);
}
}
public void write(Path path) throws IOException {
Objects.requireNonNull(path);
write(path.toFile());
@@ -1,11 +1,13 @@
package io.github.applecommander.bastools.api.shapes;
import java.io.ByteArrayOutputStream;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Queue;
public class VectorShape implements Shape {
@@ -97,6 +99,45 @@ public class VectorShape implements Shape {
}
}
public byte[] toBytes() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
LinkedList<VectorCommand> work = new LinkedList<>(vectors);
while (!work.isEmpty()) {
int section1 = work.remove().ordinal();
VectorCommand vector2 = work.poll();
int section2 = Optional.ofNullable(vector2).map(VectorCommand::ordinal).orElse(0);
VectorCommand vector3 = work.poll();
if (vector3 != null && vector3.plot) {
work.add(0, vector3);
vector3 = null;
}
int section3 = Optional.ofNullable(vector3).map(VectorCommand::ordinal).orElse(0);
if ((section1 + section2 + section3) == 0) {
// Cannot write a 0 byte except at end
if (vector2 == null) {
section2 = VectorCommand.MOVE_LEFT.ordinal();
} else if (vector3 == null) {
section3 = VectorCommand.MOVE_LEFT.ordinal();
} else {
section3 = VectorCommand.MOVE_LEFT.ordinal();
if (!work.isEmpty()) {
work.add(0, VectorCommand.MOVE_RIGHT);
}
}
} else if (vector3 == VectorCommand.MOVE_UP) {
// section 3 cannot be 0
work.add(0, vector3);
if (vector2 == VectorCommand.MOVE_UP) {
// section 2 and 3 cannot be 0
work.add(0, vector2);
}
}
outputStream.write(section3 << 6 | section2 << 3 | section1);
}
outputStream.write(0);
return outputStream.toByteArray();
}
@Override
public boolean isEmpty() {
return vectors.isEmpty();
@@ -52,7 +52,7 @@ public class TextShapeExporter implements ShapeExporter {
int width = blist.stream().mapToInt(BitmapShape::getWidth).max().getAsInt();
int height = blist.stream().mapToInt(BitmapShape::getHeight).max().getAsInt();
int columns = Math.max(1, this.maxWidth / width);
int columns = Math.min(Math.max(1, this.maxWidth / width), blist.size());
PrintWriter pw = new PrintWriter(outputStream);
drawTopLine(pw, columns, width);