Skip to content

Commit

Permalink
Remove webp dependency and some other performance tweaks, remove unused
Browse files Browse the repository at this point in the history
```
name                              old time/op    new time/op    delta
Decode/bep/imagemeta/jpg/exif-10    24.5µs ± 1%    17.8µs ± 4%  -27.46%  (p=0.029 n=4+4)

name                              old alloc/op   new alloc/op   delta
Decode/bep/imagemeta/jpg/exif-10    7.77kB ± 0%    6.01kB ± 0%  -22.62%  (p=0.029 n=4+4)

name                              old allocs/op  new allocs/op  delta
Decode/bep/imagemeta/jpg/exif-10       837 ± 0%       242 ± 0%  -71.09%  (p=0.029 n=4+4)
```
  • Loading branch information
bep committed Aug 14, 2023
1 parent c15b911 commit e76dc15
Show file tree
Hide file tree
Showing 11 changed files with 242 additions and 331 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/frankban/quicktest v1.14.5
github.com/google/go-cmp v0.5.9
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
golang.org/x/image v0.11.0
)

require (
Expand Down
33 changes: 0 additions & 33 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,3 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/image v0.11.0 h1:ds2RoQvBvYTiJkwpSFDwCcDFNX7DqjL2WsUgTNk0Ooo=
golang.org/x/image v0.11.0/go.mod h1:bglhjqbqVuEb9e9+eNR45Jfu7D+T4Qan+NhQk8Ck2P8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.12.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
76 changes: 2 additions & 74 deletions imagedecoder_jpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ package imagemeta

import (
"encoding/binary"
"fmt"
"io"
"strings"
)

type imageDecoderJPEG struct {
Expand Down Expand Up @@ -48,7 +45,7 @@ func (e *imageDecoderJPEG) decode() (err error) {
}
}

if e.opts.SourceSet[TagSourceEXIF] {
if e.opts.Sources.Has(TagSourceEXIF) {
pos := e.pos()
if length := findMarker(markerAPP1); length > 0 {
err := func() error {
Expand Down Expand Up @@ -79,7 +76,7 @@ func (e *imageDecoderJPEG) decode() (err error) {
e.seek(pos)
}

if e.opts.SourceSet[TagSourceIPTC] {
if e.opts.Sources.Has(TagSourceIPTC) {
// EXIF may be stored in a different order, but IPTC is always big-endian.
e.byteOrder = binary.BigEndian
if length := findMarker(markerApp13); length > 0 {
Expand All @@ -98,72 +95,3 @@ func (e *imageDecoderJPEG) decode() (err error) {
}
return nil
}

// exifTy

// exifType represents the basic tiff tag data types.
type exifType uint16

// A tag is represented in 12 bytes:
// - 2 bytes for the tag ID
// - 2 bytes for the data type
// - 4 bytes for the number of data values of the specified type
// - 4 bytes for the value itself, if it fits, otherwise for a pointer to another location where the data may be found;
// this could be a pointer to the beginning of another IFD
func (e *metaDecoderEXIF) decodeTag() error {
tagID := e.read2()
tagName := fieldsAll[tagID]
if tagName == "" {
tagName = fmt.Sprintf("%s0x%x", UnknownPrefix, tagID)
}

dataType := e.read2()
count := e.read4()
if count > 0x10000 {
e.skip(4)
return nil
}
if count == 0 {
count = 1 // TODO1 make this 0.
}
typ := exifType(dataType)

size, ok := typeSize[typ]
if !ok {
return fmt.Errorf("unknown type for tag %s %d", tagName, typ)
}
valLen := size * count

var r io.Reader = e.r

if valLen > 4 {
offset := e.read4() + uint32(e.readerOffset)
r = io.NewSectionReader(e.r, int64(offset), int64(valLen))
}

val := e.convertValues(typ, int(count), int(valLen), r)

if valLen <= 4 {
padding := 4 - valLen
if padding > 0 {
e.skip(int64(padding))
}
}

if strings.HasSuffix(tagName, "IFDPointer") {
offset := val.(uint32)
return e.decodeTagsAT(int(offset))
}

tagInfo := TagInfo{
Source: TagSourceEXIF,
Tag: tagName,
Value: val,
}

if err := e.handleTag(tagInfo); err != nil {
return err
}

return nil
}
69 changes: 34 additions & 35 deletions imagedecoder_webp.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"encoding/xml"
"fmt"
"io"

"golang.org/x/image/riff"
)

var (
fccVP8X = riff.FourCC{'V', 'P', '8', 'X'}
fccWEBP = riff.FourCC{'W', 'E', 'B', 'P'}
fccEXIF = riff.FourCC{'E', 'X', 'I', 'F'}
fccXMP = riff.FourCC{'X', 'M', 'P', ' '}
fccRIFF = fourCC{'R', 'I', 'F', 'F'}
fccWEBP = fourCC{'W', 'E', 'B', 'P'}
fccVP8X = fourCC{'V', 'P', '8', 'X'}
fccEXIF = fourCC{'E', 'X', 'I', 'F'}
fccXMP = fourCC{'X', 'M', 'P', ' '}
)

var errInvalidFormat = fmt.Errorf("imagemeta: invalid format")
Expand All @@ -28,23 +27,13 @@ type decoderWebP struct {
}

func (e *decoderWebP) decode() (err error) {
handleEXIF := e.opts.SourceSet[TagSourceEXIF]
handleXMP := e.opts.SourceSet[TagSourceXMP]
handleEXIF := e.opts.Sources.Has(TagSourceEXIF)
handleXMP := e.opts.Sources.Has(TagSourceXMP)

if !handleEXIF && !handleXMP {
return nil
}

r := e.streamReader.r

formType, riffReader, err := riff.NewReader(r)
if err != nil {
return err
}
if formType != fccWEBP {
return fmt.Errorf("imagemeta: not a WebP file")
}

var (
buf [10]byte

Expand All @@ -55,45 +44,56 @@ func (e *decoderWebP) decode() (err error) {
xmpHandled = !handleXMP
)

var chunkID fourCC
// Read the RIFF header.
e.readBytes(chunkID[:])
if chunkID != fccRIFF {
return errInvalidFormat
}

// File size.
e.skip(4)

e.readBytes(chunkID[:])
if chunkID != fccWEBP {
return errInvalidFormat
}

for {
if exifHandled && xmpHandled {
return nil
}

chunkID, chunkLen, chunkData, err := riffReader.Next()
if err == io.EOF {
err = errInvalidFormat
}
if err != nil {
return err
}
e.readBytes(chunkID[:])

chunkLen := e.read4()

switch chunkID {

case fccVP8X:
if chunkLen != 10 {
return errInvalidFormat
}

const (
xmpMetadataBit = 1 << 2
exifMetadataBit = 1 << 3
)

if _, err := io.ReadFull(chunkData, buf[:10]); err != nil {
return err
}
e.readBytes(buf[:])

hasExif = buf[0]&exifMetadataBit != 0
hasXMP = buf[0]&xmpMetadataBit != 0

if !hasExif && !hasXMP {
return nil
}

case fccEXIF:
if exifHandled {
continue
}
dec := newMetaDecoderEXIF(chunkData, e.opts.HandleTag)
r := io.LimitReader(e.r, int64(chunkLen))
dec := newMetaDecoderEXIF(r, e.opts.HandleTag)

if err := dec.decode(); err != nil {
return err
Expand All @@ -106,9 +106,9 @@ func (e *decoderWebP) decode() (err error) {
}

xmpHandled = true

r := io.LimitReader(e.r, int64(chunkLen))
var meta xmpmeta
if err := xml.NewDecoder(chunkData).Decode(&meta); err != nil {
if err := xml.NewDecoder(r).Decode(&meta); err != nil {
return err
}

Expand All @@ -124,9 +124,8 @@ func (e *decoderWebP) decode() (err error) {
return err
}
}

default:
e.skip(int64(chunkLen))
}

}

}
20 changes: 10 additions & 10 deletions imageformat_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e76dc15

Please sign in to comment.