-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
edit.go
355 lines (305 loc) · 6.98 KB
/
edit.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"bytes"
"context"
"errors"
"image"
"io/fs"
"math"
"os"
"path/filepath"
"strings"
"github.com/ncruces/rethinkraw/pkg/osutil"
"github.com/ncruces/rethinkraw/pkg/xmp"
)
func loadEdit(path string) (xmp xmpSettings, err error) {
wk, err := openWorkspace(path)
if err != nil {
return xmp, err
}
defer wk.close()
return loadXMP(wk.origXMP())
}
func saveEdit(ctx context.Context, path string, xmp xmpSettings) error {
wk, err := openWorkspace(path)
if err != nil {
return err
}
defer wk.close()
if xmp.WhiteBalance == "Camera Matching…" {
xmp.WhiteBalance = cameraMatchingWhiteBalance(wk.orig())
}
err = editXMP(wk.origXMP(), xmp)
if err != nil {
return err
}
dest, err := destSidecar(path)
if err != nil {
return err
}
if path == dest {
exp := exportSettings{
DNG: true,
Embed: true,
Preview: dngPreview(ctx, wk.orig()),
}
err = runDNGConverter(ctx, wk.orig(), wk.temp(), 0, &exp)
if err != nil {
return err
}
err = osutil.Move(wk.temp(), dest+".bak")
} else {
err = osutil.Copy(wk.origXMP(), dest+".bak")
}
if err != nil {
return err
}
return os.Rename(dest+".bak", dest)
}
func previewEdit(ctx context.Context, path string, size int, xmp xmpSettings) ([]byte, error) {
wk, err := openWorkspace(path)
if err != nil {
return nil, err
}
defer wk.close()
if xmp.WhiteBalance == "Camera Matching…" {
xmp.WhiteBalance = cameraMatchingWhiteBalance(wk.orig())
}
if size == 0 {
// use the original RAW file for a full resolution preview
err = editXMP(wk.origXMP(), xmp)
if err != nil {
return nil, err
}
err = runDNGConverter(ctx, wk.orig(), wk.temp(), 0, &exportSettings{})
if err != nil {
return nil, err
}
return previewJPEG(ctx, wk.temp())
} else if wk.hasEdit {
// use edit.dng (downscaled to at most 2560 on the widest side)
err = editXMP(wk.edit(), xmp)
if err != nil {
return nil, err
}
err = runDNGConverter(ctx, wk.edit(), wk.temp(), size, nil)
if err != nil {
return nil, err
}
return previewJPEG(ctx, wk.temp())
} else {
// create edit.dng (downscaled to 2560 on the widest side)
err = editXMP(wk.origXMP(), xmp)
if err != nil {
return nil, err
}
err = runDNGConverter(ctx, wk.orig(), wk.edit(), 2560, nil)
if err != nil {
return nil, err
}
return previewJPEG(ctx, wk.edit())
}
}
func exportEdit(ctx context.Context, path string, xmp xmpSettings, exp exportSettings) ([]byte, error) {
wk, err := openWorkspace(path)
if err != nil {
return nil, err
}
defer wk.close()
if xmp.WhiteBalance == "Camera Matching…" {
xmp.WhiteBalance = cameraMatchingWhiteBalance(wk.orig())
}
err = editXMP(wk.origXMP(), xmp)
if err != nil {
return nil, err
}
err = runDNGConverter(ctx, wk.orig(), wk.temp(), 0, &exp)
if err != nil {
return nil, err
}
if exp.DNG {
err = fixMetaDNG(wk.orig(), wk.temp(), path)
if err != nil {
return nil, err
}
return os.ReadFile(wk.temp())
} else {
data, err := exportJPEG(ctx, wk.temp())
if err != nil {
return nil, err
}
if exp.Resample {
return resampleJPEG(data, exp)
}
err = os.WriteFile(wk.jpeg(), data, 0600)
if err != nil {
return nil, err
}
err = fixMetaJPEG(wk.orig(), wk.jpeg())
if err != nil {
return nil, err
}
return os.ReadFile(wk.jpeg())
}
}
func exportPath(path string, exp exportSettings) string {
var ext string
if exp.DNG {
ext = ".dng"
} else {
ext = ".jpg"
}
return strings.TrimSuffix(path, filepath.Ext(path)) + ext
}
func loadWhiteBalance(ctx context.Context, path string, coords []float64) (wb xmpWhiteBalance, err error) {
wk, err := openWorkspace(path)
if err != nil {
return wb, err
}
defer wk.close()
if !wk.hasEdit {
// create edit.dng (downscaled to at most 2560 on the widest side)
err = runDNGConverter(ctx, wk.orig(), wk.edit(), 2560, nil)
if err != nil {
return wb, err
}
}
if !wk.hasPixels && len(coords) == 2 {
err = getRawPixels(ctx, wk.edit(), wk.pixels())
if err != nil {
return wb, err
}
}
return computeWhiteBalance(wk.edit(), wk.pixels(), coords)
}
type exportSettings struct {
DNG bool
Preview string
Lossy bool
Embed bool
Both bool
Resample bool
Quality int
Fit string
Long float64
Short float64
Width float64
Height float64
DimUnit string
Density int
DenUnit string
MPixels float64
}
func (ex *exportSettings) FitImage(size image.Point) (fit image.Point) {
if ex.Fit == "mpix" {
mul := math.Sqrt(1e6 * ex.MPixels / float64(size.X*size.Y))
if size.X > size.Y {
fit.X = math.MaxInt
fit.Y = int(mul * float64(size.Y))
} else {
fit.X = int(mul * float64(size.X))
fit.Y = math.MaxInt
}
} else {
mul := 1.0
if ex.DimUnit != "px" {
density := float64(ex.Density)
if ex.DimUnit == "in" {
if ex.DenUnit == "ppi" {
mul = density
} else {
mul = density * 2.54
}
} else {
if ex.DenUnit == "ppi" {
mul = density / 2.54
} else {
mul = density
}
}
}
round := func(x float64) int {
if x == 0.0 {
return math.MaxInt
}
i := int(x + 0.5)
if i < 16 {
return 16
} else {
return i
}
}
if ex.Fit == "dims" {
long, short := ex.Long, ex.Short
if 0 < long && long < short {
long, short = short, long
}
if size.X > size.Y {
fit.X = round(mul * long)
fit.Y = round(mul * short)
} else {
fit.X = round(mul * short)
fit.Y = round(mul * long)
}
} else {
fit.X = round(mul * ex.Width)
fit.Y = round(mul * ex.Height)
}
}
return fit
}
func loadSidecar(src, dst string) error {
var data []byte
err := os.ErrNotExist
ext := filepath.Ext(src)
if ext != "" {
// if NAME.xmp is there for NAME.EXT, use it
name := strings.TrimSuffix(src, ext) + ".xmp"
data, err = os.ReadFile(name)
if err == nil && !xmp.IsSidecarForExt(bytes.NewReader(data), ext) {
err = os.ErrNotExist
}
}
if errors.Is(err, fs.ErrNotExist) {
// if NAME.EXT.xmp is there for NAME.EXT, use it
data, err = os.ReadFile(src + ".xmp")
if err == nil && !xmp.IsSidecarForExt(bytes.NewReader(data), ext) {
err = os.ErrNotExist
}
}
if err == nil {
// copy xmp file
err = os.WriteFile(dst, data, 0600)
}
if err == nil || errors.Is(err, fs.ErrNotExist) {
// extract embed XMP data
return extractXMP(src, dst)
}
return err
}
func destSidecar(src string) (string, error) {
ext := filepath.Ext(src)
if ext != "" {
// if NAME.xmp is there for NAME.EXT, use it
name := strings.TrimSuffix(src, ext) + ".xmp"
data, err := os.ReadFile(name)
if err == nil && xmp.IsSidecarForExt(bytes.NewReader(data), ext) {
return name, nil
}
if !errors.Is(err, fs.ErrNotExist) {
return "", err
}
}
// if NAME.EXT.xmp exists, use it
if _, err := os.Stat(src + ".xmp"); err == nil {
return src + ".xmp", nil
} else if !errors.Is(err, fs.ErrNotExist) {
return "", err
}
// if NAME.DNG was edited, use it
if strings.EqualFold(ext, ".dng") && dngHasEdits(src) {
return src, nil
}
// fallback to NAME.xmp
return strings.TrimSuffix(src, ext) + ".xmp", nil
}