forked from chunky-dev/chunky-denoiser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOidnBinaryDenoiser.java
More file actions
111 lines (95 loc) · 3.96 KB
/
OidnBinaryDenoiser.java
File metadata and controls
111 lines (95 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package de.lemaik.chunky.denoiser;
import de.lemaik.chunky.denoiser.pfm.PortableFloatMap;
import se.llbit.chunky.PersistentSettings;
import se.llbit.log.Log;
import java.io.*;
import java.nio.ByteOrder;
import java.util.ArrayList;
import java.util.List;
public class OidnBinaryDenoiser implements Denoiser {
private String oidnPath;
public OidnBinaryDenoiser() {
loadPath();
}
public void setOidnPath(String oidnPath) {
this.oidnPath = oidnPath;
}
public void loadPath() {
setOidnPath(PersistentSettings.settings.getString("oidnPath", ""));
}
@Override
public void init() {
this.loadPath();
}
@Override
public float[] denoise(int width, int height, float[] beauty, float[] albedo, float[] normal)
throws DenoisingFailedException{
File beautyFile;
File outputFile;
File albedoFile = null;
File normalFile = null;
try {
beautyFile = File.createTempFile("chunky-denoiser", ".beauty.pfm");
beautyFile.deleteOnExit();
outputFile = File.createTempFile("chunky-denoiser", ".denoised.pfm");
outputFile.deleteOnExit();
if (albedo != null) {
albedoFile = File.createTempFile("chunky-denoiser", ".albedo.pfm");
albedoFile.deleteOnExit();
}
if (normal != null) {
normalFile = File.createTempFile("chunky-denoiser", ".normal.pfm");
normalFile.deleteOnExit();
}
} catch (IOException e) {
throw new DenoisingFailedException("Failed to create temporary files.", e);
}
try {
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(beautyFile))) {
PortableFloatMap.writeImage(beauty, width, height, ByteOrder.LITTLE_ENDIAN, os);
}
if (albedo != null) {
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(albedoFile))) {
PortableFloatMap.writeImage(albedo, width, height, ByteOrder.LITTLE_ENDIAN, os);
}
}
if (normal != null) {
try (OutputStream os = new BufferedOutputStream(new FileOutputStream(normalFile))) {
PortableFloatMap.writeImage(normal, width, height, ByteOrder.LITTLE_ENDIAN, os);
}
}
callDenoise(oidnPath, beautyFile, albedoFile, normalFile, outputFile);
try (InputStream is = new BufferedInputStream(new FileInputStream(outputFile))) {
return PortableFloatMap.readImage(is);
}
} catch (IOException | InterruptedException e) {
throw new DenoisingFailedException(e);
} finally {
if (!beautyFile.delete()) Log.warnf("Could not delete file: %s", beautyFile.getAbsolutePath());
if (!outputFile.delete()) Log.warnf("Could not delete file: %s", outputFile.getAbsolutePath());
if (albedoFile != null && !albedoFile.delete()) Log.warnf("Could not delete file: %s", albedoFile.getAbsolutePath());
if (normalFile != null && !normalFile.delete()) Log.warnf("Could not delete file: %s", normalFile.getAbsolutePath());
}
}
private static void callDenoise(String oidnPath, File beauty, File albedo, File normal, File output)
throws IOException, InterruptedException {
List<String> args = new ArrayList<>();
args.add(oidnPath);
args.add("-ldr");
args.add(beauty.getAbsolutePath());
if (albedo != null) {
args.add("-alb");
args.add(albedo.getAbsolutePath());
}
if (normal != null) {
args.add("-nrm");
args.add(normal.getAbsolutePath());
}
args.add("-o");
args.add(output.getAbsolutePath());
new ProcessBuilder().command(args)
.inheritIO()
.start()
.waitFor();
}
}