MCPcopy Create free account
hub / github.com/DentonW/DevIL / GenBitMask

Function GenBitMask

DevIL/src-IL/src/il_dds-save.cpp:1010–1070  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1008
1009
1010ILuint GenBitMask(ILushort ex0, ILushort ex1, ILuint NumCols, ILushort *In, ILubyte *Alpha, Color888 *OutCol)
1011{
1012 ILuint i, j, Closest, Dist, BitMask = 0;
1013 ILubyte Mask[16];
1014 Color888 c, Colours[4];
1015
1016 ShortToColor888(ex0, &Colours[0]);
1017 ShortToColor888(ex1, &Colours[1]);
1018 if (NumCols == 3) {
1019 Colours[2].r = (Colours[0].r + Colours[1].r) / 2;
1020 Colours[2].g = (Colours[0].g + Colours[1].g) / 2;
1021 Colours[2].b = (Colours[0].b + Colours[1].b) / 2;
1022 Colours[3].r = (Colours[0].r + Colours[1].r) / 2;
1023 Colours[3].g = (Colours[0].g + Colours[1].g) / 2;
1024 Colours[3].b = (Colours[0].b + Colours[1].b) / 2;
1025 }
1026 else { // NumCols == 4
1027 Colours[2].r = (2 * Colours[0].r + Colours[1].r + 1) / 3;
1028 Colours[2].g = (2 * Colours[0].g + Colours[1].g + 1) / 3;
1029 Colours[2].b = (2 * Colours[0].b + Colours[1].b + 1) / 3;
1030 Colours[3].r = (Colours[0].r + 2 * Colours[1].r + 1) / 3;
1031 Colours[3].g = (Colours[0].g + 2 * Colours[1].g + 1) / 3;
1032 Colours[3].b = (Colours[0].b + 2 * Colours[1].b + 1) / 3;
1033 }
1034
1035 for (i = 0; i < 16; i++) {
1036 if (Alpha) { // Test to see if we have 1-bit transparency
1037 if (Alpha[i] < 128) {
1038 Mask[i] = 3; // Transparent
1039 if (OutCol) {
1040 OutCol[i].r = Colours[3].r;
1041 OutCol[i].g = Colours[3].g;
1042 OutCol[i].b = Colours[3].b;
1043 }
1044 continue;
1045 }
1046 }
1047
1048 // If no transparency, try to find which colour is the closest.
1049 Closest = UINT_MAX;
1050 ShortToColor888(In[i], &c);
1051 for (j = 0; j < NumCols; j++) {
1052 Dist = Distance(&c, &Colours[j]);
1053 if (Dist < Closest) {
1054 Closest = Dist;
1055 Mask[i] = j;
1056 if (OutCol) {
1057 OutCol[i].r = Colours[j].r;
1058 OutCol[i].g = Colours[j].g;
1059 OutCol[i].b = Colours[j].b;
1060 }
1061 }
1062 }
1063 }
1064
1065 for (i = 0; i < 16; i++) {
1066 BitMask |= (Mask[i] << (i*2));
1067 }

Callers 1

CompressFunction · 0.85

Calls 2

ShortToColor888Function · 0.85
DistanceFunction · 0.85

Tested by

no test coverage detected