-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathjpeg.go
166 lines (144 loc) · 3.67 KB
/
jpeg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"bytes"
"context"
"encoding/binary"
"errors"
"image/jpeg"
"log"
"os"
"github.com/ncruces/go-image/resize"
"github.com/ncruces/go-image/rotateflip"
"github.com/ncruces/rethinkraw/pkg/dcraw"
)
func previewJPEG(ctx context.Context, path string) ([]byte, error) {
log.Print("dcraw (get thumb)...")
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return dcraw.GetThumbJPEG(ctx, f)
}
func exportJPEG(ctx context.Context, path string) ([]byte, error) {
log.Print("dcraw (get thumb)...")
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
data, err := dcraw.GetThumb(ctx, f)
if err != nil {
return nil, err
}
if !bytes.HasPrefix(data, []byte("\xff\xd8")) {
return nil, errors.New("not a JPEG file")
}
return data, nil
}
func resampleJPEG(data []byte, settings exportSettings) ([]byte, error) {
img, err := jpeg.Decode(bytes.NewReader(data))
if err != nil {
return nil, err
}
exf := rotateflip.Orientation(exifOrientation(data))
img = rotateflip.Image(img, exf.Op())
fit := settings.FitImage(img.Bounds().Size())
img = resize.Thumbnail(uint(fit.X), uint(fit.Y), img, resize.Lanczos2)
buf := bytes.Buffer{}
// https://fotoforensics.com/tutorial.php?tt=estq
opt := jpeg.Options{Quality: [13]int{30, 34, 47, 62, 69, 76, 79, 82, 86, 90, 93, 97, 99}[settings.Quality]}
if err := jpeg.Encode(&buf, img, &opt); err != nil {
return nil, err
}
return append(jfifHeader(settings), buf.Bytes()[2:]...), nil
}
func exifOrientation(data []byte) int {
if !bytes.HasPrefix(data, []byte("\xff\xd8")) {
return -1
}
data = data[2:]
for len(data) >= 2 {
var marker = binary.BigEndian.Uint16(data)
switch {
case marker == 0xffff: // padding
data = data[1:]
case marker == 0xffe1: // APP1
if len(data) > 4 {
size := int(binary.BigEndian.Uint16(data[2:])) + 2
if 4 <= size && size <= len(data) {
data = data[4:size]
if !bytes.HasPrefix(data, []byte("Exif\x00\x00")) {
return 0
}
data = data[6:]
if len(data) < 8 {
return -2
}
var endian binary.ByteOrder
switch string(data[0:4]) {
case "II*\x00":
endian = binary.LittleEndian
case "MM\x00*":
endian = binary.BigEndian
default:
return -2
}
offset := endian.Uint32(data[4:])
if len(data) < int(offset)+2 {
return -2
}
count := endian.Uint16(data[offset:])
entries := data[offset+2:]
if len(data) < 12*int(count) {
return -2
} else {
entries = entries[:12*count]
}
for i := 0; i < len(entries); i += 12 {
tag := endian.Uint16(entries[i:])
if tag == 0x0112 { // Orientation
typ := endian.Uint16(entries[i+2:]) // SHORT
cnt := endian.Uint32(entries[i+4:]) // 1
val := endian.Uint32(entries[i+8:])
if typ == 3 && cnt == 1 && val <= 9 {
return int(val)
}
return -2
}
}
return 0
}
}
return -2
case marker >= 0xffe0: // APPn, JPGn, COM
if len(data) > 4 {
size := int(binary.BigEndian.Uint16(data[2:])) + 2
if 4 <= size && size <= len(data) {
data = data[size:]
continue
}
}
return -2
case marker == 0xff00: // invalid
return -2
default:
return 0
}
}
return -2
}
func jfifHeader(settings exportSettings) []byte {
if settings.DimUnit == "px" {
return []byte{'\xff', '\xd8'}
}
data := [20]byte{'\xff', '\xd8', '\xff', '\xe0', 0, 16, 'J', 'F', 'I', 'F', 0, 1, 2}
binary.BigEndian.PutUint16(data[14:], uint16(settings.Density))
binary.BigEndian.PutUint16(data[16:], uint16(settings.Density))
if settings.DenUnit == "ppi" {
data[13] = 1
} else {
data[13] = 2
}
return data[:]
}