A tool for archiving & converting scans of postcards, and information about them.
at main 41 lines 896 B view raw
1package resolution 2 3import ( 4 "fmt" 5 "math/big" 6) 7 8var readers = []struct { 9 magicBytes []byte 10 fn func([]byte) (*big.Rat, *big.Rat, error) 11}{ 12 {[]byte(pngHeader), decodePNG}, 13 {[]byte("\xff\xd8"), decodeJPEG}, 14 {[]byte("RIFF????WEBPVP8"), decodeWebP}, 15 {[]byte{0x4D, 0x4D, 0x00, 0x2A}, decodeTIFF}, 16 {[]byte{0x49, 0x49, 0x2A, 0x00}, decodeTIFF}, 17} 18 19// Decode returns the width and height resolution (number of pixels per centimetre) an image declares it is stored with 20func Decode(data []byte) (*big.Rat, *big.Rat, error) { 21 for _, r := range readers { 22 if isMagic(data[0:len(r.magicBytes)], r.magicBytes) { 23 return r.fn(data) 24 } 25 } 26 return nil, nil, fmt.Errorf("unknown image format") 27} 28 29func isMagic(data, magic []byte) bool { 30 if len(magic) != len(data) { 31 return false 32 } 33 34 for i, b := range data { 35 if magic[i] != b && magic[i] != '?' { 36 return false 37 } 38 } 39 40 return true 41}