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

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


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

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

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

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

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