Skip to content

Add an example of denoising an image with its alpha channel preserved #124

@Calinou

Description

@Calinou

For context, I'm working on a Godot pull request that adds shadowmasking to the CPU lightmapper, which uses OpenImageDenoise. The shadowmask is stored in the lightmap's alpha channel to prevent the need to use a separate texture (which adds a whole lot of complexity). However, if I enable the denoiser, this alpha channels is lost when the image is denoised.

I've tried the following to allow preserving the alpha channel of the input image:

bool oidn_denoise(void *deviceptr, float *p_floats, int p_width, int p_height) {
	OIDNDeviceImpl *device = (OIDNDeviceImpl *)deviceptr;
	OIDNFilter filter = oidnNewFilter(device, "RTLightmap");
-	oidnSetSharedFilterImage(filter, "color", (void *)p_floats, OIDN_FORMAT_FLOAT3, p_width, p_height, 0, 0, 0);
-	oidnSetSharedFilterImage(filter, "output", (void *)p_floats, OIDN_FORMAT_FLOAT3, p_width, p_height, 0, 0, 0);
+	// Pass 16-byte pixel stride to preserve the unmodified 32-bit alpha channel.
+	oidnSetSharedFilterImage(filter, "color", (void *)p_floats, OIDN_FORMAT_FLOAT3, p_width, p_height, 0, 16, 0);
+	oidnSetSharedFilterImage(filter, "output", (void *)p_floats, OIDN_FORMAT_FLOAT3, p_width, p_height, 0, 16, 0);
	oidnSetFilter1b(filter, "hdr", true);
	oidnCommitFilter(filter);
	oidnExecuteFilter(filter);
	const char *msg;
	bool success = true;
	if (oidnGetDeviceError(device, &msg) != OIDN_ERROR_NONE) {
		printf("LightmapDenoiser: %s\n", msg);
		success = false;
	}
	oidnReleaseFilter(filter);
	return success;
}

I'm not sure I understand what size to pass as a stride. My image data is always Image::FORMAT_RGBAF (floating-point RGBA with 32 bpc precision).
I've tried different values such as 4, 8, 12, 16 but they all resulted in an error message (LightmapDenoiser: pixel stride smaller than pixel size), or an image that looked corrupted. I've also tried specifying different strides for the "color" and "output" images to no avail.

Would it be possible to add a code sample to the README that describes how to preserve an image's alpha channel, assuming the image uses RGBAF format (not ARGBF)? Thanks in advance 🙂

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions