Extract one element (red, green, blue or alpha) from a pixel */
| 574 | |
| 575 | /* Extract one element (red, green, blue or alpha) from a pixel */ |
| 576 | static unsigned char |
| 577 | pixel_element(uint32 mask, uint32 color) |
| 578 | { |
| 579 | uint32 bits, shift; |
| 580 | |
| 581 | if (mask == 0) return 0; |
| 582 | bits = 0xFFFF; /* 0xFF, 0xF, 0x3, 0x1 */ |
| 583 | shift = 16; /* 8, 4, 2, 1 */ |
| 584 | while (shift != 0) { |
| 585 | if ((mask & bits) == 0) { |
| 586 | mask >>= shift; |
| 587 | color >>= shift; |
| 588 | } |
| 589 | shift /= 2; |
| 590 | bits >>= shift; |
| 591 | } |
| 592 | color &= mask; |
| 593 | return color * 255 / mask; |
| 594 | } |