-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathformat.go
265 lines (227 loc) · 6.15 KB
/
format.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
package imgconv
import (
"encoding"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"image/jpeg"
"image/png"
"io"
"strings"
"github.com/sunshineplan/pdf"
"golang.org/x/image/bmp"
"golang.org/x/image/tiff"
_ "golang.org/x/image/webp" // decode webp format
)
var (
_ encoding.TextUnmarshaler = new(Format)
_ encoding.TextMarshaler = Format(0)
)
// Format is an image file format.
type Format int
// Image file formats.
const (
JPEG Format = iota
PNG
GIF
TIFF
BMP
PDF
)
var formatExts = [][]string{
{"jpg", "jpeg"},
{"png"},
{"gif"},
{"tif", "tiff"},
{"bmp"},
{"pdf"},
}
func (f Format) String() (format string) {
defer func() {
if err := recover(); err != nil {
format = "unknown"
}
}()
return formatExts[f][0]
}
// FormatFromExtension parses image format from filename extension:
// "jpg" (or "jpeg"), "png", "gif", "tif" (or "tiff"), "bmp" and "pdf" are supported.
func FormatFromExtension(ext string) (Format, error) {
ext = strings.ToLower(ext)
for index, exts := range formatExts {
for _, i := range exts {
if ext == i {
return Format(index), nil
}
}
}
return -1, image.ErrFormat
}
func (f *Format) UnmarshalText(text []byte) error {
format, err := FormatFromExtension(string(text))
if err != nil {
return err
}
*f = format
return nil
}
func (f Format) MarshalText() ([]byte, error) {
return []byte(f.String()), nil
}
var (
_ encoding.TextUnmarshaler = new(TIFFCompression)
_ encoding.TextMarshaler = TIFFCompression(0)
)
// TIFFCompression describes the type of compression used in Options.
type TIFFCompression int
// Constants for supported TIFF compression types.
const (
TIFFUncompressed TIFFCompression = iota
TIFFDeflate
)
var tiffCompression = []string{
"none",
"deflate",
}
func (c TIFFCompression) value() tiff.CompressionType {
switch c {
case TIFFDeflate:
return tiff.Deflate
}
return tiff.Uncompressed
}
func (c *TIFFCompression) UnmarshalText(text []byte) error {
t := strings.ToLower(string(text))
for index, tt := range tiffCompression {
if t == tt {
*c = TIFFCompression(index)
return nil
}
}
return fmt.Errorf("tiff: unsupported compression: %s", t)
}
func (c TIFFCompression) MarshalText() (b []byte, err error) {
defer func() {
if err := recover(); err != nil {
b = []byte("unknown")
}
}()
ct := tiffCompression[c]
return []byte(ct), nil
}
// FormatOption is format option
type FormatOption struct {
Format Format
EncodeOption []EncodeOption
}
type encodeConfig struct {
Quality int
gifNumColors int
gifQuantizer draw.Quantizer
gifDrawer draw.Drawer
pngCompressionLevel png.CompressionLevel
tiffCompressionType TIFFCompression
background color.Color
}
var defaultEncodeConfig = encodeConfig{
Quality: 75,
gifNumColors: 256,
gifQuantizer: nil,
gifDrawer: nil,
pngCompressionLevel: png.DefaultCompression,
tiffCompressionType: TIFFDeflate,
}
// EncodeOption sets an optional parameter for the Encode and Save functions.
// https://github.com/disintegration/imaging
type EncodeOption func(*encodeConfig)
// Quality returns an EncodeOption that sets the output JPEG or PDF quality.
// Quality ranges from 1 to 100 inclusive, higher is better.
func Quality(quality int) EncodeOption {
return func(c *encodeConfig) {
c.Quality = quality
}
}
// GIFNumColors returns an EncodeOption that sets the maximum number of colors
// used in the GIF-encoded image. It ranges from 1 to 256. Default is 256.
func GIFNumColors(numColors int) EncodeOption {
return func(c *encodeConfig) {
c.gifNumColors = numColors
}
}
// GIFQuantizer returns an EncodeOption that sets the quantizer that is used to produce
// a palette of the GIF-encoded image.
func GIFQuantizer(quantizer draw.Quantizer) EncodeOption {
return func(c *encodeConfig) {
c.gifQuantizer = quantizer
}
}
// GIFDrawer returns an EncodeOption that sets the drawer that is used to convert
// the source image to the desired palette of the GIF-encoded image.
func GIFDrawer(drawer draw.Drawer) EncodeOption {
return func(c *encodeConfig) {
c.gifDrawer = drawer
}
}
// PNGCompressionLevel returns an EncodeOption that sets the compression level
// of the PNG-encoded image. Default is png.DefaultCompression.
func PNGCompressionLevel(level png.CompressionLevel) EncodeOption {
return func(c *encodeConfig) {
c.pngCompressionLevel = level
}
}
// TIFFCompressionType returns an EncodeOption that sets the compression type
// of the TIFF-encoded image. Default is tiff.Deflate.
func TIFFCompressionType(compressionType TIFFCompression) EncodeOption {
return func(c *encodeConfig) {
c.tiffCompressionType = compressionType
}
}
// BackgroundColor returns an EncodeOption that sets the background color.
func BackgroundColor(color color.Color) EncodeOption {
return func(c *encodeConfig) {
c.background = color
}
}
// Encode writes the image img to w in the specified format (JPEG, PNG, GIF, TIFF, BMP or PDF).
func (f *FormatOption) Encode(w io.Writer, img image.Image) error {
cfg := defaultEncodeConfig
for _, option := range f.EncodeOption {
option(&cfg)
}
if cfg.background != nil {
i := image.NewNRGBA(img.Bounds())
draw.Draw(i, i.Bounds(), &image.Uniform{cfg.background}, img.Bounds().Min, draw.Src)
draw.Draw(i, i.Bounds(), img, img.Bounds().Min, draw.Over)
img = i
}
switch f.Format {
case JPEG:
if nrgba, ok := img.(*image.NRGBA); ok && nrgba.Opaque() {
rgba := &image.RGBA{
Pix: nrgba.Pix,
Stride: nrgba.Stride,
Rect: nrgba.Rect,
}
return jpeg.Encode(w, rgba, &jpeg.Options{Quality: cfg.Quality})
}
return jpeg.Encode(w, img, &jpeg.Options{Quality: cfg.Quality})
case PNG:
encoder := png.Encoder{CompressionLevel: cfg.pngCompressionLevel}
return encoder.Encode(w, img)
case GIF:
return gif.Encode(w, img, &gif.Options{
NumColors: cfg.gifNumColors,
Quantizer: cfg.gifQuantizer,
Drawer: cfg.gifDrawer,
})
case TIFF:
return tiff.Encode(w, img, &tiff.Options{Compression: cfg.tiffCompressionType.value(), Predictor: true})
case BMP:
return bmp.Encode(w, img)
case PDF:
return pdf.Encode(w, []image.Image{img}, &pdf.Options{Quality: cfg.Quality})
}
return image.ErrFormat
}