00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00049 unsigned int colorkey;
00050
00051
00052 unsigned char alpha;
00053 };
00054
00055
00056
00057
00058
00059
00060 #define FORMAT_EQUAL(A, B) \
00061 (((A)->BitsPerPixel == (B)->BitsPerPixel) && ((A)->Rmask == (B)->Rmask))
00062
00063
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
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: \
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: { \
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
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