Adds an alpha channel to an 8 or 24-bit image, making the image transparent where Key is equal to the pixel.
| 677 | // Adds an alpha channel to an 8 or 24-bit image, |
| 678 | // making the image transparent where Key is equal to the pixel. |
| 679 | ILboolean ilAddAlphaKey(ILimage *Image) |
| 680 | { |
| 681 | ILfloat KeyRed = 0, KeyGreen = 0, KeyBlue = 0, KeyAlpha = 0; |
| 682 | ILubyte *NewData, NewBpp; |
| 683 | ILfloat KeyColour[3]; |
| 684 | ILuint i = 0, j = 0, c, Size; |
| 685 | ILboolean Same; |
| 686 | |
| 687 | if (Image == NULL) { |
| 688 | ilSetError(IL_ILLEGAL_OPERATION); |
| 689 | return IL_FALSE; |
| 690 | } |
| 691 | |
| 692 | if (Image->Format != IL_COLOUR_INDEX) { |
| 693 | if (Image->Bpp != 3) { |
| 694 | ilSetError(IL_INVALID_VALUE); |
| 695 | return IL_FALSE; |
| 696 | } |
| 697 | |
| 698 | if (Image->Format == IL_BGR || Image->Format == IL_BGRA) { |
| 699 | KeyColour[0] = KeyBlue; |
| 700 | KeyColour[1] = KeyGreen; |
| 701 | KeyColour[2] = KeyRed; |
| 702 | } |
| 703 | else { |
| 704 | KeyColour[0] = KeyRed; |
| 705 | KeyColour[1] = KeyGreen; |
| 706 | KeyColour[2] = KeyBlue; |
| 707 | } |
| 708 | |
| 709 | Size = Image->Bps * Image->Height / Image->Bpc; |
| 710 | |
| 711 | NewBpp = (ILubyte)(Image->Bpp + 1); |
| 712 | |
| 713 | NewData = (ILubyte*)ialloc(NewBpp * Image->Bpc * Image->Width * Image->Height); |
| 714 | if (NewData == NULL) { |
| 715 | return IL_FALSE; |
| 716 | } |
| 717 | |
| 718 | switch (Image->Type) |
| 719 | { |
| 720 | case IL_BYTE: |
| 721 | case IL_UNSIGNED_BYTE: |
| 722 | for (; i < Size; i += Image->Bpp, j += NewBpp) { |
| 723 | NewData[j] = Image->Data[i]; |
| 724 | NewData[j+1] = Image->Data[i+1]; |
| 725 | NewData[j+2] = Image->Data[i+2]; |
| 726 | Same = IL_TRUE; |
| 727 | for (c = 0; c < Image->Bpp; c++) { |
| 728 | if (Image->Data[i+c] != KeyColour[c] * UCHAR_MAX) |
| 729 | Same = IL_FALSE; |
| 730 | } |
| 731 | |
| 732 | if (Same) |
| 733 | NewData[j+3] = 0; // Transparent - matches key colour |
| 734 | else |
| 735 | NewData[j+3] = UCHAR_MAX; |
| 736 | } |
no test coverage detected