-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
1494 lines (1301 loc) · 36.1 KB
/
config.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package imageflux
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"errors"
"fmt"
"image"
"image/color"
"math"
"strconv"
"strings"
"time"
)
const rectangleScale = 65536
// nowFunc is for testing.
var nowFunc = time.Now
// ErrExpired is returned when the image is expired.
var ErrExpired = errors.New("imageflux: expired")
// ErrInvalidSignature is returned when the signature is invalid.
var ErrInvalidSignature = errors.New("imageflux: invalid signature")
// Config is configure of image.
type Config struct {
// Width is width in pixel of the scaled image.
Width int
// Height is height in pixel of the scaled image.
Height int
// DisableEnlarge disables enlarge.
DisableEnlarge bool
// AspectMode is aspect mode.
AspectMode AspectMode
// DevicePixelRatio is a scale factor of device pixel ratio.
// If DevicePixelRatio is 0, it is ignored.
DevicePixelRatio float64
// InputClip is a position in pixel of clipping area.
// This is used for the input image.
InputClip image.Rectangle
// InputClipRatio is a position in ratio of clipping area.
// The coordinates of the rectangle are divided by ClipMax.X or ClipMax.Y.
// This is used for the input image.
InputClipRatio image.Rectangle
// InputOrigin is the position of the input image origin.
InputOrigin Origin
// OutputClip is a position in pixel of clipping area.
// This is used for the output image.
OutputClip image.Rectangle
// Clip is an alias of OutputClip.
// If both Clip and OutputClip are set, OutputClip is used.
//
// Deprecated: Use OutputClip instead.
Clip image.Rectangle
// OutputClipRatio is a position in ratio of clipping area.
// The coordinates of the rectangle are divided by ClipMax.X or ClipMax.Y.
OutputClipRatio image.Rectangle
// ClipRatio is an alias of OutputClipRatio.
// If both ClipRatio and OutputClipRatio are set, OutputClipRatio is used.
//
// Deprecated: Use OutputClipRatio instead.
ClipRatio image.Rectangle
// OutputOrigin is the position of the output image origin.
OutputOrigin Origin
// ClipMax is the denominators of ClipRatio.
ClipMax image.Point
// Origin is the position of the image origin.
Origin Origin
// Background is background color.
Background color.Color
// InputRotate rotates the image before processing.
InputRotate Rotate
// OutputRotate rotates the image after processing.
OutputRotate Rotate
// OutputRotate rotates the image after processing.
// This is an alias of OutputRotate.
// If both Rotate and OutputRotate are set, OutputRotate is used.
//
// Deprecated: Use OutputRotate instead.
Rotate Rotate
// Through is a format to pass through.
Through Through
// Overlay Parameters.
Overlays []*Overlay
// Output Parameters.
Format Format
// Quality is quality of the output image.
// It is used when the output format is JPEG or WebP.
Quality int
// DisableOptimization disables optimization of the Huffman coding table
// of the output image when the output format is JPEG.
DisableOptimization bool
// Lossless enables lossless compression when the output format is WebP.
Lossless bool
// ExifOption specifies the Exif information to be included in the output image.
ExifOption ExifOption
// Unsharp configures unsharp mask.
Unsharp Unsharp
// Blur configures blur.
Blur Blur
// GrayScale converts to gray scale.
// 0 means no conversion and 100 means full conversion.
GrayScale int
// Sepia converts to sepia.
// 0 means no conversion and 100 means full conversion.
Sepia int
// Brightness adjusts brightness.
// The value set in Brightness plus 100 is actually used.
Brightness int
// Contrast adjusts contrast.
// The value set in Contrast plus 100 is actually used.
Contrast int
// Invert inverts the image if it is true.
Invert bool
}
// Unsharp is an unsharp filter config.
type Unsharp struct {
Radius int
Sigma float64
Gain float64
Threshold float64
}
func (u Unsharp) append(buf []byte) []byte {
buf = strconv.AppendInt(buf, int64(u.Radius), 10)
buf = append(buf, 'x')
buf = strconv.AppendFloat(buf, u.Sigma, 'f', -1, 64)
if u.Threshold != 0 {
buf = append(buf, '+')
buf = strconv.AppendFloat(buf, u.Gain, 'f', -1, 64)
buf = append(buf, '+')
buf = strconv.AppendFloat(buf, u.Threshold, 'f', -1, 64)
}
return buf
}
func parseUnsharp(s string) (Unsharp, error) {
var u Unsharp
// radius
idx := strings.IndexByte(s, 'x')
if idx < 0 {
return Unsharp{}, errors.New("imageflux: invalid unsharp format")
}
r, err := strconv.ParseInt(s[:idx], 10, 0)
if err != nil {
return Unsharp{}, fmt.Errorf("imageflux: invalid unsharp format: %w", err)
}
if r <= 0 {
return Unsharp{}, errors.New("imageflux: invalid unsharp format")
}
u.Radius = int(r)
s = s[idx+1:]
// sigma
idx = strings.IndexByte(s, '+')
if idx < 0 {
sigma, err := strconv.ParseFloat(s, 64)
if err != nil {
return u, fmt.Errorf("imageflux: invalid unsharp format: %w", err)
}
if sigma <= 0 || math.IsNaN(sigma) || math.IsInf(sigma, 0) {
return u, errors.New("imageflux: invalid unsharp format")
}
u.Sigma = sigma
return u, nil
}
sigma, err := strconv.ParseFloat(s[:idx], 64)
if err != nil {
return Unsharp{}, fmt.Errorf("imageflux: invalid unsharp format: %w", err)
}
if sigma <= 0 || math.IsNaN(sigma) || math.IsInf(sigma, 0) {
return u, errors.New("imageflux: invalid unsharp format")
}
u.Sigma = sigma
s = s[idx+1:]
// gain
idx = strings.IndexByte(s, '+')
if idx < 0 {
return Unsharp{}, errors.New("imageflux: invalid unsharp format")
}
gain, err := strconv.ParseFloat(s[:idx], 64)
if err != nil {
return Unsharp{}, fmt.Errorf("imageflux: invalid unsharp format: %w", err)
}
if math.IsNaN(gain) || math.IsInf(gain, 0) {
return Unsharp{}, errors.New("imageflux: invalid unsharp format")
}
u.Gain = gain
s = s[idx+1:]
// threshold
threshold, err := strconv.ParseFloat(s, 64)
if err != nil {
return Unsharp{}, fmt.Errorf("imageflux: invalid unsharp format: %w", err)
}
if threshold <= 0 || threshold >= 1 || math.IsNaN(threshold) {
return Unsharp{}, errors.New("imageflux: invalid unsharp format")
}
u.Threshold = threshold
return u, nil
}
// Blur is a blur config.
type Blur struct {
Radius int
Sigma float64
}
func (b Blur) append(buf []byte) []byte {
buf = strconv.AppendInt(buf, int64(b.Radius), 10)
buf = append(buf, 'x')
buf = strconv.AppendFloat(buf, b.Sigma, 'f', -1, 64)
return buf
}
func parseBlur(s string) (Blur, error) {
idx := strings.IndexByte(s, 'x')
if idx < 0 {
return Blur{}, errors.New("imageflux: invalid blur format")
}
// radius
r, err := strconv.ParseInt(s[:idx], 10, 0)
if err != nil {
return Blur{}, fmt.Errorf("imageflux: invalid blur format: %w", err)
}
if r <= 0 {
return Blur{}, errors.New("imageflux: invalid blur format")
}
// sigma
sigma, err := strconv.ParseFloat(s[idx+1:], 64)
if err != nil {
return Blur{}, fmt.Errorf("imageflux: invalid blur format: %w", err)
}
if sigma <= 0 || math.IsNaN(sigma) || math.IsInf(sigma, 0) {
return Blur{}, errors.New("imageflux: invalid blur format")
}
return Blur{
Radius: int(r),
Sigma: sigma,
}, nil
}
// AspectMode is aspect mode.
type AspectMode int
const (
// AspectModeDefault is the default value of aspect mode.
AspectModeDefault AspectMode = iota
// AspectModeScale holds the the aspect ratio of the input image,
// and scales to fit in the specified size.
AspectModeScale
// AspectModeForceScale ignores the aspect ratio of the input image.
AspectModeForceScale
// AspectModeCrop holds the the aspect ratio of the input image,
// and crops the image.
AspectModeCrop
// AspectModePad holds the the aspect ratio of the input image,
// and fills the unfilled portion with the specified background color.
AspectModePad
aspectModeMax
)
// Origin is the origin.
type Origin int
const (
// OriginDefault is default origin.
OriginDefault Origin = 0
// OriginTopLeft is top-left
OriginTopLeft Origin = 1
// OriginTopCenter is top-center
OriginTopCenter Origin = 2
// OriginTopRight is top-right
OriginTopRight Origin = 3
// OriginMiddleLeft is middle-left
OriginMiddleLeft Origin = 4
// OriginMiddleCenter is middle-center
OriginMiddleCenter Origin = 5
// OriginMiddleRight is middle-right
OriginMiddleRight Origin = 6
// OriginBottomLeft is bottom-left
OriginBottomLeft Origin = 7
// OriginBottomCenter is bottom-center
OriginBottomCenter Origin = 8
// OriginBottomRight is bottom-right
OriginBottomRight Origin = 9
originMax Origin = 10
)
func (o Origin) String() string {
switch o {
case OriginDefault:
return "default"
case OriginTopLeft:
return "top-left"
case OriginTopCenter:
return "top-center"
case OriginTopRight:
return "top-right"
case OriginMiddleLeft:
return "middle-left"
case OriginMiddleCenter:
return "middle-center"
case OriginMiddleRight:
return "middle-right"
case OriginBottomLeft:
return "bottom-left"
case OriginBottomCenter:
return "bottom-center"
case OriginBottomRight:
return "bottom-right"
}
return ""
}
// Format is the format of the output image.
type Format string
const (
// FormatAuto encodes the image by the same format with the input image.
FormatAuto Format = "auto"
// FormatJPEG encodes the image as JPEG.
FormatJPEG Format = "jpg"
// FormatPNG encodes the image as PNG.
FormatPNG Format = "png"
// FormatGIF encodes the image as GIF.
FormatGIF Format = "gif"
// FormatWebP encodes the image as WebP.
FormatWebP Format = "webp"
// FormatWebPAuto encodes the image as a WebP if the client supports WebP.
// Otherwise, the image is encoded as the same format with the input image.
FormatWebPAuto Format = "webp:auto"
// FormatWebPJPEG encodes the image as a WebP if the client supports WebP.
// Otherwise, the image is encoded as JPEG.
FormatWebPJPEG Format = "webp:jpg"
// FormatWebPPNG encodes the image as a WebP if the client supports WebP.
// Otherwise, the image is encoded as PNG.
FormatWebPPNG Format = "webp:png"
// FormatWebPGIF encodes the image as a WebP if the client supports WebP.
// Otherwise, the image is encoded as GIF.
FormatWebPGIF Format = "webp:gif"
// FormatWebPFromJPEG encodes the image as a WebP.
//
// Deprecated: use FormatWebPJPEG instead.
FormatWebPFromJPEG Format = "webp:jpeg"
// FormatWebPFromPNG encodes the image as a WebP.
//
// Deprecated: use FormatWebPPNG instead.
FormatWebPFromPNG Format = "webp:png"
)
func (f Format) String() string {
return string(f)
}
func newFormat(s string) (Format, error) {
// validate the input with the regexp /[a-z]+(:[a-z]+)*/.
colon := true
for _, ch := range []byte(s) {
if ch == ':' {
if colon {
// double colons are detected. it's an error.
return "", fmt.Errorf("imageflux: invalid format %q", s)
}
colon = true
continue
}
if ch < 'a' || ch > 'z' {
return "", fmt.Errorf("imageflux: invalid format %q", s)
}
colon = false
}
if colon {
return "", fmt.Errorf("imageflux: invalid format %q", s)
}
return Format(s), nil
}
// Rotate rotates the image.
type Rotate int
const (
// RotateDefault is the default value of Rotate.
// It is same effect as RotateTopLeft.
RotateDefault Rotate = 0
rotateMin Rotate = 1
// RotateTopLeft does not anything.
RotateTopLeft Rotate = 1
// RotateTopRight flips the image left and right.
RotateTopRight Rotate = 2
// RotateBottomRight rotates the image 180 degrees.
RotateBottomRight Rotate = 3
// RotateBottomLeft flips the image upside down.
RotateBottomLeft Rotate = 4
// RotateLeftTop mirrors the image around the diagonal axis.
RotateLeftTop Rotate = 5
// RotateRightTop rotates the image left 90 degrees.
RotateRightTop Rotate = 6
// RotateRightBottom rotates the image 180 degrees and mirrors the image around the diagonal axis.
RotateRightBottom Rotate = 7
// RotateLeftBottom rotates the image right 90 degrees.
RotateLeftBottom Rotate = 8
rotateMax Rotate = 9
// RotateAuto parses the Orientation of the Exif information and rotates the image.
RotateAuto Rotate = -1
)
func (r Rotate) String() string {
switch r {
case RotateDefault:
return "default"
case RotateTopLeft:
return "top-left"
case RotateTopRight:
return "top-right"
case RotateBottomRight:
return "bottom-right"
case RotateBottomLeft:
return "bottom-left"
case RotateLeftTop:
return "left-top"
case RotateRightTop:
return "right-top"
case RotateRightBottom:
return "right-bottom"
case RotateLeftBottom:
return "left-bottom"
case RotateAuto:
return "auto"
}
return ""
}
// Through is an image format list for skipping converting.
type Through int
const (
// ThroughJPEG skips converting JPEG images.
ThroughJPEG Through = 1 << iota
// ThroughPNG skips converting PNG images.
ThroughPNG
// ThroughGIF skips converting GIF images.
ThroughGIF
// ThroughWebP skips converting WebP images.
ThroughWebP
)
func (t Through) String() string {
var buf [32]byte
return string(t.append(buf[:]))
}
func (t Through) append(buf []byte) []byte {
if (t & ThroughJPEG) != 0 {
buf = append(buf, "jpg:"...)
}
if (t & ThroughPNG) != 0 {
buf = append(buf, "png:"...)
}
if (t & ThroughGIF) != 0 {
buf = append(buf, "gif:"...)
}
if (t & ThroughWebP) != 0 {
buf = append(buf, "webp:"...)
}
if len(buf) == 0 {
return buf
}
return buf[:len(buf)-1]
}
func parseThrough(s string) (Through, error) {
var t Through
for s != "" {
var v string
if idx := strings.IndexByte(s, ':'); idx >= 0 {
v = s[:idx]
s = s[idx+1:]
} else {
v = s
s = ""
}
switch v {
case "jpg":
t |= ThroughJPEG
case "png":
t |= ThroughPNG
case "gif":
t |= ThroughGIF
case "webp":
t |= ThroughWebP
default:
return 0, fmt.Errorf("imageflux: unknown through format: %s", v)
}
}
return t, nil
}
// MaskType specifies the area to be treated as a mask.
type MaskType string
const (
// MaskTypeWhite clips the mask image leaving the white parts.
MaskTypeWhite MaskType = "white"
// MaskTypeBlack clips the mask image leaving the black parts.
MaskTypeBlack MaskType = "black"
// MaskTypeAlpha clips the mask image leaving the opaque parts.
MaskTypeAlpha MaskType = "alpha"
)
// PaddingMode specifies processing when the specified image is smaller than the input image.
type PaddingMode int
const (
// PaddingModeDefault makes the part of the image that protrudes from the specified image transparent.
PaddingModeDefault PaddingMode = 0
// PaddingModeLeave leaves the overflow area of the specified image as it is.
PaddingModeLeave PaddingMode = 1
)
// ExifOption specifies the Exif information to be included in the output image.
type ExifOption int
const (
// ExifOptionDefault is the default value of ExifOption.
ExifOptionDefault ExifOption = 0
exifOptionMin ExifOption = 1
// ExifOptionStrip removes all Exif information from the output image.
ExifOptionStrip ExifOption = 1
// ExifOptionKeepOrientation removes all Exif information
// except Orientation from the output image.
ExifOptionKeepOrientation ExifOption = 2
exifOptionMax ExifOption = 3
)
// String returns a string representing the Config.
// If c is nil or zero value, it returns "f=auto".
func (c *Config) String() string {
if c == nil {
return "f=auto"
}
buf := bufPool.Get().(*[]byte)
*buf = c.append((*buf)[:0], false)
str := string(*buf)
bufPool.Put(buf)
return str
}
func (c *Config) append(buf []byte, escapeComma bool) []byte {
var zr image.Rectangle
var zp image.Point
if c == nil {
buf = append(buf, "f=auto"...)
return buf
}
l := len(buf)
if c.Width != 0 {
buf = append(buf, "w="...)
buf = strconv.AppendInt(buf, int64(c.Width), 10)
buf = appendComma(buf, escapeComma)
}
if c.Height != 0 {
buf = append(buf, "h="...)
buf = strconv.AppendInt(buf, int64(c.Height), 10)
buf = appendComma(buf, escapeComma)
}
if c.DisableEnlarge {
buf = append(buf, "u=0"...)
buf = appendComma(buf, escapeComma)
}
if c.AspectMode != AspectModeDefault {
buf = append(buf, "a="...)
buf = strconv.AppendInt(buf, int64(c.AspectMode-1), 10)
buf = appendComma(buf, escapeComma)
}
if c.DevicePixelRatio != 0 {
buf = append(buf, "dpr="...)
buf = strconv.AppendFloat(buf, c.DevicePixelRatio, 'f', -1, 64)
buf = appendComma(buf, escapeComma)
}
// clipping parameters
if ic := c.InputClip; ic != zr {
buf = append(buf, "ic="...)
buf = strconv.AppendInt(buf, int64(ic.Min.X), 10)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(ic.Min.Y), 10)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(ic.Max.X), 10)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(ic.Max.Y), 10)
buf = appendComma(buf, escapeComma)
}
if cm, ic := c.ClipMax, c.InputClipRatio; cm != zp && ic != zr {
x1 := float64(ic.Min.X) / float64(cm.X)
y1 := float64(ic.Min.Y) / float64(cm.Y)
x2 := float64(ic.Max.X) / float64(cm.X)
y2 := float64(ic.Max.Y) / float64(cm.Y)
buf = append(buf, "icr="...)
buf = strconv.AppendFloat(buf, x1, 'f', -1, 64)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, y1, 'f', -1, 64)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, x2, 'f', -1, 64)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, y2, 'f', -1, 64)
buf = appendComma(buf, escapeComma)
}
if ig := c.InputOrigin; ig != OriginDefault {
buf = append(buf, "ig="...)
buf = strconv.AppendInt(buf, int64(ig), 10)
buf = appendComma(buf, escapeComma)
}
if c, oc := c.Clip, c.OutputClip; c != zr || oc != zr {
if oc == zr {
oc = c
}
buf = append(buf, "oc="...)
buf = strconv.AppendInt(buf, int64(oc.Min.X), 10)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(oc.Min.Y), 10)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(oc.Max.X), 10)
buf = append(buf, ':')
buf = strconv.AppendInt(buf, int64(oc.Max.Y), 10)
buf = appendComma(buf, escapeComma)
}
if c, oc, cm := c.ClipRatio, c.OutputClipRatio, c.ClipMax; cm != zp && (c != zr || oc != zr) {
if oc == zr {
oc = c
}
x1 := float64(oc.Min.X) / float64(cm.X)
y1 := float64(oc.Min.Y) / float64(cm.Y)
x2 := float64(oc.Max.X) / float64(cm.X)
y2 := float64(oc.Max.Y) / float64(cm.Y)
buf = append(buf, "ocr="...)
buf = strconv.AppendFloat(buf, x1, 'f', -1, 64)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, y1, 'f', -1, 64)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, x2, 'f', -1, 64)
buf = append(buf, ':')
buf = strconv.AppendFloat(buf, y2, 'f', -1, 64)
buf = appendComma(buf, escapeComma)
}
if og := c.OutputOrigin; og != OriginDefault {
buf = append(buf, "og="...)
buf = strconv.AppendInt(buf, int64(og), 10)
buf = appendComma(buf, escapeComma)
}
if c.Origin != OriginDefault {
buf = append(buf, "g="...)
buf = strconv.AppendInt(buf, int64(c.Origin), 10)
buf = appendComma(buf, escapeComma)
}
if c.Background != nil {
b := color.NRGBAModel.Convert(c.Background).(color.NRGBA)
if b.A == 0xff {
// opaque background
buf = append(buf, "b="...)
buf = appendByte(buf, b.R)
buf = appendByte(buf, b.G)
buf = appendByte(buf, b.B)
buf = appendComma(buf, escapeComma)
} else {
buf = append(buf, "b="...)
buf = appendByte(buf, b.R)
buf = appendByte(buf, b.G)
buf = appendByte(buf, b.B)
buf = appendByte(buf, b.A)
buf = appendComma(buf, escapeComma)
}
}
// rotation
if ir := c.InputRotate; ir != RotateDefault {
if ir == RotateAuto {
buf = append(buf, "ir=auto"...)
buf = appendComma(buf, escapeComma)
} else {
buf = append(buf, "ir="...)
buf = strconv.AppendInt(buf, int64(ir), 10)
buf = appendComma(buf, escapeComma)
}
}
if r, or := c.Rotate, c.OutputRotate; r != RotateDefault || or != RotateDefault {
if or == RotateDefault {
or = r
}
if or == RotateAuto {
buf = append(buf, "or=auto"...)
buf = appendComma(buf, escapeComma)
} else {
buf = append(buf, "or="...)
buf = strconv.AppendInt(buf, int64(or), 10)
buf = appendComma(buf, escapeComma)
}
}
if c.Through != 0 {
buf = append(buf, "through="...)
buf = c.Through.append(buf)
buf = appendComma(buf, escapeComma)
}
if len(c.Overlays) > 0 {
for _, overlay := range c.Overlays {
buf = append(buf, "l=("...)
buf = overlay.append(buf, escapeComma)
buf = append(buf, ')')
buf = appendComma(buf, escapeComma)
}
}
// output formats
if c.Format != "" {
buf = append(buf, "f="...)
buf = append(buf, c.Format...)
buf = appendComma(buf, escapeComma)
}
if c.Quality != 0 {
buf = append(buf, "q="...)
buf = strconv.AppendInt(buf, int64(c.Quality), 10)
buf = appendComma(buf, escapeComma)
}
if c.DisableOptimization {
buf = append(buf, "o=0"...)
buf = appendComma(buf, escapeComma)
}
if c.Lossless {
buf = append(buf, "lossless=1"...)
buf = appendComma(buf, escapeComma)
}
if c.ExifOption != ExifOptionDefault {
buf = append(buf, "s="...)
buf = strconv.AppendInt(buf, int64(c.ExifOption), 10)
buf = appendComma(buf, escapeComma)
}
// image filters
if c.Unsharp.Radius != 0 {
buf = append(buf, "unsharp="...)
buf = c.Unsharp.append(buf)
buf = appendComma(buf, escapeComma)
}
if c.Blur.Radius != 0 {
buf = append(buf, "blur="...)
buf = c.Blur.append(buf)
buf = appendComma(buf, escapeComma)
}
if c.GrayScale != 0 {
buf = append(buf, "grayscale="...)
buf = strconv.AppendInt(buf, int64(c.GrayScale), 10)
buf = appendComma(buf, escapeComma)
}
if c.Sepia != 0 {
buf = append(buf, "sepia="...)
buf = strconv.AppendInt(buf, int64(c.Sepia), 10)
buf = appendComma(buf, escapeComma)
}
if c.Brightness != 0 {
buf = append(buf, "brightness="...)
buf = strconv.AppendInt(buf, int64(c.Brightness+100), 10)
buf = appendComma(buf, escapeComma)
}
if c.Contrast != 0 {
buf = append(buf, "contrast="...)
buf = strconv.AppendInt(buf, int64(c.Contrast+100), 10)
buf = appendComma(buf, escapeComma)
}
if c.Invert {
buf = append(buf, "invert=1"...)
buf = appendComma(buf, escapeComma)
}
if len(buf) == l {
buf = append(buf, "f=auto"...)
buf = appendComma(buf, escapeComma)
}
if escapeComma {
return buf[:len(buf)-3]
}
return buf[:len(buf)-1]
}
func appendByte(buf []byte, b byte) []byte {
const digits = "0123456789abcdef"
return append(buf, digits[b>>4], digits[b&0x0F])
}
func appendComma(buf []byte, escape bool) []byte {
if escape {
return append(buf, "%2C"...)
}
return append(buf, ',')
}
func (a AspectMode) String() string {
switch a {
case AspectModeDefault:
return "default"
case AspectModeScale:
return "scale"
case AspectModeForceScale:
return "force-scale"
case AspectModePad:
return "pad"
}
return ""
}
func ParseConfig(s string) (config *Config, rest string, err error) {
state := parseState{
s: s,
config: &Config{},
}
return state.parseConfig()
}
type parseState struct {
s string
idx int
config *Config
// the signature that the user provided.
signature string
}
func (s *parseState) parseConfig() (*Config, string, error) {
if !s.hasParameter() {
return s.config, s.rest(), nil
}
for {
key, foundEqual := s.getKey()
if !foundEqual {
if key != "" {
return nil, "", fmt.Errorf("imageflux: missing '=' after key %q", key)
}
break
}
value, err := s.getValue()
if err != nil {
return nil, "", err
}
s.skipComma()
if err := s.setValue(key, value); err != nil {
return nil, "", err
}
}
return s.config, s.rest(), nil
}
func (s *parseState) parseConfigAndVerifySignature(secret []byte) (*Config, string, error) {
if !s.hasParameter() {
buf := []byte(s.s)
if err := s.verifySignature(secret, buf); err != nil {
return nil, "", err
}
return s.config, s.rest(), nil
}
buf := make([]byte, 0, len(s.s))
if len(s.s) == 0 || s.s[0] != '/' {
buf = append(buf, '/')
}
buf = append(buf, s.s[:s.idx]...)
hasParam := false
for {
start := s.idx
key, foundEqual := s.getKey()
if !foundEqual {
if key != "" {
return nil, "", fmt.Errorf("imageflux: missing '=' after key %q", key)
}
break
}
value, err := s.getValue()
if err != nil {
return nil, "", err
}
s.skipComma()
if err := s.setValue(key, value); err != nil {
return nil, "", err
}
end := s.idx
if key != "sig" {
hasParam = true
buf = append(buf, s.s[start:end]...)
}
}
if hasParam {
if len(buf) >= 1 && buf[len(buf)-1] == ',' {
buf = buf[:len(buf)-1]
} else if len(buf) >= 3 && string(buf[len(buf)-3:]) == "%2C" {
buf = buf[:len(buf)-3]
} else if len(buf) >= 3 && string(buf[len(buf)-3:]) == "%2c" {
buf = buf[:len(buf)-3]
}
} else {
buf = buf[:0]
}
buf = append(buf, s.rest()...)
if err := s.verifySignature(secret, buf); err != nil {
return nil, "", err