Skip to content

Instantly share code, notes, and snippets.

@oldpatricka
Created February 16, 2018 16:28
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save oldpatricka/f655c5a754e10775955df7458ecc0c91 to your computer and use it in GitHub Desktop.
Save oldpatricka/f655c5a754e10775955df7458ecc0c91 to your computer and use it in GitHub Desktop.
Basic tool to dump TIFF Headers
// dump-tiff.go - Basic tool to dump TIFF Headers
//
// depends on github.com/google/tiff, which you can install with:
// $ go get github.com/google/tiff
package main
import (
"os"
"log"
"github.com/google/tiff"
_ "github.com/google/tiff/geotiff"
"fmt"
)
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "usage: %s file.tif", os.Args[0])
os.Exit(1)
}
filename := os.Args[1]
reader, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer reader.Close()
t, err := tiff.Parse(reader, nil, nil)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Dumping TIFF headers for %s:\n", filename)
PrintTiff(t)
PrintCOGInfo(t)
}
func PrintTiff(t tiff.TIFF) {
fmt.Printf("Version: %d\n", t.Version())
fmt.Printf("Byte Order: %s\n", EndianString(t.Order()))
for i, ifd := range t.IFDs() {
fmt.Printf("IFD %d:\n", i)
for _, f := range ifd.Fields() {
PrintField(f)
}
}
}
func PrintCOGInfo(t tiff.TIFF) {
fmt.Println("\nTags for Cloud Optimized TIFFs:")
for i, ifd := range t.IFDs() {
fmt.Printf("IFD %d:\n", i)
for _, f := range ifd.Fields() {
tag := f.Tag()
if tag.Name() == "TileWidth" {
PrintField(f)
}
if tag.Name() == "TileOffsets" {
PrintField(f)
}
if tag.Name() == "TileByteCounts" {
PrintField(f)
}
}
}
}
func PrintField(f tiff.Field) {
tag := f.Tag()
fmt.Printf("Tag: ID: %d\tName: %s\tType: %s\n", tag.ID(), tag.Name(), f.Type().Name())
}
func EndianString(magicEndian string) string {
if magicEndian == tiff.MagicBigEndian {
return "big endian"
} else {
return "little endian"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment