2 * Copyright 2009 Jeremy White <jwhite@codeweavers.com> for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(twain
);
29 TW_UINT16
sane_option_get_value( int optno
, void *val
)
31 struct option_get_value_params params
= { optno
, val
};
33 return SANE_CALL( option_get_value
, ¶ms
);
36 TW_UINT16
sane_option_set_value( int optno
, void *val
, BOOL
*reload
)
38 struct option_set_value_params params
= { optno
, val
, reload
};
40 return SANE_CALL( option_set_value
, ¶ms
);
43 static TW_UINT16
sane_find_option( const char *name
, int type
, struct option_descriptor
*descr
)
45 struct option_find_descriptor_params params
= { name
, type
, descr
};
46 return SANE_CALL( option_find_descriptor
, ¶ms
) ? TWCC_CAPUNSUPPORTED
: TWCC_SUCCESS
;
49 TW_UINT16
sane_option_get_int(const char *option_name
, int *val
)
51 struct option_descriptor opt
;
52 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_INT
, &opt
);
54 if (rc
== TWCC_SUCCESS
) rc
= sane_option_get_value( opt
.optno
, val
);
58 TW_UINT16
sane_option_set_int(const char *option_name
, int val
, BOOL
*needs_reload
)
60 struct option_descriptor opt
;
61 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_INT
, &opt
);
63 if (rc
== TWCC_SUCCESS
) rc
= sane_option_set_value( opt
.optno
, &val
, needs_reload
);
67 TW_UINT16
sane_option_get_bool(const char *option_name
, int *val
)
69 struct option_descriptor opt
;
70 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_BOOL
, &opt
);
72 if (rc
== TWCC_SUCCESS
) rc
= sane_option_get_value( opt
.optno
, val
);
76 TW_UINT16
sane_option_set_bool(const char *option_name
, int val
)
78 struct option_descriptor opt
;
79 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_BOOL
, &opt
);
81 if (rc
== TWCC_SUCCESS
) rc
= sane_option_set_value( opt
.optno
, &val
, NULL
);
85 TW_UINT16
sane_option_get_str(const char *option_name
, char *val
, int len
)
87 struct option_descriptor opt
;
88 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_STRING
, &opt
);
90 if (rc
== TWCC_SUCCESS
)
93 rc
= sane_option_get_value( opt
.optno
, val
);
100 /* Important: SANE has the side effect of overwriting val with the returned value */
101 TW_UINT16
sane_option_set_str(const char *option_name
, char *val
, BOOL
*needs_reload
)
103 struct option_descriptor opt
;
104 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_STRING
, &opt
);
106 if (rc
== TWCC_SUCCESS
) rc
= sane_option_set_value( opt
.optno
, &val
, needs_reload
);
110 TW_UINT16
sane_option_probe_resolution(const char *option_name
, int *minval
, int *maxval
, int *quant
)
112 struct option_descriptor opt
;
113 TW_UINT16 rc
= sane_find_option(option_name
, TYPE_INT
, &opt
);
115 if (rc
!= TWCC_SUCCESS
) return rc
;
116 if (opt
.constraint_type
!= CONSTRAINT_RANGE
) return TWCC_CAPUNSUPPORTED
;
118 *minval
= opt
.constraint
.range
.min
;
119 *maxval
= opt
.constraint
.range
.max
;
120 *quant
= opt
.constraint
.range
.quant
;
124 TW_UINT16
sane_option_probe_mode(TW_UINT16
*current
, TW_UINT32
*choices
, int *count
)
128 struct option_descriptor opt
;
129 TW_UINT16 rc
= sane_find_option("mode", TYPE_STRING
, &opt
);
131 if (rc
!= TWCC_SUCCESS
) return rc
;
132 if (opt
.size
> sizeof(buffer
)) return TWCC_BADVALUE
;
133 rc
= sane_option_get_value( opt
.optno
, buffer
);
134 if (rc
!= TWCC_SUCCESS
) return rc
;
136 if (!strcmp( buffer
, "Lineart" )) *current
= TWPT_BW
;
137 else if (!strcmp( buffer
, "Color" )) *current
= TWPT_RGB
;
138 else if (!strncmp( buffer
, "Gray", 4 )) *current
= TWPT_GRAY
;
141 if (opt
.constraint_type
== CONSTRAINT_STRING_LIST
)
143 for (p
= opt
.constraint
.strings
; *p
; p
+= lstrlenW(p
) + 1)
145 if (!wcscmp( p
, L
"Lineart" )) choices
[(*count
)++] = TWPT_BW
;
146 else if (!wcscmp( p
, L
"Color" )) choices
[(*count
)++] = TWPT_RGB
;
147 else if (!wcsncmp( p
, L
"Gray", 4 )) choices
[(*count
)++] = TWPT_GRAY
;
153 TW_UINT16
sane_option_get_scan_area( int *tlx
, int *tly
, int *brx
, int *bry
)
156 struct option_descriptor opt
;
158 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
159 if ((rc
= sane_option_get_value( opt
.optno
, tlx
)) != TWCC_SUCCESS
) return rc
;
160 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
161 if ((rc
= sane_option_get_value( opt
.optno
, tly
)) != TWCC_SUCCESS
) return rc
;
162 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
163 if ((rc
= sane_option_get_value( opt
.optno
, brx
)) != TWCC_SUCCESS
) return rc
;
164 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
165 if ((rc
= sane_option_get_value( opt
.optno
, bry
)) != TWCC_SUCCESS
) return rc
;
166 if (opt
.unit
!= UNIT_MM
) FIXME( "unsupported unit %u\n", opt
.unit
);
170 TW_UINT16
sane_option_get_max_scan_area( int *tlx
, int *tly
, int *brx
, int *bry
)
173 struct option_descriptor opt
;
175 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
176 *tlx
= opt
.constraint
.range
.min
;
177 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
178 *tly
= opt
.constraint
.range
.min
;
179 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
180 *brx
= opt
.constraint
.range
.max
;
181 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
182 *bry
= opt
.constraint
.range
.max
;
183 if (opt
.unit
!= UNIT_MM
) FIXME( "unsupported unit %u\n", opt
.unit
);
187 TW_UINT16
sane_option_set_scan_area( int tlx
, int tly
, int brx
, int bry
, BOOL
*reload
)
190 struct option_descriptor opt
;
192 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
193 if ((rc
= sane_option_set_value( opt
.optno
, &tlx
, reload
)) != TWCC_SUCCESS
) return rc
;
194 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
195 if ((rc
= sane_option_set_value( opt
.optno
, &tly
, reload
)) != TWCC_SUCCESS
) return rc
;
196 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
197 if ((rc
= sane_option_set_value( opt
.optno
, &brx
, reload
)) != TWCC_SUCCESS
) return rc
;
198 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
199 if ((rc
= sane_option_set_value( opt
.optno
, &bry
, reload
)) != TWCC_SUCCESS
) return rc
;
203 TW_FIX32
convert_sane_res_to_twain(int res
)
206 res
= MulDiv( res
, 10, 254 ); /* mm -> inch */
207 value
.Whole
= res
/ 65536;
208 value
.Frac
= res
& 0xffff;
212 TW_UINT16
get_sane_params( struct frame_parameters
*params
)
214 return SANE_CALL( get_params
, params
);