/
ozmap.cpp
300 lines (259 loc) · 8.35 KB
/
ozmap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*********************************************************************************/
/* GPSTurbo */
/* */
/* Programmed by Kevin Pickell */
/* */
/* http://www.scale18.com/cgi-bin/page/gpsturbo.html */
/* */
/* GPSTurbo is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; version 2. */
/* */
/* GPSTurbo is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* http://www.gnu.org/licenses/gpl.txt */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with GPSTurbo; if not, write to the Free Software */
/* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
/* */
/*********************************************************************************/
#include "gpsturbo.h"
/* oziformat uses zlib compression */
#include "zlib/zlib.h"
#pragma pack( push, 1)
typedef struct
{
short wSignature; // 0x7778
int dw0; // 0x00000000
int dw1; // 0x00010040
int dw2; // 0x00000436
int dw3; // 0x00000028
int dwWidth;
int dwHeight;
short wPlanes; // ?? - 0x0001
short wBitsPerPixel; // ?? - 0x0008
int dw4; // 0x00000000
int dwMemSize; // Assumed that 1 unsigned char per pixel, = dwWidth*dwHeight.
int dw5; // 0x00000000
int dw6; // 0x00000000
int dw7; // 0x00000100
int dw8; // 0x00000000 for 1.x, 0x00000100 for 2.x
}OZ2FHEADER_DEF;
#pragma pack( pop)
OZF2GPXMap::OZF2GPXMap(const char *fn)
{
kGUIString ozfn;
DataHandle dh;
int linenum;
kGUIString line;
kGUIStringSplit ss;
int index;
int z;
int nf;
unsigned char *fp;
unsigned long filesize;
OZ2FHEADER_DEF *oh;
double maxlat,maxlon;
double maxx,maxy;
m_coords=0;
dh.SetFilename(fn);
if(dh.Open()==false)
return; /* couldn't load file */
linenum=0;
do
{
dh.ReadLine(&line);
nf=ss.Split(&line,",");
if(linenum==2)
{
int sl;
static char dirchar[]={ DIRCHAR };
ozfn.SetString(fn); /* extract path from filename */
sl=ozfn.GetLen()-1;
/* cut off last subdir */
while(sl>0)
{
if(ozfn.GetChar(sl)==dirchar[0])
{
++sl;
break;
}
--sl;
};
ozfn.Clip(sl);
ozfn.Append(line.GetString());
}
else if(linenum==4)
{
/* datum */
if(!strcmp(ss.GetWord(0)->GetString(),"WGS 84"))
{
/* ok */
}
else
{
dh.Close();
return;
}
//if (! FindOziDatumToWGS84 (strDatum.c_str (), m_pDatumTransformation)) {
// m_pDatumTransformation = NULL;
// ReportWarning ("Some unsupported datum is used: '%s'. No datum conversion performed.", strDatum.c_str ());
}
else
{
if(!strcmp(ss.GetWord(0)->GetString(),"Map Projection"))
{
/* todo */
}
else if(!strcmp(ss.GetWord(0)->GetString(),"MMPNUM"))
{
m_coords=new OZF2Corners[ss.GetWord(1)->GetInt()];
}
else if(!strcmp(ss.GetWord(0)->GetString(),"MMPXY"))
{
/* pixel position */
index=atoi(ss.GetWord(1)->GetString())-1;
m_coords[index].x=atoi(ss.GetWord(2)->GetString());
m_coords[index].y=atoi(ss.GetWord(3)->GetString());
}
else if(!strcmp(ss.GetWord(0)->GetString(),"MMPLL"))
{
/* latitude/longitude position */
index=atoi(ss.GetWord(1)->GetString())-1;
m_coords[index].lon=atof(ss.GetWord(2)->GetString());
m_coords[index].lat=atof(ss.GetWord(3)->GetString());
}
}
++linenum;
}while(dh.Eof()==false);
dh.Close();
/* calculate bounding rectanle for lat/lon */
m_minlat=m_coords[0].lat;
maxlat=m_coords[0].lat;
m_minlon=m_coords[0].lon;
maxlon=m_coords[0].lon;
m_minx=m_coords[0].x;
maxx=m_coords[0].x;
m_miny=m_coords[0].y;
maxy=m_coords[0].y;
for(index=1;index<4;++index)
{
if(m_coords[index].lat<m_minlat)
{
m_minlat=m_coords[index].lat;
m_miny=m_coords[index].y;
}
if(m_coords[index].lat>maxlat)
{
maxlat=m_coords[index].lat;
maxy=m_coords[index].y;
}
if(m_coords[index].lon<m_minlon)
{
m_minlon=m_coords[index].lon;
m_minx=m_coords[index].x;
}
if(m_coords[index].lon>maxlon)
{
maxlon=m_coords[index].lon;
maxx=m_coords[index].x;
}
}
/* assume linear for now until I figure out the projection */
m_slx=(maxlon-m_minlon)/(maxx-m_minx);
m_sly=(maxlat-m_minlat)/(maxy-m_miny);
m_lsx=(maxx-m_minx)/(maxlon-m_minlon);
m_lsy=(maxy-m_miny)/(maxlat-m_minlat);
for(index=0;index<4;++index)
{
GPXCoord c;
int sx,sy;
c.Set(m_coords[index].lat,m_coords[index].lon);
ToMap(&c,&sx,&sy);
DebugPrint("Coord testing %f,%f %d,%d - %d,%d",m_coords[index].lat,m_coords[index].lon,m_coords[index].x,m_coords[index].y,sx,sy);
}
/* load the map file with the tiles */
fp=kGUI::LoadFile(ozfn.GetString(),&filesize);
if(!fp)
return; /* couldn't load file */
m_filedata=fp; /* save pointer to data so destructor will free it up upon exiting */
oh=(OZ2FHEADER_DEF *)fp;
if(oh->wSignature!=0x7778)
return; /* not valid format */
assert ((oh->wPlanes==1) && (oh->wBitsPerPixel==8),"Unsupported bitmap format" );
SetTileSize(64,64);
/* parse the tile data */
const int dwMasterTblOffset = * reinterpret_cast<const int *> (fp + filesize - 4);
const int * const pdwMasterTbl = reinterpret_cast<const int *> (fp + dwMasterTblOffset);
const int zoomlevels = (filesize - 4 - dwMasterTblOffset)/4;
SetZoomLevels(1,zoomlevels);
m_bitmaps.Alloc(zoomlevels);
z=zoomlevels-1;
for (int zoomlevel = 0; zoomlevel < zoomlevels; ++ zoomlevel)
{
OZ2FBITMAP_DEF *ih = (OZ2FBITMAP_DEF *) (fp + pdwMasterTbl [zoomlevel]);
m_bitmaps.SetEntry(z,ih);
SetSize(z,ih->dwImageWidth,ih->dwImageHeight);
--z;
}
}
OZF2GPXMap::~OZF2GPXMap()
{
if(m_coords)
delete []m_coords;
if(m_filedata)
delete []m_filedata;
}
void OZF2GPXMap::ToMap(class GPXCoord *c,int *sx,int *sy)
{
double lat=c->GetLat();
double lon=c->GetLon();
*(sx)=(int)((lon-m_minlon)*m_lsx)+m_minx;
*(sy)=(int)((lat-m_minlat)*m_lsy)+m_miny;
}
void OZF2GPXMap::FromMap(int sx,int sy,class GPXCoord *c)
{
c->Set(((sy-m_miny)*m_sly)+m_minlat,((sx-m_minx)*m_slx)+m_minlon);
}
/* draw tile at this position */
int OZF2GPXMap::DrawTile(int tx,int ty)
{
int x,y;
OZ2FBITMAP_DEF *b=m_bitmaps.GetEntry(GetZoom());
OZ2FCOLOUR_DEF *c;
unsigned char *up;
unsigned char unpacked[64*64];
z_stream d_stream; /* decompression stream */
int dwTileOffset;
unsigned char *pTileData;
if(tx<0 || tx>=b->wXTilesNum || ty<0 || ty>=b->wYTilesNum)
return(TILE_ERROR);
dwTileOffset = b->dwTileDataOffset [(ty*b->wXTilesNum)+tx];
pTileData = (unsigned char *) (m_filedata + dwTileOffset);
d_stream.zalloc = (alloc_func)0;
d_stream.zfree = (free_func)0;
d_stream.opaque = (voidpf)0;
d_stream.next_in = pTileData;
d_stream.avail_in=64*64;
d_stream.next_out = unpacked;
d_stream.avail_out=64*64;
inflateInit(&d_stream);
inflate(&d_stream, Z_NO_FLUSH);
inflateEnd(&d_stream);
//decompress(
up=unpacked;
for(y=0;y<64;++y)
{
kGUIColor *sp=kGUI::GetSurfacePtr(0,63-y);
for(x=0;x<64;++x)
{
c=b->dwColor+*(up++);
*(sp++)=DrawColor(c->r,c->g,c->b);
}
}
return(TILE_OK);
}