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
, struct option_descriptor
*opt
)
112 return sane_find_option(option_name
, TYPE_INT
, opt
);
115 TW_UINT16
sane_option_probe_mode(TW_UINT16
*current
, TW_UINT32
*choices
, int *count
)
119 struct option_descriptor opt
;
120 TW_UINT16 rc
= sane_find_option("mode", TYPE_STRING
, &opt
);
122 if (rc
!= TWCC_SUCCESS
) return rc
;
123 if (opt
.size
> sizeof(buffer
)) return TWCC_BADVALUE
;
124 rc
= sane_option_get_value( opt
.optno
, buffer
);
125 if (rc
!= TWCC_SUCCESS
) return rc
;
127 if (!strcmp( buffer
, "Lineart" )) *current
= TWPT_BW
;
128 else if (!strcmp( buffer
, "Color" )) *current
= TWPT_RGB
;
129 else if (!strncmp( buffer
, "Gray", 4 )) *current
= TWPT_GRAY
;
132 if (opt
.constraint_type
== CONSTRAINT_STRING_LIST
)
134 for (p
= opt
.constraint
.strings
; *p
; p
+= lstrlenW(p
) + 1)
136 if (!wcscmp( p
, L
"Lineart" )) choices
[(*count
)++] = TWPT_BW
;
137 else if (!wcscmp( p
, L
"Color" )) choices
[(*count
)++] = TWPT_RGB
;
138 else if (!wcsncmp( p
, L
"Gray", 4 )) choices
[(*count
)++] = TWPT_GRAY
;
144 TW_UINT16
sane_option_get_scan_area( int *tlx
, int *tly
, int *brx
, int *bry
)
147 struct option_descriptor opt
;
149 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
150 if ((rc
= sane_option_get_value( opt
.optno
, tlx
)) != TWCC_SUCCESS
) return rc
;
151 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
152 if ((rc
= sane_option_get_value( opt
.optno
, tly
)) != TWCC_SUCCESS
) return rc
;
153 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
154 if ((rc
= sane_option_get_value( opt
.optno
, brx
)) != TWCC_SUCCESS
) return rc
;
155 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
156 if ((rc
= sane_option_get_value( opt
.optno
, bry
)) != TWCC_SUCCESS
) return rc
;
157 if (opt
.unit
!= UNIT_MM
) FIXME( "unsupported unit %u\n", opt
.unit
);
161 TW_UINT16
sane_option_get_max_scan_area( int *tlx
, int *tly
, int *brx
, int *bry
)
164 struct option_descriptor opt
;
166 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
167 *tlx
= opt
.constraint
.range
.min
;
168 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
169 *tly
= opt
.constraint
.range
.min
;
170 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
171 *brx
= opt
.constraint
.range
.max
;
172 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
173 *bry
= opt
.constraint
.range
.max
;
174 if (opt
.unit
!= UNIT_MM
) FIXME( "unsupported unit %u\n", opt
.unit
);
178 TW_UINT16
sane_option_set_scan_area( int tlx
, int tly
, int brx
, int bry
, BOOL
*reload
)
181 struct option_descriptor opt
;
183 if ((rc
= sane_find_option( "tl-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
184 if ((rc
= sane_option_set_value( opt
.optno
, &tlx
, reload
)) != TWCC_SUCCESS
) return rc
;
185 if ((rc
= sane_find_option( "tl-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
186 if ((rc
= sane_option_set_value( opt
.optno
, &tly
, reload
)) != TWCC_SUCCESS
) return rc
;
187 if ((rc
= sane_find_option( "br-x", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
188 if ((rc
= sane_option_set_value( opt
.optno
, &brx
, reload
)) != TWCC_SUCCESS
) return rc
;
189 if ((rc
= sane_find_option( "br-y", TYPE_FIXED
, &opt
)) != TWCC_SUCCESS
) return rc
;
190 if ((rc
= sane_option_set_value( opt
.optno
, &bry
, reload
)) != TWCC_SUCCESS
) return rc
;
194 TW_FIX32
convert_sane_res_to_twain(int res
)
197 res
= MulDiv( res
, 10, 254 ); /* mm -> inch */
198 value
.Whole
= res
/ 65536;
199 value
.Frac
= res
& 0xffff;
203 int convert_twain_res_to_sane( TW_FIX32 res
)
205 return MulDiv( res
.Whole
* 65536 + res
.Frac
, 254, 10 ); /* inch -> mm */
208 TW_UINT16
get_sane_params( struct frame_parameters
*params
)
210 return SANE_CALL( get_params
, params
);