cpvoids program to copy voids from one HGT dataset to another
[tecorrec.git] / cpvoids / cpVoids.cpp
blob6a4398839fdbb4235f1f8d3d22df617c53803dd2
1 /***************************************************************************
2 * This file is part of Tecorrec. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * Tecorrec is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * Tecorrec is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with Tecorrec. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 #include <QString>
21 #include <QFile>
22 #include <QtDebug>
23 #include <QFileInfo>
25 #include <cmath>
27 int cpVoids(const QString filename, char* voids)
29 qDebug() << "Reading" << filename;
31 // Find corresponding file in void directory
32 QFileInfo fi(filename);
33 QString name = fi.fileName();
35 QFile srtm(filename);
36 if (srtm.open(QIODevice::ReadWrite))
38 QString pathFname = QString("%1/%2").arg(voids).arg(name);
39 QFile voidfs(pathFname);
40 if (voidfs.open(QIODevice::ReadOnly))
42 // Check they're the same size
43 int len = srtm.size();
44 if (voidfs.size() == len)
46 QDataStream srtmStream(&srtm);
47 QDataStream voidStream(&voidfs);
48 srtmStream.setByteOrder(QDataStream::BigEndian);
49 voidStream.setByteOrder(QDataStream::BigEndian);
50 for (int i = 0; i < len; i += 2)
52 // Read value and voidness
53 uint16_t srtmness;
54 uint16_t voidness;
55 srtmStream >> srtmness;
56 voidStream >> voidness;
58 // Copy voidness over
59 srtmness = (srtmness & 0x7FFF) | (voidness & 0x8000);
60 srtm.seek(i);
61 srtmStream << srtmness;
64 else
66 qDebug() << "Size mismatch with " << filename << " and " << pathFname;
67 return 8;
70 else
72 qDebug() << "Could not access " << pathFname;
73 return 4;
76 else
78 qDebug() << "Could not access " << filename;
79 return 2;
81 return 0;
84 int main(int argc, char** argv)
86 qDebug() << "Welcome to cpVoids";
87 int error = 0;
88 // Copy void data into HGT files specified
89 char * voids = 0;
90 for (int i = 1; i < argc; ++i)
92 if (0 == strcmp(argv[i], "--voids"))
94 // next argument is voids directory
95 if (++i < argc)
97 voids = argv[i];
98 qDebug() << "Using voids from " << voids;
101 else
103 if (0 == voids)
105 qDebug() << "Cannot copy voids into " << argv[i] << ", no void directory specified, use --voids void/dir before HGT files";
106 return 1;
108 error |= cpVoids(argv[i], voids);
111 return error;