mirror of
https://github.com/dsoprea/go-exif.git
synced 2025-05-31 11:41:57 +00:00
exif: Dropped requirement for incorrect/non-standard EXIF prefix.
This commit is contained in:
parent
d05c9b4c81
commit
2eb57331ff
24
exif.go
24
exif.go
@ -15,7 +15,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
// ExifAddressableAreaStart is the absolute offset in the file that all
|
// ExifAddressableAreaStart is the absolute offset in the file that all
|
||||||
// offsets are relative to.
|
// offsets are relative to.
|
||||||
ExifAddressableAreaStart = uint32(0x6)
|
ExifAddressableAreaStart = uint32(0x0)
|
||||||
|
|
||||||
// ExifDefaultFirstIfdOffset is essentially the number of bytes in addition
|
// ExifDefaultFirstIfdOffset is essentially the number of bytes in addition
|
||||||
// to `ExifAddressableAreaStart` that you have to move in order to escape
|
// to `ExifAddressableAreaStart` that you have to move in order to escape
|
||||||
@ -29,8 +29,6 @@ const (
|
|||||||
var (
|
var (
|
||||||
exifLogger = log.NewLogger("exif.exif")
|
exifLogger = log.NewLogger("exif.exif")
|
||||||
|
|
||||||
ExifHeaderPrefixBytes = []byte("Exif\000\000")
|
|
||||||
|
|
||||||
// EncodeDefaultByteOrder is the default byte-order for encoding operations.
|
// EncodeDefaultByteOrder is the default byte-order for encoding operations.
|
||||||
EncodeDefaultByteOrder = binary.BigEndian
|
EncodeDefaultByteOrder = binary.BigEndian
|
||||||
|
|
||||||
@ -135,16 +133,12 @@ func ParseExifHeader(data []byte) (eh ExifHeader, err error) {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if bytes.Compare(data[:6], ExifHeaderPrefixBytes) != 0 {
|
|
||||||
log.Panic(ErrNotExif)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Good reference:
|
// Good reference:
|
||||||
//
|
//
|
||||||
// CIPA DC-008-2016; JEITA CP-3451D
|
// CIPA DC-008-2016; JEITA CP-3451D
|
||||||
// -> http://www.cipa.jp/std/documents/e/DC-008-Translation-2016-E.pdf
|
// -> http://www.cipa.jp/std/documents/e/DC-008-Translation-2016-E.pdf
|
||||||
|
|
||||||
byteOrderBytes := [2]byte { data[6], data[7] }
|
byteOrderBytes := [2]byte { data[0], data[1] }
|
||||||
|
|
||||||
byteOrder, found := ByteOrderLookup[byteOrderBytes]
|
byteOrder, found := ByteOrderLookup[byteOrderBytes]
|
||||||
if found == false {
|
if found == false {
|
||||||
@ -152,18 +146,13 @@ func ParseExifHeader(data []byte) (eh ExifHeader, err error) {
|
|||||||
log.Panic(ErrNotExif)
|
log.Panic(ErrNotExif)
|
||||||
}
|
}
|
||||||
|
|
||||||
fixedBytes := [2]byte { data[8], data[9] }
|
fixedBytes := [2]byte { data[2], data[3] }
|
||||||
if fixedBytes != ExifFixedBytes {
|
if fixedBytes != ExifFixedBytes {
|
||||||
exifLogger.Warningf(nil, "EXIF header fixed-bytes should be 0x002a but are: [%v]", fixedBytes)
|
exifLogger.Warningf(nil, "EXIF header fixed-bytes should be 0x002a but are: [%v]", fixedBytes)
|
||||||
log.Panic(ErrNotExif)
|
log.Panic(ErrNotExif)
|
||||||
}
|
}
|
||||||
|
|
||||||
firstIfdOffset := uint32(0)
|
firstIfdOffset := byteOrder.Uint32(data[4:8])
|
||||||
if byteOrder == binary.BigEndian {
|
|
||||||
firstIfdOffset = binary.BigEndian.Uint32(data[10:14])
|
|
||||||
} else {
|
|
||||||
firstIfdOffset = binary.LittleEndian.Uint32(data[10:14])
|
|
||||||
}
|
|
||||||
|
|
||||||
eh = ExifHeader{
|
eh = ExifHeader{
|
||||||
ByteOrder: byteOrder,
|
ByteOrder: byteOrder,
|
||||||
@ -221,10 +210,7 @@ func BuildExifHeader(byteOrder binary.ByteOrder, firstIfdOffset uint32) (headerB
|
|||||||
|
|
||||||
b := new(bytes.Buffer)
|
b := new(bytes.Buffer)
|
||||||
|
|
||||||
_, err = b.Write(ExifHeaderPrefixBytes)
|
// This is the point in the data that all offsets are relative to.
|
||||||
log.PanicIf(err)
|
|
||||||
|
|
||||||
// NOTE: This is the point in the data that all offsets are relative to.
|
|
||||||
boBytes := ByteOrderLookupR[byteOrder]
|
boBytes := ByteOrderLookupR[byteOrder]
|
||||||
_, err = b.WriteString(string(boBytes[:]))
|
_, err = b.WriteString(string(boBytes[:]))
|
||||||
log.PanicIf(err)
|
log.PanicIf(err)
|
||||||
|
@ -30,11 +30,7 @@ func init() {
|
|||||||
|
|
||||||
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
|
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
|
||||||
|
|
||||||
exifData, err := ioutil.ReadFile(filepath)
|
var err error
|
||||||
|
testExifData, err = ioutil.ReadFile(filepath)
|
||||||
log.PanicIf(err)
|
log.PanicIf(err)
|
||||||
|
|
||||||
// TODO(dustin): !! We're currently built to expect the JPEG EXIF header-prefix, but our test-data doesn't have that.So, artificially prefix it, for now.
|
|
||||||
testExifData = make([]byte, len(exifData) + len(ExifHeaderPrefixBytes))
|
|
||||||
copy(testExifData[0:], ExifHeaderPrefixBytes)
|
|
||||||
copy(testExifData[len(ExifHeaderPrefixBytes):], exifData)
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user