<*+M2EXTENSIONS *>
DEFINITION MODULE maptool; (* aprs tracks on osm map by oe5dxl *)

FROM SYSTEM IMPORT FILL, ADR, INT16, INT8, CAST, MOVE, CARD8, CARD16, SHIFT;
FROM aprsstr IMPORT TIME, POSITION;
FROM aprsdecode IMPORT COLTYP, AREASYMB, MULTILINE; 

CONST WHITELEVEL=1024;
      TILESIZE=256;
      MAXLAT=1.484;
      MINZOOM=1;
      MAXZOOM=18; 
      MAXCHAR=32+97;                         (* highest font chars *)
      FLOATERRCORR=-0.0000006;  (* try to correct summed rounding errors by multiple zoom in *)

TYPE
     PIX=RECORD r,g,b:CARD16 END;
     IMAGELINE=ARRAY OF PIX;
     IMAGE=ARRAY OF IMAGELINE;
     pIMAGE=POINTER TO IMAGE;

     PANOWIN=RECORD
       isicon,
       empty,
       hover,
       on,
       flatscreen:BOOLEAN;
       eye, horizon:POSITION;
       eyealt:INTEGER;
       angle, elevation, yzoom:REAL;
       actx:CARDINAL;
       hx, hy, mx, my, ximg, yimg:INTEGER;                 (* mouse pos on panwin and fullwin *)
       image:pIMAGE;
     END;

VAR
    xsize, ysize:INTEGER;
    shiftx, shifty:REAL;
    mappack:RECORD
      leftup, rightdown:POSITION;
      tozoom:INTEGER;
      retrys,
      givups,
      mapscnt,
      needcnt,
      donecnt,
      delay,
      retrysum:CARDINAL;
      zoom, tx, ty:INTEGER;
      run:BOOLEAN;
    END;
    fontloadmsg:ARRAY[0..70] OF CHAR;



PROCEDURE Colset(VAR c:COLTYP; w:CHAR);
PROCEDURE waypoint(image:pIMAGE; x,y,r:REAL; rr,gg,bb:INTEGER);
PROCEDURE vector(image:pIMAGE; x0,y0,x1,y1:REAL; rr,gg,bb:INTEGER; width:CARDINAL; glow:REAL);
PROCEDURE drawchar(img:pIMAGE; ch:CHAR; x0r,y0r:REAL; VAR inc:INTEGER;
                   bri, contrast:CARDINAL; col:COLTYP; dryrun:BOOLEAN);
PROCEDURE drawstr(image:pIMAGE; s:ARRAY OF CHAR; xr, yr:REAL; bri, contrast:CARDINAL;
                  col:COLTYP; VAR pos:INT8; slide:CARDINAL; fixpos, dryrun:BOOLEAN);
PROCEDURE drawstri(image:pIMAGE; s:ARRAY OF CHAR; xr, yr:INTEGER; bri, contrast:CARDINAL;
                   col:COLTYP; proportional, dryrun:BOOLEAN);
PROCEDURE drawsym(image:pIMAGE; tab,sym:CHAR; mirror:BOOLEAN; x0r,y0r:REAL; bri:CARDINAL);
PROCEDURE drawarrow(image:pIMAGE; x0, y0, len, ang:REAL; wind, bri:CARDINAL; col:COLTYP);
PROCEDURE drawpois(image:pIMAGE);
PROCEDURE shine(image:pIMAGE; lum:INTEGER);
PROCEDURE loadmap(map:pIMAGE; tx,ty,zoom:INTEGER; fzoom, shftx, shfty:REAL;
                  VAR done, blown:BOOLEAN; noblow, dryrun:BOOLEAN);
PROCEDURE IsMapLoaded():BOOLEAN;
PROCEDURE addmap(image, map:pIMAGE);
PROCEDURE mercator(len, lat:REAL; zoom:INTEGER; VAR tilex, tiley:INTEGER; VAR x, y:REAL);
PROCEDURE mapxy(pos:POSITION; VAR x, y:REAL):INTEGER;
PROCEDURE xytodeg(x, y:REAL; VAR pos:POSITION);
PROCEDURE saveppm(fn:ARRAY OF CHAR; image:pIMAGE; xsize, ysize:INTEGER; tmp:BOOLEAN):INTEGER;
PROCEDURE area(img:pIMAGE; x0,y0,x1,y1:INTEGER; col:COLTYP; add:BOOLEAN);
PROCEDURE shiftmap(x, y, ysize:INTEGER; zoom:REAL; VAR pos:POSITION);
PROCEDURE center(xsize, ysize:INTEGER; zoom:REAL; centpos:POSITION; VAR pos:POSITION);
PROCEDURE limpos(VAR pos:POSITION);
PROCEDURE makebw(p:pIMAGE);
PROCEDURE realzoom(zi:INTEGER; zf:REAL):REAL;
PROCEDURE setmark(image:pIMAGE; pos:POSITION; hard:BOOLEAN);
PROCEDURE cc(img:pIMAGE; from, to:TIME);
PROCEDURE ruler(img:pIMAGE);
PROCEDURE clr(img:pIMAGE);
PROCEDURE xytoloc(mpos:POSITION; VAR s:ARRAY OF CHAR);
PROCEDURE POIname(VAR mpos:POSITION; VAR s:ARRAY OF CHAR; VAR info:ARRAY OF CHAR);
PROCEDURE POIfind(VAR mpos:POSITION; all:BOOLEAN; s:ARRAY OF CHAR);
PROCEDURE charwidth(ch:CHAR):CARDINAL;
PROCEDURE vistime(t:TIME):BOOLEAN;
PROCEDURE StartMapPackage(lu, rd:POSITION; tillzoom:INTEGER; dryrun:BOOLEAN);
PROCEDURE MapPackageJob(dryrun:BOOLEAN);
PROCEDURE loadfont;
PROCEDURE startmapdelay;
PROCEDURE pullmap(x, y, x0:INTEGER; init:BOOLEAN);
PROCEDURE geoprofile(image:pIMAGE; pos0, pos1:POSITION; lambda:REAL;
                     ant1nn:BOOLEAN; ant1, ant2:INTEGER; VAR dist, h1, h2, ele1, ele2:REAL):INTEGER;
(*
PROCEDURE getsrtm(pos:POSITION; quality:CARDINAL; VAR resolution:REAL):REAL;
PROCEDURE closesrtmfile;
*)
PROCEDURE Radiorange(image:pIMAGE; txpos:POSITION; ant1, ant2:INTEGER;
                     smooth, colnr, qualnum:CARDINAL; VAR abort:BOOLEAN; refrac:REAL);
PROCEDURE Panorama(testimg:pIMAGE; panpar:PANOWIN; VAR abort:BOOLEAN);
PROCEDURE SimpleRelief(image:pIMAGE):BOOLEAN;
PROCEDURE findpanopos(panpar:PANOWIN; VAR pos:POSITION; VAR dist:REAL; VAR alt:INTEGER);
(*
PROCEDURE rdmountains(fn:ARRAY OF CHAR; add:BOOLEAN);
*)
PROCEDURE drawareasym(image:pIMAGE; pm:POSITION; area:AREASYMB; bri:CARDINAL);
PROCEDURE drawpoligon(image:pIMAGE; pm:POSITION; md:MULTILINE; tab, sym:CHAR; bri:CARDINAL);
PROCEDURE drawpoliobj(image:pIMAGE);
PROCEDURE findmultiline(pos:POSITION; VAR foundpos:POSITION):BOOLEAN;
PROCEDURE readpoifiles;
PROCEDURE poisactiv():BOOLEAN;
PROCEDURE cmpwild(a, b:ARRAY OF CHAR):BOOLEAN;
PROCEDURE cleanfind(VAR s:ARRAY OF CHAR);
PROCEDURE zoominout(in, fine, allowrev, mouseismiddle:BOOLEAN);

END maptool.
