Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

blit_macros.h

Go to the documentation of this file.
00001 /*
00002         The ClanLib Game SDK, but the source has been taken from:
00003         (just yet ANOTHER advantage of open source :-) )
00004 
00005     SDL - Simple DirectMedia Layer
00006     Copyright (C) 1997, 1998  Sam Lantinga
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public
00019     License along with this library; if not, write to the Free
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022     Sam Lantinga
00023     5635-34 Springhouse Dr.
00024     Pleasanton, CA 94588 (USA)
00025     slouken@devolution.com
00026 */
00027 
00028 #ifndef header_blit_macros
00029 #define header_blit_macros
00030 
00031 struct SDL_PixelFormat
00032 {
00033         unsigned char  BitsPerPixel;
00034         unsigned char  BytesPerPixel;
00035         unsigned char  Rloss;
00036         unsigned char  Gloss;
00037         unsigned char  Bloss;   
00038         unsigned char  Aloss;   
00039         unsigned char  Rshift;
00040         unsigned char  Gshift;
00041         unsigned char  Bshift;
00042         unsigned char  Ashift;
00043         unsigned int   Rmask;
00044         unsigned int   Gmask;
00045         unsigned int   Bmask;
00046         unsigned int   Amask;
00047 
00048         /* RGB color key information */
00049         unsigned int   colorkey;
00050 
00051         /* Alpha value information (per-surface alpha) */
00052         unsigned char  alpha;
00053 };
00054 
00055 
00056 /*
00057  * Useful macros for blitting routines
00058  */
00059 
00060 #define FORMAT_EQUAL(A, B) \
00061         (((A)->BitsPerPixel == (B)->BitsPerPixel) && ((A)->Rmask == (B)->Rmask))
00062 
00063 /* Load pixel of the specified format from a buffer and get its R-G-B values */
00064 #define RGB_FROM_PIXEL(pixel, fmt, r, g, b)                             \
00065 {                                                                       \
00066         r = (((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss);            \
00067         g = (((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss);            \
00068         b = (((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss);            \
00069 }
00070 #define DISEMBLE_RGB(buf, bpp, fmt, pixel, r, g, b)                     \
00071 {                                                                       \
00072         switch (bpp) {                                                  \
00073                 case 2:                                                 \
00074                         pixel = *((unsigned short *)(buf));                     \
00075                         RGB_FROM_PIXEL(pixel, fmt, r, g, b);            \
00076                 break;                                                  \
00077                                                                         \
00078                 case 3:                                                 \
00079                         r = *(((unsigned char *)buf)+fmt->Rshift/8);            \
00080                         g = *(((unsigned char *)buf)+fmt->Gshift/8);            \
00081                         b = *(((unsigned char *)buf)+fmt->Bshift/8);            \
00082                         pixel = (r<<fmt->Rshift)|                       \
00083                                 (g<<fmt->Gshift)|                       \
00084                                 (b<<fmt->Bshift);                       \
00085                 break;                                                  \
00086                                                                         \
00087                 case 4:                                                 \
00088                         pixel = *((unsigned int *)(buf));                       \
00089                         RGB_FROM_PIXEL(pixel, fmt, r, g, b);            \
00090                 break;                                                  \
00091         }                                                               \
00092 }
00093 /* Assemble R-G-B values into a specified pixel format and store them */
00094 #define PIXEL_FROM_RGB(pixel, fmt, r, g, b)                             \
00095 {                                                                       \
00096         pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|                         \
00097                 ((g>>fmt->Gloss)<<fmt->Gshift)|                         \
00098                 ((b>>fmt->Bloss)<<fmt->Bshift);                         \
00099 }
00100 #define ASSEMBLE_RGB(buf, bpp, fmt, r, g, b)                            \
00101 {                                                                       \
00102         switch (bpp) {                                                  \
00103                 case 2: {                                               \
00104                         unsigned short pixel;                                   \
00105                                                                         \
00106                         PIXEL_FROM_RGB(pixel, fmt, r, g, b);            \
00107                         *((unsigned short *)(buf)) = pixel;                     \
00108                 }                                                       \
00109                 break;                                                  \
00110                                                                         \
00111                 case 3: {                                               \
00112                         *((buf)+fmt->Rshift/8) = r;                     \
00113                         *((buf)+fmt->Gshift/8) = g;                     \
00114                         *((buf)+fmt->Bshift/8) = b;                     \
00115                 }                                                       \
00116                 break;                                                  \
00117                                                                         \
00118                 case 4: {                                               \
00119                         unsigned int pixel;                                     \
00120                                                                         \
00121                         PIXEL_FROM_RGB(pixel, fmt, r, g, b);            \
00122                         *((unsigned int *)(buf)) = pixel;                       \
00123                 }                                                       \
00124                 break;                                                  \
00125         }                                                               \
00126 }
00127 
00128 #define RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a)                         \
00129 {                                                                       \
00130         r = (((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss);            \
00131         g = (((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss);            \
00132         b = (((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss);            \
00133         a =  ((pixel&fmt->Amask)>>fmt->Ashift);                         \
00134 }
00135 #define DISEMBLE_RGBA(buf, bpp, fmt, pixel, r, g, b, a)                 \
00136 {                                                                       \
00137         switch (bpp) {                                                  \
00138                 case 2:                                                 \
00139                         pixel = *((unsigned short *)(buf));                     \
00140                         RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a);        \
00141                         pixel &= ~fmt->Amask;                           \
00142                 break;                                                  \
00143                                                                         \
00144                 case 3: /* FIXME: broken code (no alpha) */             \
00145                         r = *(((unsigned char *)buf)+fmt->Rshift/8);            \
00146                         g = *(((unsigned char *)buf)+fmt->Gshift/8);            \
00147                         b = *(((unsigned char *)buf)+fmt->Bshift/8);            \
00148                         a = 0;                                          \
00149                         pixel = (r<<fmt->Rshift)|                       \
00150                                 (g<<fmt->Gshift)|                       \
00151                                 (b<<fmt->Bshift);                       \
00152                 break;                                                  \
00153                                                                         \
00154                 case 4:                                                 \
00155                         pixel = *((unsigned int *)(buf));                       \
00156                         RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a);        \
00157                         pixel &= ~fmt->Amask;                           \
00158                 break;                                                  \
00159         }                                                               \
00160 }
00161 
00162 #define PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a)                         \
00163 {                                                                       \
00164         pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|                         \
00165                 ((g>>fmt->Gloss)<<fmt->Gshift)|                         \
00166                 ((b>>fmt->Bloss)<<fmt->Bshift)|                         \
00167                 (a<<fmt->Ashift);                                       \
00168 }
00169 #define ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)                        \
00170 {                                                                       \
00171         switch (bpp) {                                                  \
00172                 case 2: {                                               \
00173                         unsigned short pixel;                                   \
00174                                                                         \
00175                         PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a);        \
00176                         *((unsigned short *)(buf)) = pixel;                     \
00177                 }                                                       \
00178                 break;                                                  \
00179                                                                         \
00180                 case 3: { /* FIXME: broken code (no alpha) */           \
00181                         *((buf)+fmt->Rshift/8) = r;                     \
00182                         *((buf)+fmt->Gshift/8) = g;                     \
00183                         *((buf)+fmt->Bshift/8) = b;                     \
00184                 }                                                       \
00185                 break;                                                  \
00186                                                                         \
00187                 case 4: {                                               \
00188                         unsigned int pixel;                                     \
00189                                                                         \
00190                         PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a);        \
00191                         *((unsigned int *)(buf)) = pixel;                       \
00192                 }                                                       \
00193                 break;                                                  \
00194         }                                                               \
00195 }
00196 
00197 /* Blend the RGB values of two pixels based on a source alpha value */
00198 #define ALPHA_BLEND(sR, sG, sB, A, Adiv, dR, dG, dB)                    \
00199 {                                                                       \
00200         dR = ((unsigned short)sR*(Adiv-A) + (unsigned short)dR*A) / Adiv;               \
00201         dG = ((unsigned short)sG*(Adiv-A) + (unsigned short)dG*A) / Adiv;               \
00202         dB = ((unsigned short)sB*(Adiv-A) + (unsigned short)dB*A) / Adiv;               \
00203 }
00204 
00205 #endif

Generated at Wed Apr 4 19:53:58 2001 for ClanLib by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001