documentation: Added examples.

- Renamed DefaultEncodeByteOrder to EncodeDefaultByteOrder.
pull/3/head
Dustin Oprea 2018-05-01 10:57:55 -04:00
parent e8c30b7811
commit 88cb7331cf
5 changed files with 84 additions and 6 deletions

View File

@ -36,8 +36,8 @@ var (
ExifHeaderPrefixBytes = []byte("Exif\000\000")
// DefaultEncodeByteOrder is the default byte-order for encoding operations.
DefaultEncodeByteOrder = binary.BigEndian
// EncodeDefaultByteOrder is the default byte-order for encoding operations.
EncodeDefaultByteOrder = binary.BigEndian
)
var (

View File

@ -333,6 +333,21 @@ func TestBuildAndParseExifHeader(t *testing.T) {
} else if eh.FirstIfdOffset != 0x11223344 {
t.Fatalf("First IFD offset not correct.")
}
fmt.Printf("%v\n", eh)
}
func ExampleBuildExifHeader() {
headerBytes, err := BuildExifHeader(EncodeDefaultByteOrder, 0x11223344)
log.PanicIf(err)
e := NewExif()
eh, err := e.ParseExifHeader(headerBytes)
log.PanicIf(err)
fmt.Printf("%v\n", eh)
// Output: ExifHeader<BYTE-ORDER=[BigEndian] FIRST-IFD-OFFSET=(0x11223344)>
}
func init() {

View File

@ -407,7 +407,7 @@ func (ibe *IfdByteEncoder) EncodeToExif(ib *IfdBuilder) (data []byte, err error)
b := new(bytes.Buffer)
headerBytes, err := BuildExifHeader(DefaultEncodeByteOrder, ExifDefaultFirstIfdOffset)
headerBytes, err := BuildExifHeader(EncodeDefaultByteOrder, ExifDefaultFirstIfdOffset)
log.PanicIf(err)
_, err = b.Write(headerBytes)

View File

@ -4,6 +4,7 @@ import (
"testing"
"bytes"
"reflect"
"fmt"
"github.com/dsoprea/go-logging"
)
@ -742,6 +743,66 @@ func Test_IfdByteEncoder_EncodeToExif(t *testing.T) {
validateExifSimpleTestIb(exifData, t)
}
func ExampleIfdByteEncoder_EncodeToExif() {
// Construct an IFD.
ib := NewIfdBuilder(RootIi, EncodeDefaultByteOrder)
err := ib.AddFromConfig(0x000b, "asciivalue")
log.PanicIf(err)
err = ib.AddFromConfig(0x0150, []uint8 { 0x11 })
log.PanicIf(err)
err = ib.AddFromConfig(0x00ff, []uint16 { 0x2233 })
log.PanicIf(err)
err = ib.AddFromConfig(0x0100, []uint32 { 0x44556677 })
log.PanicIf(err)
err = ib.AddFromConfig(0x013e, []Rational { { Numerator: 0x11112222, Denominator: 0x33334444 } })
log.PanicIf(err)
err = ib.AddFromConfig(0x9201, []SignedRational { { Numerator: 0x11112222, Denominator: 0x33334444 } })
log.PanicIf(err)
// Encode it.
ibe := NewIfdByteEncoder()
exifData, err := ibe.EncodeToExif(ib)
log.PanicIf(err)
// Parse it so we can see it.
e := NewExif()
_, index, err := e.Collect(exifData)
log.PanicIf(err)
// addressableData is the byte-slice where the allocated data can be
// resolved (where position 0x0 will correlate with offset 0x0).
addressableData := exifData[ExifAddressableAreaStart:]
for i, e := range index.RootIfd.Entries {
value, err := e.Value(EncodeDefaultByteOrder, addressableData)
log.PanicIf(err)
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}]
}
// TODO(dustin): !! Write test with both chained and child IFDs
// TODO(dustin): !! Test all types.

View File

@ -16,9 +16,11 @@ var (
// IfdTagEnumerator knows how to decode an IFD and all of the tags it
// describes. Note that the IFDs and the actual values floating throughout the
// whole EXIF block, but the IFD itself has just a minor header and a set of
// repeating, statically-sized records. So, the tags (though not their values)
// describes.
//
// The IFDs and the actual values can float throughout the EXIF block, but the
// IFD itself is just a minor header followed by a set of repeating,
// statically-sized records. So, the tags (though notnecessarily their values)
// are fairly simple to enumerate.
type IfdTagEnumerator struct {
byteOrder binary.ByteOrder