mirror of https://github.com/dsoprea/go-exif.git
ifdBuilder.dumpToStrings: Fixed usage of II.
- We were ambiguously treating it as both the parent II in some places and the child II in others. It's supposed to be the parent II.pull/3/head
parent
e3c96766ca
commit
843976865e
|
@ -62,7 +62,7 @@ func (ibtv IfdBuilderTagValue) Ib() *IfdBuilder {
|
|||
|
||||
|
||||
type builderTag struct {
|
||||
// ii is non-empty if represents a child-IFD.
|
||||
// ii is the IfdIdentity of the IFD that hosts this tag.
|
||||
ii IfdIdentity
|
||||
|
||||
tagId uint16
|
||||
|
@ -446,14 +446,15 @@ func (ib *IfdBuilder) dumpToStrings(thisIb *IfdBuilder, prefix string, lines []s
|
|||
}
|
||||
|
||||
for i, tag := range thisIb.tags {
|
||||
line := fmt.Sprintf("<PARENTS=[%s] IFD-NAME=[%s]> IFD-TAG-ID=(0x%02x) CHILD-IFD=[%s] INDEX=(%d) TAG=[0x%02x]", prefix, thisIb.ii.IfdName, thisIb.ifdTagId, tag.ii.IfdName, i, tag.tagId)
|
||||
childIfdName := ""
|
||||
if tag.value.IsIb() == true {
|
||||
childIfdName = tag.value.Ib().ii.IfdName
|
||||
}
|
||||
|
||||
line := fmt.Sprintf("<PARENTS=[%s] IFD-NAME=[%s]> IFD-TAG-ID=(0x%02x) CHILD-IFD=[%s] INDEX=(%d) TAG=[0x%02x]", prefix, thisIb.ii.IfdName, thisIb.ifdTagId, childIfdName, i, tag.tagId)
|
||||
linesOutput = append(linesOutput, line)
|
||||
|
||||
if tag.ii.IfdName != "" {
|
||||
if tag.value.IsIb() == false {
|
||||
log.Panicf("tag has IFD tag-ID (0x%02x) and is acting like a child IB but does not *look* like a child IB: %v", tag.tagId, tag)
|
||||
}
|
||||
|
||||
if tag.value.IsIb() == true {
|
||||
childPrefix := ""
|
||||
if prefix == "" {
|
||||
childPrefix = fmt.Sprintf("%s", thisIb.ii.IfdName)
|
||||
|
@ -669,7 +670,7 @@ func (ib *IfdBuilder) AddChildIb(childIb *IfdBuilder) (err error) {
|
|||
value := NewIfdBuilderTagValueFromIfdBuilder(childIb)
|
||||
|
||||
bt := NewChildIfdBuilderTag(
|
||||
childIb.ii,
|
||||
ib.ii,
|
||||
childIb.ifdTagId,
|
||||
value)
|
||||
|
||||
|
|
|
@ -6,8 +6,6 @@ import (
|
|||
"reflect"
|
||||
"fmt"
|
||||
"strings"
|
||||
"path"
|
||||
"sort"
|
||||
|
||||
"github.com/dsoprea/go-logging"
|
||||
)
|
||||
|
@ -927,113 +925,3 @@ func ExampleIfdByteEncoder_EncodeToExif() {
|
|||
// 4: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x13e) TAG-TYPE=[RATIONAL] UNIT-COUNT=(1)> [[{286335522 858997828}]]
|
||||
// 5: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x9201) TAG-TYPE=[SRATIONAL] UNIT-COUNT=(1)> [[{286335522 858997828}]]
|
||||
}
|
||||
|
||||
func TestNewIfdBuilderFromExistingChain_RealData(t *testing.T) {
|
||||
filepath := path.Join(assetsPath, "NDM_8901.jpg")
|
||||
|
||||
e := NewExif()
|
||||
|
||||
|
||||
rawExif, err := e.SearchAndExtractExif(filepath)
|
||||
log.PanicIf(err)
|
||||
|
||||
// Decode from binary.
|
||||
|
||||
_, index, err := e.Collect(rawExif)
|
||||
log.PanicIf(err)
|
||||
|
||||
originalTags := index.RootIfd.DumpTags()
|
||||
|
||||
// Encode back to binary.
|
||||
|
||||
ibe := NewIfdByteEncoder()
|
||||
|
||||
rootIb := NewIfdBuilderFromExistingChain(index.RootIfd, rawExif)
|
||||
|
||||
updatedExif, err := ibe.EncodeToExif(rootIb)
|
||||
log.PanicIf(err)
|
||||
|
||||
// Parse again.
|
||||
|
||||
_, index, err = e.Collect(updatedExif)
|
||||
log.PanicIf(err)
|
||||
|
||||
recoveredTags := index.RootIfd.DumpTags()
|
||||
|
||||
|
||||
// Validate that all of the same IFDs were presented.
|
||||
|
||||
originalIfdTags := make([][2]interface{}, 0)
|
||||
for _, ite := range originalTags {
|
||||
if ite.ChildIfdName != "" {
|
||||
originalIfdTags = append(originalIfdTags, [2]interface{} { ite.Ii, ite.TagId })
|
||||
}
|
||||
}
|
||||
|
||||
recoveredIfdTags := make([][2]interface{}, 0)
|
||||
for _, ite := range recoveredTags {
|
||||
if ite.ChildIfdName != "" {
|
||||
recoveredIfdTags = append(recoveredIfdTags, [2]interface{} { ite.Ii, ite.TagId })
|
||||
}
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(recoveredIfdTags, originalIfdTags) != true {
|
||||
fmt.Printf("Original IFD tags:\n\n")
|
||||
|
||||
for i, x := range originalIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\nRecovered IFD tags:\n\n")
|
||||
|
||||
for i, x := range recoveredIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
|
||||
t.Fatalf("Recovered IFD tags are not correct.")
|
||||
}
|
||||
|
||||
|
||||
// Validate that all of the tags owned by the IFDs were presented. The tags
|
||||
// might not be in the same order since the IFD tags are allocated after
|
||||
// the non-IFD ones.
|
||||
|
||||
originalNonIfdTags := make([]string, 0)
|
||||
for _, ite := range originalTags {
|
||||
if ite.ChildIfdName == "" {
|
||||
originalNonIfdTags = append(originalNonIfdTags, fmt.Sprintf("%s 0x%02x", ite.Ii, ite.TagId))
|
||||
}
|
||||
}
|
||||
|
||||
sort.StringSlice(originalNonIfdTags).Sort()
|
||||
|
||||
recoveredNonIfdTags := make([]string, 0)
|
||||
for _, ite := range recoveredTags {
|
||||
if ite.ChildIfdName == "" {
|
||||
recoveredNonIfdTags = append(recoveredNonIfdTags, fmt.Sprintf("%s 0x%02x", ite.Ii, ite.TagId))
|
||||
}
|
||||
}
|
||||
|
||||
sort.StringSlice(recoveredNonIfdTags).Sort()
|
||||
|
||||
|
||||
if reflect.DeepEqual(recoveredNonIfdTags, originalNonIfdTags) != true {
|
||||
fmt.Printf("Original non-IFD tags:\n\n")
|
||||
|
||||
for i, x := range originalNonIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\nRecovered non-IFD tags:\n\n")
|
||||
|
||||
for i, x := range recoveredNonIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
|
||||
t.Fatalf("Recovered non-IFD tags are not correct.")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,6 +5,8 @@ import (
|
|||
"reflect"
|
||||
"bytes"
|
||||
"path"
|
||||
"fmt"
|
||||
"sort"
|
||||
|
||||
"github.com/dsoprea/go-logging"
|
||||
)
|
||||
|
@ -1263,7 +1265,7 @@ func TestNewIfdBuilderFromExistingChain(t *testing.T) {
|
|||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(18) TAG=[0x9290]",
|
||||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(19) TAG=[0x9291]",
|
||||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(20) TAG=[0x9292]",
|
||||
- "<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(21) TAG=[0xa000]",
|
||||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(21) TAG=[0xa000]",
|
||||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(22) TAG=[0xa001]",
|
||||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(23) TAG=[0xa002]",
|
||||
"<PARENTS=[IFD] IFD-NAME=[Exif]> IFD-TAG-ID=(0x8769) CHILD-IFD=[] INDEX=(24) TAG=[0xa003]",
|
||||
|
@ -1293,6 +1295,115 @@ func TestNewIfdBuilderFromExistingChain(t *testing.T) {
|
|||
|
||||
// TODO(dustin): !! Test with an actual GPS-attached image.
|
||||
|
||||
func TestNewIfdBuilderFromExistingChain_RealData(t *testing.T) {
|
||||
filepath := path.Join(assetsPath, "NDM_8901.jpg")
|
||||
|
||||
e := NewExif()
|
||||
|
||||
|
||||
rawExif, err := e.SearchAndExtractExif(filepath)
|
||||
log.PanicIf(err)
|
||||
|
||||
// Decode from binary.
|
||||
|
||||
_, index, err := e.Collect(rawExif)
|
||||
log.PanicIf(err)
|
||||
|
||||
originalTags := index.RootIfd.DumpTags()
|
||||
|
||||
// Encode back to binary.
|
||||
|
||||
ibe := NewIfdByteEncoder()
|
||||
|
||||
rootIb := NewIfdBuilderFromExistingChain(index.RootIfd, rawExif)
|
||||
|
||||
updatedExif, err := ibe.EncodeToExif(rootIb)
|
||||
log.PanicIf(err)
|
||||
|
||||
// Parse again.
|
||||
|
||||
_, index, err = e.Collect(updatedExif)
|
||||
log.PanicIf(err)
|
||||
|
||||
recoveredTags := index.RootIfd.DumpTags()
|
||||
|
||||
|
||||
// Validate that all of the same IFDs were presented.
|
||||
|
||||
originalIfdTags := make([][2]interface{}, 0)
|
||||
for _, ite := range originalTags {
|
||||
if ite.ChildIfdName != "" {
|
||||
originalIfdTags = append(originalIfdTags, [2]interface{} { ite.Ii, ite.TagId })
|
||||
}
|
||||
}
|
||||
|
||||
recoveredIfdTags := make([][2]interface{}, 0)
|
||||
for _, ite := range recoveredTags {
|
||||
if ite.ChildIfdName != "" {
|
||||
recoveredIfdTags = append(recoveredIfdTags, [2]interface{} { ite.Ii, ite.TagId })
|
||||
}
|
||||
}
|
||||
|
||||
if reflect.DeepEqual(recoveredIfdTags, originalIfdTags) != true {
|
||||
fmt.Printf("Original IFD tags:\n\n")
|
||||
|
||||
for i, x := range originalIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\nRecovered IFD tags:\n\n")
|
||||
|
||||
for i, x := range recoveredIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
|
||||
t.Fatalf("Recovered IFD tags are not correct.")
|
||||
}
|
||||
|
||||
|
||||
// Validate that all of the tags owned by the IFDs were presented. The tags
|
||||
// might not be in the same order since the IFD tags are allocated after
|
||||
// the non-IFD ones.
|
||||
|
||||
originalNonIfdTags := make([]string, 0)
|
||||
for _, ite := range originalTags {
|
||||
if ite.ChildIfdName == "" {
|
||||
originalNonIfdTags = append(originalNonIfdTags, fmt.Sprintf("%s 0x%02x", ite.Ii, ite.TagId))
|
||||
}
|
||||
}
|
||||
|
||||
sort.StringSlice(originalNonIfdTags).Sort()
|
||||
|
||||
recoveredNonIfdTags := make([]string, 0)
|
||||
for _, ite := range recoveredTags {
|
||||
if ite.ChildIfdName == "" {
|
||||
recoveredNonIfdTags = append(recoveredNonIfdTags, fmt.Sprintf("%s 0x%02x", ite.Ii, ite.TagId))
|
||||
}
|
||||
}
|
||||
|
||||
sort.StringSlice(recoveredNonIfdTags).Sort()
|
||||
|
||||
|
||||
if reflect.DeepEqual(recoveredNonIfdTags, originalNonIfdTags) != true {
|
||||
fmt.Printf("Original non-IFD tags:\n\n")
|
||||
|
||||
for i, x := range originalNonIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\nRecovered non-IFD tags:\n\n")
|
||||
|
||||
for i, x := range recoveredNonIfdTags {
|
||||
fmt.Printf(" %02d %v\n", i, x)
|
||||
}
|
||||
|
||||
fmt.Printf("\n")
|
||||
|
||||
t.Fatalf("Recovered non-IFD tags are not correct.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIfdBuilderWithExistingIfd(t *testing.T) {
|
||||
tagId := IfdTagIdWithIdentityOrFail(GpsIi)
|
||||
|
@ -1387,3 +1498,4 @@ func TestAddFromConfigWithName(t *testing.T) {
|
|||
t.Fatalf("Value not correct: (%d) [%s]", len(s), s)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -426,7 +426,7 @@ func (ifd Ifd) dumpTags(tags []*IfdTagEntry) []*IfdTagEntry {
|
|||
return tags
|
||||
}
|
||||
|
||||
// PrintTagTree prints the IFD hierarchy.
|
||||
// DumpTags prints the IFD hierarchy.
|
||||
func (ifd Ifd) DumpTags() []*IfdTagEntry {
|
||||
return ifd.dumpTags(nil)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue