98 lines
1.9 KiB
Go
98 lines
1.9 KiB
Go
package radarfetch
|
|
|
|
import (
|
|
"image"
|
|
"image/color"
|
|
"image/png"
|
|
"os"
|
|
)
|
|
|
|
var colors15 = []string{
|
|
"#0000F6", "#01A0F6", "#00ECEC", "#01FF00", "#00C800",
|
|
"#019000", "#FFFF00", "#E7C000", "#FF9000", "#FF0000",
|
|
"#D60000", "#C00000", "#FF00F0", "#780084", "#AD90F0",
|
|
}
|
|
|
|
func hexToRGBA(s string, a uint8) color.RGBA {
|
|
if len(s) >= 7 && s[0] == '#' {
|
|
r := xtoi(s[1:3])
|
|
g := xtoi(s[3:5])
|
|
b := xtoi(s[5:7])
|
|
return color.RGBA{uint8(r), uint8(g), uint8(b), a}
|
|
}
|
|
return color.RGBA{0, 0, 0, 0}
|
|
}
|
|
|
|
func xtoi(h string) int {
|
|
v := 0
|
|
for i := 0; i < len(h); i++ {
|
|
c := h[i]
|
|
v <<= 4
|
|
switch {
|
|
case c >= '0' && c <= '9':
|
|
v |= int(c - '0')
|
|
case c >= 'a' && c <= 'f':
|
|
v |= int(c-'a') + 10
|
|
case c >= 'A' && c <= 'F':
|
|
v |= int(c-'A') + 10
|
|
}
|
|
}
|
|
return v
|
|
}
|
|
|
|
func colorForDBZ(dbz float64) color.RGBA {
|
|
if dbz < 0 {
|
|
return color.RGBA{0, 0, 0, 0}
|
|
}
|
|
idx := int(dbz / 5.0)
|
|
if idx < 0 {
|
|
idx = 0
|
|
}
|
|
if idx >= len(colors15) {
|
|
idx = len(colors15) - 1
|
|
}
|
|
return hexToRGBA(colors15[idx], 255)
|
|
}
|
|
|
|
// RenderBinToPNG renders 256x256 BE int16 .bin into a PNG using CMA-style colors.
|
|
func RenderBinToPNG(srcPath, dstPath string, flipY bool) error {
|
|
b, err := os.ReadFile(srcPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
const w, h = 256, 256
|
|
if len(b) != w*h*2 {
|
|
return ErrSize
|
|
}
|
|
img := image.NewRGBA(image.Rect(0, 0, w, h))
|
|
for row := 0; row < h; row++ {
|
|
outRow := row
|
|
if flipY {
|
|
outRow = h - 1 - row
|
|
}
|
|
for col := 0; col < w; col++ {
|
|
off := (row*w + col) * 2
|
|
u := uint16(b[off])<<8 | uint16(b[off+1])
|
|
v := int16(u)
|
|
if v == 32767 || v < 0 {
|
|
img.SetRGBA(col, outRow, color.RGBA{0, 0, 0, 0})
|
|
continue
|
|
}
|
|
dbz := float64(v) / 10.0
|
|
img.SetRGBA(col, outRow, colorForDBZ(dbz))
|
|
}
|
|
}
|
|
f, err := os.Create(dstPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer f.Close()
|
|
return png.Encode(f, img)
|
|
}
|
|
|
|
var ErrSize = errSize{}
|
|
|
|
type errSize struct{}
|
|
|
|
func (errSize) Error() string { return "unexpected .bin size (expected 131072 bytes)" }
|