Skip to content

Commit

Permalink
Custom convertMaskToAlpha for #431
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonN committed Mar 4, 2023
1 parent 0d827d7 commit a5debc2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
39 changes: 39 additions & 0 deletions src/basics/alleg5.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
11 changes: 0 additions & 11 deletions src/graphic/color.d
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions src/graphic/cutbit.d
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
3 changes: 1 addition & 2 deletions src/graphic/internal/loadfile.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit a5debc2

Please sign in to comment.