From 4974fb7203bf6268a1d2f6315136f89e14b9b042 Mon Sep 17 00:00:00 2001 From: KitsuneAlex Date: Mon, 13 May 2024 06:15:26 +0200 Subject: [PATCH] Add API functions for all edge coloring algorithms & error correction algorithms --- core/msdfgen-c.cpp | 122 +++++++++++++++++++++++++++++++++++++-------- msdfgen-c.h | 28 ++++++++++- 2 files changed, 128 insertions(+), 22 deletions(-) diff --git a/core/msdfgen-c.cpp b/core/msdfgen-c.cpp index 2e751f1..38e2ed9 100644 --- a/core/msdfgen-c.cpp +++ b/core/msdfgen-c.cpp @@ -252,7 +252,7 @@ MSDF_API int msdf_shape_orient_contours(msdf_shape_handle shape) { return MSDF_SUCCESS; } -MSDF_API int msdf_shape_simple_edge_colors(msdf_shape_handle shape, const double angle_threshold) { +MSDF_API int msdf_shape_edge_colors_simple(msdf_shape_handle shape, const double angle_threshold) { if(shape == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -260,6 +260,22 @@ MSDF_API int msdf_shape_simple_edge_colors(msdf_shape_handle shape, const double return MSDF_SUCCESS; } +MSDF_API int msdf_shape_edge_colors_ink_trap(msdf_shape_handle shape, const double angle_threshold) { + if(shape == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + msdfgen::edgeColoringInkTrap(*reinterpret_cast(shape), angle_threshold); + return MSDF_SUCCESS; +} + +MSDF_API int msdf_shape_edge_colors_by_distance(msdf_shape_handle shape, const double angle_threshold) { + if(shape == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + msdfgen::edgeColoringByDistance(*reinterpret_cast(shape), angle_threshold); + return MSDF_SUCCESS; +} + MSDF_API void msdf_shape_free(msdf_shape_handle shape) { delete reinterpret_cast(shape); } @@ -548,9 +564,73 @@ MSDF_API void msdf_segment_free(msdf_segment_handle segment) { } } +// Error correction functions + +MSDF_API int msdf_error_correction(msdf_bitmap_t* bitmap, msdf_shape_const_handle shape, const msdf_transform_t* transform) { + if(bitmap == nullptr || shape == nullptr || transform == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + const msdfgen::Projection projection(*reinterpret_cast(&transform->scale), + *reinterpret_cast(&transform->translation)); + const msdfgen::SDFTransformation actual_transform(projection, *reinterpret_cast(&transform->distance_mapping)); + switch(bitmap->type) { + case MSDF_BITMAP_TYPE_MSDF: + msdfgen::msdfErrorCorrection(*static_cast(bitmap->handle), *reinterpret_cast(shape), + actual_transform); + break; + case MSDF_BITMAP_TYPE_MTSDF: + msdfgen::msdfErrorCorrection(*static_cast(bitmap->handle), *reinterpret_cast(shape), + actual_transform); + break; + default: + return MSDF_ERR_INVALID_TYPE; + } + return MSDF_SUCCESS; +} + +MSDF_API int msdf_error_correction_fast_distance(msdf_bitmap_t* bitmap, const msdf_transform_t* transform) { + if(bitmap == nullptr || transform == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + const msdfgen::Projection projection(*reinterpret_cast(&transform->scale), + *reinterpret_cast(&transform->translation)); + const msdfgen::SDFTransformation actual_transform(projection, *reinterpret_cast(&transform->distance_mapping)); + switch(bitmap->type) { + case MSDF_BITMAP_TYPE_MSDF: + msdfgen::msdfFastDistanceErrorCorrection(*static_cast(bitmap->handle), actual_transform); + break; + case MSDF_BITMAP_TYPE_MTSDF: + msdfgen::msdfFastDistanceErrorCorrection(*static_cast(bitmap->handle), actual_transform); + break; + default: + return MSDF_ERR_INVALID_TYPE; + } + return MSDF_SUCCESS; +} + +MSDF_API int msdf_error_correction_fast_edge(msdf_bitmap_t* bitmap, const msdf_transform_t* transform) { + if(bitmap == nullptr || transform == nullptr) { + return MSDF_ERR_INVALID_ARG; + } + const msdfgen::Projection projection(*reinterpret_cast(&transform->scale), + *reinterpret_cast(&transform->translation)); + const msdfgen::SDFTransformation actual_transform(projection, *reinterpret_cast(&transform->distance_mapping)); + switch(bitmap->type) { + case MSDF_BITMAP_TYPE_MSDF: + msdfgen::msdfFastEdgeErrorCorrection(*static_cast(bitmap->handle), actual_transform); + break; + case MSDF_BITMAP_TYPE_MTSDF: + msdfgen::msdfFastEdgeErrorCorrection(*static_cast(bitmap->handle), actual_transform); + break; + default: + return MSDF_ERR_INVALID_TYPE; + } + return MSDF_SUCCESS; +} + // Main msdfgen APIs -int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { +MSDF_API int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -564,7 +644,7 @@ int msdf_generate_sdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, cons return MSDF_SUCCESS; } -int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { +MSDF_API int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -578,7 +658,7 @@ int msdf_generate_psdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, con return MSDF_SUCCESS; } -int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { +MSDF_API int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -592,7 +672,7 @@ int msdf_generate_msdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, con return MSDF_SUCCESS; } -int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { +MSDF_API int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, const msdf_transform_t* transform) { if(output == nullptr || shape == nullptr || transform == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -606,10 +686,10 @@ int msdf_generate_mtsdf(msdf_bitmap_t* output, msdf_shape_const_handle shape, co return MSDF_SUCCESS; } -int msdf_generate_sdf_with_config(msdf_bitmap_t* output, - msdf_shape_const_handle shape, - const msdf_transform_t* transform, - const msdf_config_t* config) { +MSDF_API int msdf_generate_sdf_with_config(msdf_bitmap_t* output, + msdf_shape_const_handle shape, + const msdf_transform_t* transform, + const msdf_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -625,10 +705,10 @@ int msdf_generate_sdf_with_config(msdf_bitmap_t* output, return MSDF_SUCCESS; } -int msdf_generate_psdf_with_config(msdf_bitmap_t* output, - msdf_shape_const_handle shape, - const msdf_transform_t* transform, - const msdf_config_t* config) { +MSDF_API int msdf_generate_psdf_with_config(msdf_bitmap_t* output, + msdf_shape_const_handle shape, + const msdf_transform_t* transform, + const msdf_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -644,10 +724,10 @@ int msdf_generate_psdf_with_config(msdf_bitmap_t* output, return MSDF_SUCCESS; } -int msdf_generate_msdf_with_config(msdf_bitmap_t* output, - msdf_shape_const_handle shape, - const msdf_transform_t* transform, - const msdf_multichannel_config_t* config) { +MSDF_API int msdf_generate_msdf_with_config(msdf_bitmap_t* output, + msdf_shape_const_handle shape, + const msdf_transform_t* transform, + const msdf_multichannel_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { return MSDF_ERR_INVALID_ARG; } @@ -667,10 +747,10 @@ int msdf_generate_msdf_with_config(msdf_bitmap_t* output, return MSDF_SUCCESS; } -int msdf_generate_mtsdf_with_config(msdf_bitmap_t* output, - msdf_shape_const_handle shape, - const msdf_transform_t* transform, - const msdf_multichannel_config_t* config) { +MSDF_API int msdf_generate_mtsdf_with_config(msdf_bitmap_t* output, + msdf_shape_const_handle shape, + const msdf_transform_t* transform, + const msdf_multichannel_config_t* config) { if(output == nullptr || shape == nullptr || transform == nullptr || config == nullptr) { return MSDF_ERR_INVALID_ARG; } diff --git a/msdfgen-c.h b/msdfgen-c.h index 6be5c79..e896e47 100644 --- a/msdfgen-c.h +++ b/msdfgen-c.h @@ -280,7 +280,25 @@ MSDF_API int msdf_shape_orient_contours(msdf_shape_handle shape); * @param angle_threshold The threshold angle in degrees. * @returns @code MSDF_SUCCESS@endcode on success, otherwise one of the constants prefixed with @code MSDF_ERR_@endcode. */ -MSDF_API int msdf_shape_simple_edge_colors(msdf_shape_handle shape, double angle_threshold); +MSDF_API int msdf_shape_edge_colors_simple(msdf_shape_handle shape, double angle_threshold); + +/** + * Colors the edges of the given shape using the default MSDF colors specified by the + * MSDF_COLOR_ prefixed constants using the ink trap algorithm. + * @param shape A pointer to a shape whose edges to color with the default MSDF colors. + * @param angle_threshold The threshold angle in degrees. + * @returns @code MSDF_SUCCESS@endcode on success, otherwise one of the constants prefixed with @code MSDF_ERR_@endcode. + */ +MSDF_API int msdf_shape_edge_colors_ink_trap(msdf_shape_handle shape, double angle_threshold); + +/** + * Colors the edges of the given shape using the default MSDF colors specified by the + * MSDF_COLOR_ prefixed constants using the distance. + * @param shape A pointer to a shape whose edges to color with the default MSDF colors. + * @param angle_threshold The threshold angle in degrees. + * @returns @code MSDF_SUCCESS@endcode on success, otherwise one of the constants prefixed with @code MSDF_ERR_@endcode. + */ +MSDF_API int msdf_shape_edge_colors_by_distance(msdf_shape_handle shape, double angle_threshold); /** * Calls the destructor of the given bitmap and frees its memory using the @@ -493,6 +511,14 @@ MSDF_API int msdf_segment_move_end_point(msdf_segment_handle segment, const msdf */ MSDF_API void msdf_segment_free(msdf_segment_handle segment); +// Error correction functions + +MSDF_API int msdf_error_correction(msdf_bitmap_t* bitmap, msdf_shape_const_handle shape, const msdf_transform_t* transform); + +MSDF_API int msdf_error_correction_fast_distance(msdf_bitmap_t* bitmap, const msdf_transform_t* transform); + +MSDF_API int msdf_error_correction_fast_edge(msdf_bitmap_t* bitmap, const msdf_transform_t* transform); + // msdfgen-core API functions /**