TGC Codebase Backup



Heightmaps from USGS .flt files by Visigoth

10th Sep 2009 1:54
Summary

function to create greyscale, or color maps with some tweaking, of USGS .flt files.



Description



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    function make_flt_heightmap(infile$,in_cols,in_rows,left,top,right,bottom,outfile$)
//params are:
//infile$ is the filename of the USGS .flt file, in_cols is the number of columns in the .flt file
//in_rows is the number of rows in the .flt file, top,left,bottom,right are the coordinates of the image
//you want to grab, outfile$ is the filename to save the .bmp"

open to read 1,infile$
make memblock from file 1,1
close file 1

dim vertices(3,in_rows,in_cols) as integer

//find lowest, highest height
lo_height# = 20000
hi_height# = 0
  
for r = 1 to in_rows
	for c = 1 to in_cols
		height# = memblock float(1,mem_pos)
		
		if height# > hi_height#
			hi_height# = height#
		endif
		
		if height# < lo_height#
			lo_height# = height#
		endif

		inc mem_pos,4
	next c 
next r

//compute the color scale
c_scale# = (255.0 / (hi_height# - lo_height#))
//set the color value for each position, load into array
mem_pos = 0
for r = 1 to in_rows
	for c = 1 to in_cols
		height# = memblock float(1,mem_pos)
		color = int((height# - lo_height#) * c_scale#)
		vertices(1,r,c) = color //you can have different color settings if you want to use certain channels
		vertices(2,r,c) = color
		vertices(3,r,c) = color
		inc mem_pos,4
	next c 
next r

delete memblock 1

//make the bitmap
make memblock 1,((in_cols * in_rows) * 4) + 12
write memblock dword 1,0,in_cols
write memblock dword 1,4,in_rows
write memblock dword 1,8,32
mem_pos = 12

for r = 1 to in_rows
	for c = 1 to in_cols
		write memblock byte 1,mem_pos,int(vertices(1,r,c)) : inc mem_pos,1 		//blue
		write memblock byte 1,mem_pos,int(vertices(2,r,c)) : inc mem_pos,1		//green
		write memblock byte 1,mem_pos,int(vertices(3,r,c)) : inc mem_pos,1		//red
		write memblock byte 1,mem_pos,0 : inc mem_pos,1
	next c
next r

make bitmap from memblock 1,1
set current bitmap 1
get image 1,left,top,right,bottom,1
save image outfile$,1
delete bitmap 1
delete image 1
delete memblock 1
undim vertices()

endfunction