Commit f673fd06 authored by Andreas König's avatar Andreas König
Browse files

Correct brightness

parent 279dd385
package ch.bergturbenthal.display.test.display.impl;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.BufferedImageOp;
import java.awt.image.ByteLookupTable;
import java.awt.image.LookupOp;
import java.awt.image.LookupTable;
import java.awt.image.Raster;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
......@@ -18,14 +23,34 @@ import lombok.Cleanup;
public class RowDisplay implements Display {
private static final int WIDTH = 32 * 4;
private static final int HEIGHT = 32 * 2;
private final SocketAddress address;
private static final int WIDTH = 32 * 4;
private static final int HEIGHT = 32 * 2;
private final SocketAddress address;
private final BufferedImageOp colorOperation = createColorOp(0.3f);
public RowDisplay(final SocketAddress address) {
this.address = address;
}
private BufferedImageOp createColorOp(final float brightness) {
if (brightness < 0 || brightness > 1) {
throw new IllegalArgumentException("Brightness " + brightness + " is out of range (0<=value<=1)");
}
final byte[][] data = new byte[3][];
final byte[] greyLookup = new byte[256];
for (int i = 0; i < data.length; i++) {
data[i] = greyLookup;
}
for (int i = 0; i < 256; i++) {
final byte value = (byte) (i * brightness);
greyLookup[i] = value;
}
final LookupTable lookup = new ByteLookupTable(0, data);
final BufferedImageOp op = new LookupOp(lookup, new RenderingHints(null));
return op;
}
@Override
public DrawContext createContext() {
final BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
......@@ -55,6 +80,7 @@ public class RowDisplay implements Display {
final DatagramSocket datagramSocket = new DatagramSocket();
final DatagramPacket packet = new DatagramPacket(data, data.length, address);
datagramSocket.send(packet);
graphics.clearRect(0, 0, WIDTH, HEIGHT);
} catch (final IOException e) {
throw new RuntimeException("Cannot show content", e);
}
......@@ -66,9 +92,10 @@ public class RowDisplay implements Display {
private BufferedImage transformImage(final BufferedImage image) {
final BufferedImage outImage = new BufferedImage(8 * 32, 32, BufferedImage.TYPE_INT_RGB);
final Graphics2D outGraphics = outImage.createGraphics();
outGraphics.drawImage(image, 4 * 32, -32, null);
outGraphics.drawImage(image, colorOperation, 4 * 32, -32);
// outGraphics.drawImage(image, 4 * 32, -32, null);
outGraphics.transform(AffineTransform.getQuadrantRotateInstance(2, image.getWidth() / 2, image.getHeight() / 2));
outGraphics.drawImage(image, 0, 32, null);
outGraphics.drawImage(image, colorOperation, 0, 32);
return outImage;
}
......
......@@ -6,44 +6,49 @@ import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.time.Instant;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Collections;
import javax.imageio.ImageIO;
import ch.bergturbenthal.display.test.display.DrawContext;
import ch.bergturbenthal.display.test.display.impl.RowDisplay;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class TestCreatePPM {
public static void main(final String[] args) throws IOException {
public static void main(final String[] args) throws IOException, InterruptedException {
final BufferedImage image = new BufferedImage(4 * 32, 2 * 32, BufferedImage.TYPE_INT_RGB);
final RowDisplay display = new RowDisplay(new InetSocketAddress(InetAddress.getByName("192.168.1.51"), 1337));
final DrawContext drawContext = display.createContext();
final Graphics2D graphics = drawContext.getGraphics();
final String text = "HelloWorld";
graphics.setFont(new Font(Font.SERIF, 0, 19));
graphics.setColor(Color.BLUE.darker().darker().darker());
graphics.setRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
final FontMetrics fontMetrics = graphics.getFontMetrics();
final int width = fontMetrics.stringWidth(text);
final int height = fontMetrics.getHeight();
final int x = image.getWidth() / 2 - width / 2;
final int y = image.getHeight() / 2 + height / 2 - fontMetrics.getDescent();
graphics.fillRect(0, y - fontMetrics.getAscent(), image.getWidth(), fontMetrics.getHeight());
graphics.setColor(Color.WHITE);
graphics.drawString(text, x, y);
final BufferedImage image2 = ImageIO.read(new File("/tmp/IMG_20170428_090123.jpg"));
graphics.drawImage(image2, 0, 0, drawContext.getWidth(), drawContext.getHeight(), null);
final DateTimeFormatter formatter = DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM);
for (int i = 0; i < 100000; i++) {
final LocalTime time = Instant.now().atZone(ZoneId.systemDefault()).toLocalTime();
final String text = formatter.format(time);
graphics.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 25));
graphics.setColor(Color.BLUE);
graphics.setRenderingHints(Collections.singletonMap(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON));
final FontMetrics fontMetrics = graphics.getFontMetrics();
final int width = fontMetrics.stringWidth(text);
final int height = fontMetrics.getHeight();
final int x = image.getWidth() / 2 - width / 2;
final int y = image.getHeight() / 2 + height / 2 - fontMetrics.getDescent();
graphics.fillRect(0, y - fontMetrics.getAscent(), image.getWidth(), fontMetrics.getHeight());
graphics.setColor(Color.WHITE);
graphics.drawString(text, x, y);
drawContext.show();
Thread.sleep(100);
}
drawContext.show();
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment