Last active
October 10, 2018 01:26
-
-
Save ruilin/a6e3432ac89aa370fe9e to your computer and use it in GitHub Desktop.
带alpha通道的图层合并算法,实现多个图层叠加后产生的新图层rgba数据
This file contains 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
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