properly implement transfer functions and apply them to the output image
parent
b8aae8c2e5
commit
533461204a
@ -1,135 +0,0 @@
|
||||
package eu.jonahbauer.raytracing.render;
|
||||
|
||||
import eu.jonahbauer.raytracing.render.canvas.Canvas;
|
||||
import eu.jonahbauer.raytracing.render.color.ColorSpaces;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.CheckedOutputStream;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
|
||||
public enum ImageFormat {
|
||||
PPM {
|
||||
@Override
|
||||
public void write(@NotNull Canvas image, @NotNull OutputStream out) throws IOException {
|
||||
try (var writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.US_ASCII))) {
|
||||
writer.write("P3\n");
|
||||
writer.write(String.valueOf(image.getWidth()));
|
||||
writer.write(" ");
|
||||
writer.write(String.valueOf(image.getHeight()));
|
||||
writer.write("\n255\n");
|
||||
|
||||
for (int y = 0; y < image.getHeight(); y++) {
|
||||
for (int x = 0; x < image.getWidth(); x++) {
|
||||
var color = image.getRGB(x, y, ColorSpaces.sRGB);
|
||||
writer.write(String.valueOf(color.red()));
|
||||
writer.write(" ");
|
||||
writer.write(String.valueOf(color.green()));
|
||||
writer.write(" ");
|
||||
writer.write(String.valueOf(color.blue()));
|
||||
writer.write("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
PNG {
|
||||
private static final byte[] MAGIC = new byte[] { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
|
||||
private static final int IHDR_LENGTH = 13;
|
||||
private static final int IHDR_TYPE = 0x49484452;
|
||||
private static final int IDAT_TYPE = 0x49444154;
|
||||
private static final int IEND_TYPE = 0x49454E44;
|
||||
private static final int IEND_CRC = 0xAE426082;
|
||||
|
||||
@Override
|
||||
public void write(@NotNull Canvas image, @NotNull OutputStream out) throws IOException {
|
||||
try (var data = new NoCloseDataOutputStream(out); var _ = data.closeable()) {
|
||||
data.write(MAGIC);
|
||||
|
||||
writeIHDR(image, data);
|
||||
writeIDAT(image, data);
|
||||
writeIEND(image, data);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeIHDR(@NotNull Canvas image, @NotNull DataOutputStream data) throws IOException {
|
||||
data.writeInt(IHDR_LENGTH);
|
||||
try (
|
||||
var crc = new CheckedOutputStream(data, new CRC32());
|
||||
var ihdr = new DataOutputStream(crc)
|
||||
) {
|
||||
ihdr.writeInt(IHDR_TYPE);
|
||||
ihdr.writeInt(image.getWidth()); // image width
|
||||
ihdr.writeInt(image.getHeight()); // image height
|
||||
ihdr.writeByte(8); // bit depth
|
||||
ihdr.writeByte(2); // color type
|
||||
ihdr.writeByte(0); // compression method
|
||||
ihdr.writeByte(0); // filter method
|
||||
ihdr.writeByte(0); // interlace method
|
||||
ihdr.flush();
|
||||
data.writeInt((int) crc.getChecksum().getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeIDAT(@NotNull Canvas image, @NotNull DataOutputStream data) throws IOException {
|
||||
try (
|
||||
var baos = new ByteArrayOutputStream();
|
||||
var crc = new CheckedOutputStream(baos, new CRC32());
|
||||
var idat = new DataOutputStream(crc)
|
||||
) {
|
||||
idat.writeInt(IDAT_TYPE);
|
||||
|
||||
try (var deflate = new DataOutputStream(new DeflaterOutputStream(idat))) {
|
||||
for (int y = 0; y < image.getHeight(); y++) {
|
||||
deflate.writeByte(0); // filter type
|
||||
for (int x = 0; x < image.getWidth(); x++) {
|
||||
var pixel = image.getRGB(x, y, ColorSpaces.sRGB);
|
||||
deflate.writeByte(pixel.red());
|
||||
deflate.writeByte(pixel.green());
|
||||
deflate.writeByte(pixel.blue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var bytes = baos.toByteArray();
|
||||
data.writeInt(bytes.length - 4); // don't include type in length
|
||||
data.write(bytes);
|
||||
data.writeInt((int) crc.getChecksum().getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeIEND(@NotNull Canvas image, @NotNull DataOutputStream data) throws IOException {
|
||||
data.writeInt(0);
|
||||
data.writeInt(IEND_TYPE);
|
||||
data.writeInt(IEND_CRC);
|
||||
}
|
||||
|
||||
private static class NoCloseDataOutputStream extends DataOutputStream {
|
||||
public NoCloseDataOutputStream(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public Closeable closeable() {
|
||||
return super::close;
|
||||
}
|
||||
}
|
||||
},
|
||||
;
|
||||
|
||||
public void write(@NotNull Canvas image, @NotNull Path path) throws IOException {
|
||||
try (var out = Files.newOutputStream(path)) {
|
||||
write(image, out);
|
||||
}
|
||||
}
|
||||
|
||||
public abstract void write(@NotNull Canvas image, @NotNull OutputStream out) throws IOException;
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package eu.jonahbauer.raytracing.render.color;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface TransferFunction {
|
||||
@NotNull ColorRGB decode(@NotNull ColorRGB rgb);
|
||||
@NotNull ColorRGB encode(@NotNull ColorRGB rgb);
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package eu.jonahbauer.raytracing.render.color;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public final class TransferFunctions {
|
||||
public static final @NotNull TransferFunction sRGB = new ComponentTransferFunction() {
|
||||
@Override
|
||||
protected double encode(double value) {
|
||||
if (value <= 0.0031308) return 12.92 * value;
|
||||
return 1.055 * Math.pow(value, 1. / 2.4) - 0.055;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected double decode(double value) {
|
||||
if (value <= 0.04045) return value / 12.92;
|
||||
return Math.pow((value + 0.055) / 1.055, 2.4d);
|
||||
}
|
||||
};
|
||||
|
||||
public static final @NotNull TransferFunction LINEAR = new TransferFunction() {
|
||||
@Override
|
||||
public @NotNull ColorRGB encode(@NotNull ColorRGB rgb) {
|
||||
return rgb;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull ColorRGB decode(@NotNull ColorRGB rgb) {
|
||||
return rgb;
|
||||
}
|
||||
};
|
||||
|
||||
private TransferFunctions() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
private abstract static class ComponentTransferFunction implements TransferFunction {
|
||||
@Override
|
||||
public final @NotNull ColorRGB decode(@NotNull ColorRGB rgb) {
|
||||
return new ColorRGB(decode(rgb.r()), decode(rgb.g()), decode(rgb.b()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public final @NotNull ColorRGB encode(@NotNull ColorRGB rgb) {
|
||||
return new ColorRGB(encode(rgb.r()), encode(rgb.g()), encode(rgb.b()));
|
||||
}
|
||||
|
||||
protected abstract double encode(double value);
|
||||
protected abstract double decode(double value);
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
package eu.jonahbauer.raytracing.render.image;
|
||||
|
||||
import eu.jonahbauer.raytracing.render.canvas.Canvas;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
|
||||
public interface ImageWriter {
|
||||
default void write(@NotNull Canvas image, @NotNull Path path) throws IOException {
|
||||
try (var out = Files.newOutputStream(path)) {
|
||||
write(image, out);
|
||||
}
|
||||
}
|
||||
|
||||
void write(@NotNull Canvas canvas, @NotNull OutputStream out) throws IOException;
|
||||
}
|
@ -0,0 +1,107 @@
|
||||
package eu.jonahbauer.raytracing.render.image;
|
||||
|
||||
import eu.jonahbauer.raytracing.render.canvas.Canvas;
|
||||
import eu.jonahbauer.raytracing.render.color.ColorSpace;
|
||||
import eu.jonahbauer.raytracing.render.color.ColorSpaces;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.*;
|
||||
import java.util.Objects;
|
||||
import java.util.zip.CRC32;
|
||||
import java.util.zip.CheckedOutputStream;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
|
||||
public class PNGImageWriter implements ImageWriter {
|
||||
public static final @NotNull PNGImageWriter sRGB = new PNGImageWriter(ColorSpaces.sRGB);
|
||||
|
||||
private static final byte[] MAGIC = new byte[] { (byte) 0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A };
|
||||
private static final int IHDR_LENGTH = 13;
|
||||
private static final int IHDR_TYPE = 0x49484452;
|
||||
private static final int IDAT_TYPE = 0x49444154;
|
||||
private static final int IEND_TYPE = 0x49454E44;
|
||||
private static final int IEND_CRC = 0xAE426082;
|
||||
|
||||
private final @NotNull ColorSpace cs;
|
||||
|
||||
public PNGImageWriter(@NotNull ColorSpace cs) {
|
||||
this.cs = Objects.requireNonNull(cs, "cs");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(@NotNull Canvas image, @NotNull OutputStream out) throws IOException {
|
||||
try (var data = new NoCloseDataOutputStream(out); var _ = data.closeable()) {
|
||||
data.write(MAGIC);
|
||||
|
||||
writeIHDR(image, data);
|
||||
writeIDAT(image, data);
|
||||
writeIEND(image, data);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeIHDR(@NotNull Canvas image, @NotNull DataOutputStream data) throws IOException {
|
||||
data.writeInt(IHDR_LENGTH);
|
||||
try (
|
||||
var crc = new CheckedOutputStream(data, new CRC32());
|
||||
var ihdr = new DataOutputStream(crc)
|
||||
) {
|
||||
ihdr.writeInt(IHDR_TYPE);
|
||||
ihdr.writeInt(image.getWidth()); // image width
|
||||
ihdr.writeInt(image.getHeight()); // image height
|
||||
ihdr.writeByte(8); // bit depth
|
||||
ihdr.writeByte(2); // color type
|
||||
ihdr.writeByte(0); // compression method
|
||||
ihdr.writeByte(0); // filter method
|
||||
ihdr.writeByte(0); // interlace method
|
||||
ihdr.flush();
|
||||
data.writeInt((int) crc.getChecksum().getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeIDAT(@NotNull Canvas image, @NotNull DataOutputStream data) throws IOException {
|
||||
try (
|
||||
var baos = new ByteArrayOutputStream();
|
||||
var crc = new CheckedOutputStream(baos, new CRC32());
|
||||
var idat = new DataOutputStream(crc)
|
||||
) {
|
||||
idat.writeInt(IDAT_TYPE);
|
||||
|
||||
try (var deflate = new DataOutputStream(new DeflaterOutputStream(idat))) {
|
||||
for (int y = 0; y < image.getHeight(); y++) {
|
||||
deflate.writeByte(0); // filter type
|
||||
for (int x = 0; x < image.getWidth(); x++) {
|
||||
var pixel = cs.encode(image.getRGB(x, y, cs));
|
||||
deflate.writeByte(pixel.red());
|
||||
deflate.writeByte(pixel.green());
|
||||
deflate.writeByte(pixel.blue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var bytes = baos.toByteArray();
|
||||
data.writeInt(bytes.length - 4); // don't include type in length
|
||||
data.write(bytes);
|
||||
data.writeInt((int) crc.getChecksum().getValue());
|
||||
}
|
||||
}
|
||||
|
||||
private void writeIEND(@NotNull Canvas image, @NotNull DataOutputStream data) throws IOException {
|
||||
data.writeInt(0);
|
||||
data.writeInt(IEND_TYPE);
|
||||
data.writeInt(IEND_CRC);
|
||||
}
|
||||
|
||||
private static class NoCloseDataOutputStream extends DataOutputStream {
|
||||
public NoCloseDataOutputStream(OutputStream out) {
|
||||
super(out);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
public Closeable closeable() {
|
||||
return super::close;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,46 @@
|
||||
package eu.jonahbauer.raytracing.render.image;
|
||||
|
||||
import eu.jonahbauer.raytracing.render.canvas.Canvas;
|
||||
import eu.jonahbauer.raytracing.render.color.ColorSpace;
|
||||
import eu.jonahbauer.raytracing.render.color.ColorSpaces;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.OutputStream;
|
||||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Objects;
|
||||
|
||||
public class PPMImageWriter implements ImageWriter {
|
||||
public static final PPMImageWriter sRGB = new PPMImageWriter(ColorSpaces.sRGB);
|
||||
|
||||
private final @NotNull ColorSpace cs;
|
||||
|
||||
public PPMImageWriter(@NotNull ColorSpace cs) {
|
||||
this.cs = Objects.requireNonNull(cs, "cs");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void write(@NotNull Canvas image, @NotNull OutputStream out) throws IOException {
|
||||
try (var writer = new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.US_ASCII))) {
|
||||
writer.write("P3\n");
|
||||
writer.write(String.valueOf(image.getWidth()));
|
||||
writer.write(" ");
|
||||
writer.write(String.valueOf(image.getHeight()));
|
||||
writer.write("\n255\n");
|
||||
|
||||
for (int y = 0; y < image.getHeight(); y++) {
|
||||
for (int x = 0; x < image.getWidth(); x++) {
|
||||
var color = cs.encode(image.getRGB(x, y, cs));
|
||||
writer.write(String.valueOf(color.red()));
|
||||
writer.write(" ");
|
||||
writer.write(String.valueOf(color.green()));
|
||||
writer.write(" ");
|
||||
writer.write(String.valueOf(color.blue()));
|
||||
writer.write("\n");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue