~masgk3team/masgk3/roads

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include "ImageParser.hpp"
//-----//
#include "Utils.hpp"
//-----//
#include <QImage>
//-----//
#include <QDebug>
//-----//

ImageParser::ImageParser()
{

}
//-----//
QRgb ImageParser::InterpolateCol(const QRgb &clrMin, const QRgb &clrMax, int lvl, int lvlMin, int lvlMax, ColorDirection dir) const
{
    const QRgb &qc1 = dir == CD_UP ? clrMin : clrMax;
    const QRgb &qc2 = dir == CD_UP ? clrMax : clrMin;
    int diff = abs(lvlMax - lvlMin);
    int lmin = std::min(lvlMin, lvlMax);
    return qRgb(qRed  (qc1) + (qRed  (qc2) - qRed  (qc1)) * ((float)(lvl - lmin) / diff),
                qGreen(qc1) + (qGreen(qc2) - qGreen(qc1)) * ((float)(lvl - lmin) / diff),
                qBlue (qc1) + (qBlue (qc2) - qBlue (qc1)) * ((float)(lvl - lmin) / diff));
}
//-----//
QImage* ImageParser::Parse(const QImage *input) const
{
    int w = input->width();
    int h = input->height();
    QImage *result = new QImage(w, h, input->format());

    if (!input->allGray())
        throw "Only grayscale images allowed as input";

    for (int i = 0; i < w; ++i)
        for (int j = 0; j < h; ++j)
        {
            const QRgb &px = input->pixel(i, j);
            const int r = qRed(px);
            QRgb newPx;
            if      (r <= Utils::LEVEL_WATER)
                newPx = InterpolateCol(Utils::COLOR_WATER_MIN, Utils::COLOR_WATER_MAX, r, 0                 , Utils::LEVEL_WATER, CD_UP);
            else if (r <= Utils::LEVEL_LOW)
                newPx = InterpolateCol(Utils::COLOR_LOW_MIN  , Utils::COLOR_LOW_MAX  , r, Utils::LEVEL_WATER, Utils::LEVEL_LOW  , CD_UP);
            else if (r <= Utils::LEVEL_HIGH)
                newPx = InterpolateCol(Utils::COLOR_HIGH_MIN , Utils::COLOR_HIGH_MAX , r, Utils::LEVEL_LOW  , Utils::LEVEL_HIGH , CD_UP);
            else
                newPx = InterpolateCol(Utils::COLOR_TOP_MIN  , Utils::COLOR_TOP_MAX  , r, Utils::LEVEL_HIGH , 255               , CD_DOWN);
            result->setPixel(i, j, newPx);
        }
    return result;
}