forked from joonhoj/mogrify-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgd_image.go
executable file
·114 lines (90 loc) · 2.71 KB
/
gd_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
package mogrify
import (
"fmt"
)
// GdImage implements a wrapper around libgd.
type GdImage struct {
gd *gdImage
}
// NewImage creates an image that can be modified.
func NewImage(width, height int) Image {
var image GdImage
image.gd = gdCreate(width, height)
if image.gd == nil {
return nil
}
return &image
}
// NewResized builds a copy of this image, resized to fit the new
// bounds.
func (img *GdImage) NewResized(bounds Bounds) (*GdImage, error) {
bounds, err := calculateBounds(bounds, img)
if err != nil {
return nil, err
}
resized := img.image().gdCopyResized(0, 0, 0, 0, bounds.Width, bounds.Height, img.image().width(), img.image().height())
if resized == nil {
return nil, fmt.Errorf("cgo call to gdCopyResized failed")
}
return &GdImage{resized}, nil
}
// NewResampled builds a copy of this image, resampled within the new
// bounds.
func (img *GdImage) NewResampled(bounds Bounds) (*GdImage, error) {
bounds, err := calculateBounds(bounds, img)
if err != nil {
return nil, err
}
resized := img.image().gdCopyResampled(0, 0, 0, 0, bounds.Width, bounds.Height, img.image().width(), img.image().height())
if resized == nil {
return nil, fmt.Errorf("cgo call to gdCopyResampled failed")
}
return &GdImage{resized}, nil
}
// NewCropped builds a copy of this image, cropped by the bounds.
func (img *GdImage) NewCropped(x int, y int, bounds Bounds) (*GdImage, error) {
bounds, err := calculateBounds(bounds, img)
if err != nil {
return nil, err
}
cropped := img.image().gdCopy(0, 0, x, y, bounds.Width, bounds.Height)
if cropped == nil {
return nil, fmt.Errorf("cgo call to gdCopy failed")
}
return &GdImage{cropped}, nil
}
// Bounds within which this image holds.
func (img *GdImage) Bounds() Bounds {
return Bounds{img.image().width(), img.image().height()}
}
func (img *GdImage) image() *gdImage {
return img.gd
}
// Destroy cleans up the resources used by this image.
func (img *GdImage) Destroy() {
img.image().gdDestroy()
}
func calculateBounds(bounds Bounds, img *GdImage) (Bounds, error) {
if bounds == (Bounds{0, 0}) {
return bounds, fmt.Errorf("both sides can't be of length 0")
}
if bounds.Width == 0 {
bounds.Width = img.image().width() * bounds.Height / img.image().height()
}
if bounds.Height == 0 {
bounds.Height = img.image().height() * bounds.Width / img.image().width()
}
return bounds, nil
}
func (img *GdImage) Pixels() ([]byte, error) {
return img.image().gdImagePixels()
}
func (img *GdImage) RGBPixels() ([]byte, error) {
return img.image().gdImageRGBPixels()
}
func (img *GdImage) QuantizationPixels() ([]byte, error) {
return img.image().gdImageQuantizationPixels()
}
func (img *GdImage) YCbCr() ([][]byte, error) {
return img.image().gdImageYCbCr()
}