1 /*
2 
3 SDL_gfxBlitFunc.h: custom blitters
4 
5 Copyright (C) 2001-2012  Andreas Schiffler
6 
7 This software is provided 'as-is', without any express or implied
8 warranty. In no event will the authors be held liable for any damages
9 arising from the use of this software.
10 
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute it
13 freely, subject to the following restrictions:
14 
15 1. The origin of this software must not be misrepresented; you must not
16 claim that you wrote the original software. If you use this software
17 in a product, an acknowledgment in the product documentation would be
18 appreciated but is not required.
19 
20 2. Altered source versions must be plainly marked as such, and must not be
21 misrepresented as being the original software.
22 
23 3. This notice may not be removed or altered from any source
24 distribution.
25 
26 Andreas Schiffler -- aschiffler at ferzkopp dot net
27 
28 */
29 
30 #ifndef _SDL_gfxBlitFunc_h
31 #define _SDL_gfxBlitFunc_h
32 
33 /* Set up for C function definitions, even when using C++ */
34 #ifdef __cplusplus
35 extern    "C" {
36 #endif
37 
38 #include <stdio.h>
39 #include <stdlib.h>
40 
41 #include "SDL.h"
42 #include "SDL_video.h"
43 
44 
45 	extern const unsigned int GFX_ALPHA_ADJUST_ARRAY[256];
46 
47 	/* ---- Function Prototypes */
48 
49 #ifdef _MSC_VER
50 #  if defined(DLL_EXPORT) && !defined(LIBSDL_GFX_DLL_IMPORT)
51 #    define SDL_GFXBLITFUNC_SCOPE __declspec(dllexport)
52 #  else
53 #    ifdef LIBSDL_GFX_DLL_IMPORT
54 #      define SDL_GFXBLITFUNC_SCOPE __declspec(dllimport)
55 #    endif
56 #  endif
57 #endif
58 #ifndef SDL_GFXBLITFUNC_SCOPE
59 #  define SDL_GFXBLITFUNC_SCOPE extern
60 #endif
61 
62 
63 	SDL_GFXBLITFUNC_SCOPE int SDL_gfxBlitRGBA(SDL_Surface * src, SDL_Rect * srcrect, SDL_Surface * dst, SDL_Rect * dstrect);
64 
65 	SDL_GFXBLITFUNC_SCOPE int SDL_gfxSetAlpha(SDL_Surface * src, Uint8 a);
66 
67 	SDL_GFXBLITFUNC_SCOPE int SDL_gfxMultiplyAlpha(SDL_Surface * src, Uint8 a);
68 
69 	/* -------- Macros */
70 
71 	/* Define SDL macros locally as a substitute for an #include "SDL_blit.h", */
72 	/* which doesn't work since the include file doesn't get installed.       */
73 
74 	/*!
75 	\brief The structure passed to the low level blit functions.
76 	*/
77 	typedef struct {
78 		Uint8    *s_pixels;
79 		int       s_width;
80 		int       s_height;
81 		int       s_skip;
82 		Uint8    *d_pixels;
83 		int       d_width;
84 		int       d_height;
85 		int       d_skip;
86 		void     *aux_data;
87 		SDL_PixelFormat *src;
88 		Uint8    *table;
89 		SDL_PixelFormat *dst;
90 	} SDL_gfxBlitInfo;
91 
92 	/*!
93 	\brief Unwrap RGBA values from a pixel using mask, shift and loss for surface.
94 	*/
95 #define GFX_RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a)				\
96 	{									\
97 	r = ((pixel&fmt->Rmask)>>fmt->Rshift)<<fmt->Rloss; 		\
98 	g = ((pixel&fmt->Gmask)>>fmt->Gshift)<<fmt->Gloss; 		\
99 	b = ((pixel&fmt->Bmask)>>fmt->Bshift)<<fmt->Bloss; 		\
100 	a = ((pixel&fmt->Amask)>>fmt->Ashift)<<fmt->Aloss;	 	\
101 	}
102 
103 	/*!
104 	\brief Disassemble buffer pointer into a pixel and separate RGBA values.
105 	*/
106 #define GFX_DISASSEMBLE_RGBA(buf, bpp, fmt, pixel, r, g, b, a)			   \
107 	do {									   \
108 	pixel = *((Uint32 *)(buf));			   		   \
109 	GFX_RGBA_FROM_PIXEL(pixel, fmt, r, g, b, a);			   \
110 	pixel &= ~fmt->Amask;						   \
111 	} while(0)
112 
113 	/*!
114 	\brief Wrap a pixel from RGBA values using mask, shift and loss for surface.
115 	*/
116 #define GFX_PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a)				\
117 	{									\
118 	pixel = ((r>>fmt->Rloss)<<fmt->Rshift)|				\
119 	((g>>fmt->Gloss)<<fmt->Gshift)|				\
120 	((b>>fmt->Bloss)<<fmt->Bshift)|				\
121 	((a<<fmt->Aloss)<<fmt->Ashift);				\
122 	}
123 
124 	/*!
125 	\brief Assemble pixel into buffer pointer from separate RGBA values.
126 	*/
127 #define GFX_ASSEMBLE_RGBA(buf, bpp, fmt, r, g, b, a)			\
128 	{									\
129 	Uint32 pixel;					\
130 	\
131 	GFX_PIXEL_FROM_RGBA(pixel, fmt, r, g, b, a);	\
132 	*((Uint32 *)(buf)) = pixel;			\
133 	}
134 
135 	/*!
136 	\brief Blend the RGB values of two pixels based on a source alpha value.
137 	*/
138 #define GFX_ALPHA_BLEND(sR, sG, sB, A, dR, dG, dB)	\
139 	do {						\
140 	dR = (((sR-dR)*(A))/255)+dR;		\
141 	dG = (((sG-dG)*(A))/255)+dG;		\
142 	dB = (((sB-dB)*(A))/255)+dB;		\
143 	} while(0)
144 
145 	/*!
146 	\brief 4-times unrolled DUFFs loop.
147 
148 	This is a very useful loop for optimizing blitters.
149 	*/
150 #define GFX_DUFFS_LOOP4(pixel_copy_increment, width)			\
151 	{ int n = (width+3)/4;							\
152 	switch (width & 3) {						\
153 	case 0: do {	pixel_copy_increment;				\
154 	case 3:		pixel_copy_increment;				\
155 	case 2:		pixel_copy_increment;				\
156 	case 1:		pixel_copy_increment;				\
157 	} while ( --n > 0 );					\
158 	}								\
159 	}
160 
161 
162 
163 	/* Ends C function definitions when using C++ */
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif /* _SDL_gfxBlitFunc_h */
169