mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
[bugfix] Don't remove jpeg orientation metadata (#663)
This commit is contained in:
8
vendor/github.com/superseriousbusiness/exif-terminator/README.md
generated
vendored
8
vendor/github.com/superseriousbusiness/exif-terminator/README.md
generated
vendored
@ -53,10 +53,14 @@ Exif removal is a pain in the arse. Most other libraries seem to parse the whole
|
||||
|
||||
`exif-terminator` differs in that it removes exif data *while scanning through the image bytes*, and it doesn't do any reencoding of the image. Bytes of exif data are simply all set to 0, and the image data is piped back out again into the returned reader.
|
||||
|
||||
The only exception is orientation data: if an image contains orientation data, this and only this data will be preserved since it's *actually useful*.
|
||||
|
||||
## Example
|
||||
|
||||
You can run the following example with `go run ./example/main.go`:
|
||||
|
||||
```go
|
||||
package test
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
@ -71,6 +75,7 @@ func main() {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer sloth.Close()
|
||||
|
||||
// get the length of the file
|
||||
stat, err := sloth.Stat()
|
||||
@ -103,6 +108,7 @@ func main() {
|
||||
|
||||
`exif-terminator` borrows heavily from the two [`dsoprea`](https://github.com/dsoprea) libraries credited below. In fact, it's basically a hack on top of those libraries. Thanks `dsoprea`!
|
||||
|
||||
- [dsoprea/go-exif](https://github.com/dsoprea/go-exif): exif header reconstruction. [MIT License](https://spdx.org/licenses/MIT.html).
|
||||
- [dsoprea/go-jpeg-image-structure](https://github.com/dsoprea/go-jpeg-image-structure): jpeg structure parsing. [MIT License](https://spdx.org/licenses/MIT.html).
|
||||
- [dsoprea/go-png-image-structure](https://github.com/dsoprea/go-png-image-structure): png structure parsing. [MIT License](https://spdx.org/licenses/MIT.html).
|
||||
- [stretchr/testify](https://github.com/stretchr/testify); test framework. [MIT License](https://spdx.org/licenses/MIT.html).
|
||||
|
127
vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go
generated
vendored
127
vendor/github.com/superseriousbusiness/exif-terminator/jpeg.go
generated
vendored
@ -19,10 +19,12 @@
|
||||
package terminator
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
exif "github.com/dsoprea/go-exif/v3"
|
||||
jpegstructure "github.com/superseriousbusiness/go-jpeg-image-structure/v2"
|
||||
)
|
||||
|
||||
@ -121,18 +123,129 @@ func (v *jpegVisitor) writeSegment(s *jpegstructure.Segment) error {
|
||||
}
|
||||
}
|
||||
|
||||
if s.IsExif() {
|
||||
// if this segment is exif data, write blank bytes
|
||||
blank := make([]byte, len(s.Data))
|
||||
if _, err := w.Write(blank); err != nil {
|
||||
if !s.IsExif() {
|
||||
// if this isn't exif data just copy it over and bail
|
||||
_, err := w.Write(s.Data)
|
||||
return err
|
||||
}
|
||||
|
||||
ifd, _, err := s.Exif()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// amount of bytes we've written into the exif body
|
||||
var written int
|
||||
|
||||
if orientationEntries, err := ifd.FindTagWithName("Orientation"); err == nil && len(orientationEntries) == 1 {
|
||||
// If we have an orientation entry, we don't want to completely obliterate the exif data.
|
||||
// Instead, we want to surgically obliterate everything *except* the orientation tag, so
|
||||
// that the image will still be rotated correctly when shown in client applications etc.
|
||||
//
|
||||
// To accomplish this, we're going to extract just the bytes that we need and write them
|
||||
// in according to the exif specification, then fill in the rest of the space with empty
|
||||
// bytes.
|
||||
//
|
||||
// First we need to write the exif prefix for this segment.
|
||||
//
|
||||
// Then we write the exif header which contains the byte order and offset of the first ifd.
|
||||
//
|
||||
// Then we write the ifd0 entry which contains the orientation data.
|
||||
//
|
||||
// After that we just fill fill fill.
|
||||
|
||||
newData := &bytes.Buffer{}
|
||||
|
||||
// 1. Write exif prefix.
|
||||
// https://www.ozhiker.com/electronics/pjmt/jpeg_info/app_segments.html
|
||||
prefix := []byte{'E', 'x', 'i', 'f', 0, 0}
|
||||
if err := binary.Write(newData, ifd.ByteOrder(), &prefix); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
// otherwise write the data
|
||||
if _, err := w.Write(s.Data); err != nil {
|
||||
written += 6
|
||||
|
||||
// 2. Write exif header, taking the existing byte order.
|
||||
exifHeader, err := exif.BuildExifHeader(ifd.ByteOrder(), exif.ExifDefaultFirstIfdOffset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
hWritten, err := newData.Write(exifHeader)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
written += hWritten
|
||||
|
||||
// https://web.archive.org/web/20190624045241if_/http://www.cipa.jp:80/std/documents/e/DC-008-Translation-2019-E.pdf
|
||||
//
|
||||
// An ifd with one orientation entry is structured like this:
|
||||
// 2 bytes: the number of entries in the ifd uint16(1)
|
||||
// 2 bytes: the tag id uint16(274)
|
||||
// 2 bytes: the tag type uint16(3)
|
||||
// 4 bytes: the tag count uint32(1)
|
||||
// 4 bytes: the tag value offset: uint32(one of the below with padding on the end)
|
||||
// 1 = Horizontal (normal)
|
||||
// 2 = Mirror horizontal
|
||||
// 3 = Rotate 180
|
||||
// 4 = Mirror vertical
|
||||
// 5 = Mirror horizontal and rotate 270 CW
|
||||
// 6 = Rotate 90 CW
|
||||
// 7 = Mirror horizontal and rotate 90 CW
|
||||
// 8 = Rotate 270 CW
|
||||
orientationEntry := orientationEntries[0]
|
||||
|
||||
ifdCount := uint16(1) // we're only adding one entry into the ifd
|
||||
if err := binary.Write(newData, ifd.ByteOrder(), &ifdCount); err != nil {
|
||||
return err
|
||||
}
|
||||
written += 2
|
||||
|
||||
tagID := orientationEntry.TagId()
|
||||
if err := binary.Write(newData, ifd.ByteOrder(), &tagID); err != nil {
|
||||
return err
|
||||
}
|
||||
written += 2
|
||||
|
||||
tagType := orientationEntry.TagType()
|
||||
if err := binary.Write(newData, ifd.ByteOrder(), &tagType); err != nil {
|
||||
return err
|
||||
}
|
||||
written += 2
|
||||
|
||||
tagCount := orientationEntry.UnitCount()
|
||||
if err := binary.Write(newData, ifd.ByteOrder(), &tagCount); err != nil {
|
||||
return err
|
||||
}
|
||||
written += 4
|
||||
|
||||
valueOffset, err := orientationEntry.GetRawBytes()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
vWritten, err := newData.Write(valueOffset)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
written += vWritten
|
||||
|
||||
valuePad := make([]byte, 4-vWritten)
|
||||
pWritten, err := newData.Write(valuePad)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
written += pWritten
|
||||
|
||||
// write everything in
|
||||
if _, err := io.Copy(w, newData); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
// fill in the (remaining) exif body with blank bytes
|
||||
blank := make([]byte, len(s.Data)-written)
|
||||
if _, err := w.Write(blank); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
2
vendor/github.com/superseriousbusiness/exif-terminator/terminator.go
generated
vendored
2
vendor/github.com/superseriousbusiness/exif-terminator/terminator.go
generated
vendored
@ -25,8 +25,8 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
jpegstructure "github.com/superseriousbusiness/go-jpeg-image-structure/v2"
|
||||
pngstructure "github.com/dsoprea/go-png-image-structure/v2"
|
||||
jpegstructure "github.com/superseriousbusiness/go-jpeg-image-structure/v2"
|
||||
)
|
||||
|
||||
func Terminate(in io.Reader, fileSize int, mediaType string) (io.Reader, error) {
|
||||
|
Reference in New Issue
Block a user