GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/dye/dye.cpp Lines: 76 112 67.9 %
Date: 2018-09-20 Branches: 53 112 47.3 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2007-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2018  The ManaPlus Developers
6
 *
7
 *  This file is part of The ManaPlus Client.
8
 *
9
 *  This program is free software; you can redistribute it and/or modify
10
 *  it under the terms of the GNU General Public License as published by
11
 *  the Free Software Foundation; either version 2 of the License, or
12
 *  any later version.
13
 *
14
 *  This program is distributed in the hope that it will be useful,
15
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 *  GNU General Public License for more details.
18
 *
19
 *  You should have received a copy of the GNU General Public License
20
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "resources/dye/dye.h"
24
25
#include "logger.h"
26
27
#include "resources/dye/dyepalette.h"
28
29
#include "utils/delete2.h"
30
31
#include <sstream>
32
33
PRAGMA48(GCC diagnostic push)
34
PRAGMA48(GCC diagnostic ignored "-Wshadow")
35
#ifndef SDL_BIG_ENDIAN
36
#include <SDL_endian.h>
37
#endif  // SDL_BYTEORDER
38
PRAGMA48(GCC diagnostic pop)
39
40
#include "debug.h"
41
42
8
Dye::Dye(const std::string &restrict description)
43
{
44
80
    for (int i = 0; i < dyePalateSize; ++i)
45
72
        mDyePalettes[i] = nullptr;
46
47
8
    if (description.empty())
48
        return;
49
50
    size_t next_pos = 0;
51
    const size_t length = description.length();
52
    do
53
    {
54
8
        const size_t pos = next_pos;
55
8
        next_pos = description.find(';', pos);
56
57
8
        if (next_pos == std::string::npos)
58
8
            next_pos = length;
59
60

16
        if (next_pos <= pos + 3 || description[pos + 1] != ':')
61
        {
62
            logger->log("Error, invalid dye: %s", description.c_str());
63
            return;
64
        }
65
66
8
        int i = 0;
67
68


8
        switch (description[pos])
69
        {
70
            case 'R': i = 0; break;
71
1
            case 'G': i = 1; break;
72
            case 'Y': i = 2; break;
73
1
            case 'B': i = 3; break;
74
            case 'M': i = 4; break;
75
            case 'C': i = 5; break;
76
            case 'W': i = 6; break;
77
3
            case 'S': i = 7; break;
78
1
            case 'A': i = 8; break;
79
            default:
80
                logger->log("Error, invalid dye: %s", description.c_str());
81
                return;
82
        }
83
24
        mDyePalettes[i] = new DyePalette(description.substr(
84

16
            pos + 2, next_pos - pos - 2), i != 8 ? 6 : 8);
85
8
        ++next_pos;
86
    }
87
8
    while (next_pos < length);
88
}
89
90
16
Dye::~Dye()
91
{
92
80
    for (int i = 0; i < dyePalateSize; ++i)
93
80
        delete2(mDyePalettes[i])
94
8
}
95
96
1293
void Dye::instantiate(std::string &restrict target,
97
                      const std::string &restrict palettes)
98
{
99
1293
    size_t next_pos = target.find('|');
100
101

1293
    if (next_pos == std::string::npos || palettes.empty())
102
1293
        return;
103
104
    ++next_pos;
105
106
    std::ostringstream s;
107
    s << target.substr(0, next_pos);
108
    size_t last_pos = target.length(), pal_pos = 0;
109
    do
110
    {
111
        const size_t pos = next_pos;
112
        next_pos = target.find(';', pos);
113
114
        if (next_pos == std::string::npos)
115
            next_pos = last_pos;
116
117
        if (next_pos == pos + 1 && pal_pos != std::string::npos)
118
        {
119
            const size_t pal_next_pos = palettes.find(';', pal_pos);
120
            s << target[pos] << ':';
121
            if (pal_next_pos == std::string::npos)
122
            {
123
                s << palettes.substr(pal_pos);
124
                s << target.substr(next_pos);
125
                break;
126
            }
127
            s << palettes.substr(pal_pos, pal_next_pos - pal_pos);
128
            pal_pos = pal_next_pos + 1;
129
        }
130
        else if (next_pos > pos + 2)
131
        {
132
            s << target.substr(pos, next_pos - pos);
133
        }
134
        else
135
        {
136
            logger->log("Error, invalid dye placeholder: %s", target.c_str());
137
            return;
138
        }
139
        s << target[next_pos];
140
        ++next_pos;
141
    }
142
    while (next_pos < last_pos);
143
144
    target = s.str();
145
}
146
147
5
int Dye::getType() const restrict2 noexcept2
148
{
149
5
    if (mDyePalettes[sPaleteIndex] != nullptr)
150
        return 1;
151
2
    if (mDyePalettes[aPaleteIndex] != nullptr)
152
        return 2;
153
1
    return 0;
154
}
155
156
3
void Dye::normalDye(uint32_t *restrict pixels,
157
                    const int bufSize) const restrict2
158
{
159
3
    if (pixels == nullptr)
160
        return;
161
162
1029
    for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
163
1029
         pixels != p_end;
164
         ++ pixels)
165
    {
166
1026
        const uint32_t p = *pixels;
167
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
168
        const int alpha = p & 0xff000000;
169
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
170
171
1026
        const int alpha = p & 0xff;
172
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
173
174
1026
        if (alpha == 0)
175
814
            continue;
176
        unsigned int color[3];
177
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
178
        color[0] = (p) & 255U;
179
        color[1] = (p >> 8U) & 255U;
180
        color[2] = (p >> 16U) & 255U;
181
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
182
183
240
        color[0] = (p >> 24U) & 255U;
184
240
        color[1] = (p >> 16U) & 255U;
185
240
        color[2] = (p >> 8U) & 255U;
186
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
187
188
        const unsigned int cmax = std::max(
189
480
            color[0], std::max(color[1], color[2]));
190
240
        if (cmax == 0)
191
            continue;
192
193
        const unsigned int cmin = std::min(
194
480
            color[0], std::min(color[1], color[2]));
195
240
        const unsigned int intensity = color[0] + color[1] + color[2];
196
197

240
        if (cmin != cmax && (cmin != 0 || (intensity != cmax
198
            && intensity != 2 * cmax)))
199
        {
200
            // not pure
201
            continue;
202
        }
203
204
424
        const unsigned int i = static_cast<int>(color[0] != 0) |
205
424
            (static_cast<int>(color[1] != 0) << 1) |
206
424
            (static_cast<int>(color[2] != 0) << 2);
207
208
212
        if (mDyePalettes[i - 1] != nullptr)
209
212
            mDyePalettes[i - 1]->getColor(cmax, color);
210
211
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
212
        *pixels = (color[0]) | (color[1] << 8)
213
            | (color[2] << 16) | alpha;
214
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
215
216
424
        *pixels = (color[0] << 24) | (color[1] << 16)
217
212
            | (color[2] << 8) | alpha;
218
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
219
    }
220
}
221
222
1
void Dye::normalOGLDye(uint32_t *restrict pixels,
223
                       const int bufSize) const restrict2
224
{
225
1
    if (pixels == nullptr)
226
        return;
227
228
2
    for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
229
2
         pixels != p_end;
230
         ++ pixels)
231
    {
232
1
        const uint32_t p = *pixels;
233
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
234
        const int alpha = p & 255;
235
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
236
237
1
        const int alpha = p & 0xff000000;
238
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
239
240
1
        if (alpha == 0)
241
            continue;
242
        unsigned int color[3];
243
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
244
        color[0] = (p >> 24U) & 255U;
245
        color[1] = (p >> 16U) & 255U;
246
        color[2] = (p >> 8U) & 255U;
247
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
248
249
1
        color[0] = (p) & 255U;
250
1
        color[1] = (p >> 8U) & 255U;
251
1
        color[2] = (p >> 16U) & 255U;
252
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
253
254
        const unsigned int cmax = std::max(
255
2
            color[0], std::max(color[1], color[2]));
256
1
        if (cmax == 0)
257
            continue;
258
259
        const unsigned int cmin = std::min(
260
2
            color[0], std::min(color[1], color[2]));
261
1
        const unsigned int intensity = color[0] + color[1] + color[2];
262
263

1
        if (cmin != cmax && (cmin != 0 || (intensity != cmax
264
            && intensity != 2 * cmax)))
265
        {
266
            // not pure
267
            continue;
268
        }
269
270
2
        const unsigned int i = static_cast<int>(color[0] != 0) |
271
2
            (static_cast<int>(color[1] != 0) << 1) |
272
2
            (static_cast<int>(color[2] != 0) << 2);
273
274
1
        if (mDyePalettes[i - 1] != nullptr)
275
1
            mDyePalettes[i - 1]->getColor(cmax, color);
276
277
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
278
        *pixels = (color[0] << 24) | (color[1] << 16)
279
            | (color[2] << 8) | alpha;
280
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
281
282
2
        *pixels = (color[0]) | (color[1] << 8)
283
1
            | (color[2] << 16) | alpha;
284
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
285
    }
286
}