GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/sdlpixel.h Lines: 0 73 0.0 %
Date: 2017-11-29 Branches: 0 14 0.0 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2011-2017  The ManaPlus Developers
4
 *
5
 *  This file is part of The ManaPlus Client.
6
 *
7
 *  This program is free software; you can redistribute it and/or modify
8
 *  it under the terms of the GNU General Public License as published by
9
 *  the Free Software Foundation; either version 2 of the License, or
10
 *  any later version.
11
 *
12
 *  This program is distributed in the hope that it will be useful,
13
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 *  GNU General Public License for more details.
16
 *
17
 *  You should have received a copy of the GNU General Public License
18
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/*      _______   __   __  __   ______   __   __   _______   __   __
22
 *     / _____/\ / /\ / /\ / /\ / ____/\ / /\ / /\ / ___  /\ /  |\/ /\
23
 *    / /\____\// / // / // / // /\___\// /_// / // /\_/ / // , |/ / /
24
 *   / / /__   / / // / // / // / /    / ___  / // ___  / // /| ' / /
25
 *  / /_// /\ / /_// / // / // /_/_   / / // / // /\_/ / // / |  / /
26
 * /______/ //______/ //_/ //_____/\ /_/ //_/ //_/ //_/ //_/ /|_/ /
27
 * \______\/ \______\/ \_\/ \_____\/ \_\/ \_\/ \_\/ \_\/ \_\/ \_\/
28
 *
29
 * Copyright (c) 2004 - 2008 Olof Naessén and Per Larsson
30
 *
31
 *
32
 * Per Larsson a.k.a finalman
33
 * Olof Naessén a.k.a jansem/yakslem
34
 *
35
 * Visit: http://guichan.sourceforge.net
36
 *
37
 * License: (BSD)
38
 * Redistribution and use in source and binary forms, with or without
39
 * modification, are permitted provided that the following conditions
40
 * are met:
41
 * 1. Redistributions of source code must retain the above copyright
42
 *    notice, this list of conditions and the following disclaimer.
43
 * 2. Redistributions in binary form must reproduce the above copyright
44
 *    notice, this list of conditions and the following disclaimer in
45
 *    the documentation and/or other materials provided with the
46
 *    distribution.
47
 * 3. Neither the name of Guichan nor the names of its contributors may
48
 *    be used to endorse or promote products derived from this software
49
 *    without specific prior written permission.
50
 *
51
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
52
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
53
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
54
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
55
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
56
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
57
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
58
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
59
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
60
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
61
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
62
 */
63
64
#ifndef UTILS_SDLPIXEL_H
65
#define UTILS_SDLPIXEL_H
66
67
#include "gui/color.h"
68
69
PRAGMA48(GCC diagnostic push)
70
PRAGMA48(GCC diagnostic ignored "-Wshadow")
71
#include <SDL.h>
72
PRAGMA48(GCC diagnostic pop)
73
74
#ifndef SDL_BYTEORDER
75
#error missing SDL_endian.h
76
#endif  // SDL_BYTEORDER
77
78
/**
79
  * Puts a pixel on an SDL_Surface.
80
  *
81
  * @param x the x coordinate on the surface.
82
  * @param y the y coordinate on the surface.
83
  * @param color the color the pixel should be in.
84
  */
85
inline void SDLputPixel(SDL_Surface* surface, int x, int y,
86
                        const Color& color)
87
{
88
    if (surface == nullptr)
89
        return;
90
91
    const int bpp = surface->format->BytesPerPixel;
92
93
    SDL_LockSurface(surface);
94
95
    Uint8 *const p = static_cast<uint8_t*>(surface->pixels)
96
        + CAST_SIZE(y * surface->pitch + x * bpp);
97
98
    const Uint32 pixel = SDL_MapRGB(surface->format,
99
        CAST_U8(color.r), CAST_U8(color.g),
100
        CAST_U8(color.b));
101
102
    switch (bpp)
103
    {
104
        case 1:
105
            *p = CAST_U8(pixel);
106
            break;
107
108
        case 2:
109
            *reinterpret_cast<uint16_t*>(p) = CAST_U16(pixel);
110
            break;
111
112
        case 3:
113
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
114
            p[0] = CAST_U8((pixel >> 16) & 0xff);
115
            p[1] = CAST_U8((pixel >> 8) & 0xff);
116
            p[2] = CAST_U8((pixel) & 0xff);
117
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
118
119
            p[0] = CAST_U8((pixel) & 0xff);
120
            p[1] = CAST_U8((pixel >> 8) & 0xff);
121
            p[2] = CAST_U8((pixel >> 16) & 0xff);
122
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
123
124
            break;
125
126
        case 4:
127
            *reinterpret_cast<Uint32*>(p) = pixel;
128
            break;
129
130
        default:
131
            break;
132
    }
133
134
    SDL_UnlockSurface(surface);
135
}
136
137
/**
138
  * Blends two 32 bit colors together.
139
  *
140
  * @param src the source color.
141
  * @param dst the destination color.
142
  * @param a alpha.
143
  */
144
inline unsigned int SDLAlpha32(const unsigned int src,
145
                               const unsigned int dst,
146
                               const unsigned char a)
147
{
148
    const unsigned int b = ((src & 0xff) * a + (dst & 0xff) * (255 - a)) >> 8;
149
    const unsigned int g = ((src & 0xff00) * a + (dst & 0xff00)
150
        * (255 - a)) >> 8;
151
    const unsigned int r = ((src & 0xff0000) * a + (dst & 0xff0000)
152
        * (255 - a)) >> 8;
153
154
    return (b & 0xff) | (g & 0xff00) | (r & 0xff0000);
155
}
156
157
inline unsigned short SDLAlpha16(const unsigned short src,
158
                                 const unsigned short dst,
159
                                 const unsigned char a,
160
                                 const SDL_PixelFormat *const f) A_NONNULL(4);
161
162
/**
163
  * Blends two 16 bit colors together.
164
  *
165
  * @param src the source color.
166
  * @param dst the destination color.
167
  * @param a alpha.
168
  */
169
inline unsigned short SDLAlpha16(const unsigned short src,
170
                                 const unsigned short dst,
171
                                 const unsigned char a,
172
                                 const SDL_PixelFormat *const f)
173
{
174
    unsigned int b = ((src & f->Rmask) * a + (dst & f->Rmask)
175
        * (255 - a)) >> 8;
176
    unsigned int g = ((src & f->Gmask) * a + (dst & f->Gmask)
177
        * (255 - a)) >> 8;
178
    unsigned int r = ((src & f->Bmask) * a + (dst & f->Bmask)
179
        * (255 - a)) >> 8;
180
181
    return static_cast<unsigned short>((b & f->Rmask)
182
        | (g & f->Gmask) | (r & f->Bmask));
183
}
184
185
/**
186
  * Puts a pixel on an SDL_Surface with alpha
187
  *
188
  * @param x the x coordinate on the surface.
189
  * @param y the y coordinate on the surface.
190
  * @param color the color the pixel should be in.
191
  */
192
inline void SDLputPixelAlpha(SDL_Surface* surface, int x, int y,
193
                             const Color& color)
194
{
195
    if (surface == nullptr)
196
        return;
197
    const int bpp = surface->format->BytesPerPixel;
198
199
    SDL_LockSurface(surface);
200
201
    Uint8 *const p = static_cast<uint8_t*>(surface->pixels)
202
        + CAST_SIZE(y * surface->pitch + x * bpp);
203
204
    const Uint32 pixel = SDL_MapRGB(surface->format,
205
        CAST_U8(color.r),
206
        CAST_U8(color.g),
207
        CAST_U8(color.b));
208
209
    switch (bpp)
210
    {
211
        case 1:
212
            *p = CAST_U8(pixel);
213
            break;
214
215
        case 2:
216
            *reinterpret_cast<Uint16*>(p) = SDLAlpha16(
217
                static_cast<unsigned short>(pixel),
218
                *reinterpret_cast<unsigned short*>(p),
219
                CAST_U8(color.a), surface->format);
220
            break;
221
222
        case 3:
223
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
224
            p[2] = CAST_U8((p[2] * (255 - color.a)
225
                + color.b * color.a) >> 8);
226
            p[1] = CAST_U8((p[1] * (255 - color.a)
227
                + color.g * color.a) >> 8);
228
            p[0] = CAST_U8((p[0] * (255 - color.a)
229
                + color.r * color.a) >> 8);
230
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
231
232
            p[0] = CAST_U8((p[0] * (255 - color.a)
233
                + color.b * color.a) >> 8);
234
            p[1] = CAST_U8((p[1] * (255 - color.a)
235
                + color.g * color.a) >> 8);
236
            p[2] = CAST_U8((p[2] * (255 - color.a)
237
                + color.r * color.a) >> 8);
238
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
239
240
            break;
241
242
        case 4:
243
            *reinterpret_cast<Uint32*>(p) = SDLAlpha32(pixel,
244
                *reinterpret_cast<Uint32*>(p),
245
                CAST_U8(color.a));
246
            break;
247
        default:
248
            break;
249
    }
250
251
    SDL_UnlockSurface(surface);
252
}
253
254
#endif  // UTILS_SDLPIXEL_H