-
Notifications
You must be signed in to change notification settings - Fork 35
/
image.go
259 lines (236 loc) · 7.03 KB
/
image.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
package magick
// #include <stdio.h>
// #include <string.h>
// #include <stdlib.h>
// #include <magick/api.h>
import "C"
import (
"io"
"io/ioutil"
"os"
"runtime"
"sync"
"unsafe"
)
// Image represents an in-memory decoded image.
// Some images (like e.g. GIF animations) might have
// multiple frames. Unless indicated otherwise, functions
// work in the same way with both single a multi-frame images,
// performing coalescing when needed. To operate on a single
// frame use e.g. im.Frame(i).Resize() instead of im.Resize().
// Frames might be added or removed with list related functions,
// like Append() or Remove().
type Image struct {
image *C.Image
parent *Image
coalesced bool
mu sync.Mutex
}
// Width returns the image width in pixels.
func (im *Image) Width() int {
return int(im.image.columns)
}
// Height returns the image height in pixels.
func (im *Image) Height() int {
return int(im.image.rows)
}
// Rect is a conveniency function which returns a Rect
// at (0, 0) with the image dimensions.
func (im *Image) Rect() Rect {
return Rect{0, 0, uint(im.Width()), uint(im.Height())}
}
// Format returns the format used to decode
// this image.
func (im *Image) Format() string {
return C.GoString(&im.image.magick[0])
}
// Depth returns the pixel depth of the image,
// usually 8.
func (im *Image) Depth() int {
return int(im.image.depth)
}
// Delay returns the time this image stays visible in
// an animation, in 1/100ths of a second. If this image
// is not part of a sequence of animated images, it returns 0.
func (im *Image) Delay() int {
return int(im.image.delay)
}
// Clone returns a copy of the image. If the image
// has multiple frames, it copies all of them. To
// Clone just one frame use im.Frame(i).Clone().
func (im *Image) Clone() (*Image, error) {
var ex C.ExceptionInfo
C.GetExceptionInfo(&ex)
defer C.DestroyExceptionInfo(&ex)
var image *C.Image
if im.parent == nil {
image = C.CloneImageList(im.image, &ex)
} else {
image = C.CloneImage(im.image, magickSize(0), magickSize(0), 1, &ex)
}
return checkImage(image, nil, &ex, "cloning")
}
// Dispose frees the memory associated with the image.
// If you try to use a disposed image, you'll get undefined
// behavior. Note that you don't usually need to call
// Dispose manually. Just before an Image is collected by the GC,
// its Dispose method will be called for you. However, if you're
// allocating multiple images in a loop, it's probably better to
// manually Dispose them as soon as you don't need them anymore,
// to avoid the temporary memory usage from getting too high.
// Behind the scenes, Image uses a finalizer to call Dispose. Please,
// see http://golang.org/pkg/runtime/#SetFinalizer for more
// information about finalizers.
func (im *Image) Dispose() {
if im.image != nil {
if im.parent == nil {
unrefImages(im.image)
} else {
unrefImage(im.image)
}
im.image = nil
}
runtime.SetFinalizer(im, nil)
}
// Encode writes the image to the given io.Writer, encoding
// it according to the info parameter. Please, see the
// Info type for the available encoding options.
func (im *Image) Encode(w io.Writer, info *Info) error {
var ex C.ExceptionInfo
C.GetExceptionInfo(&ex)
defer C.DestroyExceptionInfo(&ex)
if info == nil {
info = NewInfo()
}
/* ImageToBlob copies the format from the image into
the image info. Overwrite if required and then restore
*/
im.mu.Lock()
var format *C.char
copied := false
if info.info.magick[0] != 0 {
copied = true
if im.image.magick[0] != 0 {
format = C.strdup(&im.image.magick[0])
}
C.strncpy(&im.image.magick[0], &info.info.magick[0], C.MaxTextExtent)
}
var s C.size_t
mem := imageToBlob(info, im, &s, &ex)
if copied {
/* Restore image format */
if format != nil {
C.strncpy(&im.image.magick[0], format, C.MaxTextExtent)
C.free(unsafe.Pointer(format))
} else {
C.memset(unsafe.Pointer(&im.image.magick[0]), 0, C.MaxTextExtent)
}
}
im.mu.Unlock()
if mem == nil {
return exError(&ex, "encoding")
}
b := goBytes(mem, int(s))
w.Write(b)
C.free(mem)
return nil
}
// Image returns the underlying *C.Image. This is useful for
// calling GM or IM directly and performing operations which
// are not yet supported by magick.
func (im *Image) Image() *C.Image {
return im.image
}
// New returns a new RGBA image with the given size. If you
// need more granularity, see Constitute.
func New(width int, height int) (*Image, error) {
return Constitute(width, height, "RGBA", CharPixel, nil)
}
// Decode tries to decode an image from the given io.Reader.
// If the image can't be decoded or it's corrupt, an error
// will be returned. Depending on the backend and compile
// time options, the number of supported formats might
// vary. Use SupportedFormats() to list the all.
func Decode(r io.Reader) (*Image, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
return DecodeData(data)
}
// DecodeData works like Decode, but accepts a
// []byte rather than an io.Reader.
func DecodeData(data []byte) (*Image, error) {
return decodeData(data, 0)
}
// DecodeFile works like Decode, but accepts a
// filename.
func DecodeFile(filename string) (*Image, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
return Decode(f)
}
func decodeData(data []byte, try int) (*Image, error) {
if len(data) == 0 {
return nil, ErrNoData
}
info := C.CloneImageInfo(nil)
defer C.DestroyImageInfo(info)
var ex C.ExceptionInfo
C.GetExceptionInfo(&ex)
defer C.DestroyExceptionInfo(&ex)
im := C.BlobToImage(info, unsafe.Pointer(&data[0]), C.size_t(len(data)), &ex)
if im == nil && try < maxGifTries && ex.severity == C.CorruptImageError && looksLikeGif(data) {
return fixAndDecodeGif(data, try)
}
return checkImage(im, nil, &ex, "decoding")
}
func initializeRefCounts(im *C.Image) {
for cur := (*C.Image)(im.previous); cur != nil; cur = (*C.Image)(cur.previous) {
p := (*int32)(unsafe.Pointer(&cur.client_data))
*p = 0
}
for cur := im; cur != nil; cur = (*C.Image)(cur.next) {
p := (*int32)(unsafe.Pointer(&cur.client_data))
*p = 0
}
}
func newImage(im *C.Image, parent *Image) *Image {
image := new(Image)
image.image = im
if parent != nil {
for parent.parent != nil {
parent = parent.parent
}
image.parent = parent
refImage(im)
} else {
// WARNING: Set the reference count to 0 before calling refImages.
// Functions which return an image from another image (e.g. crop, resize, etc...)
// copy the client_data parameter, which is what we're using for reference
// counting. Since the image has not been initialized yet, only this
// goroutine can be accessing it, so we may safely just set all the
// reference counts to 0.
initializeRefCounts(im)
refImages(im)
}
freeWhenDone(image)
return image
}
func freeWhenDone(im *Image) {
runtime.SetFinalizer(im, func(i *Image) {
i.Dispose()
})
}
func dontFree(im *Image) {
runtime.SetFinalizer(im, nil)
}
func checkImage(im *C.Image, parent *Image, ex *C.ExceptionInfo, what string) (*Image, error) {
if im != nil {
return newImage(im, parent), nil
}
return nil, exError(ex, what)
}