1 // xImalpha.cpp : Alpha channel functions
2 /* 07/08/2001 v1.00 - Davide Pizzolato - www.xdp.it
3 * CxImage version 6.0.0 02/Feb/2008
8 #if CXIMAGE_SUPPORT_ALPHA
10 ////////////////////////////////////////////////////////////////////////////////
14 BYTE CxImage::AlphaGetMax() const
16 return info.nAlphaMax;
18 ////////////////////////////////////////////////////////////////////////////////
20 * Sets global Alpha (opacity) value applied to the whole image,
21 * valid only for painting functions.
22 * \param nAlphaMax: can be from 0 to 255
24 void CxImage::AlphaSetMax(BYTE nAlphaMax)
26 info.nAlphaMax=nAlphaMax;
28 ////////////////////////////////////////////////////////////////////////////////
30 * Checks if the image has a valid alpha channel.
32 bool CxImage::AlphaIsValid()
36 ////////////////////////////////////////////////////////////////////////////////
38 * Enables the alpha palette, so the Draw() function changes its behavior.
40 void CxImage::AlphaPaletteEnable(bool enable)
42 info.bAlphaPaletteEnabled=enable;
44 ////////////////////////////////////////////////////////////////////////////////
46 * True if the alpha palette is enabled for painting.
48 bool CxImage::AlphaPaletteIsEnabled()
50 return info.bAlphaPaletteEnabled;
52 ////////////////////////////////////////////////////////////////////////////////
54 * Sets the alpha channel to full transparent. AlphaSet(0) has the same effect
56 void CxImage::AlphaClear()
58 if (pAlpha) memset(pAlpha,0,head.biWidth * head.biHeight);
60 ////////////////////////////////////////////////////////////////////////////////
62 * Sets the alpha level for the whole image.
63 * \param level : from 0 (transparent) to 255 (opaque)
65 void CxImage::AlphaSet(BYTE level)
67 if (pAlpha) memset(pAlpha,level,head.biWidth * head.biHeight);
69 ////////////////////////////////////////////////////////////////////////////////
71 * Allocates an empty (opaque) alpha channel.
73 bool CxImage::AlphaCreate()
76 pAlpha = (BYTE*)malloc(head.biWidth * head.biHeight);
77 if (pAlpha) memset(pAlpha,255,head.biWidth * head.biHeight);
81 ////////////////////////////////////////////////////////////////////////////////
82 void CxImage::AlphaDelete()
84 if (pAlpha) { free(pAlpha); pAlpha=0; }
86 ////////////////////////////////////////////////////////////////////////////////
87 void CxImage::AlphaInvert()
91 long n=head.biHeight*head.biWidth;
92 for(long i=0; i < n; i++){
93 *iSrc=(BYTE)~(*(iSrc));
98 ////////////////////////////////////////////////////////////////////////////////
100 * Imports an existing alpa channel from another image with the same width and height.
102 bool CxImage::AlphaCopy(CxImage &from)
104 if (from.pAlpha == NULL || head.biWidth != from.head.biWidth || head.biHeight != from.head.biHeight) return false;
105 if (pAlpha==NULL) pAlpha = (BYTE*)malloc(head.biWidth * head.biHeight);
106 if (pAlpha==NULL) return false;
107 memcpy(pAlpha,from.pAlpha,head.biWidth * head.biHeight);
108 info.nAlphaMax=from.info.nAlphaMax;
111 ////////////////////////////////////////////////////////////////////////////////
113 * Creates the alpha channel from a gray scale image.
115 bool CxImage::AlphaSet(CxImage &from)
117 if (!from.IsGrayScale() || head.biWidth != from.head.biWidth || head.biHeight != from.head.biHeight) return false;
118 if (pAlpha==NULL) pAlpha = (BYTE*)malloc(head.biWidth * head.biHeight);
119 BYTE* src = from.info.pImage;
121 if (src==NULL || dst==NULL) return false;
122 for (long y=0; y<head.biHeight; y++){
123 memcpy(dst,src,head.biWidth);
125 src += from.info.dwEffWidth;
129 ////////////////////////////////////////////////////////////////////////////////
131 * Sets the alpha level for a single pixel
133 void CxImage::AlphaSet(const long x,const long y,const BYTE level)
135 if (pAlpha && IsInside(x,y)) pAlpha[x+y*head.biWidth]=level;
137 ////////////////////////////////////////////////////////////////////////////////
139 * Gets the alpha level for a single pixel
141 BYTE CxImage::AlphaGet(const long x,const long y)
143 if (pAlpha && IsInside(x,y)) return pAlpha[x+y*head.biWidth];
146 ////////////////////////////////////////////////////////////////////////////////
148 * Returns pointer to alpha data for pixel (x,y).
150 * \author ***bd*** 2.2004
152 BYTE* CxImage::AlphaGetPointer(const long x,const long y)
154 if (pAlpha && IsInside(x,y)) return pAlpha+x+y*head.biWidth;
157 ////////////////////////////////////////////////////////////////////////////////
159 * Get alpha value without boundscheck (a bit faster). Pixel must be inside the image.
161 * \author ***bd*** 2.2004
163 BYTE CxImage::BlindAlphaGet(const long x,const long y)
166 if (!IsInside(x,y) || (pAlpha==0))
167 #if CXIMAGE_SUPPORT_EXCEPTION_HANDLING
173 return pAlpha[x+y*head.biWidth];
175 ////////////////////////////////////////////////////////////////////////////////
177 * Resets the alpha palette
179 void CxImage::AlphaPaletteClear()
182 for(WORD ip=0; ip<head.biClrUsed;ip++){
183 c=GetPaletteColor((BYTE)ip);
185 SetPaletteColor((BYTE)ip,c);
188 ////////////////////////////////////////////////////////////////////////////////
190 * Checks if the image has a valid alpha palette.
192 bool CxImage::AlphaPaletteIsValid()
195 for(WORD ip=0; ip<head.biClrUsed;ip++){
196 c=GetPaletteColor((BYTE)ip);
197 if (c.rgbReserved != 0) return true;
201 ////////////////////////////////////////////////////////////////////////////////
203 * Blends the alpha channel and the alpha palette with the pixels. The result is a 24 bit image.
204 * The background color can be selected using SetTransColor().
206 void CxImage::AlphaStrip()
208 bool bAlphaPaletteIsValid = AlphaPaletteIsValid();
209 bool bAlphaIsValid = AlphaIsValid();
210 if (!(bAlphaIsValid || bAlphaPaletteIsValid)) return;
213 if (head.biBitCount==24){
214 for(long y=0; y<head.biHeight; y++){
215 for(long x=0; x<head.biWidth; x++){
216 c = BlindGetPixelColor(x,y);
217 if (bAlphaIsValid) a=(BlindAlphaGet(x,y)*info.nAlphaMax)/255; else a=info.nAlphaMax;
219 c.rgbBlue = (BYTE)((c.rgbBlue * a + a1 * info.nBkgndColor.rgbBlue)>>8);
220 c.rgbGreen = (BYTE)((c.rgbGreen * a + a1 * info.nBkgndColor.rgbGreen)>>8);
221 c.rgbRed = (BYTE)((c.rgbRed * a + a1 * info.nBkgndColor.rgbRed)>>8);
222 BlindSetPixelColor(x,y,c);
227 CxImage tmp(head.biWidth,head.biHeight,24);
229 strcpy(info.szLastError,tmp.GetLastError());
233 for(long y=0; y<head.biHeight; y++){
234 for(long x=0; x<head.biWidth; x++){
235 c = BlindGetPixelColor(x,y);
236 if (bAlphaIsValid) a=(BlindAlphaGet(x,y)*info.nAlphaMax)/255; else a=info.nAlphaMax;
237 if (bAlphaPaletteIsValid) a=(c.rgbReserved*a)/255;
239 c.rgbBlue = (BYTE)((c.rgbBlue * a + a1 * info.nBkgndColor.rgbBlue)>>8);
240 c.rgbGreen = (BYTE)((c.rgbGreen * a + a1 * info.nBkgndColor.rgbGreen)>>8);
241 c.rgbRed = (BYTE)((c.rgbRed * a + a1 * info.nBkgndColor.rgbRed)>>8);
242 tmp.BlindSetPixelColor(x,y,c);
249 ////////////////////////////////////////////////////////////////////////////////
250 bool CxImage::AlphaFlip()
252 if (!pAlpha) return false;
254 BYTE *buff = (BYTE*)malloc(head.biWidth);
255 if (!buff) return false;
258 iSrc = pAlpha + (head.biHeight-1)*head.biWidth;
260 for (long i=0; i<(head.biHeight/2); ++i)
262 memcpy(buff, iSrc, head.biWidth);
263 memcpy(iSrc, iDst, head.biWidth);
264 memcpy(iDst, buff, head.biWidth);
273 ////////////////////////////////////////////////////////////////////////////////
274 bool CxImage::AlphaMirror()
276 if (!pAlpha) return false;
277 BYTE* pAlpha2 = (BYTE*)malloc(head.biWidth * head.biHeight);
278 if (!pAlpha2) return false;
280 long wdt=head.biWidth-1;
283 for(long y=0; y < head.biHeight; y++){
284 for(long x=0; x <= wdt; x++)
293 ////////////////////////////////////////////////////////////////////////////////
295 * Exports the alpha channel in a 8bpp grayscale image.
297 bool CxImage::AlphaSplit(CxImage *dest)
299 if (!pAlpha || !dest) return false;
301 CxImage tmp(head.biWidth,head.biHeight,8);
303 strcpy(info.szLastError,tmp.GetLastError());
307 for(long y=0; y<head.biHeight; y++){
308 for(long x=0; x<head.biWidth; x++){
309 tmp.BlindSetPixelIndex(x,y,pAlpha[x+y*head.biWidth]);
313 tmp.SetGrayPalette();
318 ////////////////////////////////////////////////////////////////////////////////
320 * Exports the alpha palette channel in a 8bpp grayscale image.
322 bool CxImage::AlphaPaletteSplit(CxImage *dest)
324 if (!AlphaPaletteIsValid() || !dest) return false;
326 CxImage tmp(head.biWidth,head.biHeight,8);
328 strcpy(info.szLastError,tmp.GetLastError());
332 for(long y=0; y<head.biHeight; y++){
333 for(long x=0; x<head.biWidth; x++){
334 tmp.BlindSetPixelIndex(x,y,BlindGetPixelColor(x,y).rgbReserved);
338 tmp.SetGrayPalette();
343 ////////////////////////////////////////////////////////////////////////////////
345 * Merge in the alpha layer the transparent color mask
346 * (previously set with SetTransColor or SetTransIndex)
348 bool CxImage::AlphaFromTransparency()
350 if (!IsValid() || !IsTransparent())
355 for(long y=0; y<head.biHeight; y++){
356 for(long x=0; x<head.biWidth; x++){
357 if (IsTransparent(x,y)){
364 ////////////////////////////////////////////////////////////////////////////////
365 #endif //CXIMAGE_SUPPORT_ALPHA