mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2026-02-16 10:30:42 +00:00
Implement stitched image support
This commit is contained in:
@@ -5,6 +5,7 @@ import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.dfsek.terra.addons.image.config.image.ImageTemplate;
|
||||
import com.dfsek.terra.addons.image.config.image.StitchedImageTemplate;
|
||||
import com.dfsek.terra.addons.image.config.sampler.ConstantColorSamplerTemplate;
|
||||
import com.dfsek.terra.addons.image.config.sampler.image.SingleImageColorSamplerTemplate;
|
||||
import com.dfsek.terra.addons.image.config.sampler.image.TileImageColorSamplerTemplate;
|
||||
@@ -47,6 +48,7 @@ public class ImageLibraryAddon implements AddonInitializer {
|
||||
ConfigPack pack = event.getPack();
|
||||
CheckedRegistry<Supplier<ObjectTemplate<Image>>> imageRegistry = pack.getOrCreateRegistry(IMAGE_REGISTRY_KEY);
|
||||
imageRegistry.register(addon.key("BITMAP"), () -> new ImageTemplate(pack.getLoader(), pack));
|
||||
imageRegistry.register(addon.key("STITCHED_BITMAP"), () -> new StitchedImageTemplate(pack.getLoader(), pack));
|
||||
})
|
||||
.then(event -> {
|
||||
CheckedRegistry<Supplier<ObjectTemplate<ColorSampler>>> colorSamplerRegistry = event.getPack().getOrCreateRegistry(
|
||||
|
||||
@@ -0,0 +1,76 @@
|
||||
package com.dfsek.terra.addons.image.config.image;
|
||||
|
||||
import com.dfsek.tectonic.api.config.template.ValidatedConfigTemplate;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Default;
|
||||
import com.dfsek.tectonic.api.config.template.annotations.Value;
|
||||
import com.dfsek.tectonic.api.config.template.object.ObjectTemplate;
|
||||
import com.dfsek.tectonic.api.exception.ValidationException;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import com.dfsek.terra.addons.image.image.Image;
|
||||
import com.dfsek.terra.addons.image.image.StitchedImage;
|
||||
import com.dfsek.terra.api.config.ConfigPack;
|
||||
import com.dfsek.terra.api.config.Loader;
|
||||
|
||||
|
||||
public class StitchedImageTemplate implements ObjectTemplate<Image>, ValidatedConfigTemplate {
|
||||
|
||||
@Value("path-format")
|
||||
private String path;
|
||||
|
||||
@Value("rows")
|
||||
private int rows;
|
||||
|
||||
@Value("columns")
|
||||
private int cols;
|
||||
|
||||
@Value("zero-indexed")
|
||||
@Default
|
||||
private boolean zeroIndexed = false;
|
||||
|
||||
private final Loader files;
|
||||
|
||||
private final ConfigPack pack;
|
||||
|
||||
public StitchedImageTemplate(Loader files, ConfigPack pack) {
|
||||
this.files = files;
|
||||
this.pack = pack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Image get() {
|
||||
Image[][] grid = new Image[rows][cols];
|
||||
for(int i = 0; i < rows; i++) {
|
||||
for(int j = 0; j < cols; j++) {
|
||||
try {
|
||||
grid[i][j] = ImageCache.load(getFormattedPath(i, j), pack, files);
|
||||
} catch(IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
return new StitchedImage(grid, zeroIndexed);
|
||||
}
|
||||
|
||||
private String getFormattedPath(int row, int column) {
|
||||
if (!zeroIndexed) {
|
||||
row++;
|
||||
column++;
|
||||
}
|
||||
return path.replaceFirst("\\{row}", String.valueOf(row)).replaceFirst("\\{column}", String.valueOf(column));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validate() throws ValidationException {
|
||||
if(!path.contains("{row}"))
|
||||
throw new ValidationException("Path format does not contain sequence '{row}'");
|
||||
if(!path.contains("{column}"))
|
||||
throw new ValidationException("Path format does not contain sequence '{column}'");
|
||||
if(rows < 1)
|
||||
throw new ValidationException("Must have at least one row");
|
||||
if(cols < 1)
|
||||
throw new ValidationException("Must have at least one column");
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.dfsek.terra.addons.image.image;
|
||||
|
||||
public class StitchedImage implements Image {
|
||||
|
||||
private final Image[][] images;
|
||||
|
||||
private final int[] rowOffsets, columnOffsets;
|
||||
|
||||
private final int width, height;
|
||||
|
||||
public StitchedImage(Image[][] images, boolean zeroIndexed) throws IllegalArgumentException {
|
||||
int width = 0;
|
||||
int height = 0;
|
||||
int rows = images.length;
|
||||
int columns = images[0].length;
|
||||
this.rowOffsets = new int[rows];
|
||||
this.columnOffsets = new int[columns];
|
||||
for(int i = 0; i < rows; i++) {
|
||||
int rowHeight = images[i][0].getHeight();
|
||||
rowOffsets[i] = height;
|
||||
height += rowHeight;
|
||||
for(int j = 1; j < columns; j++) {
|
||||
if(images[i][j].getHeight() != rowHeight)
|
||||
throw new IllegalArgumentException("Image heights in row " + (i + (zeroIndexed ? 0 : 1)) + " do not match");
|
||||
}
|
||||
}
|
||||
for(int i = 0; i < columns; i++) {
|
||||
int columnWidth = images[0][i].getWidth();
|
||||
columnOffsets[i] = width;
|
||||
width += columnWidth;
|
||||
for(int j = 1; j < rows; j++) {
|
||||
if(images[i][j].getWidth() != columnWidth)
|
||||
throw new IllegalArgumentException("Image widths in column " + (i + (zeroIndexed ? 0 : 1)) + " do not match");
|
||||
}
|
||||
}
|
||||
|
||||
this.width = width;
|
||||
this.height = height;
|
||||
this.images = images;
|
||||
}
|
||||
|
||||
private int getColumn(int x) {
|
||||
for(int i = columnOffsets.length-1; i > 0; i--) {
|
||||
if(x >= columnOffsets[i])
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
private int getRow(int y) {
|
||||
for(int i = rowOffsets.length-1; i > 0; i--) {
|
||||
if(y >= rowOffsets[i])
|
||||
return i;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRGB(int x, int y) {
|
||||
int row = getRow(y);
|
||||
int column = getColumn(x);
|
||||
return images[row][column].getRGB(x-columnOffsets[column], y-rowOffsets[row]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth() {
|
||||
return width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight() {
|
||||
return height;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user