/* Extend A1/2/4 to A8 with interpolation to reduce rounding error.  */
static uint8_t bit_extend(uint8_t value, uint8_t bpp)
/* Extend A1/2/4 to A8 with interpolation to reduce rounding error.  */
static uint8_t bit_extend(uint8_t value, uint8_t bpp)
{
    if(value == 0) return 0;
    uint8_t res = value;
    uint8_t bpp_now = bpp;
    while(bpp_now < 8) {
        res |= value << (8 - bpp_now);
        bpp_now += bpp;
    };
    return res;
}