-
Notifications
You must be signed in to change notification settings - Fork 92
/
writer.go
186 lines (170 loc) · 3.96 KB
/
writer.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
// Copyright 2014 <chaishushan{AT}gmail.com>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build cgo
// +build cgo
package webp
import (
"image"
"image/color"
"io"
"os"
"reflect"
)
const DefaulQuality = 90
// Options are the encoding parameters.
type Options struct {
Lossless bool
Quality float32 // 0 ~ 100
Exact bool // Preserve RGB values in transparent area.
}
type colorModeler interface {
ColorModel() color.Model
}
func Save(name string, m image.Image, opt *Options) (err error) {
f, err := os.Create(name)
if err != nil {
return err
}
defer f.Close()
return encode(f, m, opt)
}
// Encode writes the image m to w in WEBP format.
func Encode(w io.Writer, m image.Image, opt *Options) (err error) {
return encode(w, m, opt)
}
func encode(w io.Writer, m image.Image, opt *Options) (err error) {
var output []byte
if opt != nil && opt.Lossless {
switch m := adjustImage(m).(type) {
case *image.Gray:
if output, err = EncodeLosslessGray(m); err != nil {
return
}
case *RGBImage:
if output, err = EncodeLosslessRGB(m); err != nil {
return
}
case *image.RGBA:
if opt.Exact {
output, err = EncodeExactLosslessRGBA(m)
} else {
output, err = EncodeLosslessRGBA(m)
}
if err != nil {
return
}
default:
panic("image/webp: Encode, unreachable!")
}
} else {
quality := float32(DefaulQuality)
if opt != nil {
quality = opt.Quality
}
switch m := adjustImage(m).(type) {
case *image.Gray:
if output, err = EncodeGray(m, quality); err != nil {
return
}
case *RGBImage:
if output, err = EncodeRGB(m, quality); err != nil {
return
}
case *image.RGBA:
if output, err = EncodeRGBA(m, quality); err != nil {
return
}
default:
panic("image/webp: Encode, unreachable!")
}
}
_, err = w.Write(output)
return
}
func adjustImage(m image.Image) image.Image {
if p, ok := AsMemPImage(m); ok {
switch {
case p.XChannels == 1 && p.XDataType == reflect.Uint8:
m = &image.Gray{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}
case p.XChannels == 1 && p.XDataType == reflect.Uint16:
m = toGrayImage(m) // MemP is little endian
case p.XChannels == 3 && p.XDataType == reflect.Uint8:
m = &RGBImage{
XPix: p.XPix,
XStride: p.XStride,
XRect: p.XRect,
}
case p.XChannels == 3 && p.XDataType == reflect.Uint16:
m = NewRGBImageFrom(m) // MemP is little endian
case p.XChannels == 4 && p.XDataType == reflect.Uint8:
m = &image.RGBA{
Pix: p.XPix,
Stride: p.XStride,
Rect: p.XRect,
}
case p.XChannels == 4 && p.XDataType == reflect.Uint16:
m = toRGBAImage(m) // MemP is little endian
}
}
switch m := m.(type) {
case *image.Gray:
return m
case *RGBImage:
return m
case *RGB48Image:
return NewRGBImageFrom(m)
case *image.RGBA:
return m
case *image.YCbCr:
return NewRGBImageFrom(m)
case *image.Gray16:
return toGrayImage(m)
case *image.RGBA64:
return toRGBAImage(m)
case *image.NRGBA:
return toRGBAImage(m)
case *image.NRGBA64:
return toRGBAImage(m)
default:
return toRGBAImage(m)
}
}
func toGrayImage(m image.Image) *image.Gray {
if m, ok := m.(*image.Gray); ok {
return m
}
b := m.Bounds()
gray := image.NewGray(b)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
c := color.GrayModel.Convert(m.At(x, y)).(color.Gray)
gray.SetGray(x, y, c)
}
}
return gray
}
func toRGBAImage(m image.Image) *image.RGBA {
if m, ok := m.(*image.RGBA); ok {
return m
}
b := m.Bounds()
rgba := image.NewRGBA(b)
dstColorRGBA64 := &color.RGBA64{}
dstColor := color.Color(dstColorRGBA64)
for y := b.Min.Y; y < b.Max.Y; y++ {
for x := b.Min.X; x < b.Max.X; x++ {
pr, pg, pb, pa := m.At(x, y).RGBA()
dstColorRGBA64.R = uint16(pr)
dstColorRGBA64.G = uint16(pg)
dstColorRGBA64.B = uint16(pb)
dstColorRGBA64.A = uint16(pa)
rgba.Set(x, y, dstColor)
}
}
return rgba
}