Rev 401 | Rev 639 | 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" |
487 | werner | 7 | |
8 | const Grid<float> *Stamp::mDistanceGrid = 0; |
||
9 | |||
30 | Werner | 10 | Stamp::Stamp() |
11 | { |
||
12 | m_data=0; |
||
13 | } |
||
487 | werner | 14 | |
30 | Werner | 15 | Stamp::~Stamp() |
16 | { |
||
17 | if( m_data) |
||
18 | delete m_data; |
||
19 | } |
||
20 | |||
32 | Werner | 21 | void Stamp::setup(const int size) |
30 | Werner | 22 | { |
32 | Werner | 23 | int c=size*size; |
24 | m_size=size; |
||
34 | Werner | 25 | m_offset=0; |
47 | Werner | 26 | m_reader = 0; |
149 | werner | 27 | m_crownArea=0.f; |
28 | m_crownRadius=0.f; |
||
32 | Werner | 29 | if (m_data) |
30 | delete[] m_data; |
||
30 | Werner | 31 | m_data=new float[c]; |
32 | for (int i=0;i<c;i++) |
||
33 | m_data[i]=0.; |
||
487 | werner | 34 | // set static variable values |
35 | mDistanceGrid = &StampContainer::distanceGrid(); |
||
30 | Werner | 36 | } |
32 | Werner | 37 | |
487 | werner | 38 | //inline float Stamp::distanceToCenter(const int ix, const int iy) const |
39 | //{ |
||
40 | // // |
||
41 | // return StampContainer::distanceGrid().constValueAtIndex(abs(ix-m_offset), abs(iy-m_offset)); |
||
42 | //} |
||
401 | werner | 43 | |
321 | werner | 44 | QString Stamp::dump() const |
45 | { |
||
46 | QString result, line; |
||
47 | int x,y; |
||
48 | for (y=0;y<m_size;++y) { |
||
49 | line=""; |
||
50 | for (x=0;x<m_size;++x) { |
||
51 | line+= QString::number(*data(x,y)) + " "; |
||
52 | } |
||
53 | line+="\r\n"; |
||
54 | result+=line; |
||
55 | } |
||
56 | return result; |
||
57 | } |
||
58 | |||
32 | Werner | 59 | void Stamp::loadFromFile(const QString &fileName) |
60 | { |
||
61 | QString txt = Helper::loadTextFile(fileName); |
||
62 | QStringList lines = txt.split("\n"); |
||
63 | |||
64 | setup(lines.count()); |
||
65 | int l=0; |
||
66 | foreach(QString line, lines) { |
||
67 | QStringList cols=line.split(";"); |
||
68 | if (cols.count() != lines.count()) |
||
69 | MSGRETURN("Stamp::loadFromFile: invalid count of rows/cols."); |
||
70 | for (int i=0;i<cols.count();i++) |
||
71 | *data(i,l)=cols[i].toFloat(); |
||
72 | l++; |
||
73 | } |
||
74 | } |
||
33 | Werner | 75 | |
76 | // load from stream.... |
||
77 | void Stamp::load(QDataStream &in) |
||
78 | { |
||
79 | // see StampContainer doc for file stamp binary format |
||
80 | qint32 offset; |
||
81 | in >> offset; |
||
82 | m_offset = offset; |
||
83 | // load data |
||
84 | float data; |
||
35 | Werner | 85 | for (int i=0;i<count(); i++) { |
33 | Werner | 86 | in >> data; |
87 | m_data[i]=data; |
||
88 | } |
||
89 | } |
||
90 | |||
91 | void Stamp::save(QDataStream &out) |
||
92 | { |
||
93 | // see StampContainer doc for file stamp binary format |
||
94 | out << (qint32) m_offset; |
||
35 | Werner | 95 | for (int i=0;i<count(); i++) { |
33 | Werner | 96 | out << m_data[i]; |
97 | } |
||
98 | } |
||
34 | Werner | 99 | |
100 | |||
101 | Stamp *stampFromGrid(const FloatGrid& grid, const int width) |
||
102 | { |
||
103 | Stamp::StampType type=Stamp::est4x4; |
||
35 | Werner | 104 | int c = grid.sizeX(); // total size of input grid |
34 | Werner | 105 | if (c%2==0 || width%2==0) { |
106 | qDebug() << "both grid and width should be uneven!!! returning NULL."; |
||
107 | return NULL; |
||
108 | } |
||
109 | |||
35 | Werner | 110 | if (width<=4) type = Stamp::est4x4; |
111 | else if (width<=8) type = Stamp::est8x8; |
||
112 | else if (width<=12) type = Stamp::est12x12; |
||
113 | else if (width<=16) type = Stamp::est16x16; |
||
114 | else if (width<=24) type = Stamp::est24x24; |
||
115 | else if (width<=32) type = Stamp::est32x32; |
||
131 | Werner | 116 | else if (width<=48) type = Stamp::est48x48; |
117 | else type = Stamp::est64x64; |
||
34 | Werner | 118 | |
119 | Stamp *stamp = StampContainer::newStamp(type); |
||
52 | Werner | 120 | int swidth = width; |
131 | Werner | 121 | if (width>63) { |
122 | qDebug() << "Warning: grid too big, truncated stamp to 63x63px!"; |
||
123 | swidth = 63; |
||
52 | Werner | 124 | } |
125 | stamp->setOffset(swidth/2); |
||
126 | int coff = c/2 - swidth/2; // e.g.: grid=25, width=7 -> coff = 12 - 3 = 9 |
||
34 | Werner | 127 | int x,y; |
52 | Werner | 128 | for (x=0;x<swidth; x++) |
129 | for (y=0; y<swidth; y++) |
||
34 | Werner | 130 | stamp->setData(x,y, grid(coff+x, coff+y) ); // copy data (from a different rectangle) |
131 | return stamp; |
||
132 | |||
133 | } |