GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/resources/dye/dye.cpp Lines: 77 117 65.8 %
Date: 2017-11-29 Branches: 51 108 47.2 %

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-2017  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
16
Dye::Dye(const std::string &restrict description)
43
{
44
160
    for (int i = 0; i < dyePalateSize; ++i)
45
144
        mDyePalettes[i] = nullptr;
46
47
16
    if (description.empty())
48
        return;
49
50
    size_t next_pos = 0;
51
    const size_t length = description.length();
52
    do
53
    {
54
16
        const size_t pos = next_pos;
55
16
        next_pos = description.find(';', pos);
56
57
16
        if (next_pos == std::string::npos)
58
16
            next_pos = length;
59
60

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


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

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

2310
    if (next_pos == std::string::npos || palettes.empty())
102
2310
        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
10
int Dye::getType() const restrict2 noexcept2
148
{
149
10
    if (mDyePalettes[sPaleteIndex] != nullptr)
150
        return 1;
151
4
    if (mDyePalettes[aPaleteIndex] != nullptr)
152
        return 2;
153
2
    return 0;
154
}
155
156
6
void Dye::normalDye(uint32_t *restrict pixels,
157
                    const int bufSize) const restrict2
158
{
159
6
    if (pixels == nullptr)
160
        return;
161
162
#ifdef ENABLE_CILKPLUS
163
    cilk_for (int ptr = 0; ptr < bufSize; ptr ++)
164
    {
165
        const uint32_t p = pixels[ptr];
166
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
167
        const int alpha = p & 0xff000000;
168
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
169
170
        const int alpha = p & 0xff;
171
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
172
173
        if (alpha)
174
        {
175
            unsigned int color[3];
176
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
177
            color[0] = (p) & 255U;
178
            color[1] = (p >> 8U) & 255U;
179
            color[2] = (p >> 16U) & 255U;
180
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
181
182
            color[0] = (p >> 24U) & 255U;
183
            color[1] = (p >> 16U) & 255U;
184
            color[2] = (p >> 8U) & 255U;
185
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
186
187
            const unsigned int cmax = std::max(
188
                color[0], std::max(color[1], color[2]));
189
            if (cmax == 0)
190
                goto endlabel;
191
192
            const unsigned int cmin = std::min(
193
                color[0], std::min(color[1], color[2]));
194
            const unsigned int intensity = color[0] + color[1] + color[2];
195
            unsigned int i;
196
197
            if (cmin != cmax && (cmin != 0 || (intensity != cmax
198
                && intensity != 2 * cmax)))
199
            {
200
                // not pure
201
                goto endlabel;
202
            }
203
204
            i = (color[0] != 0) | ((color[1] != 0) << 1)
205
                | ((color[2] != 0) << 2);
206
207
            if (mDyePalettes[i - 1])
208
                mDyePalettes[i - 1]->getColor(cmax, color);
209
210
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
211
            pixels[ptr] = (color[0]) | (color[1] << 8)
212
                | (color[2] << 16) | alpha;
213
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
214
215
            pixels[ptr] = (color[0] << 24) | (color[1] << 16)
216
                | (color[2] << 8) | alpha;
217
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
218
        }
219
endlabel:{}
220
    }
221
222
#else  // ENABLE_CILKPLUS
223
224
2058
    for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
225
2058
         pixels != p_end;
226
         ++ pixels)
227
    {
228
2052
        const uint32_t p = *pixels;
229
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
230
        const int alpha = p & 0xff000000;
231
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
232
233
2052
        const int alpha = p & 0xff;
234
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
235
236
2052
        if (alpha == 0)
237
3200
            continue;
238
        unsigned int color[3];
239
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
240
        color[0] = (p) & 255U;
241
        color[1] = (p >> 8U) & 255U;
242
        color[2] = (p >> 16U) & 255U;
243
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
244
245
480
        color[0] = (p >> 24U) & 255U;
246
480
        color[1] = (p >> 16U) & 255U;
247
480
        color[2] = (p >> 8U) & 255U;
248
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
249
250
        const unsigned int cmax = std::max(
251
960
            color[0], std::max(color[1], color[2]));
252
480
        if (cmax == 0)
253
            continue;
254
255
        const unsigned int cmin = std::min(
256
960
            color[0], std::min(color[1], color[2]));
257
480
        const unsigned int intensity = color[0] + color[1] + color[2];
258
259

536
        if (cmin != cmax && (cmin != 0 || (intensity != cmax
260
            && intensity != 2 * cmax)))
261
        {
262
            // not pure
263
56
            continue;
264
        }
265
266
1272
        const unsigned int i = static_cast<int>(color[0] != 0) |
267
848
            (static_cast<int>(color[1] != 0) << 1) |
268
424
            (static_cast<int>(color[2] != 0) << 2);
269
270
424
        if (mDyePalettes[i - 1] != nullptr)
271
424
            mDyePalettes[i - 1]->getColor(cmax, color);
272
273
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
274
        *pixels = (color[0]) | (color[1] << 8)
275
            | (color[2] << 16) | alpha;
276
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
277
278
848
        *pixels = (color[0] << 24) | (color[1] << 16)
279
424
            | (color[2] << 8) | alpha;
280
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
281
    }
282
#endif  // ENABLE_CILKPLUS
283
}
284
285
2
void Dye::normalOGLDye(uint32_t *restrict pixels,
286
                       const int bufSize) const restrict2
287
{
288
2
    if (pixels == nullptr)
289
        return;
290
291
#ifdef ENABLE_CILKPLUS
292
    cilk_for (int ptr = 0; ptr < bufSize; ptr ++)
293
    {
294
        const uint32_t p = pixels[ptr];
295
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
296
        const int alpha = p & 255;
297
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
298
299
        const int alpha = p & 0xff000000;
300
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
301
302
        if (alpha)
303
        {
304
            unsigned int color[3];
305
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
306
            color[0] = (p >> 24U) & 255U;
307
            color[1] = (p >> 16U) & 255U;
308
            color[2] = (p >> 8U) & 255U;
309
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
310
311
            color[0] = (p) & 255U;
312
            color[1] = (p >> 8U) & 255U;
313
            color[2] = (p >> 16U) & 255U;
314
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
315
316
            const unsigned int cmax = std::max(
317
                color[0], std::max(color[1], color[2]));
318
            if (cmax == 0)
319
                goto endlabel;
320
321
            const unsigned int cmin = std::min(
322
                color[0], std::min(color[1], color[2]));
323
            const unsigned int intensity = color[0] + color[1] + color[2];
324
325
            if (cmin != cmax && (cmin != 0 || (intensity != cmax
326
                && intensity != 2 * cmax)))
327
            {
328
                // not pure
329
                goto endlabel;
330
            }
331
332
            const unsigned int i = (color[0] != 0) | ((color[1] != 0) << 1)
333
                | ((color[2] != 0) << 2);
334
335
            if (mDyePalettes[i - 1])
336
                mDyePalettes[i - 1]->getColor(cmax, color);
337
338
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
339
            pixels[ptr] = (color[0] << 24) | (color[1] << 16)
340
                | (color[2] << 8) | alpha;
341
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
342
343
            pixels[ptr] = (color[0]) | (color[1] << 8)
344
                | (color[2] << 16) | alpha;
345
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
346
        }
347
endlabel:{}
348
    }
349
350
#else  // ENABLE_CILKPLUS
351
352
4
    for (const uint32_t *const p_end = pixels + CAST_SIZE(bufSize);
353
4
         pixels != p_end;
354
         ++ pixels)
355
    {
356
2
        const uint32_t p = *pixels;
357
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
358
        const int alpha = p & 255;
359
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
360
361
2
        const int alpha = p & 0xff000000;
362
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
363
364
2
        if (alpha == 0)
365
            continue;
366
        unsigned int color[3];
367
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
368
        color[0] = (p >> 24U) & 255U;
369
        color[1] = (p >> 16U) & 255U;
370
        color[2] = (p >> 8U) & 255U;
371
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
372
373
2
        color[0] = (p) & 255U;
374
2
        color[1] = (p >> 8U) & 255U;
375
2
        color[2] = (p >> 16U) & 255U;
376
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
377
378
        const unsigned int cmax = std::max(
379
4
            color[0], std::max(color[1], color[2]));
380
2
        if (cmax == 0)
381
            continue;
382
383
        const unsigned int cmin = std::min(
384
4
            color[0], std::min(color[1], color[2]));
385
2
        const unsigned int intensity = color[0] + color[1] + color[2];
386
387

2
        if (cmin != cmax && (cmin != 0 || (intensity != cmax
388
            && intensity != 2 * cmax)))
389
        {
390
            // not pure
391
            continue;
392
        }
393
394
6
        const unsigned int i = static_cast<int>(color[0] != 0) |
395
4
            (static_cast<int>(color[1] != 0) << 1) |
396
2
            (static_cast<int>(color[2] != 0) << 2);
397
398
2
        if (mDyePalettes[i - 1] != nullptr)
399
2
            mDyePalettes[i - 1]->getColor(cmax, color);
400
401
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
402
        *pixels = (color[0] << 24) | (color[1] << 16)
403
            | (color[2] << 8) | alpha;
404
#else  // SDL_BYTEORDER == SDL_BIG_ENDIAN
405
406
4
        *pixels = (color[0]) | (color[1] << 8)
407
2
            | (color[2] << 16) | alpha;
408
#endif  // SDL_BYTEORDER == SDL_BIG_ENDIAN
409
    }
410
#endif  // ENABLE_CILKPLUS
411
}