Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hankcs/9e789419fc5572df9908c7da105fbffc to your computer and use it in GitHub Desktop.
Save hankcs/9e789419fc5572df9908c7da105fbffc to your computer and use it in GitHub Desktop.
带alpha通道的图层合并算法,实现多个图层叠加后产生的新图层rgba数据
int clamp(int val) {
if (val < 0) return 0;
if (val > 255) return 255;
return val;
}
unsigned char layerMerge(unsigned char **layers, unsigned int layerCount, unsigned int layerWidth, unsigned int layerHeight) {
if (1 >= layerCount) return 0;
unsigned char isNewLayer = 0;
unsigned int i, j, byteCount;
byteCount = layerWidth * layerHeight * 4; /* byte count of each layer */
unsigned char *result = layers[0];
for (i = 1; i < layerCount; i++) {
unsigned char *tile = layers[i];
for (j = 0; j < byteCount; j += 4) {
int r = j; int g = j + 1; int b = j + 2; int a = j + 3;
if (255 != result[a] && 0 != tile[a]) {
isNewLayer = 1;
}
float alpha1 = result[a] / 255.0f;
float alpha2 = tile[a] / 255.0f;
float R = result[r] * alpha1 + tile[r] * alpha2 * (1 - alpha1);
float G = result[g] * alpha1 + tile[g] * alpha2 * (1 - alpha1);
float B = result[b] * alpha1 + tile[b] * alpha2 * (1 - alpha1);
float A = 1 - (1 - alpha1) * (1 - alpha2);
if (0 == result[a] && 0 == tile[a]) {
result[r] = result[g] = result[b] = result[a] = 0;
} else {
result[r] = clamp((int)(R / A) + 0.5);
result[g] = clamp((int)(G / A) + 0.5);
result[b] = clamp((int)(B / A) + 0.5);
result[a] = clamp((int)(A * 255) + 0.5);
}
}
}
return isNewLayer;
}
/* out put layers[0] */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment