ifd_builder_encode: Replaced `EntrySize()` with a constant.

pull/3/head
Dustin Oprea 2018-05-05 00:15:59 -04:00
parent 306b314f7a
commit 4e0ba639f9
2 changed files with 19 additions and 17 deletions

View File

@ -2,12 +2,17 @@ package exif
import (
"bytes"
// "fmt"
"encoding/binary"
"github.com/dsoprea/go-logging"
)
const (
// Tag-ID + Tag-Type + Unit-Count + Value/Offset.
IfdTagEntrySize = uint32(2 + 2 + 4 + 4)
)
type ByteWriter struct {
b *bytes.Buffer
@ -122,14 +127,9 @@ func NewIfdByteEncoder() (ibe *IfdByteEncoder) {
return new(IfdByteEncoder)
}
func (ibe *IfdByteEncoder) EntrySize() uint32 {
// Tag-ID + Tag-Type + Unit-Count + Value/Offset.
return uint32(2 + 2 + 4 + 4)
}
func (ibe *IfdByteEncoder) TableSize(entryCount int) uint32 {
// Tag-Count + (Entry-Size * Entry-Count) + Next-IFD-Offset.
return uint32(2) + (ibe.EntrySize() * uint32(entryCount)) + uint32(4)
return uint32(2) + (IfdTagEntrySize * uint32(entryCount)) + uint32(4)
}
// encodeTagToBytes encodes the given tag to a byte stream. If
@ -289,8 +289,10 @@ func (ibe *IfdByteEncoder) encodeIfdToBytes(ib *IfdBuilder, ifdAddressableOffset
// next cycle.
if setNextIb == true {
nextIfdOffsetToWrite += tableSize + dataSize + childIfdsTotalSize
// fmt.Printf("SETTING NEXT-IFD TO: (0x%02x)\n", nextIfdOffsetToWrite)
} else {
nextIfdOffsetToWrite = 0
// fmt.Printf("NOT SETTING NEXT-IFD.\n")
}
// Write address of next IFD in chain.
@ -362,7 +364,7 @@ func (ibe *IfdByteEncoder) encodeAndAttachIfd(ib *IfdBuilder, ifdAddressableOffs
log.PanicIf(err)
// This will include the child-IFDs, as well. This will actually advance the offset for our next loop.
ifdAddressableOffset = ifdAddressableOffset + uint32(tableSize + allocatedDataSize)
ifdAddressableOffset += uint32(tableSize + allocatedDataSize)
}
return b.Bytes(), nil

View File

@ -172,9 +172,9 @@ func Test_IfdDataAllocator_Allocate_InitialOffset2(t *testing.T) {
func Test_IfdByteEncoder__Arithmetic(t *testing.T) {
ibe := NewIfdByteEncoder()
if (ibe.TableSize(1) - ibe.TableSize(0)) != ibe.EntrySize() {
if (ibe.TableSize(1) - ibe.TableSize(0)) != IfdTagEntrySize {
t.Fatalf("table-size/entry-size not consistent (1)")
} else if (ibe.TableSize(11) - ibe.TableSize(10)) != ibe.EntrySize() {
} else if (ibe.TableSize(11) - ibe.TableSize(10)) != IfdTagEntrySize {
t.Fatalf("table-size/entry-size not consistent (2)")
}
}
@ -784,7 +784,7 @@ func Test_IfdByteEncoder_EncodeToExif_WithChildAndSibling(t *testing.T) {
// in some JPEGs (for thumbnail or FlashPix images).
// TODO(dustin): !! Debugging.
// TODO(dustin): !! Debug this.
// nextIb := NewIfdBuilder(RootIi, TestDefaultByteOrder)
// err = nextIb.AddFromConfig(0x0101, []uint32 { 0x11223344 })
@ -878,17 +878,17 @@ func ExampleIfdByteEncoder_EncodeToExif() {
value, err := e.Value(addressableData, EncodeDefaultByteOrder)
log.PanicIf(err)
fmt.Printf("%d: %s %v\n", i, e, value)
fmt.Printf("%d: %s [%v]\n", i, e, value)
}
// Output:
//
// 0: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x0b) TAG-TYPE=[ASCII] UNIT-COUNT=(11)> asciivalue
// 1: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x150) TAG-TYPE=[BYTE] UNIT-COUNT=(1)> [17]
// 2: IfdTagEntry<TAG-IFD=[] TAG-ID=(0xff) TAG-TYPE=[SHORT] UNIT-COUNT=(1)> [8755]
// 3: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x100) TAG-TYPE=[LONG] UNIT-COUNT=(1)> [1146447479]
// 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}]
// 0: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x0b) TAG-TYPE=[ASCII] UNIT-COUNT=(11)> [asciivalue]
// 1: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x150) TAG-TYPE=[BYTE] UNIT-COUNT=(1)> [[17]]
// 2: IfdTagEntry<TAG-IFD=[] TAG-ID=(0xff) TAG-TYPE=[SHORT] UNIT-COUNT=(1)> [[8755]]
// 3: IfdTagEntry<TAG-IFD=[] TAG-ID=(0x100) TAG-TYPE=[LONG] UNIT-COUNT=(1)> [[1146447479]]
// 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 ExampleIfdByteEncoder_EncodeToExif_WithChild() {