-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlisoption.go
378 lines (336 loc) · 9.36 KB
/
lisoption.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package lisgo
/*
#include <libinsane/capi.h>
#include <lislib.h>
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
)
//lis_value_type enum
const (
LisTypeBool = iota
LisTypeInteger
LisTypeDouble
LisTypeString
LisTypeImageFormat
)
//lis_constraint_type enum
const (
LisConstraintNone = iota
LisConstraintRange
LisConstraintList
)
//capabilitites bit mask flags
const (
LisCapEmulated = 1 << 0 // emulated option (either by LibInsane or backend)
LisCapAutomatic = 1 << 1 // can be set automatically by the driver
LisCapHwSelect = 1 << 2 // can be set by a hardware switch
LisCapSwSelect = 1 << 3 // read/write ; can be set by software
LisCapInactive = 1 << 4 //requires another option to be set to a specific value to become active
)
/*
* Image format.
*
* Used to defined the type of content that will be returned by \ref lis_scan_session.scan_read() . */
const (
/* Raw image, 24bits per pixel.
*
* For each pixel:
* - 8bits for red,
* - 8bits for green,
* - 8bits for blue.
*
* No header, just pixels. */
LisImgFormatRawRGB24 = iota
LisImgFormatGrayScale8
LisImgFormatBW1
LisImgFormatBmp
LisImgFormatCiff
LisImgFormatExif
LisImgFormatFlashPix
LisImgFormatGif
LisImgFormatJpeg
LisImgFormatPng
LisImgFormatIco
LisImgFormatJpeg2k
LisImgFormatJpeg2kx
LisImgFormatmMemoryBmp // Windows BMP without header
LisImgFormatPhotoCD
LisImgFormatPict
LisImgFormatTiff
)
//enum lis_unit
const (
LisUnitNone = iota
LisUnitPixel
LisUnitBit
LisUnitMM
LisUnitDPI
LisUnitPercent
LisUnitMicrosecond
)
var (
lisCapNames = map[int]string{
LisCapEmulated: "LisCapEmulated",
LisCapAutomatic: "LisCapAutomatic",
LisCapHwSelect: "LisCapHwSelect",
LisCapSwSelect: "LisCapSwSelect",
LisCapInactive: "LisCapInactive"}
lisTypeNames = map[uint32]string{
LisTypeBool: "Bool",
LisTypeInteger: "Integer",
LisTypeDouble: "Double",
LisTypeString: "String",
LisTypeImageFormat: "ImageFormat"}
lisConstraintNames = map[int]string{
LisConstraintNone: "None",
LisConstraintRange: "Range",
LisConstraintList: "List"}
lisImageFormatNames = map[uint32]string{
LisImgFormatRawRGB24: "Raw RGB24",
LisImgFormatGrayScale8: "Grayscale 8",
LisImgFormatBW1: "BW1",
LisImgFormatBmp: "BMP",
LisImgFormatCiff: "CIFF",
LisImgFormatExif: "EXIF",
LisImgFormatFlashPix: "FlashPix",
LisImgFormatGif: "GIF",
LisImgFormatJpeg: "JPEG",
LisImgFormatPng: "PNG",
LisImgFormatIco: "ICO",
LisImgFormatJpeg2k: "JPEG2K",
LisImgFormatJpeg2kx: "JPEG2KX",
LisImgFormatmMemoryBmp: "Memory BMP", // Windows BMP without header
LisImgFormatPhotoCD: "PhotoCD",
LisImgFormatPict: "Pict",
LisImgFormatTiff: "TIFF",
}
lisUnitNames = map[uint32]string{
LisUnitNone: "None",
LisUnitPixel: "Pixel",
LisUnitBit: "Bit",
LisUnitMM: "MM",
LisUnitDPI: "DPI",
LisUnitPercent: "Percent",
LisUnitMicrosecond: "Microsecond",
}
)
type (
//LisValue is value of option
LisValue struct {
ValType uint32
BoolValue bool
IntValue int
DoubleValue float64
StringValue string
ImgFormat C.enum_lis_img_format
}
//ValueRange define constraints applied to value
ValueRange struct {
MinValue *LisValue
MaxValue *LisValue
Interval *LisValue
}
//ValueList is an array of values
ValueList []*LisValue
//OptionDescriptor is a definition of an scan option
OptionDescriptor struct {
//Name option name / identifier (ex: "source", "resolution", etc).
Name string
//Title Human readable title (usually in English).
Title string
//Desc Human readable description (usually in English).
Desc string
/* Capabilities is the option capabilities.
*
* Bit fields:
* - LIS_CAP_EMULATED
* - LIS_CAP_AUTOMATIC
* - LIS_CAP_HW_SELECT
* - LIS_CAP_SW_SELECT
* - LIS_CAP_INACTIVE */
Capabilities int
// Type of this option.
ValueType C.enum_lis_value_type
// Unit of this value. Only useful for integers and float.
ValueUnit C.enum_lis_unit
Constraint *OptionConstraint
optStruct *C.struct_lis_option_descriptor
}
//OptionConstraint describe restrictions defining the possible values for this option.
OptionConstraint struct {
/*
LIS_CONSTRAINT_NONE = 0, No constraint
LIS_CONSTRAINT_RANGE = 1, Range of values (integers only)
LIS_CONSTRAINT_LIST = 2, The structure 'possible' contains a list of values
*/
ConstraintType int
//If LIS_CONSTRAINT_RANGE.
PossibleRange *ValueRange
//If LIS_CONSTRAINT_LIST.
PossibleList ValueList
}
)
//GetValue obtains value of an option
func (o *OptionDescriptor) GetValue() (*LisValue, error) {
if !o.IsReadable() {
return nil, errors.New("сannot read the option")
}
errProxy := getErrorProxy()
defer releaseErrorProxy(errProxy)
var err error
val := C.lis_option_descriptor_get_value_proxy(o.optStruct, errProxy.GetProxy())
if errProxy.ErrNum() != LisOk {
err = errors.New(errProxy.Error())
return nil, err
}
return NewValue(val, o.ValueType), nil
}
//Print option using fmt
func (o *OptionDescriptor) String() string {
var valStr = ""
if o.IsReadable() {
val, err := o.GetValue()
if err != nil {
valStr = err.Error() + "\n"
} else {
valStr = fmt.Sprintf("Value: %v\n", val)
}
}
return fmt.Sprintf(
"%s (%s;%s)\n"+
"Caps: %v %s\n"+
"Type: %v (%v)\n"+
"Units: %v (%s)\n"+
"Constraint: %v\n",
o.Name, o.Title, o.Desc,
o.Capabilities, o.formatCaps(),
o.ValueType, o.formatType(),
o.ValueUnit, lisUnitNames[o.ValueUnit],
o.formatConstraint()) + valStr
}
func (o *OptionDescriptor) bitTest(mask int) bool {
return (o.Capabilities & mask) == mask
}
//IsReadable indicates that option can be read
func (o *OptionDescriptor) IsReadable() bool {
return !o.bitTest(LisCapInactive)
}
//IsWritable indicates that option can be written
func (o *OptionDescriptor) IsWritable() bool {
return o.bitTest(LisCapSwSelect)
}
func (o *OptionDescriptor) formatCaps() string {
caps := ""
for k, v := range lisCapNames {
if o.bitTest(k) {
caps += v + ","
}
}
if caps == "" {
return "[]"
}
return fmt.Sprintf("[%s]", caps[:len(caps)-1])
}
func (o *OptionDescriptor) formatType() string {
return lisTypeNames[o.ValueType]
}
func (o *OptionDescriptor) formatConstraint() string {
//cons := lisConstraintNames[o.Constraint.ConstraintType]
cons := ""
if o.Constraint.ConstraintType == LisConstraintList {
for _, v := range o.Constraint.PossibleList {
cons += v.String() + ","
}
if len(cons) > 0 {
cons = cons[:len(cons)-1]
}
}
if o.Constraint.ConstraintType == LisConstraintRange {
cons = fmt.Sprintf("min: %v, max: %v, interval: %v",
o.Constraint.PossibleRange.MinValue,
o.Constraint.PossibleRange.MaxValue,
o.Constraint.PossibleRange.Interval)
}
name := lisConstraintNames[o.Constraint.ConstraintType]
if cons == "" {
return name
}
return fmt.Sprintf("%s (%s)", name, cons)
}
//Print prints LisValue depending on type
func (v *LisValue) String() string {
switch v.ValType {
case LisTypeBool:
return fmt.Sprintf("%v", v.BoolValue)
case LisTypeInteger:
return fmt.Sprintf("%v", v.IntValue)
case LisTypeDouble:
return fmt.Sprintf("%v", v.DoubleValue)
case LisTypeString:
return fmt.Sprintf("%v", v.StringValue)
case LisTypeImageFormat:
return fmt.Sprintf("%v", v.ImgFormat)
default:
panic("Unknown value type")
}
}
//NewValue constructs GO LisValue struct from lis_value C-struct
func NewValue(val *C.union_lis_value, typ C.enum_lis_value_type) *LisValue {
var res LisValue
res.ValType = typ
switch res.ValType {
case LisTypeBool:
res.BoolValue = *((*int)(unsafe.Pointer(val))) != 0
case LisTypeInteger:
res.IntValue = int(*((*C.int)(unsafe.Pointer(val))))
case LisTypeDouble:
res.DoubleValue = *((*float64)(unsafe.Pointer(val)))
case LisTypeString:
res.StringValue = C.GoString(*(**C.char)(unsafe.Pointer(val)))
case LisTypeImageFormat:
res.ImgFormat = *(*C.enum_lis_img_format)(unsafe.Pointer(val))
default:
panic("Unknown value type")
}
return &res
}
//NewConstraint costructs Go ValueConstraint struct from C-structs
func NewConstraint(valType C.enum_lis_value_type, conType C.int, conPossible unsafe.Pointer) *OptionConstraint {
con := OptionConstraint{
ConstraintType: int(conType),
}
//conRange *C.struct_lis_value_range, conList *C.struct_lis_value_list
if con.ConstraintType == LisConstraintList {
conList := (*C.struct_lis_value_list)(conPossible)
//fmt.Printf("GO array address: %p\n", conList.values)
//C.lis_value_array_print(valType, conList.values, conList.nb_values)
con.PossibleList = []*LisValue{}
slice := carrToSlice(conList.values, int(conList.nb_values))
for i := range slice {
//C.lis_value_print(valType, &v)
c := NewValue(&slice[i], valType)
//fmt.Printf("GO element %p\n", &slice[i])
//fmt.Println(t)
con.PossibleList = append(con.PossibleList, c)
}
//fmt.Printf("%+v", con.PossibleList)
}
if con.ConstraintType == LisConstraintRange {
conRange := (*C.struct_lis_value_range)(conPossible)
var c ValueRange
c.MinValue = NewValue(&conRange.min, valType)
c.MaxValue = NewValue(&conRange.max, valType)
c.Interval = NewValue(&conRange.interval, valType)
con.PossibleRange = &c
}
return &con
}
func carrToSlice(arr *C.union_lis_value, count int) []C.union_lis_value {
//Apply strong magic to get GO slice backed by C null-terminated array
slice := (*[maxSliceLen]C.union_lis_value)(unsafe.Pointer(arr))[:count:count]
return slice
}