aexlo 0.2.0

Run After Effects plugins without Adobe dependencies.
Documentation
//! `PF Color Settings Suite` (wire version 3, `AEGP_ColorSettingsSuite2`).
//!
//! aexlo does no color management: pixels go in and out untouched, which is
//! what an sRGB working space without linearization means. So the comp's
//! working-space profile is sRGB, an RGB profile with an approximate gamma of
//! 2.2. Profiles are opaque boxes the plugin frees with
//! `AEGP_DisposeColorProfile`. ICC data, descriptions (which need the AEGP
//! Memory Suite), views and blending tables are refused with `A_Err_GENERIC`.

use crate::sys::*;

use super::layer::Timeline;
use crate::core::diagnostics::diag;

/// What an `AEGP_ColorProfileP` points at.
struct ColorProfile {
	approximate_gamma: A_FpShort,
}

/// The sRGB working space.
const SRGB: ColorProfile = ColorProfile { approximate_gamma: 2.2 };

fn unsupported(name: &str) -> A_Err {
	log::warn!("AEGP_ColorSettingsSuite2/{name}: aexlo does no color management");
	A_Err_GENERIC
}

/// # Safety
/// `color_profileP` must be null or a profile from this module that has not
/// been disposed.
unsafe fn profile_of<'a>(color_profileP: AEGP_ConstColorProfileP) -> Option<&'a ColorProfile> {
	unsafe { (color_profileP as *const ColorProfile).as_ref() }
}

unsafe extern "C" fn get_blending_tables(
	_render_contextH: PR_RenderContextH,
	_blending_tables: *mut PF_EffectBlendingTables,
) -> A_Err {
	crate::core::ffi::guard("get_blending_tables", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_GetBlendingTables");
		unsupported("AEGP_GetBlendingTables")
	})
}

unsafe extern "C" fn does_view_have_color_space_xform(_viewP: AEGP_ItemViewP, _has_xformPB: *mut A_Boolean) -> A_Err {
	crate::core::ffi::guard("does_view_have_color_space_xform", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_DoesViewHaveColorSpaceXform");
		unsupported("AEGP_DoesViewHaveColorSpaceXform")
	})
}

unsafe extern "C" fn xform_working_to_view_color_space(
	_viewP: AEGP_ItemViewP,
	_srcH: AEGP_WorldH,
	_dstH: AEGP_WorldH,
) -> A_Err {
	crate::core::ffi::guard("xform_working_to_view_color_space", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_XformWorkingToViewColorSpace");
		unsupported("AEGP_XformWorkingToViewColorSpace")
	})
}

unsafe extern "C" fn get_new_working_space_color_profile(
	_aegp_plugin_id: AEGP_PluginID,
	compH: AEGP_CompH,
	color_profilePP: *mut AEGP_ColorProfileP,
) -> A_Err {
	crate::core::ffi::guard("get_new_working_space_color_profile", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_GetNewWorkingSpaceColorProfile",
			"compH" => format!("{:#x}", compH as usize),
		);

		if Timeline::of(compH as _).is_none() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let Some(out) = (unsafe { color_profilePP.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		*out = Box::into_raw(Box::new(SRGB)) as AEGP_ColorProfileP;
		PF_Err_NONE
	})
}

unsafe extern "C" fn get_new_color_profile_from_icc_profile(
	_aegp_plugin_id: AEGP_PluginID,
	_icc_sizeL: A_long,
	_icc_dataPV: *const std::ffi::c_void,
	_color_profilePP: *mut AEGP_ColorProfileP,
) -> A_Err {
	crate::core::ffi::guard("get_new_color_profile_from_icc_profile", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_GetNewColorProfileFromICCProfile");
		unsupported("AEGP_GetNewColorProfileFromICCProfile")
	})
}

unsafe extern "C" fn get_new_icc_profile_from_color_profile(
	_aegp_plugin_id: AEGP_PluginID,
	_color_profileP: AEGP_ConstColorProfileP,
	_icc_profilePH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_new_icc_profile_from_color_profile", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_GetNewICCProfileFromColorProfile");
		unsupported("AEGP_GetNewICCProfileFromColorProfile")
	})
}

unsafe extern "C" fn get_new_color_profile_description(
	_aegp_plugin_id: AEGP_PluginID,
	_color_profileP: AEGP_ConstColorProfileP,
	_unicode_descPH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_new_color_profile_description", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_GetNewColorProfileDescription");
		unsupported("AEGP_GetNewColorProfileDescription")
	})
}

unsafe extern "C" fn dispose_color_profile(color_profileP: AEGP_ColorProfileP) -> A_Err {
	crate::core::ffi::guard("dispose_color_profile", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_DisposeColorProfile",
			"color_profileP" => format!("{:#x}", color_profileP as usize),
		);

		if color_profileP.is_null() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		// SAFETY: non-null profiles only come from `get_new_working_space_color_profile`.
		drop(unsafe { Box::from_raw(color_profileP as *mut ColorProfile) });
		PF_Err_NONE
	})
}

unsafe extern "C" fn get_color_profile_approximate_gamma(
	color_profileP: AEGP_ConstColorProfileP,
	approx_gammaP: *mut A_FpShort,
) -> A_Err {
	crate::core::ffi::guard("get_color_profile_approximate_gamma", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_GetColorProfileApproximateGamma");

		let Some(profile) = (unsafe { profile_of(color_profileP) }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		let Some(out) = (unsafe { approx_gammaP.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		*out = profile.approximate_gamma;
		PF_Err_NONE
	})
}

unsafe extern "C" fn is_rgb_color_profile(color_profileP: AEGP_ConstColorProfileP, is_rgbPB: *mut A_Boolean) -> A_Err {
	crate::core::ffi::guard("is_rgb_color_profile", || {
		diag!("AEGP_ColorSettingsSuite2/AEGP_IsRGBColorProfile");

		if unsafe { profile_of(color_profileP) }.is_none() {
			return PF_Err_BAD_CALLBACK_PARAM;
		}
		let Some(out) = (unsafe { is_rgbPB.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		*out = 1;
		PF_Err_NONE
	})
}

unsafe extern "C" fn set_working_color_space(
	_aegp_plugin_id: AEGP_PluginID,
	_compH: AEGP_CompH,
	_color_profileP: AEGP_ConstColorProfileP,
) -> A_Err {
	crate::core::ffi::guard("set_working_color_space", || {
		diag!("AEGP_ColorSettingsSuite5/AEGP_SetWorkingColorSpace");
		// The working space stays sRGB. Accepting the call lets a plugin that
		// asked for wire version 6 continue into its render.
		PF_Err_NONE
	})
}

unsafe extern "C" fn is_ocio_used(_aegp_plugin_id: AEGP_PluginID, is_used_out: *mut A_Boolean) -> A_Err {
	crate::core::ffi::guard("is_ocio_used", || {
		diag!("AEGP_ColorSettingsSuite5/AEGP_IsOCIOColorManagementUsed");
		let Some(out) = (unsafe { is_used_out.as_mut() }) else {
			return PF_Err_BAD_CALLBACK_PARAM;
		};
		*out = 0;
		PF_Err_NONE
	})
}

unsafe extern "C" fn ocio_unavailable() -> A_Err {
	crate::core::ffi::guard("ocio_unavailable", || PF_Err_BAD_CALLBACK_PARAM)
}

unsafe extern "C" fn get_ocio_configuration_file(
	_aegp_plugin_id: AEGP_PluginID,
	_config_filePH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_ocio_configuration_file", || {
		diag!("AEGP_ColorSettingsSuite5/AEGP_GetOCIOConfigurationFile");
		unsafe { ocio_unavailable() }
	})
}

unsafe extern "C" fn get_ocio_configuration_file_path(
	_aegp_plugin_id: AEGP_PluginID,
	_config_filePH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_ocio_configuration_file_path", || {
		diag!("AEGP_ColorSettingsSuite5/AEGP_GetOCIOConfigurationFilePath");
		unsafe { ocio_unavailable() }
	})
}

unsafe extern "C" fn get_ocio_working_color_space(
	_aegp_plugin_id: AEGP_PluginID,
	_ocio_working_colorspaceH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_ocio_working_color_space", || {
		diag!("AEGP_ColorSettingsSuite5/AEGPD_GetOCIOWorkingColorSpace");
		unsafe { ocio_unavailable() }
	})
}

unsafe extern "C" fn get_ocio_display_color_space(
	_aegp_plugin_id: AEGP_PluginID,
	_ocio_displayH: *mut AEGP_MemHandle,
	_ocio_viewH: *mut AEGP_MemHandle,
) -> A_Err {
	crate::core::ffi::guard("get_ocio_display_color_space", || {
		diag!("AEGP_ColorSettingsSuite5/AEGPD_GetOCIODisplayColorSpace");
		unsafe { ocio_unavailable() }
	})
}

/// Builds the `AEGP_ColorSettingsSuite2` vtable.
pub(super) const fn create_color_settings_suite_2() -> AEGP_ColorSettingsSuite2 {
	AEGP_ColorSettingsSuite2 {
		AEGP_GetBlendingTables: Some(get_blending_tables),
		AEGP_DoesViewHaveColorSpaceXform: Some(does_view_have_color_space_xform),
		AEGP_XformWorkingToViewColorSpace: Some(xform_working_to_view_color_space),
		AEGP_GetNewWorkingSpaceColorProfile: Some(get_new_working_space_color_profile),
		AEGP_GetNewColorProfileFromICCProfile: Some(get_new_color_profile_from_icc_profile),
		AEGP_GetNewICCProfileFromColorProfile: Some(get_new_icc_profile_from_color_profile),
		AEGP_GetNewColorProfileDescription: Some(get_new_color_profile_description),
		AEGP_DisposeColorProfile: Some(dispose_color_profile),
		AEGP_GetColorProfileApproximateGamma: Some(get_color_profile_approximate_gamma),
		AEGP_IsRGBColorProfile: Some(is_rgb_color_profile),
	}
}

/// Wire version 6 (`kAEGPColorSettingsSuiteVersion5`). Same calls as suite 2,
/// plus a working-space setter and OCIO queries. OCIO is not in use.
pub(super) const fn create_color_settings_suite_5() -> AEGP_ColorSettingsSuite5 {
	AEGP_ColorSettingsSuite5 {
		AEGP_GetBlendingTables: Some(get_blending_tables),
		AEGP_DoesViewHaveColorSpaceXform: Some(does_view_have_color_space_xform),
		AEGP_XformWorkingToViewColorSpace: Some(xform_working_to_view_color_space),
		AEGP_GetNewWorkingSpaceColorProfile: Some(get_new_working_space_color_profile),
		AEGP_GetNewColorProfileFromICCProfile: Some(get_new_color_profile_from_icc_profile),
		AEGP_GetNewICCProfileFromColorProfile: Some(get_new_icc_profile_from_color_profile),
		AEGP_GetNewColorProfileDescription: Some(get_new_color_profile_description),
		AEGP_DisposeColorProfile: Some(dispose_color_profile),
		AEGP_GetColorProfileApproximateGamma: Some(get_color_profile_approximate_gamma),
		AEGP_IsRGBColorProfile: Some(is_rgb_color_profile),
		AEGP_SetWorkingColorSpace: Some(set_working_color_space),
		AEGP_IsOCIOColorManagementUsed: Some(is_ocio_used),
		AEGP_GetOCIOConfigurationFile: Some(get_ocio_configuration_file),
		AEGP_GetOCIOConfigurationFilePath: Some(get_ocio_configuration_file_path),
		AEGPD_GetOCIOWorkingColorSpace: Some(get_ocio_working_color_space),
		AEGPD_GetOCIODisplayColorSpace: Some(get_ocio_display_color_space),
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	#[test]
	fn profiles_report_srgb_and_dispose() {
		let suite = create_color_settings_suite_2();
		let profile = Box::into_raw(Box::new(SRGB)) as AEGP_ColorProfileP;
		let mut gamma: A_FpShort = 0.0;
		let mut rgb: A_Boolean = 0;
		unsafe {
			assert_eq!(
				suite.AEGP_GetColorProfileApproximateGamma.unwrap()(profile, &mut gamma),
				PF_Err_NONE
			);
			assert_eq!(suite.AEGP_IsRGBColorProfile.unwrap()(profile, &mut rgb), PF_Err_NONE);
			assert_eq!(suite.AEGP_DisposeColorProfile.unwrap()(profile), PF_Err_NONE);
		}
		assert_eq!((gamma, rgb), (2.2, 1));
	}

	#[test]
	fn null_comps_get_no_profile() {
		let suite = create_color_settings_suite_2();
		let mut profile: AEGP_ColorProfileP = std::ptr::null_mut();
		let err = unsafe { suite.AEGP_GetNewWorkingSpaceColorProfile.unwrap()(0, std::ptr::null_mut(), &mut profile) };
		assert_eq!(err, PF_Err_BAD_CALLBACK_PARAM);
		assert!(profile.is_null());
	}
}