forked from evanoberholster/imagemeta
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for extracting EXIF data from PNG files.
Closes evanoberholster#41.
- Loading branch information
Showing
3 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package imagemeta | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/evanoberholster/imagemeta/exif2" | ||
"github.com/evanoberholster/imagemeta/png" | ||
) | ||
|
||
// DecodePng decodes a PNG file from an io.Reader returning Exif or an error. | ||
func DecodePng(r io.ReadSeeker) (exif2.Exif, error) { | ||
header, err := png.ScanPngHeader(r) | ||
if err != nil { | ||
return exif2.Exif{}, err | ||
} | ||
|
||
ir := exif2.NewIfdReader(r) | ||
defer ir.Close() | ||
|
||
if err := ir.DecodeTiff(nil, header); err != nil { | ||
return ir.Exif, err | ||
} | ||
|
||
return ir.Exif, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Package png reads PNG Header metadata information from image files before being processed by exif package | ||
package png | ||
|
||
import ( | ||
"encoding/binary" | ||
"io" | ||
|
||
"github.com/evanoberholster/imagemeta/imagetype" | ||
"github.com/evanoberholster/imagemeta/meta" | ||
) | ||
|
||
func ScanPngHeader(r io.ReadSeeker) (header meta.ExifHeader, err error) { | ||
// 5.2 PNG signature | ||
const signature = "\x89PNG\r\n\x1a\n" | ||
|
||
// 5.3 Chunk layout | ||
const crcSize = 4 | ||
|
||
// 8 is the size of both the signature and the chunk | ||
// id (4 bytes) + chunk length (4 bytes). | ||
// This is just a coincidence. | ||
buf := make([]byte, 8) | ||
|
||
var n int | ||
n, err = r.Read(buf) | ||
if err != nil { | ||
return | ||
} | ||
|
||
if n != len(signature) || string(buf) != signature { | ||
err = meta.ErrNoExif | ||
|
||
return | ||
} | ||
|
||
for { | ||
// 5.3 Chunk layout | ||
n, err = r.Read(buf) | ||
if err != nil { | ||
break | ||
} | ||
|
||
if n != len(buf) { | ||
break | ||
} | ||
|
||
length := binary.BigEndian.Uint32(buf[0:4]) | ||
chunkType := string(buf[4:8]) | ||
|
||
switch chunkType { | ||
case "eXIf": | ||
offset, _ := r.Seek(0, io.SeekCurrent) | ||
|
||
return meta.NewExifHeader(meta.BigEndian, 8, uint32(offset), length, imagetype.ImagePNG), nil | ||
|
||
default: | ||
// Discard the chunk length + CRC. | ||
_, err := r.Seek(int64(length+crcSize), io.SeekCurrent) | ||
if err != nil { | ||
return header, err | ||
} | ||
} | ||
} | ||
|
||
return header, meta.ErrNoExif | ||
} |
afd7cfe
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't been able to test this but it looks appropriate. Thanks for your work on this.