Subversion Repositories public iLand

Rev

Rev 1104 | Rev 1217 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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