package to.etc.domui.test.ui.imagehelper;

import org.junit.Assert;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;

/**
 * @author <a href="mailto:jal@etc.to">Frits Jalvingh</a>
 * Created on 3-9-17.
 */
public class TestImageHelper {
	@Test
	public void testUnborderedInput() throws Exception {
		String baseName = "/tmp/" + getClass().getSimpleName() + "_testUnborderedInput";

		BufferedImage srcBi;
		try(InputStream is = getClass().getResourceAsStream("input-1.png")) {
			srcBi = ImageIO.read(is);
		}
		ByteImage bi = ByteImage.create(srcBi);
		ByteImage borderBi = bi.stripBorder();

		int[] bl = borderBi.findFontBaselines();

		int sy = borderBi.getRootLocation().y + bl[0];
		int ey = borderBi.getRootLocation().y + bl[1];

		BufferedImage outBi = borderBi.save();
		ImageIO.write(outBi, "png", new File(baseName + "-bw.png"));

		Graphics2D graphics = (Graphics2D) srcBi.getGraphics();
		graphics.setStroke(new BasicStroke(1));
		graphics.setColor(Color.RED);
		graphics.drawLine(0, ey, srcBi.getWidth()-1, ey);
		graphics.dispose();

		ImageIO.write(srcBi, "png", new File(baseName + "-baseline.png"));
		Assert.assertEquals("Baseline should be correct", 11, ey);

	}

	@Test
	public void testBorderedInput() throws Exception {
		String baseName = "/tmp/" + getClass().getSimpleName() + "_testBorderedInput";

		BufferedImage srcBi;
		try(InputStream is = getClass().getResourceAsStream("input-2.png")) {
			srcBi = ImageIO.read(is);
		}
		ByteImage bi = ByteImage.create(srcBi);
		ByteImage borderBi = bi.stripBorder();

		int[] bl = borderBi.findFontBaselines();

		int sy = borderBi.getRootLocation().y + bl[0];
		int ey = borderBi.getRootLocation().y + bl[1];

		BufferedImage outBi = borderBi.save();
		ImageIO.write(outBi, "png", new File(baseName + "-bw.png"));

		Graphics2D graphics = (Graphics2D) srcBi.getGraphics();
		graphics.setStroke(new BasicStroke(1));
		graphics.setColor(Color.RED);
		graphics.drawLine(0, ey, srcBi.getWidth()-1, ey);
		graphics.dispose();

		ImageIO.write(srcBi, "png", new File(baseName + "-baseline.png"));
		Assert.assertEquals("Baseline should be correct", 13, ey);
	}




}

@@ -395,32 +395,34 @@
 s Exception %7B%0A%09%09
+//
 String baseName 
@@ -834,32 +834,89 @@
 n().y + bl%5B1%5D;%0A%0A
+%09%09File f1 = File.createTempFile(%22ubborder-%22, %22-bw.png%22);%0A
 %09%09BufferedImage 
@@ -974,38 +974,10 @@
 g%22, 
-new File(baseName + %22-bw.png%22)
+f1
 );%0A%0A
@@ -1189,72 +1189,101 @@
 %0A%0A%09%09
-ImageIO.write(srcBi, %22png%22, new File(baseName + %22-baseline.png%22)
+File f2 = File.createTempFile(%22unborder-%22, %22-baseline.png%22);%0A%09%09ImageIO.write(srcBi, %22png%22, f2
 );%0A%09
@@ -1341,16 +1341,46 @@
 11, ey);
+%0A%09%09f1.delete();%0A%09%09f2.delete();
 %0A%0A%09%7D%0A%0A%09@
@@ -1438,16 +1438,18 @@
 ion %7B%0A%09%09
+//
 String b
@@ -1922,66 +1922,97 @@
 ;%0A%09%09
-ImageIO.write(outBi, %22png%22, new File(baseName + %22-bw.png%22)
+File f1 = File.createTempFile(%22testborder-%22, %22-bw.png%22);%0A%09%09ImageIO.write(outBi, %22png%22, f1
 );%0A%0A
@@ -2224,72 +2224,103 @@
 %0A%0A%09%09
-ImageIO.write(srcBi, %22png%22, new File(baseName + %22-baseline.png%22)
+File f2 = File.createTempFile(%22testborder-%22, %22-baseline.png%22);%0A%09%09ImageIO.write(srcBi, %22png%22, f2
 );%0A%09
@@ -2379,16 +2379,47 @@
 3, ey);%0A
+%09%09f1.delete();%0A%09%09f2.delete();%0A%0A
 %09%7D%0A%0A%0A%0A%0A%7D

Fix direct access to /tmp.