Skip to content

Instantly share code, notes, and snippets.

@zgiber
Last active August 29, 2015 14:06
Show Gist options
  • Save zgiber/48f99261b34dd131eb30 to your computer and use it in GitHub Desktop.
Save zgiber/48f99261b34dd131eb30 to your computer and use it in GitHub Desktop.
convert normal map to blender's format (swap Red-Green channels)
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/png"
"os"
)
func main() {
var fname string
flag.StringVar(&fname, "f", "", "A .png image (normal map)")
flag.Parse()
file, err := os.Open(fname) // opening the file
if err != nil {
fmt.Println(err)
return
}
defer file.Close()
pic, err := png.Decode(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", fname, err)
return
}
rgba, ok := pic.(*image.RGBA)
if !ok {
fmt.Println("Only RGBA .png files are supported.")
return
}
b := pic.Bounds()
fmt.Println("Swapping channels...")
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
col := rgba.At(x, y).(color.RGBA)
col.G, col.R = col.R, col.G
rgba.Set(x, y, col)
}
}
fmt.Println("Writing...")
out_file, err := os.Create(fname)
if err != nil {
fmt.Println(err)
return
}
defer out_file.Close()
err = png.Encode(out_file, pic)
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Done!")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment