ximalpha.cpp
Upload User: kairuinn
Upload Date: 2009-02-07
Package Size: 2922k
Code Size: 6k
Category:

Graph program

Development Platform:

Visual C++

  1. // xImalpha.cpp : Alpha channel functions
  2. /* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
  3.  * CxImage version 5.99a 08/Feb/2004
  4.  */
  5. #include "ximage.h"
  6. #if CXIMAGE_SUPPORT_ALPHA
  7. ////////////////////////////////////////////////////////////////////////////////
  8. void CxImage::AlphaClear()
  9. {
  10. if (pAlpha) memset(pAlpha,0,head.biWidth * head.biHeight);
  11. }
  12. ////////////////////////////////////////////////////////////////////////////////
  13. void CxImage::AlphaSet(BYTE level)
  14. {
  15. if (pAlpha) memset(pAlpha,level,head.biWidth * head.biHeight);
  16. }
  17. ////////////////////////////////////////////////////////////////////////////////
  18. void CxImage::AlphaCreate()
  19. {
  20. if (pAlpha==NULL) {
  21. pAlpha = (BYTE*)malloc(head.biWidth * head.biHeight);
  22. if (pAlpha) memset(pAlpha,255,head.biWidth * head.biHeight);
  23. }
  24. }
  25. ////////////////////////////////////////////////////////////////////////////////
  26. void CxImage::AlphaDelete()
  27. {
  28. if (pAlpha) { free(pAlpha); pAlpha=0; }
  29. }
  30. ////////////////////////////////////////////////////////////////////////////////
  31. void CxImage::AlphaInvert()
  32. {
  33. if (pAlpha) {
  34. BYTE *iSrc=pAlpha;
  35. long n=head.biHeight*head.biWidth;
  36. for(long i=0; i < n; i++){
  37. *iSrc=(BYTE)~(*(iSrc));
  38. iSrc++;
  39. }
  40. }
  41. }
  42. ////////////////////////////////////////////////////////////////////////////////
  43. bool CxImage::AlphaCopy(CxImage &from)
  44. {
  45. if (from.pAlpha == NULL || head.biWidth != from.head.biWidth || head.biHeight != from.head.biHeight) return false;
  46. if (pAlpha==NULL) pAlpha = (BYTE*)malloc(head.biWidth * head.biHeight);
  47. memcpy(pAlpha,from.pAlpha,head.biWidth * head.biHeight);
  48. info.nAlphaMax=from.info.nAlphaMax;
  49. return true;
  50. }
  51. ////////////////////////////////////////////////////////////////////////////////
  52. bool CxImage::AlphaSet(CxImage &from)
  53. {
  54. if (!from.IsGrayScale() || head.biWidth != from.head.biWidth || head.biHeight != from.head.biHeight) return false;
  55. if (pAlpha==NULL) pAlpha = (BYTE*)malloc(head.biWidth * head.biHeight);
  56. BYTE* src = from.info.pImage;
  57. BYTE* dst = pAlpha;
  58. for (long y=0; y<head.biHeight; y++){
  59. memcpy(dst,src,head.biWidth);
  60. dst += head.biWidth;
  61. src += from.info.dwEffWidth;
  62. }
  63. return true;
  64. }
  65. ////////////////////////////////////////////////////////////////////////////////
  66. void CxImage::AlphaSet(long x,long y,BYTE level)
  67. {
  68. if (pAlpha && IsInside(x,y)) pAlpha[x+y*head.biWidth]=level;
  69. }
  70. ////////////////////////////////////////////////////////////////////////////////
  71. BYTE CxImage::AlphaGet(long x,long y)
  72. {
  73. if (pAlpha && IsInside(x,y)) return pAlpha[x+y*head.biWidth];
  74. return 0;
  75. }
  76. ////////////////////////////////////////////////////////////////////////////////
  77. void CxImage::AlphaPaletteClear()
  78. {
  79. RGBQUAD c;
  80. for(WORD ip=0; ip<head.biClrUsed;ip++){
  81. c=GetPaletteColor((BYTE)ip);
  82. c.rgbReserved=0;
  83. SetPaletteColor((BYTE)ip,c);
  84. }
  85. }
  86. ////////////////////////////////////////////////////////////////////////////////
  87. bool CxImage::AlphaPaletteIsValid()
  88. {
  89. RGBQUAD c;
  90. for(WORD ip=0; ip<head.biClrUsed;ip++){
  91. c=GetPaletteColor((BYTE)ip);
  92. if (c.rgbReserved != 0) return true;
  93. }
  94. return false;
  95. }
  96. ////////////////////////////////////////////////////////////////////////////////
  97. void CxImage::AlphaStrip()
  98. {
  99. bool bAlphaPaletteIsValid = AlphaPaletteIsValid();
  100. bool bAlphaIsValid = AlphaIsValid();
  101. if (!(bAlphaIsValid || bAlphaPaletteIsValid)) return;
  102. RGBQUAD c;
  103. long a, a1;
  104. if (head.biBitCount==24){
  105. for(long y=0; y<head.biHeight; y++){
  106. for(long x=0; x<head.biWidth; x++){
  107. c=GetPixelColor(x,y);
  108. if (bAlphaIsValid) a=(AlphaGet(x,y)*info.nAlphaMax)/255; else a=info.nAlphaMax;
  109. a1 = 255-a;
  110. c.rgbBlue = (BYTE)((c.rgbBlue * a + a1 * info.nBkgndColor.rgbBlue)/255);
  111. c.rgbGreen = (BYTE)((c.rgbGreen * a + a1 * info.nBkgndColor.rgbGreen)/255);
  112. c.rgbRed = (BYTE)((c.rgbRed * a + a1 * info.nBkgndColor.rgbRed)/255);
  113. SetPixelColor(x,y,c);
  114. }
  115. }
  116. AlphaDelete();
  117. } else {
  118. CxImage tmp(head.biWidth,head.biHeight,24);
  119. for(long y=0; y<head.biHeight; y++){
  120. for(long x=0; x<head.biWidth; x++){
  121. c=GetPixelColor(x,y);
  122. if (bAlphaIsValid) a=(AlphaGet(x,y)*info.nAlphaMax)/255; else a=info.nAlphaMax;
  123. if (bAlphaPaletteIsValid) a=(c.rgbReserved*a)/255;
  124. a1 = 255-a;
  125. c.rgbBlue = (BYTE)((c.rgbBlue * a + a1 * info.nBkgndColor.rgbBlue)/255);
  126. c.rgbGreen = (BYTE)((c.rgbGreen * a + a1 * info.nBkgndColor.rgbGreen)/255);
  127. c.rgbRed = (BYTE)((c.rgbRed * a + a1 * info.nBkgndColor.rgbRed)/255);
  128. tmp.SetPixelColor(x,y,c);
  129. }
  130. }
  131. Transfer(tmp);
  132. }
  133. return;
  134. }
  135. ////////////////////////////////////////////////////////////////////////////////
  136. bool CxImage::AlphaFlip()
  137. {
  138. if (!pAlpha) return false;
  139. BYTE* pAlpha2 = (BYTE*)malloc(head.biWidth * head.biHeight);
  140. if (!pAlpha2) return false;
  141. BYTE *iSrc,*iDst;
  142. iSrc=pAlpha + (head.biHeight-1)*head.biWidth;
  143. iDst=pAlpha2;
  144.     for(long y=0; y < head.biHeight; y++){
  145. memcpy(iDst,iSrc,head.biWidth);
  146. iSrc-=head.biWidth;
  147. iDst+=head.biWidth;
  148. }
  149. free(pAlpha);
  150. pAlpha=pAlpha2;
  151. return true;
  152. }
  153. ////////////////////////////////////////////////////////////////////////////////
  154. bool CxImage::AlphaMirror()
  155. {
  156. if (!pAlpha) return false;
  157. BYTE* pAlpha2 = (BYTE*)malloc(head.biWidth * head.biHeight);
  158. if (!pAlpha2) return false;
  159. BYTE *iSrc,*iDst;
  160. long wdt=head.biWidth-1;
  161. iSrc=pAlpha + wdt;
  162. iDst=pAlpha2;
  163. for(long y=0; y < head.biHeight; y++){
  164. for(long x=0; x <= wdt; x++)
  165. *(iDst+x)=*(iSrc-x);
  166. iSrc+=head.biWidth;
  167. iDst+=head.biWidth;
  168. }
  169. free(pAlpha);
  170. pAlpha=pAlpha2;
  171. return true;
  172. }
  173. ////////////////////////////////////////////////////////////////////////////////
  174. bool CxImage::AlphaSplit(CxImage *dest)
  175. {
  176. if (!pAlpha || !dest) return false;
  177. CxImage tmp(head.biWidth,head.biHeight,8);
  178. for(long y=0; y<head.biHeight; y++){
  179. for(long x=0; x<head.biWidth; x++){
  180. tmp.SetPixelIndex(x,y,pAlpha[x+y*head.biWidth]);
  181. }
  182. }
  183. tmp.SetGrayPalette();
  184. dest->Transfer(tmp);
  185. return true;
  186. }
  187. ////////////////////////////////////////////////////////////////////////////////
  188. bool CxImage::AlphaPaletteSplit(CxImage *dest)
  189. {
  190. if (!AlphaPaletteIsValid() || !dest) return false;
  191. CxImage tmp(head.biWidth,head.biHeight,8);
  192. for(long y=0; y<head.biHeight; y++){
  193. for(long x=0; x<head.biWidth; x++){
  194. tmp.SetPixelIndex(x,y,GetPixelColor(x,y).rgbReserved);
  195. }
  196. }
  197. tmp.SetGrayPalette();
  198. dest->Transfer(tmp);
  199. return true;
  200. }
  201. ////////////////////////////////////////////////////////////////////////////////
  202. #endif //CXIMAGE_SUPPORT_ALPHA