Subversion Repositories public iLand

Rev

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

Rev Author Line No. Line
1
 
93 Werner 2
#include "global.h"
30 Werner 3
#include "stamp.h"
83 Werner 4
#include "grid.h"
5
#include "stampcontainer.h"
93 Werner 6
#include "helper.h"
30 Werner 7
Stamp::Stamp()
8
{
9
    m_data=0;
10
}
11
Stamp::~Stamp()
12
{
13
   if( m_data)
14
       delete m_data;
15
}
16
 
32 Werner 17
void Stamp::setup(const int size)
30 Werner 18
{
32 Werner 19
    int c=size*size;
20
    m_size=size;
34 Werner 21
    m_offset=0;
42 Werner 22
    m_readsum = 0.f;
47 Werner 23
    m_reader = 0;
149 werner 24
    m_dominance=0.f;
25
    m_crownArea=0.f;
26
    m_crownRadius=0.f;
32 Werner 27
    if (m_data)
28
        delete[] m_data;
30 Werner 29
    m_data=new float[c];
30
    for (int i=0;i<c;i++)
31
        m_data[i]=0.;
32
}
32 Werner 33
 
34
void Stamp::loadFromFile(const QString &fileName)
35
{
36
    QString txt = Helper::loadTextFile(fileName);
37
    QStringList lines = txt.split("\n");
38
 
39
    setup(lines.count());
40
    int l=0;
41
    foreach(QString line, lines) {
42
        QStringList cols=line.split(";");
43
        if (cols.count() != lines.count())
44
            MSGRETURN("Stamp::loadFromFile: invalid count of rows/cols.");
45
        for (int i=0;i<cols.count();i++)
46
            *data(i,l)=cols[i].toFloat();
47
        l++;
48
    }
49
}
33 Werner 50
 
51
// load from stream....
52
void Stamp::load(QDataStream &in)
53
{
54
   // see StampContainer doc for file stamp binary format
55
   qint32 offset;
56
   in >> offset;
57
   m_offset = offset;
58
   // load data
59
   float data;
35 Werner 60
   for (int i=0;i<count(); i++) {
33 Werner 61
       in >> data;
62
       m_data[i]=data;
63
   }
64
}
65
 
66
void Stamp::save(QDataStream &out)
67
{
68
    // see StampContainer doc for file stamp binary format
69
   out << (qint32) m_offset;
35 Werner 70
   for (int i=0;i<count(); i++) {
33 Werner 71
       out << m_data[i];
72
   }
73
}
34 Werner 74
 
75
 
76
Stamp *stampFromGrid(const FloatGrid& grid, const int width)
77
{
78
    Stamp::StampType type=Stamp::est4x4;
35 Werner 79
    int c = grid.sizeX(); // total size of input grid
34 Werner 80
    if (c%2==0 || width%2==0) {
81
        qDebug() << "both grid and width should be uneven!!! returning NULL.";
82
        return NULL;
83
    }
84
 
35 Werner 85
    if (width<=4) type = Stamp::est4x4;
86
    else if (width<=8) type = Stamp::est8x8;
87
    else if (width<=12) type = Stamp::est12x12;
88
    else if (width<=16) type = Stamp::est16x16;
89
    else if (width<=24) type = Stamp::est24x24;
90
    else if (width<=32) type = Stamp::est32x32;
131 Werner 91
    else if (width<=48) type = Stamp::est48x48;
92
    else type = Stamp::est64x64;
34 Werner 93
 
94
    Stamp *stamp = StampContainer::newStamp(type);
52 Werner 95
    int swidth = width;
131 Werner 96
    if (width>63) {
97
        qDebug() << "Warning: grid too big, truncated stamp to 63x63px!";
98
        swidth = 63;
52 Werner 99
    }
100
    stamp->setOffset(swidth/2);
101
    int coff = c/2 - swidth/2; // e.g.: grid=25, width=7 -> coff = 12 - 3 = 9
34 Werner 102
    int x,y;
52 Werner 103
    for (x=0;x<swidth; x++)
104
        for (y=0; y<swidth; y++)
34 Werner 105
            stamp->setData(x,y, grid(coff+x, coff+y) ); // copy data (from a different rectangle)
106
    return stamp;
107
 
108
}