Skip to content

Commit

Permalink
Add AllTags function to Xmp (#7)
Browse files Browse the repository at this point in the history
* Add AllTags function to Xmp
  • Loading branch information
rtio authored Mar 3, 2023
1 parent 7236edd commit e5d2eb9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
3 changes: 2 additions & 1 deletion exif.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (d *ExifDatum) String() string {
return C.GoString(cstr)
}

// Returns all EXIF tags
// AllTags returns all EXIF tags
func (d *ExifData) AllTags() map[string]string {
keyValues := map[string]string{}
for i := d.Iterator(); i.HasNext(); {
Expand All @@ -132,6 +132,7 @@ func (i *ExifDatumIterator) Next() *ExifDatum {
return makeExifDatum(i.data, C.exiv2_exif_datum_iterator_next(i.iter))
}

// makeExifDatumIterator creates a new ExifDatumIterator and sets a finalizer to free the C++ object.
func makeExifDatumIterator(data *ExifData, cIter *C.Exiv2ExifDatumIterator) *ExifDatumIterator {
datum := &ExifDatumIterator{data, cIter}

Expand Down
4 changes: 3 additions & 1 deletion exiv.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (i *Image) ReadMetadata() error {
return nil
}

// Returns an image contents.
// GetBytes returns an image contents.
// If its metadata has been changed, the changes are reflected here.
func (i *Image) GetBytes() []byte {
size := C.exiv_image_get_size(i.img)
Expand Down Expand Up @@ -233,6 +233,7 @@ func (i *Image) SetMetadataShort(f MetadataFormat, key, value string) error {
return nil
}

// StripKey removes a key from the metadata
func (i *Image) StripKey(f MetadataFormat, key string) error {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))
Expand All @@ -259,6 +260,7 @@ func (i *Image) StripKey(f MetadataFormat, key string) error {
return nil
}

// StripMetadata removes all metadata from the image except the keys in unless
func (i *Image) StripMetadata(unless []string) error {
var err error
err = i.ExifStripMetadata(unless)
Expand Down
24 changes: 19 additions & 5 deletions exiv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/rtio/goexiv"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io/ioutil"
"os"
"runtime"
"sync"
"testing"
Expand Down Expand Up @@ -42,7 +42,7 @@ func TestOpenImage(t *testing.T) {
}

func Test_OpenBytes(t *testing.T) {
bytes, err := ioutil.ReadFile("testdata/pixel.jpg")
bytes, err := os.ReadFile("testdata/pixel.jpg")
require.NoError(t, err)

img, err := goexiv.OpenBytes(bytes)
Expand Down Expand Up @@ -173,6 +173,16 @@ func TestMetadata(t *testing.T) {
"Iptc.Application2.DateCreated": "2012-10-13",
"Iptc.Application2.TimeCreated": "12:49:32+01:00",
}, iptcData.AllTags())

//
// XMP
//
xmpData := img.GetXmpData()
assert.Equal(t, map[string]string{
"Xmp.iptc.CopyrightNotice": "this is the copy, right?",
"Xmp.iptc.CreditLine": "John Doe",
"Xmp.iptc.JobId": "12345",
}, xmpData.AllTags())
}

func TestNoMetadata(t *testing.T) {
Expand Down Expand Up @@ -398,7 +408,7 @@ func Test_SetMetadataShortInt(t *testing.T) {
}

func Test_GetBytes(t *testing.T) {
bytes, err := ioutil.ReadFile("testdata/stripped_pixel.jpg")
bytes, err := os.ReadFile("testdata/stripped_pixel.jpg")
require.NoError(t, err)

img, err := goexiv.OpenBytes(bytes)
Expand Down Expand Up @@ -432,7 +442,7 @@ func Test_GetBytes_Goroutine(t *testing.T) {
var wg sync.WaitGroup
iterations := 0

bytes, err := ioutil.ReadFile("testdata/stripped_pixel.jpg")
bytes, err := os.ReadFile("testdata/stripped_pixel.jpg")
require.NoError(t, err)

for i := 0; i < 100; i++ {
Expand Down Expand Up @@ -681,7 +691,7 @@ func BenchmarkImage_GetBytes_KeepAlive(b *testing.B) {
}

func BenchmarkImage_GetBytes_NoKeepAlive(b *testing.B) {
bytes, err := ioutil.ReadFile("testdata/stripped_pixel.jpg")
bytes, err := os.ReadFile("testdata/stripped_pixel.jpg")
require.NoError(b, err)
var wg sync.WaitGroup

Expand Down Expand Up @@ -728,4 +738,8 @@ func initializeImage(path string, t *testing.T) {
err = img.SetExifString(k, v)
require.NoError(t, err, k, v)
}

img.SetXmpString("Xmp.iptc.CreditLine", "John Doe")
img.SetXmpString("Xmp.iptc.CopyrightNotice", "this is the copy, right?")
img.SetXmpString("Xmp.iptc.JobId", "12345")
}
3 changes: 2 additions & 1 deletion iptc.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (d *IptcDatum) String() string {
return C.GoString(cstr)
}

// Returns all IPTC tags
// AllTags returns all IPTC tags
func (d *IptcData) AllTags() map[string]string {
keyValues := map[string]string{}
for i := d.Iterator(); i.HasNext(); {
Expand All @@ -136,6 +136,7 @@ func (i *IptcDatumIterator) Next() *IptcDatum {
return makeIptcDatum(i.data, C.exiv2_iptc_datum_iterator_next(i.iter))
}

// makeIptcDatumIterator creates a new IptcDatumIterator.
func makeIptcDatumIterator(data *IptcData, cIter *C.Exiv2IptcDatumIterator) *IptcDatumIterator {
datum := &IptcDatumIterator{data, cIter}

Expand Down

0 comments on commit e5d2eb9

Please sign in to comment.