1 /*      _______   __   __   __   ______   __   __   _______   __   __
2  *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
3  *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
4  *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
5  *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
6  * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
7  * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
8  *
9  * Copyright (c) 2004, 2005, 2006, 2007 Olof Naess�n and Per Larsson
10  *
11  *                                                         Js_./
12  * Per Larsson a.k.a finalman                          _RqZ{a<^_aa
13  * Olof Naess�n a.k.a jansem/yakslem                _asww7!uY`>  )\a//
14  *                                                 _Qhm`] _f "'c  1!5m
15  * Visit: http://guichan.darkbits.org             )Qk<P ` _: :+' .'  "{[
16  *                                               .)j(] .d_/ '-(  P .   S
17  * License: (BSD)                                <Td/Z <fP"5(\"??"\a.  .L
18  * Redistribution and use in source and          _dV>ws?a-?'      ._/L  #'
19  * binary forms, with or without                 )4d[#7r, .   '     )d`)[
20  * modification, are permitted provided         _Q-5'5W..j/?'   -?!\)cam'
21  * that the following conditions are met:       j<<WP+k/);.        _W=j f
22  * 1. Redistributions of source code must       .$%w\/]Q  . ."'  .  mj$
23  *    retain the above copyright notice,        ]E.pYY(Q]>.   a     J@\
24  *    this list of conditions and the           j(]1u<sE"L,. .   ./^ ]{a
25  *    following disclaimer.                     4'_uomm\.  )L);-4     (3=
26  * 2. Redistributions in binary form must        )_]X{Z('a_"a7'<a"a,  ]"[
27  *    reproduce the above copyright notice,       #}<]m7`Za??4,P-"'7. ).m
28  *    this list of conditions and the            ]d2e)Q(<Q(  ?94   b-  LQ/
29  *    following disclaimer in the                <B!</]C)d_, '(<' .f. =C+m
30  *    documentation and/or other materials      .Z!=J ]e []('-4f _ ) -.)m]'
31  *    provided with the distribution.          .w[5]' _[ /.)_-"+?   _/ <W"
32  * 3. Neither the name of Guichan nor the      :$we` _! + _/ .        j?
33  *    names of its contributors may be used     =3)= _f  (_yQmWW$#(    "
34  *    to endorse or promote products derived     -   W,  sQQQQmZQ#Wwa]..
35  *    from this software without specific        (js, \[QQW$QWW#?!V"".
36  *    prior written permission.                    ]y:.<\..          .
37  *                                                 -]n w/ '         [.
38  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT       )/ )/           !
39  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY         <  (; sac    ,    '
40  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,               ]^ .-  %
41  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF            c <   r
42  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR            aga<  <La
43  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE          5%  )P'-3L
44  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR        _bQf` y`..)a
45  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,          ,J?4P'.P"_(\?d'.,
46  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES               _Pa,)!f/<[]/  ?"
47  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT      _2-..:. .r+_,.. .
48  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,     ?a.<%"'  " -'.a_ _,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION)                     ^
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
51  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
53  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
54  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
55  */
56 
57 /*
58  * For comments regarding functions please see the header file.
59  */
60 
61 #include "guichan/sdl/sdlimage.hpp"
62 
63 #include <SDL/SDL_image.h>
64 
65 #include "guichan/exception.hpp"
66 #include "guichan/sdl/sdlimageloader.hpp"
67 
68 namespace gcn
69 {
load(const std::string & filename,bool convertToDisplayFormat)70     Image* SDLImageLoader::load(const std::string& filename,
71                                 bool convertToDisplayFormat)
72     {
73         SDL_Surface *loadedSurface = loadSDLSurface(filename);
74 
75         if (loadedSurface == NULL)
76         {
77             throw GCN_EXCEPTION(
78                     std::string("Unable to load image file: ") + filename);
79         }
80 
81         SDL_Surface *surface = convertToStandardFormat(loadedSurface);
82         SDL_FreeSurface(loadedSurface);
83 
84         if (surface == NULL)
85         {
86             throw GCN_EXCEPTION(
87                     std::string("Not enough memory to load: ") + filename);
88         }
89 
90         Image *image = new SDLImage(surface, true);
91 
92         if (convertToDisplayFormat)
93         {
94             image->convertToDisplayFormat();
95         }
96 
97         return image;
98     }
99 
loadSDLSurface(const std::string & filename)100     SDL_Surface* SDLImageLoader::loadSDLSurface(const std::string& filename)
101     {
102         return IMG_Load(filename.c_str());
103     }
104 
convertToStandardFormat(SDL_Surface * surface)105     SDL_Surface* SDLImageLoader::convertToStandardFormat(SDL_Surface* surface)
106     {
107         Uint32 rmask, gmask, bmask, amask;
108 #if SDL_BYTEORDER == SDL_BIG_ENDIAN
109         rmask = 0xff000000;
110         gmask = 0x00ff0000;
111         bmask = 0x0000ff00;
112         amask = 0x000000ff;
113 #else
114         rmask = 0x000000ff;
115         gmask = 0x0000ff00;
116         bmask = 0x00ff0000;
117         amask = 0xff000000;
118 #endif
119 
120         SDL_Surface *colorSurface = SDL_CreateRGBSurface(SDL_SWSURFACE,
121                 0, 0, 32,
122                 rmask, gmask, bmask, amask);
123 
124         SDL_Surface *tmp = NULL;
125 
126         if (colorSurface != NULL)
127         {
128             tmp = SDL_ConvertSurface(surface, colorSurface->format,
129                                      SDL_SWSURFACE);
130             SDL_FreeSurface(colorSurface);
131         }
132 
133         return tmp;
134     }
135 }
136