Skip to content

image/jpeg: specify APP1 segment for outputting EXIF data in jpeg.Encode()? #12202

@jdeng

Description

@jdeng

Currently there is no way to output EXIF in jpeg.Encode() so if a JPEG file is modified use image/jpeg the EXIF info is lost.

Reading EXIF using jpeg.Decode() has a similar problem but reopening the file and reading a few kilo bytes is probably an acceptable workaround. Reopening a file to insert a segment is much more cumbersome and inefficient.

One way to do this is to introduce a new field in jpeg.Options. Something like below diff. Thoughts?

diff --git a/src/image/jpeg/reader.go b/src/image/jpeg/reader.go
index adf97ab..5942609 100644
--- a/src/image/jpeg/reader.go
+++ b/src/image/jpeg/reader.go
@@ -64,6 +64,7 @@ const (
        // but in practice, their use is described at
        // http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/JPEG.html
        app0Marker  = 0xe0
+       app1Marker  = 0xe1
        app14Marker = 0xee
        app15Marker = 0xef
 )
diff --git a/src/image/jpeg/writer.go b/src/image/jpeg/writer.go
index 91bbde3..e404efa 100644
--- a/src/image/jpeg/writer.go
+++ b/src/image/jpeg/writer.go
@@ -541,6 +541,7 @@ const DefaultQuality = 75
 // Quality ranges from 1 to 100 inclusive, higher is better.
 type Options struct {
        Quality int
+       App1Data []byte
 }

 // Encode writes the Image m to w in JPEG 4:2:0 baseline format with the given
@@ -597,6 +598,12 @@ func Encode(w io.Writer, m image.Image, o *Options) error {
        e.buf[0] = 0xff
        e.buf[1] = 0xd8
        e.write(e.buf[:2])
+
+       // Write APP1 data if specified
+       if o != nil && o.App1Data != nil {
+               e.writeMarkerHeader(app1Marker, 2 + len(o.App1Data))
+               e.write(o.App1Data)
+       }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions