Skip to content

Commit

Permalink
add converter png2bin.go
Browse files Browse the repository at this point in the history
  • Loading branch information
gaoyifan committed Nov 27, 2015
1 parent 8aa0b7d commit b6ad40f
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions converter/png2bin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"bytes"
"encoding/binary"
"fmt"
"image/png"
"os"
"regexp"
"strconv"
)

func main() {
filePng, err := os.Open(os.Args[1])
re, _ := regexp.Compile("ASTGTM2_(...)(....)_dem")
submatch := re.FindSubmatch([]byte(os.Args[1]))
lat := (string)(submatch[1])
lon := (string)(submatch[2])
fmt.Println(lat,lon)

if err != nil {
fmt.Println(err)
}
defer filePng.Close()
img, err := png.Decode(filePng)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
var w bytes.Buffer

for a := 0; a < 10; a++ { // cut one image to 100 dat file (10 * 10 mesh)
for b := 0; b < 10; b++ {
fileDat,err:=os.Create(os.Args[2]+"/"+lat+lon+strconv.Itoa(a)+strconv.Itoa(b))
if err !=nil{
fmt.Println(err)
os.Exit(1)
}
for i := a * 360; i < (a+1)*360; i++ {
for j := b * 360; j < (b+1)*360; j++ {
gray, _, _, _ := img.At(i, j).RGBA()
binary.Write(&w, binary.LittleEndian, int16(gray))
}
}
w.WriteTo(fileDat)
fileDat.Close()
}
}
}

0 comments on commit b6ad40f

Please sign in to comment.