exif

package
v0.0.0-...-f9ed69e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 12 Imported by: 0

README

Exif

go get github.com/tajtiattila/metadata/exif

Package exif provides support for reading and writing JPEG/Exif files.

TODO

Add docs, tests, support functions, examples.

  • add missing Tag accessors and setters
  • merge internal Entry constructors with Tag setters

Documentation

Overview

Package exif implements an JPEG/Exif decoder and encoder.

Index

Constants

View Source
const (
	TypeByte      = 1  // (unsigned) byte
	TypeAscii     = 2  // ascii, zero-terminated
	TypeShort     = 3  // unsigned 16-bit value
	TypeLong      = 4  // unsigned 32-bit value
	TypeRational  = 5  // two 32-bit values: numerator then denominator
	TypeUndef     = 7  // raw bytes
	TypeSLong     = 9  // signed long
	TypeSRational = 10 // signed rational

	// types from TIFF spec
	TypeSByte  = 6  // signed byte
	TypeSShort = 8  // signed 16-bit value
	TypeFloat  = 11 // 4-byte IEEE floating point value
	TypeDouble = 12 // 8-byte IEEE floating point value
)
View Source
const TimeFormat = "2006:01:02 15:04:05"

Variables

View Source
var (
	// ErrCorruptHeader is returned if the Exif header is corrupt.
	ErrCorruptHeader = errors.New("exif: corrupt header")

	// ErrEmpty is returned when x.Encode is used with no exif data to encode.
	ErrEmpty = errors.New("exif: nothing to encode")

	// ErrTooLong is returned if the serialized exif is too long to be written in an Exif file.
	ErrTooLong = errors.New("exif: encoded length too long")
)
View Source
var (
	NotFound  = errors.New("exif: exif data not found")
	ErrDecode = errors.New("exif: jpeg decode error")
	ErrEncode = errors.New("exif: jpeg encode error")
)
View Source
var (
	ErrMissingDir = errors.New("exif: missing IFD dir")
	ErrMissingTag = errors.New("exif: tag missing from dir")
)
View Source
var (
	ErrNoThumbnail     = errors.New("exif: no thumbnail")
	ErrThumbnailTooBig = errors.New("exif: thumbnail too big")
)

Functions

func Copy

func Copy(w io.Writer, r io.Reader, x *Exif) error

Copy copies the data from r to w, replacing the Exif metadata in r with x. If x is nil, no Exif metadata is written to w. The original Exif metadata in r is always discarded. Other content such as raw image data is written to w unmodified.

func IsFormat

func IsFormat(err error) bool

IsFormat returns a boolean indicating whether the error is a format error in Exif.

Types

type Ascii

type Ascii string

Ascii is a string Value, marshaled as TypeAscii.

type Byte

type Byte []byte

Byte is a Value of 8-bit unsigned integers marshaled as TypeByte.

type Entry

type Entry struct {
	// Tag value (identifier).
	Tag uint16

	// Entry data type.
	Type uint16

	// Count is the number of values.
	Count uint32

	// Value is the raw data encoded using the Exif.ByteOrder.
	Value []byte
}

Entry is a tagged field within an Exif directory.

func (*Entry) SetValue

func (e *Entry) SetValue(bo binary.ByteOrder, v Value)

SetValue sets the value of e to v.

SetValue panics if v is invalid.

type Exif

type Exif struct {
	// ByteOrder is the byte order used for decoding and encoding
	binary.ByteOrder

	// Main image TIFF metadata
	IFD0 []Entry

	// Main image sub-IFDs
	Exif, GPS, Interop []Entry

	// thumbnail
	IFD1  []Entry // Metadata
	Thumb []byte  // Raw image data, typically JPEG
}

Exif represents Exif format metadata in JPEG/Exif files.

func Decode

func Decode(r io.Reader) (*Exif, error)

Decode decodes Exif data from r.

func DecodeBytes

func DecodeBytes(p []byte) (*Exif, error)

DecodeBytes decodes the raw Exif data from p.

func New

func New(dx, dy int) *Exif

New initializes a new Exif structure for an image with the provided dimensions.

func (*Exif) DateTime

func (x *Exif) DateTime() (t time.Time, ok bool)

DateTime reports the Exif datetime. The fields checked in order are Exif/DateTimeOriginal, Exif/DateTimeDigitized and Tiff/DateTime. If neither is available, ok == false is returned.

func (*Exif) EncodeBytes

func (x *Exif) EncodeBytes() ([]byte, error)

EncodeBytes encodes Exif data as a byte slice. It returns an error only if IFD0 is empty, the byte order is not set or the encoded length is too long for Exif.

To store the Exif within an image, use Copy instead.

func (*Exif) GPSInfo

func (x *Exif) GPSInfo() (i GPSInfo, ok bool)

GPSInfo returns GPS data from x. It returns ok == true if least latitude and longitude values are present.

func (*Exif) LatLong

func (x *Exif) LatLong() (lat, long float64, ok bool)

LatLong reports the GPS latitude and longitude.

func (*Exif) Set

func (x *Exif) Set(t uint32, v Value)

Set sets the value of Exif tag t in x to v.

If v is nil, t is removed from x. Otherwise the Entry for t is created if it is not present in x.

Set panics is v is invalid (but not nil), or the value of (name & exiftag.DirMask) is not one of exiftag.{Tiff, Exif, GPS, Interop}.

func (*Exif) SetDateTime

func (x *Exif) SetDateTime(t time.Time)

SetDateTime sets the fields Exif/DateTimeOriginal, Exif/DateTimeDigitized and Tiff/DateTime to t.

func (*Exif) SetGPSInfo

func (x *Exif) SetGPSInfo(i GPSInfo)

SetGPSInfo sets the GPS data in x. If i.Version is nil, then Byte{2, 2, 0, 0} is used. If i.Alt.Valid is false or i.Time.IsZero() is true then the corresponding tags will be removed from x.

func (*Exif) SetLatLong

func (x *Exif) SetLatLong(lat, long float64)

SetLatLong sets GPS latitude and longitude in x.

func (*Exif) SetThumbImage

func (x *Exif) SetThumbImage(im image.Image) error

SetThumbImage sets the thumbnail of x to im. It reports ErrThumbnailTooBig if the thumbnail is too large for use in Exif, and any errors encountered during encoding.

func (*Exif) Tag

func (x *Exif) Tag(t uint32) *Tag

Tag returns the Tag t.

An invalid tag is returned if t is not present in x.

The value of (name & exiftag.DirMask) must be one of exiftag.{Tiff, Exif, GPS, Interop} otherwise Tag panics.

func (*Exif) ThumbImage

func (x *Exif) ThumbImage() (image.Image, string, error)

ThumbImage returns the thumbnail image and its format. It reports ErrNoThumbnail if the thumbnail does not exist, or any other error encountered during decoding.

func (*Exif) Time

func (x *Exif) Time(timeTag, subSecTag uint32) (t time.Time, islocal, ok bool)

Time reports the time from the specified DateTime and SubSecTime tags.

type FormatError

type FormatError []string

FormatError holds warnings encountered by Decode or DecodeBytes if (part of) the Exif succesfully decoded but corrupt or invalid data were encountered.

Clients only interested only in reading the Exif may safely ignore a FormatError.

func (FormatError) Error

func (e FormatError) Error() string

type Formatter

type Formatter struct {
	binary.ByteOrder
}

func (*Formatter) RawValue

func (f *Formatter) RawValue(typ uint16, cnt uint32, p []byte) string

func (*Formatter) Value

func (f *Formatter) Value(typ uint16, count uint32, p []byte) string

type GPSInfo

type GPSInfo struct {
	// Version of the GPS IFD.
	Version []byte

	// Lat and Long are the GPS position in degrees.
	Lat, Long float64

	// Alt is the altitude in meters above sea level.
	// Negative values mean altitude below sea level.
	Alt struct {
		Float64 float64
		Valid   bool
	}

	// Time specifies the time of the (last) GPS fix.
	Time time.Time
}

GPSInfo represents GPS information within Exif.

type Long

type Long []uint32

Long is a Value of 32-bit unsigned integers, marshaled as TypeLong.

type Rational

type Rational []uint32

Rational is a Value of 32-bit unsigned numerator-denominator pairs, marshaled as TypeRational.

Rational is valid only if it is not empty and has an even number of elements.

func Sexagesimal

func Sexagesimal(v uint64, res uint32) Rational

Sexagesimal creates a sexagesimal triplet Rational with three components (hours/degrees, minutes and seconds) from v and res, where x = v/res means x seconds.

If res is zero, Sexagesimal panics. Res is used as the denominator of seconds. If res > 1e6, the denominator of seconds will be 1e6.

func (Rational) Sexagesimal

func (r Rational) Sexagesimal(res uint32) (hi, lo uint64, ok bool)

Sexagesimal calculates the value of a r having hour (or degree), minute and second parts in 1/res second units, and can be used for GPSLatitude, GPSLongitude and GPSTimeStamp values.

The high value will be always zero if r represents hours/degrees less than or equal to 1e6, or res itself is less than or equal to 1e6.

type Short

type Short []uint16

Short is a Value of 16-bit unsigned integers, marshaled as TypeShort.

type Tag

type Tag struct {
	// ByteOrder to decode E.Value
	binary.ByteOrder

	// Entry for this Tag from Exif
	E Entry
}

Tag represents a tagged field within Exif.

func (*Tag) Ascii

func (t *Tag) Ascii() (s string, ok bool)

Ascii returns the value of t as string. If t is invalid or is not TypeAscii, ok == false is returned.

func (*Tag) Byte

func (t *Tag) Byte() []byte

Byte returns the value of t as a slice of bytes. If t is invalid or is not TypeByte, nil is returned.

func (*Tag) IsType

func (t *Tag) IsType(typ uint16) bool

IsType returns true if t is valid and has type typ.

func (*Tag) Long

func (t *Tag) Long() []uint32

Long returns the value of t as an slice of unsigned 32-bit values. If t is invalid or is not TypeLong, nil is returned.

func (*Tag) Rational

func (t *Tag) Rational() Rational

Rational returns the value of t as an slice of unsigned rational numerator/denominator values. If t is invalid or is not TypeRational, nil is returned.

func (*Tag) SLong

func (t *Tag) SLong() []int32

SLong returns the value of t as an slice of signed 32-bit values. If t is invalid or is not TypeSLong, nil is returned.

func (*Tag) SRational

func (t *Tag) SRational() (numdenom []int32)

SRational returns the value of t as an slice of signed rational numerator/denominator values. If t is invalid or is not TypeSRational, nil is returned.

func (*Tag) Short

func (t *Tag) Short() []uint16

Short returns the value of t as a slice of unsigned 16-bit values. If t is invalid or is not TypeShort, nil is returned.

func (*Tag) String

func (t *Tag) String() string

String returns the representation of t.

func (*Tag) Type

func (t *Tag) Type() uint16

Type returns the type of t, or zero if t is invalid.

func (*Tag) Undef

func (t *Tag) Undef() []byte

Undef returns the value of t as a byte slice. If t is invalid or is not TypeUndef, nil is returned.

func (*Tag) Valid

func (t *Tag) Valid() bool

Tag returns true if t exists and its Type and Count are valid.

type Undef

type Undef []byte

Undef is a Value of 8-bit unsigned integers marshaled as TypeUndef.

type Value

type Value interface {
	// contains filtered or unexported methods
}

Value can marshal itself as Entry content, for use with with Exif.Set or Entry.SetValue.

Unless otherwise noted, a Value is valid only if it has nonzero length.

Directories

Path Synopsis
Package exiftag defines constants used for TIFF and Exif files.
Package exiftag defines constants used for TIFF and Exif files.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL