Skip to content

Instantly share code, notes, and snippets.

@oldpatricka

oldpatricka/dump-tiff.go Secret

Created Feb 16, 2018
Embed
What would you like to do?
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
You can’t perform that action at this time.