Created
February 26, 2023 18:59
-
-
Save pdewacht/614b428cd42c2052dc0fd292516c9f9f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdint.h> | |
#include <assert.h> | |
#include <math.h> | |
static uint8_t reference[256][256] = { | |
#include "reference.h" | |
}; | |
uint8_t to_premultiplied_alpha(uint8_t color, uint8_t alpha) { | |
return (((unsigned) color) * alpha + 127) / 255; | |
} | |
uint8_t to_unmultiplied_alpha(uint8_t color, int inv, int alpha) { | |
int r = (((int)color * inv + 0x8000) >> 16); | |
return r; | |
} | |
int main() { | |
int max_inv = 256*256*256; | |
for (int alpha = 1; alpha < 256; ++alpha) { | |
int best_inv = 0; | |
int best_errors = 256*256*2; | |
for (int inv = 0; inv < max_inv; ++inv) { | |
int errors = 0; | |
for (int color = 0; color < 256; ++color) { | |
int result = to_unmultiplied_alpha(to_premultiplied_alpha(color, alpha), inv, alpha); | |
int expected = reference[alpha][color]; | |
if (result != expected) errors++; | |
} | |
if (errors < best_errors) { | |
best_inv=inv; | |
best_errors=errors; | |
} | |
} | |
fprintf(stderr, "alpha %d: %d [%d]\n", alpha, best_inv, best_errors); | |
max_inv=best_inv; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment