I want to use lut image as filter, and this is my code
<code> // lutFilter
// lutSize: 64x64x64;imageSize: 512x512
float4 lutFilter(sampler inputPixel, sampler lutPixel, float intensity) {
float2 d = inputPixel.coord();
float4 color = sample(inputPixel, d);
float3 rgb = saturate(color.rgb); // [0, 1]
float3 index = rgb * 63.0;
float r = floor(index.r);
float g = floor(index.g);
float b = floor(index.b);
float x = (fmod(b, 8.0) * 64.0 + r + 0.5) / 512.0;
float y = (floor(b / 8.0) * 64.0 + g + 0.5) / 512.0;
float2 xy = float2(x, y);
float4 result = sample(lutPixel, xy);
return mix(color, result, intensity);
}
</code>
<code> // lutFilter
// lutSize: 64x64x64;imageSize: 512x512
float4 lutFilter(sampler inputPixel, sampler lutPixel, float intensity) {
float2 d = inputPixel.coord();
float4 color = sample(inputPixel, d);
float3 rgb = saturate(color.rgb); // [0, 1]
float3 index = rgb * 63.0;
float r = floor(index.r);
float g = floor(index.g);
float b = floor(index.b);
float x = (fmod(b, 8.0) * 64.0 + r + 0.5) / 512.0;
float y = (floor(b / 8.0) * 64.0 + g + 0.5) / 512.0;
float2 xy = float2(x, y);
float4 result = sample(lutPixel, xy);
return mix(color, result, intensity);
}
</code>
// lutFilter
// lutSize: 64x64x64;imageSize: 512x512
float4 lutFilter(sampler inputPixel, sampler lutPixel, float intensity) {
float2 d = inputPixel.coord();
float4 color = sample(inputPixel, d);
float3 rgb = saturate(color.rgb); // [0, 1]
float3 index = rgb * 63.0;
float r = floor(index.r);
float g = floor(index.g);
float b = floor(index.b);
float x = (fmod(b, 8.0) * 64.0 + r + 0.5) / 512.0;
float y = (floor(b / 8.0) * 64.0 + g + 0.5) / 512.0;
float2 xy = float2(x, y);
float4 result = sample(lutPixel, xy);
return mix(color, result, intensity);
}
<code> override public var outputImage: CIImage? {
guard let inputImage else {
return nil
}
return Self.kernel.apply(extent: inputImage.extent, roiCallback: { index, rect in
if index == 1 {
return Self.lutImage.extent
}
return rect
}, arguments: [inputImage, Self.lutImage, intensity])
}
</code>
<code> override public var outputImage: CIImage? {
guard let inputImage else {
return nil
}
return Self.kernel.apply(extent: inputImage.extent, roiCallback: { index, rect in
if index == 1 {
return Self.lutImage.extent
}
return rect
}, arguments: [inputImage, Self.lutImage, intensity])
}
</code>
override public var outputImage: CIImage? {
guard let inputImage else {
return nil
}
return Self.kernel.apply(extent: inputImage.extent, roiCallback: { index, rect in
if index == 1 {
return Self.lutImage.extent
}
return rect
}, arguments: [inputImage, Self.lutImage, intensity])
}
I use an origin lut but the output is not expected, it looks like this, where goes wrong?
How to fix this problem? And there is a better way to do LUT? Thanks a lot!