(* Compile with: ocamlopt.opt unix.cmxa bordeaux.ml -o bordeaux *) type map_t = { map_width: int; map_height: int; map_data: int array array; } let map_create ~width ~height = Printf.printf "Creating map..."; flush stdout; let rows = Array.init height (fun i -> Array.make width 0) in print_newline (); { map_width = width; map_height = height; map_data = rows; } let map_merge_hgt ~map ~filename ~x ~y ~width ~height = Printf.printf "Merging %s..." filename; flush stdout; try let hgt = Unix.open_process_in(Printf.sprintf "unzip -p %s.zip %s" filename filename) in let r2 = ref y in for r=0 to height-1 do let c2 = ref x in for c=0 to width-1 do let v = (input_byte hgt) lor (input_byte hgt) lsl 8 in let v = if (v land 0x8000) <> 0 then -1 - (v lxor 0xffff) else v in map.map_data.(!r2).(!c2) <- v; incr c2 done; incr r2 done; ignore (Unix.close_process_in hgt); print_newline () with _ -> Printf.eprintf "Couldn't open %s\n" filename let map_save ~map ~filename ?(base = 0) ?(x = 0) ?(y = 0) ?(width = map.map_width) ?(height = map.map_height) () = Printf.printf "Saving map to %s..." filename; flush stdout; try let tga = open_out_bin filename in let tgaheader = Array.make 18 0 in tgaheader.(2) <- 2; tgaheader.(12) <- width mod 256; tgaheader.(13) <- width / 256; tgaheader.(14) <- height mod 256; tgaheader.(15) <- height / 256; tgaheader.(16) <- 24; tgaheader.(17) <- 32; for i=0 to 17 do output_byte tga tgaheader.(i) done; for r=y to y+height-1 do for c=x to x+width-1 do let data = map.map_data.(r).(c) in let v = if data < -32000 then 0 else data-base in if v < 0 then Printf.eprintf "base too high for %s, should be less than %d\n" filename data; output_byte tga 0; output_byte tga (v mod 256); output_byte tga (v / 256); done done; close_out tga; print_newline () with _ -> Printf.eprintf "Error while writing %s\n" filename let _ = let map = map_create ~width:2401 ~height:2401 in map_merge_hgt ~map:map ~filename:"N44W002.hgt" ~x:0 ~y:1200 ~width:1201 ~height:1201; map_merge_hgt ~map:map ~filename:"N45W002.hgt" ~x:0 ~y:0 ~width:1201 ~height:1201; map_merge_hgt ~map:map ~filename:"N44W001.hgt" ~x:1200 ~y:1200 ~width:1201 ~height:1201; map_merge_hgt ~map:map ~filename:"N45W001.hgt" ~x:1200 ~y:0 ~width:1201 ~height:1201; map_save ~map:map ~filename:"bordeaux-ml.tga" ~base:(-70) ()