-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdither.go
221 lines (182 loc) · 6.85 KB
/
dither.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
package caca
// #cgo LDFLAGS: -lcaca
// #include <caca.h>
// #include <stdlib.h>
import "C"
import (
"unsafe"
)
// Dither is a dither structure.
type Dither struct {
Di *C.struct_caca_dither
}
// CreateDither creates a dither structure from its coordinates
// (depth, width, height and pitch) and pixel mask values. If the depth is 8
// bits per pixel, the mask values are ignored and the colour palette should be
// set using the SetPalette() function. For depths greater than 8 bits per
// pixel, a zero alpha mask causes the alpha values to be ignored.
//
// If an error occurs the according errno is returned.
func CreateDither(bpp int, w int, h int, pitch int, rmask uint32, gmask uint32, bmask uint32, amask uint32) (Dither, error) {
cPtr, err := C.caca_create_dither(C.int(bpp), C.int(w), C.int(h), C.int(pitch), C.uint32_t(rmask), C.uint32_t(gmask), C.uint32_t(bmask), C.uint32_t(amask))
if cPtr == nil {
return Dither{}, err
}
return Dither{Di: cPtr}, nil
}
// SetPalette sets the palette of an 8 bits per pixel bitmap. Values should be
// between 0 and 4095 (0xfff).
//
// If an error occurs the according errno is returned.
func (di Dither) SetPalette(red [256]uint32, green [256]uint32, blue [256]uint32, alpha [256]uint32) error {
ret, err := C.caca_set_dither_palette(di.Di, (*C.uint32_t)(&red[0]), (*C.uint32_t)(&green[0]), (*C.uint32_t)(&blue[0]), (*C.uint32_t)(&alpha[0]))
if int(ret) == -1 {
return err
}
return nil
}
// SetBrightness sets the brightness of the dither.
//
// If an error occurs the according errno is returned.
func (di Dither) SetBrightness(brightness float64) error {
ret, err := C.caca_set_dither_brightness(di.Di, C.float(brightness))
if int(ret) == -1 {
return err
}
return nil
}
// GetBrightness returns the brightness of the dither.
func (di Dither) GetBrightness() float64 {
return float64(C.caca_get_dither_brightness(di.Di))
}
// SetGamma sets the gamma of the dither object. A negative value causes colour
// inversion.
//
// If an error occurs the according errno is returned.
func (di Dither) SetGamma(gamma float64) error {
ret, err := C.caca_set_dither_gamma(di.Di, C.float(gamma))
if int(ret) == -1 {
return err
}
return nil
}
// GetGamma returns the gamma of the dither object.
func (di Dither) GetGamma() float64 {
return float64(C.caca_get_dither_gamma(di.Di))
}
// SetContrast sets the contrast of the dither object.
//
// If an error occurs the according errno is returned.
func (di Dither) SetContrast(gamma float64) error {
ret, err := C.caca_set_dither_contrast(di.Di, C.float(gamma))
if int(ret) == -1 {
return err
}
return nil
}
// GetContrast returns the contrast of the dither object.
func (di Dither) GetContrast() float64 {
return float64(C.caca_get_dither_contrast(di.Di))
}
// SetAntialias tells the renderer whether to antialias the dither.
// Antialiasing smoothens the rendered image and avoids the commonly seen
// staircase effect.
//
// "none": no antialiasing.
// "prefilter" or "default": simple prefilter antialiasing. This is the
// default value.
//
// If an error occurs the according errno is returned.
func (di Dither) SetAntialias(str string) error {
ret, err := C.caca_set_dither_antialias(di.Di, C.CString(str))
if int(ret) == -1 {
return err
}
return nil
}
// GetAntialias returns the antialiasing method of the dither object.
func (di Dither) GetAntialias() string {
return C.GoString(C.caca_get_dither_antialias(di.Di))
}
// SetColor tells the renderer which colours should be used to render the
// bitmap. Valid values for str are:
//
// "mono": use light gray on a black background.
// "gray": use white and two shades of gray on a black background.
// "8": use the 8 ANSI colours on a black background.
// "16": use the 16 ANSI colours on a black background.
// "fullgray": use black, white and two shades of gray for both the
// characters and the background.
// "full8": use the 8 ANSI colours for both the characters and the
// background.
// "full16" or "default": use the 16 ANSI colours for both the characters
// and the background. This is the default value.
//
// If an error occurs the according errno is returned.
func (di Dither) SetColor(str string) error {
ret, err := C.caca_set_dither_color(di.Di, C.CString(str))
if ret == -1 {
return err
}
return nil
}
// GetColor returns the current colour mode of the dither object.
func (di Dither) GetColor() string {
return C.GoString(C.caca_get_dither_color(di.Di))
}
// SetCharset tells the renderer which characters should be used to render the
// dither. Valid values for str are:
//
// "ascii" or "default": use only ASCII characters. This is the default
// value.
// "shades": use Unicode characters "U+2591 LIGHT SHADE",
// "U+2592 MEDIUM SHADE" and "U+2593 DARK SHADE". These
// characters are also present in the CP437 codepage available
// on DOS and VGA.
// "blocks": use Unicode quarter-cell block combinations. These characters
// are only found in the Unicode set.
//
// If an error occurs the according errno is returned.
func (di Dither) SetCharset(str string) error {
ret, err := C.caca_set_dither_charset(di.Di, C.CString(str))
if int(ret) == -1 {
return err
}
return nil
}
// GetCharset returns the current character set of the dither object.
func (di Dither) GetCharset() string {
return C.GoString(C.caca_get_dither_charset(di.Di))
}
// SetAlgorithm tells the renderer which dithering algorithm should be used.
// Dithering is necessary because the picture being rendered has usually far
// more colours than the available palette. Valid values for str are:
//
// "none": no dithering is used, the nearest matching colour is used.
// "ordered2": use a 2x2 Bayer matrix for dithering.
// "ordered4": use a 4x4 Bayer matrix for dithering.
// "ordered8": use a 8x8 Bayer matrix for dithering.
// "random": use random dithering.
// "fstein": use Floyd-Steinberg dithering. This is the default value.
//
// If an error occurs the according errno is returned.
func (di Dither) SetAlgorithm(str string) error {
ret, err := C.caca_set_dither_algorithm(di.Di, C.CString(str))
if int(ret) == -1 {
return err
}
return nil
}
// GetAlgorithm returns the current dithering algorithm of the dither object.
func (di Dither) GetAlgorithm() string {
return C.GoString(C.caca_get_dither_algorithm(di.Di))
}
// Bitmap dithers a bitmap at the given coordinates. The dither can be of any
// size and will be stretched to the text area.
func (di Dither) Bitmap(cv Canvas, x int, y int, w int, h int, pixels []byte) {
C.caca_dither_bitmap(cv.Cv, C.int(x), C.int(y), C.int(w), C.int(h), di.Di, unsafe.Pointer(&pixels[0]))
}
// Free frees the memory allocated by CreateDither().
func (di Dither) Free() {
C.caca_free_dither(di.Di)
}