-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOIDNHandler.m
122 lines (89 loc) · 3.27 KB
/
OIDNHandler.m
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
112
113
114
115
116
117
118
119
120
121
122
//
// OIDNHandler.m
// Pathtracer
//
// Created by Adellar Irankunda on 5/8/24.
//
#import "OIDNHandler.h"
//#import <Foundation/Foundation.h>
#import "OpenImageDenoise/oidn.h"
@implementation OIDNHandler
OIDNDevice device;
OIDNBuffer colorBuff;
OIDNBuffer albedoBuff;
OIDNBuffer normalBuff;
OIDNFilter filter;
size_t bufferSize;
- (void) InitDevice {
//printf("hello from objc");
//NSLog(@"something being called here");
const char* errorMessage;
device = oidnNewDevice(OIDN_DEVICE_TYPE_DEFAULT);
if (oidnGetDeviceError(device, &errorMessage) != OIDN_ERROR_NONE)
NSLog(@"Error: %s\n", errorMessage);
oidnCommitDevice(device);
if (oidnGetDeviceError(device, &errorMessage) != OIDN_ERROR_NONE)
NSLog(@"Error: %s\n", errorMessage);
filter = oidnNewFilter(device, "RT");
}
- (void) Release {
/*
oidnReleaseBuffer(colorBuf);
oidnReleaseFilter(filter);
oidnReleaseDevice(device);
*/
}
- (void) SetBeauty: (const float*)inputBeauty {
bufferSize = 1024 * 512 * 3 * sizeof(float);
colorBuff = oidnNewBuffer(device, bufferSize);
oidnSetFilterImage(filter, "color", colorBuff, OIDN_FORMAT_FLOAT3, 512, 1024, 0, 0, 0);
oidnSetFilterImage(filter, "output", colorBuff, OIDN_FORMAT_FLOAT3, 512, 1024, 0, 0, 0);
oidnSetFilterBool(filter, "hdr", true);
float* colorPtr = (float*)oidnGetBufferData(colorBuff);
for (int a=0; a<1024 * 512; a++)
{
//discard the alpha component by offsetting by 4 in the input array
colorPtr[a * 3] = inputBeauty[a * 4];
colorPtr[a * 3 + 1] = inputBeauty[a * 4 + 1];
colorPtr[a * 3 + 2] = inputBeauty[a * 4 + 2];
}
}
- (void) SetAlbedo: (const float*)inputAlbedo {
albedoBuff = oidnNewBuffer(device, bufferSize);
oidnSetFilterImage(filter, "albedo", albedoBuff, OIDN_FORMAT_FLOAT3, 512, 1024, 0, 0, 0);
float* albedoPtr = (float*)oidnGetBufferData(albedoBuff);
for (int a=0; a<1024 * 512; a++)
{
//discard the alpha component by offsetting by 4 in the input array
albedoPtr[a * 3] = inputAlbedo[a * 4];
albedoPtr[a * 3 + 1] = inputAlbedo[a * 4 + 1];
albedoPtr[a * 3 + 2] = inputAlbedo[a * 4 + 2];
}
}
- (void) SetNormal: (const float*)inputNormal {
normalBuff = oidnNewBuffer(device, bufferSize);
oidnSetFilterImage(filter, "normal", normalBuff, OIDN_FORMAT_FLOAT3, 512, 1024, 0, 0, 0);
float* normalPtr = (float*)oidnGetBufferData(normalBuff);
for (int a=0; a<1024 * 512; a++)
{
//discard the alpha component by offsetting by 4 in the input array
normalPtr[a * 3] = inputNormal[a * 4];
normalPtr[a * 3 + 1] = inputNormal[a * 4 + 1];
normalPtr[a * 3 + 2] = inputNormal[a * 4 + 2];
}
}
- (float*) Denoise{
oidnCommitFilter(filter);
oidnExecuteFilter(filter);
float* beautyPtr = (float*)oidnGetBufferData(colorBuff);
const char* errorMessage;
if (oidnGetDeviceError(device, &errorMessage) != OIDN_ERROR_NONE)
NSLog(@"Error: %s\n", errorMessage);
else
NSLog(@"successfully filtered image \n");
oidnReleaseBuffer(colorBuff);
oidnReleaseBuffer(normalBuff);
oidnReleaseBuffer(albedoBuff);
return beautyPtr;
}
@end