Skip to content

Commit

Permalink
refactor(Avatar): add test
Browse files Browse the repository at this point in the history
  • Loading branch information
Dituon committed May 6, 2024
1 parent 2ba9487 commit 00d6373
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/main/java/moe/dituon/petpet/share/element/FrameInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

import lombok.Getter;
import lombok.Setter;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;

import java.awt.image.BufferedImage;

@Setter
@Getter
public class FrameInfo {
public int index;
public int canvasWidth;
public int canvasHeight;
public float multiple;
public float multiple = 1.0F;

public FrameInfo(int index, int canvasWidth, int canvasHeight){
this.index = index;
this.canvasWidth = canvasWidth;
this.canvasHeight = canvasHeight;
}

@NotNull
@Contract(value = "_, _ -> new", pure = true)
public static FrameInfo fromImage(BufferedImage image, int index){
return new FrameInfo(image.getWidth(), image.getWidth(), index);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ public class AvatarDeformModel extends AvatarModel {
Supplier<List<BufferedImage>> imageSupplier,
PositionP4ACollection pos
) {
super(data, imageSupplier);
super(data, imageSupplier, false);
this.pos = pos;
buildImage();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public abstract class AvatarModel implements TemplateElement {
protected final List<AvatarFilter> filterList;

public AvatarModel(AvatarData data, Supplier<List<BufferedImage>> extraData) {
this(data, extraData, true);
}

public AvatarModel(AvatarData data, Supplier<List<BufferedImage>> extraData, boolean initFlag) {
imageList = extraData.get();
type = data.getType();
posType = data.getPosType();
Expand All @@ -46,15 +50,15 @@ public AvatarModel(AvatarData data, Supplier<List<BufferedImage>> extraData) {
onTop = data.getAvatarOnTop();
antialias = Boolean.TRUE.equals(data.getAntialias());
resampling = Boolean.TRUE.equals(data.getResampling());
buildImage();
if (initFlag) buildImage();
}

@Override
public TemplateElement.Type getElementType() {
return TemplateElement.Type.AVATAR;
}

private void buildImage() {
protected void buildImage() {
if (getPosLength() > 1 && !filterList.isEmpty()
&& filterList.stream().anyMatch(AvatarFilter::hasAnimation)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import java.util.function.Supplier;

class AvatarXYWHModel extends AvatarModel {
final PositionXYWHCollection pos;
PositionDynamicData dynamicData;
protected final PositionXYWHCollection pos;
protected PositionDynamicData dynamicData;

AvatarXYWHModel(
AvatarData data,
Supplier<List<BufferedImage>> imageSupplier,
PositionXYWHCollection pos
) {
super(data, imageSupplier);
super(data, imageSupplier, false);
this.pos = pos;
if (pos.isDynamical()) {
var firstImg = super.imageList.get(0);
Expand All @@ -33,6 +33,7 @@ class AvatarXYWHModel extends AvatarModel {
firstImg.getHeight()
);
}
buildImage();
}

@Override
Expand Down Expand Up @@ -92,6 +93,7 @@ public void draw(Graphics2D g2d, FrameInfo info) {

if (angle == 0) {
g2d.drawImage(newAvatarImage, x, y, w, h, null);
System.out.println("x: " + x + ", y: " + y + ", w: " + w + ", h: " + h);
return;
}

Expand Down
68 changes: 68 additions & 0 deletions src/test/java/moe/dituon/petpet/core/AvatarTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package moe.dituon.petpet.core;

import kotlinx.serialization.json.Json;
import kotlinx.serialization.json.JsonArray;
import moe.dituon.petpet.share.AvatarData;
import moe.dituon.petpet.share.AvatarPosType;
import moe.dituon.petpet.share.AvatarType;
import moe.dituon.petpet.share.FitType;
import moe.dituon.petpet.share.element.FrameInfo;
import moe.dituon.petpet.share.element.avatar.AvatarFactory;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class AvatarTest {
public static final String outputDir = ".test-output/avatar/";
static final FitType[] textFits = FitType.values();

public static void saveImage(BufferedImage image, String name) throws IOException {
var path = outputDir + name + ".png";
var file = new File(path);
if (!file.exists()) {
file.mkdirs();
file.createNewFile();
}
ImageIO.write(image, "png", file);
}

@Test
public void testZoom() throws IOException {
var data = new AvatarData(AvatarType.TO);
data.setPosType(AvatarPosType.ZOOM);
data.setPos(jsonArrayFromString("[0, 0, 200, 200]"));
var builder = new AvatarFactory(data);
var avatarRaw = ImageIO.read(new File("example-data/input/avatar1.png"));

var image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
var g2d = image.createGraphics();

var avatar = builder.build(() -> List.of(avatarRaw));
avatar.draw(g2d, FrameInfo.fromImage(image, 0));
saveImage(image, "avatarZoom");
}

@Test
public void testDeform() throws IOException {
var data = new AvatarData(AvatarType.TO);
data.setPosType(AvatarPosType.DEFORM);
data.setPos(jsonArrayFromString("[[0,0],[0,200],[200,200],[200,0],[0,0]]"));
var builder = new AvatarFactory(data);
var avatarRaw = ImageIO.read(new File("example-data/input/avatar1.png"));

var image = new BufferedImage(200, 200, BufferedImage.TYPE_INT_ARGB);
var g2d = image.createGraphics();

var avatar = builder.build(() -> List.of(avatarRaw));
avatar.draw(g2d, FrameInfo.fromImage(image, 0));
saveImage(image, "avatarDeform");
}

static JsonArray jsonArrayFromString(String str) {
return Json.Default.decodeFromString(JsonArray.Companion.serializer(), str);
}
}
4 changes: 2 additions & 2 deletions src/test/java/moe/dituon/petpet/core/TextTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
import java.util.List;

public class TextTest {
public static TextAlign[] testAligns = TextAlign.values();
public static final String outputDir = ".test-output/text/";
public static TextBaseline[] testBaselines = new TextBaseline[]{
public static final TextAlign[] testAligns = TextAlign.values();
public static final TextBaseline[] testBaselines = new TextBaseline[]{
TextBaseline.TOP,
TextBaseline.MIDDLE,
TextBaseline.BOTTOM
Expand Down

0 comments on commit 00d6373

Please sign in to comment.