Subversion Repositories public

Rev

Rev 158 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 158 Rev 159
Line 16... Line 16...
16
 *   Free Software Foundation, Inc.,                                       *
16
 *   Free Software Foundation, Inc.,                                       *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
17
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
18
 ***************************************************************************/
18
 ***************************************************************************/
19
 
19
 
20
#include <iostream>
20
#include <iostream>
-
 
21
#include <qimage.h>
-
 
22
#include <kmessagebox.h>
-
 
23
#include <klocale.h>
-
 
24
#include <qstring.h>
-
 
25
#include <qcstring.h>
21
#include "transform.h"
26
#include "transform.h"
22
 
27
 
23
using std::cout;
28
using std::cout;
24
using std::cerr;
29
using std::cerr;
25
using std::clog;
30
using std::clog;
Line 49... Line 54...
49
{
54
{
50
	width = _width;
55
	width = _width;
51
	height = _height;
56
	height = _height;
52
	MapXCtr = width / 2;
57
	MapXCtr = width / 2;
53
	MapYCtr = height / 2;
58
	MapYCtr = height / 2;
54
	xscale = 1.0 / ((double)width - 1.0);
59
	xscale = (rlon - llon) / (double)width;
55
	yscale = -1.0 / ((double)height - 1.0);
60
	yscale = -1.0 * ((llat - rlat) / (double)height);
56
	PixPerLatDeg = width / (llat - rlat);
61
	PixPerLatDeg = width / (llat - rlat);
57
	PixPerLonDeg = height / (rlon - llon);
62
	PixPerLonDeg = height / (rlon - llon);
58
}
63
}
59
 
64
 
60
/*
65
/*
Line 82... Line 87...
82
 
87
 
83
/*
88
/*
84
 * Calculate the pixel position on a raster map out of the
89
 * Calculate the pixel position on a raster map out of the
85
 * geo coordinates given in radian.
90
 * geo coordinates given in radian.
86
 */
91
 */
-
 
92
/*bool transform::latLonToPix(double plat, double plon, int *px, int *py)
-
 
93
{
-
 
94
	if (plat < -180.0 || plat > 180.0 || plon < -180.0 || plon > 180.0)
-
 
95
	   return false;
-
 
96
 
-
 
97
	if (px == 0 || py == 0)
-
 
98
	   return false;
-
 
99
 
-
 
100
	*px = (int)(1.0 / (xscale * (plon - llon))) / width;
-
 
101
	*py = (int)(1.0 / (yscale * (rlat - plat))) / height;
-
 
102
clog << "px: " << *px << ", py: " << *py << endl;
-
 
103
	return true;
-
 
104
}
-
 
105
*/
87
bool transform::latLonToPix(double plat, double plon, int *px, int *py)
106
bool transform::latLonToPix(double plat, double plon, int *px, int *py)
88
{
107
{
89
int x,y;
108
int x,y;
90
double lat, lon, xdist, ydist, dist, az, pixdist;
109
double lat, lon, xdist, ydist, dist, az, pixdist;
91
 
110
 
-
 
111
	if (plat < -180.0 || plat > 180.0 || plon < -180.0 || plon > 180.0)
-
 
112
	   return false;
-
 
113
 
-
 
114
	if (px == 0 || py == 0)
-
 
115
	   return false;
-
 
116
 
92
	lat = plat;
117
	lat = plat;
93
	lon = plon;
118
	lon = plon;
94
	ydist = PixPerLatDeg * (lat - MapLatCtr);
119
	ydist = PixPerLatDeg * (lat - MapLatCtr);
95
	xdist = PixPerLonDeg * (lon - MapLonCtr);
120
	xdist = PixPerLonDeg * (lon - MapLonCtr);
96
	az = atan(ydist / xdist);
121
	az = atan(ydist / xdist);
Line 116... Line 141...
116
int x, y;
141
int x, y;
117
 
142
 
118
	latLonToPix(plat, plon, &x, &y);
143
	latLonToPix(plat, plon, &x, &y);
119
	return ((long)width * (long)y + (long)x);
144
	return ((long)width * (long)y + (long)x);
120
}
145
}
-
 
146
 
-
 
147
/*
-
 
148
 * Uses QT to load an image and cut a rectangular part of the image.
-
 
149
 * The function stores the new image in a temporary file. It appends "_tmp.png"
-
 
150
 * to the name of the new file. The new file is always a PNG file!
-
 
151
 *
-
 
152
 * FIXME: Currently the directory with the source image must be writeable.
-
 
153
 *        Otherwise the new temporary file can not be written!
-
 
154
 *        We should move the file into the /temp directory...
-
 
155
 */
-
 
156
bool transform::cutImage(double _llat, double _llon, double _rlat, double _rlon, QString fName)
-
 
157
{
-
 
158
int x, y, w, h, a, b;
-
 
159
QImage src, dst;
-
 
160
 
-
 
161
	if (!latLonToPix (_llat, _llon, &x, &y))
-
 
162
	   return false;
-
 
163
 
-
 
164
	if (!latLonToPix (_rlat, _rlon, &a, &b))
-
 
165
	   return false;
-
 
166
 
-
 
167
	if (!src.load (fName))
-
 
168
	{
-
 
169
	   KMessageBox::error(0, i18n("Error loading image file " + fName + "!"));
-
 
170
	   return false;
-
 
171
	}
-
 
172
 
-
 
173
	w = a - x;
-
 
174
	h = b - y;
-
 
175
 
-
 
176
	dst.create(w, h, 32);
-
 
177
	dst.fill(0xc0c0c0);
-
 
178
	bitBlt (&dst, 0, 0, &src, x, y, w, h, 0);
-
 
179
 
-
 
180
	if (!dst.save (fName + "_tmp.png", "PNG"))
-
 
181
	{
-
 
182
	   KMessageBox::error(0, i18n("Error saving a temporary image file!"));
-
 
183
	   return false;
-
 
184
	}
-
 
185
 
-
 
186
	return true;
-
 
187
}