Skip to content

Instantly share code, notes, and snippets.

@rcombs
Created March 16, 2015 09:36
Show Gist options
  • Save rcombs/33feb90c243dadbc682e to your computer and use it in GitHub Desktop.
Save rcombs/33feb90c243dadbc682e to your computer and use it in GitHub Desktop.
void blend_alpha_to_rgba_c(unsigned char *src, intptr_t src_w,
intptr_t src_h, intptr_t src_stride,
unsigned char *dst, intptr_t dst_stride,
uint32_t color)
{
const unsigned int r = (color >> 24) & 0xff;
const unsigned int g = (color >> 16) & 0xff;
const unsigned int b = (color >> 8) & 0xff;
const unsigned int a = 0xff - (color & 0xff);
for (int y = 0; y < src_h; y++, dst += dst_stride, src += src_stride) {
uint32_t *dstrow = (uint32_t *) dst;
for (int x = 0; x < src_w; x++) {
const unsigned int v = src[x];
int rr = (r * a * v);
int gg = (g * a * v);
int bb = (b * a * v);
int aa = a * v;
uint32_t dstpix = dstrow[x];
unsigned int dstb = dstpix & 0xFF;
unsigned int dstg = (dstpix >> 8) & 0xFF;
unsigned int dstr = (dstpix >> 16) & 0xFF;
unsigned int dsta = (dstpix >> 24) & 0xFF;
dstb = (bb + dstb * (255 * 255 - aa)) / (255 * 255);
dstg = (gg + dstg * (255 * 255 - aa)) / (255 * 255);
dstr = (rr + dstr * (255 * 255 - aa)) / (255 * 255);
dsta = (aa * 255 + dsta * (255 * 255 - aa)) / (255 * 255);
dstrow[x] = dstb | (dstg << 8) | (dstr << 16) | (dsta << 24);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment