sdlpixel.hpp

00001 /*      _______   __   __   __   ______   __   __   _______   __   __
00002  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
00003  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
00004  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
00005  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
00006  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
00007  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
00008  *
00009  * Copyright (c) 2004, 2005, 2006 Olof Naessén and Per Larsson
00010  *
00011  *                                                         Js_./
00012  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
00013  * Olof Naessén a.k.a jansem/yakslem                _asww7!uY`>  )\a//
00014  *                                                 _Qhm`] _f "'c  1!5m
00015  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
00016  *                                               .)j(] .d_/ '-(  P .   S
00017  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
00018  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
00019  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
00020  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
00021  * that the following conditions are met:       j<<WP+k/);.        _W=j f
00022  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
00023  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
00024  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
00025  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
00026  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
00027  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
00028  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
00029  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
00030  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
00031  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
00032  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
00033  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
00034  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
00035  *    from this software without specific        (js, \[QQW$QWW#?!V"".
00036  *    prior written permission.                    ]y:.<\..          .
00037  *                                                 -]n w/ '         [.
00038  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
00039  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
00040  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
00041  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
00042  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
00043  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
00044  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
00045  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
00046  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
00047  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
00048  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
00049  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
00050  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00051  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00052  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00053  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00054  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00055  */
00056 
00057 #ifndef GCN_SDLPIXEL_HPP
00058 #define GCN_SDLPIXEL_HPP
00059 
00060 #include "SDL.h"
00061 #include "guichan/color.hpp"
00062 
00063 namespace gcn
00064 {
00065 
00074     inline const Color SDLgetPixel(SDL_Surface* surface, int x, int y)
00075     {
00076         int bpp = surface->format->BytesPerPixel;
00077 
00078         SDL_LockSurface(surface);
00079 
00080         Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00081 
00082         unsigned int color = 0;
00083 
00084         switch(bpp)
00085         {
00086           case 1:
00087               color = *p;
00088               break;
00089 
00090           case 2:
00091               color = *(Uint16 *)p;
00092               break;
00093 
00094           case 3:
00095               if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00096               {
00097                   color = p[0] << 16 | p[1] << 8 | p[2];
00098               }
00099               else
00100               {
00101                   color = p[0] | p[1] << 8 | p[2] << 16;
00102               }
00103               break;
00104 
00105           case 4:
00106               color = *(Uint32 *)p;
00107               break;
00108 
00109         }
00110 
00111         unsigned char r,g,b,a;
00112 
00113         SDL_GetRGBA(color, surface->format, &r, &g, &b, &a);
00114         SDL_UnlockSurface(surface);
00115 
00116         return Color(r,g,b,a);
00117     }
00118 
00126     inline void SDLputPixel(SDL_Surface* surface, int x, int y, const Color& color)
00127     {
00128         int bpp = surface->format->BytesPerPixel;
00129 
00130         SDL_LockSurface(surface);
00131 
00132         Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00133 
00134         Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b);
00135 
00136         switch(bpp)
00137         {
00138           case 1:
00139               *p = pixel;
00140               break;
00141 
00142           case 2:
00143               *(Uint16 *)p = pixel;
00144               break;
00145 
00146           case 3:
00147               if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00148               {
00149                   p[0] = (pixel >> 16) & 0xff;
00150                   p[1] = (pixel >> 8) & 0xff;
00151                   p[2] = pixel & 0xff;
00152               }
00153               else
00154               {
00155                   p[0] = pixel & 0xff;
00156                   p[1] = (pixel >> 8) & 0xff;
00157                   p[2] = (pixel >> 16) & 0xff;
00158               }
00159               break;
00160 
00161           case 4:
00162               *(Uint32 *)p = pixel;
00163               break;
00164         }
00165 
00166         SDL_UnlockSurface(surface);
00167     }
00168 
00176     inline unsigned int SDLAlpha32(unsigned int src, unsigned int dst, unsigned char a)
00177     {
00178         unsigned int b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8;
00179         unsigned int g = ((src & 0xff00) * a + (dst & 0xff00) * (255 - a)) >> 8;
00180         unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000) * (255 - a)) >> 8;
00181 
00182         return (b & 0xff) | (g & 0xff00) | (r & 0xff0000);
00183     }
00184 
00192     inline unsigned short SDLAlpha16(unsigned short src, unsigned short dst, unsigned char a, const SDL_PixelFormat *f)
00193     {
00194         unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask) * (255 - a)) >> 8;
00195         unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask) * (255 - a)) >> 8;
00196         unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask) * (255 - a)) >> 8;
00197 
00198         return (unsigned short)((b & f->Rmask) | (g & f->Gmask) | (r & f->Bmask));
00199     }
00200 
00201     /*
00202     typedef struct{
00203         SDL_Palette *palette;
00204         Uint8  BitsPerPixel;
00205         Uint8  BytesPerPixel;
00206         Uint32 Rmask, Gmask, Bmask, Amask;
00207         Uint8  Rshift, Gshift, Bshift, Ashift;
00208         Uint8  Rloss, Gloss, Bloss, Aloss;
00209         Uint32 colorkey;
00210         Uint8  alpha;
00211     } SDL_PixelFormat;
00212     */
00213 
00221     inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y, const Color& color)
00222     {
00223         int bpp = surface->format->BytesPerPixel;
00224 
00225         SDL_LockSurface(surface);
00226 
00227         Uint8 *p = (Uint8 *)surface->pixels + y * surface->pitch + x * bpp;
00228 
00229         Uint32 pixel = SDL_MapRGB(surface->format, color.r, color.g, color.b);
00230 
00231         switch(bpp)
00232         {
00233           case 1:
00234               *p = pixel;
00235               break;
00236 
00237           case 2:
00238               *(Uint16 *)p = SDLAlpha16(pixel, *(Uint32 *)p, color.a, surface->format);
00239               break;
00240 
00241           case 3:
00242               unsigned int c;
00243               if(SDL_BYTEORDER == SDL_BIG_ENDIAN)
00244               {
00245                   unsigned int r = (p[0] * (255 - color.a) + color.r * color.a) >> 8;
00246                   unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
00247                   unsigned int b = (p[2] * (255 - color.a) + color.b * color.a) >> 8;
00248 
00249                   p[2] = b;
00250                   p[1] = g;
00251                   p[0] = r;
00252               }
00253               else
00254               {
00255                   unsigned int r = (p[2] * (255 - color.a) + color.r * color.a) >> 8;
00256                   unsigned int g = (p[1] * (255 - color.a) + color.g * color.a) >> 8;
00257                   unsigned int b = (p[0] * (255 - color.a) + color.b * color.a) >> 8;
00258 
00259                   p[0] = b;
00260                   p[1] = g;
00261                   p[2] = r;
00262               }
00263               break;
00264 
00265           case 4:
00266               *(Uint32 *)p = SDLAlpha32(pixel, *(Uint32 *)p, color.a);
00267               break;
00268         }
00269 
00270         SDL_UnlockSurface(surface);
00271     }
00272 }
00273 
00274 #endif // end GCN_SDLPIXEL_HPP

Generated on Sat Jul 29 19:38:48 2006 for Guichan by  doxygen 1.4.7