Subversion Repositories public iLand

Rev

Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1211 werner 1
/********************************************************************************************
2
**    iLand - an individual based forest landscape and disturbance model
3
**    http://iland.boku.ac.at
4
**    Copyright (C) 2009-  Werner Rammer, Rupert Seidl
5
**
6
**    This program is free software: you can redistribute it and/or modify
7
**    it under the terms of the GNU General Public License as published by
8
**    the Free Software Foundation, either version 3 of the License, or
9
**    (at your option) any later version.
10
**
11
**    This program is distributed in the hope that it will be useful,
12
**    but WITHOUT ANY WARRANTY; without even the implied warranty of
13
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
**    GNU General Public License for more details.
15
**
16
**    You should have received a copy of the GNU General Public License
17
**    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
********************************************************************************************/
15 Werner 19
#include "grid.h"
285 werner 20
#include "exception.h"
373 werner 21
#include "global.h"
15 Werner 22
 
599 werner 23
QString gridToString(const FloatGrid &grid, const QChar sep, const int newline_after)
36 Werner 24
{
25
    QString res;
599 werner 26
    int newl_counter = newline_after;
709 werner 27
    for (int y=grid.sizeY()-1;y>=0;--y) {
46 Werner 28
         for (int x=0;x<grid.sizeX();x++) {
599 werner 29
            res+=QString::number(grid.constValueAtIndex(QPoint(x,y))) + sep;
30
            if (--newl_counter==0) {
31
                res += "\r\n";
32
                newl_counter = newline_after;
33
            }
36 Werner 34
        }
35
        res+="\r\n";
36
    }
37
    return res;
38
}
989 werner 39
#ifdef ILAND_GUI
991 werner 40
#include <QImage>
36 Werner 41
 
42
QImage gridToImage(const FloatGrid &grid,
43
                   bool black_white,
44
                   double min_value, double max_value,
45
                   bool reverse)
46
{
47
    QImage res(grid.sizeX(), grid.sizeY(), QImage::Format_ARGB32);
48
    QRgb col;
49
    int grey;
50
    double rval;
51
    for (int x=0;x<grid.sizeX();x++){
52
        for (int y=0;y<grid.sizeY();y++) {
53
            rval = grid.constValueAtIndex(QPoint(x,y));
54
            rval = std::max(min_value, rval);
55
            rval = std::min(max_value, rval);
56
            if (reverse) rval = max_value - rval;
57
            if (black_white) {
373 werner 58
                grey = int(255 * ( (rval-min_value) / (max_value-min_value)));
36 Werner 59
                col = QColor(grey,grey,grey).rgb();
60
            } else {
61
                col = QColor::fromHsvF(0.66666666666*rval, 0.95, 0.95).rgb();
62
            }
63
            res.setPixel(x,y,col);
64
            //res+=QString::number(grid.constValueAtIndex(QPoint(x,y))) + ";";
65
        }
66
        //res+="\r\n";
67
    }
68
    return res;
69
}
285 werner 70
 
556 werner 71
 
72
 
285 werner 73
bool loadGridFromImage(const QString &fileName, FloatGrid &rGrid)
74
{
75
    QImage image;
76
    if (!image.load(fileName))
77
        throw IException(QString("Grid::loadFromImage: could not load image file %1.").arg(fileName));
375 werner 78
    if (rGrid.isEmpty())
79
        rGrid.setup(1., image.size().width(), image.size().height() );
285 werner 80
    double value;
81
    for (int x=0;x<image.width(); x++)
82
        for (int y=0;y<image.height(); y++) {
83
            value = qGray(image.pixel(x,y))/255.;
84
            if (rGrid.isIndexValid(QPoint(x,y)))
85
                rGrid.valueAtIndex(x,y) = value;
86
        }
87
    return true;
88
}
989 werner 89
#else
90
//QImage gridToImage(const FloatGrid &grid,
91
//                   bool black_white,
92
//                   double min_value, double max_value,
93
//                   bool reverse)
94
//{
95
//}
96
 
97
bool loadGridFromImage(const QString &fileName, FloatGrid &rGrid) {
1032 werner 98
    Q_UNUSED(fileName); Q_UNUSED(rGrid);
989 werner 99
    return false;
100
}
101
#endif
102