From a5debc29a59b090ede6a3867b25f4c728e4c7fda Mon Sep 17 00:00:00 2001 From: Simon Naarmann Date: Wed, 25 Jan 2023 00:25:52 +0100 Subject: [PATCH] Custom convertMaskToAlpha for #431 --- src/basics/alleg5.d | 39 +++++++++++++++++++++++++++++++++ src/graphic/color.d | 11 ---------- src/graphic/cutbit.d | 5 +++-- src/graphic/internal/loadfile.d | 3 +-- 4 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/basics/alleg5.d b/src/basics/alleg5.d index 65deee66..eb2f1a58 100644 --- a/src/basics/alleg5.d +++ b/src/basics/alleg5.d @@ -124,6 +124,45 @@ Albit albitCreateSmoothlyScalable(in int xl, in int yl) bool isTargetBitmap(in Albit b) { return al_get_target_bitmap() == b; } } +/* + * My custom variant of al_convert_mask_to_alpha to debug github issue #431: + * Magic pink won't become transparent on macOS (both Intel and ARM64) + */ +void convertPinkToAlpha(Albit bitmap) +{ + auto region = LockReadWrite(bitmap); + auto target = TargetBitmap(bitmap); + immutable xl = bitmap.xl; + immutable yl = bitmap.yl; + immutable Alcol transp = al_map_rgba(0, 0, 0, 0); + for (int y = 0; y < yl; y++) { + for (int x = 0; x < xl; x++) { + immutable Alcol pixel = al_get_pixel(bitmap, x, y); + if (pixel.r == 1f && pixel.g == 0f && pixel.b == 1f) { + al_put_pixel(x, y, transp); + } + } + } +} + +/* + * The reason for above custom variant of al_convert_mask_to_alpha() is + * that A5's version compares colors with memcmp, but memcmp fails for float + * color components +0 and -0 which must compare equal as color components: + */ +unittest { + union Float { + float f; + ubyte[4] arr; + } + Float a; + Float b; + a.f = +0f; + b.f = -0f; + assert (a.f == b.f); + assert (a.arr != b.arr); +} + // The following structs implement RAII. struct TargetBitmap { diff --git a/src/graphic/color.d b/src/graphic/color.d index 2abccbaa..ae4573b2 100644 --- a/src/graphic/color.d +++ b/src/graphic/color.d @@ -1,7 +1,5 @@ module graphic.color; -import std.random; - public import basics.alleg5 : Alcol, al_map_rgb, al_unmap_rgb, al_map_rgb_f, al_unmap_rgb_f, @@ -40,15 +38,6 @@ void computeColors(in int r, in int g, in int b) } private class ColorPrivate { - - @property Alcol random() - { - alias rnd = uniform01!float; - float[] arr = [rnd(), 0.7 + 0.3 * rnd(), 0.3 * rnd()]; - arr.randomShuffle(); - return al_map_rgb_f(arr[0], arr[1], arr[2]); - } - // This can't be an alias to al_map_rgb because al_map_rgb expects ubytes. Alcol makecol(in int r, in int g, in int b) { diff --git a/src/graphic/cutbit.d b/src/graphic/cutbit.d index 18288377..5af95b79 100644 --- a/src/graphic/cutbit.d +++ b/src/graphic/cutbit.d @@ -79,8 +79,9 @@ public: { // Try loading the file. If not found, don't crash, but log. bitmap = al_load_bitmap(fn.stringForReading.toStringz); - if (bitmap) - al_convert_mask_to_alpha(bitmap, color.pink); + if (bitmap) { + bitmap.convertPinkToAlpha(); + } this(bitmap, cut); } diff --git a/src/graphic/internal/loadfile.d b/src/graphic/internal/loadfile.d index 62d68b33..b7ace64d 100644 --- a/src/graphic/internal/loadfile.d +++ b/src/graphic/internal/loadfile.d @@ -10,7 +10,6 @@ import std.algorithm; // find import basics.alleg5; import basics.globals : dirDataBitmap; import file.filename; -import graphic.color; import graphic.cutbit; import graphic.internal.getters; import graphic.internal.names; @@ -33,7 +32,7 @@ do { return; } loadedCutbitMayBeScaled[id] = new Cutbit(fn, Cutbit.Cut.ifGridExists); - al_convert_mask_to_alpha(loadedCutbitMayBeScaled[id].albit, color.pink); + loadedCutbitMayBeScaled[id].albit.convertPinkToAlpha(); if (id.needGuiRecoloring) { eidrecol(loadedCutbitMayBeScaled[id], 0); }