xref: /trunk/main/extensions/source/scanner/sane.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir #include <cstdarg>
31*cdf0e10cSrcweir #include <math.h>
32*cdf0e10cSrcweir #include <osl/file.h>
33*cdf0e10cSrcweir #include <tools/stream.hxx>
34*cdf0e10cSrcweir #include <sane.hxx>
35*cdf0e10cSrcweir #include <dlfcn.h>
36*cdf0e10cSrcweir #include <stdio.h>
37*cdf0e10cSrcweir #include <unistd.h>
38*cdf0e10cSrcweir #include <sys/time.h>
39*cdf0e10cSrcweir #include <sys/types.h>
40*cdf0e10cSrcweir #include <sal/config.h>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
43*cdf0e10cSrcweir #include <stdarg.h>
44*cdf0e10cSrcweir #define dump_state( a, b, c, d ) fprintf( stderr, a, b, c, d );
45*cdf0e10cSrcweir #else
46*cdf0e10cSrcweir #define dump_state( a, b, c, d ) ;
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir inline void dbg_msg( const char* pString, ... )
49*cdf0e10cSrcweir {
50*cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
51*cdf0e10cSrcweir     va_list ap;
52*cdf0e10cSrcweir     va_start( ap, pString );
53*cdf0e10cSrcweir     vfprintf( stderr, pString, ap );
54*cdf0e10cSrcweir     va_end( ap );
55*cdf0e10cSrcweir #else
56*cdf0e10cSrcweir     (void)pString;
57*cdf0e10cSrcweir #endif
58*cdf0e10cSrcweir }
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir #define FAIL_SHUTDOWN_STATE( x, y, z ) \
61*cdf0e10cSrcweir     if( x != SANE_STATUS_GOOD )                             \
62*cdf0e10cSrcweir     {                                                       \
63*cdf0e10cSrcweir         dump_state( "%s returned error %d (%s)\n",          \
64*cdf0e10cSrcweir                  y, x, p_strstatus( x ) );                  \
65*cdf0e10cSrcweir         DeInit();                                           \
66*cdf0e10cSrcweir         return z;                                           \
67*cdf0e10cSrcweir     }
68*cdf0e10cSrcweir 
69*cdf0e10cSrcweir #define FAIL_STATE( x, y, z ) \
70*cdf0e10cSrcweir     if( x != SANE_STATUS_GOOD )                             \
71*cdf0e10cSrcweir     {                                                       \
72*cdf0e10cSrcweir         dump_state( "%s returned error %d (%s)\n",          \
73*cdf0e10cSrcweir                  y, x, p_strstatus( x ) );                  \
74*cdf0e10cSrcweir         return z;                                           \
75*cdf0e10cSrcweir     }
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir #define DUMP_STATE( x, y ) \
78*cdf0e10cSrcweir     if( x != SANE_STATUS_GOOD )                             \
79*cdf0e10cSrcweir     {                                                       \
80*cdf0e10cSrcweir         dump_state( "%s returned error %d (%s)\n",          \
81*cdf0e10cSrcweir                  y, x, p_strstatus( x ) );                  \
82*cdf0e10cSrcweir     }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir #define CHECK_STATE( x, y ) \
85*cdf0e10cSrcweir     if( x != SANE_STATUS_GOOD )                             \
86*cdf0e10cSrcweir     {                                                       \
87*cdf0e10cSrcweir         dump_state( "%s returned error %d (%s)\n",          \
88*cdf0e10cSrcweir                  y, x, p_strstatus( x ) );                  \
89*cdf0e10cSrcweir     }                                                       \
90*cdf0e10cSrcweir     else
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir int             Sane::nRefCount = 0;
93*cdf0e10cSrcweir oslModule       Sane::pSaneLib = 0;
94*cdf0e10cSrcweir SANE_Int        Sane::nVersion = 0;
95*cdf0e10cSrcweir SANE_Device**   Sane::ppDevices = 0;
96*cdf0e10cSrcweir int             Sane::nDevices = 0;
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir SANE_Status     (*Sane::p_init)( SANE_Int*,
99*cdf0e10cSrcweir                                  SANE_Auth_Callback ) = 0;
100*cdf0e10cSrcweir void            (*Sane::p_exit)() = 0;
101*cdf0e10cSrcweir SANE_Status     (*Sane::p_get_devices)( const SANE_Device***,
102*cdf0e10cSrcweir                                         SANE_Bool ) = 0;
103*cdf0e10cSrcweir SANE_Status     (*Sane::p_open)( SANE_String_Const, SANE_Handle ) = 0;
104*cdf0e10cSrcweir void            (*Sane::p_close)( SANE_Handle ) = 0;
105*cdf0e10cSrcweir const SANE_Option_Descriptor* (*Sane::p_get_option_descriptor)(
106*cdf0e10cSrcweir     SANE_Handle, SANE_Int ) = 0;
107*cdf0e10cSrcweir SANE_Status     (*Sane::p_control_option)( SANE_Handle, SANE_Int,
108*cdf0e10cSrcweir                                            SANE_Action, void*,
109*cdf0e10cSrcweir                                            SANE_Int* ) = 0;
110*cdf0e10cSrcweir SANE_Status     (*Sane::p_get_parameters)( SANE_Handle,
111*cdf0e10cSrcweir                                            SANE_Parameters* ) = 0;
112*cdf0e10cSrcweir SANE_Status     (*Sane::p_start)( SANE_Handle ) = 0;
113*cdf0e10cSrcweir SANE_Status     (*Sane::p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
114*cdf0e10cSrcweir                                  SANE_Int* ) = 0;
115*cdf0e10cSrcweir void            (*Sane::p_cancel)( SANE_Handle ) = 0;
116*cdf0e10cSrcweir SANE_Status     (*Sane::p_set_io_mode)( SANE_Handle, SANE_Bool ) = 0;
117*cdf0e10cSrcweir SANE_Status     (*Sane::p_get_select_fd)( SANE_Handle, SANE_Int* ) = 0;
118*cdf0e10cSrcweir SANE_String_Const (*Sane::p_strstatus)( SANE_Status ) = 0;
119*cdf0e10cSrcweir 
120*cdf0e10cSrcweir static sal_Bool bSaneSymbolLoadFailed = sal_False;
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir inline oslGenericFunction Sane::LoadSymbol( const char* pSymbolname )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir     oslGenericFunction pFunction = osl_getAsciiFunctionSymbol( pSaneLib, pSymbolname );
125*cdf0e10cSrcweir     if( ! pFunction )
126*cdf0e10cSrcweir     {
127*cdf0e10cSrcweir         fprintf( stderr, "Could not load symbol %s\n",
128*cdf0e10cSrcweir                  pSymbolname );
129*cdf0e10cSrcweir         bSaneSymbolLoadFailed = sal_True;
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir     return pFunction;
132*cdf0e10cSrcweir }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir SANE_Status Sane::ControlOption( int nOption, SANE_Action nAction,
135*cdf0e10cSrcweir                                  void* pData )
136*cdf0e10cSrcweir {
137*cdf0e10cSrcweir     SANE_Status nStatus = SANE_STATUS_GOOD;
138*cdf0e10cSrcweir     SANE_Int    nInfo = 0;
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     nStatus = p_control_option( maHandle, (SANE_Int)nOption,
141*cdf0e10cSrcweir                                 nAction, pData, &nInfo );
142*cdf0e10cSrcweir     DUMP_STATE( nStatus, "sane_control_option" );
143*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
144*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
145*cdf0e10cSrcweir     {
146*cdf0e10cSrcweir         const char* pAction = "Unknown";
147*cdf0e10cSrcweir         switch( nAction )
148*cdf0e10cSrcweir         {
149*cdf0e10cSrcweir             case SANE_ACTION_GET_VALUE:
150*cdf0e10cSrcweir                 pAction = "SANE_ACTION_GET_VALUE";break;
151*cdf0e10cSrcweir             case SANE_ACTION_SET_VALUE:
152*cdf0e10cSrcweir                 pAction = "SANE_ACTION_SET_VALUE";break;
153*cdf0e10cSrcweir             case SANE_ACTION_SET_AUTO:
154*cdf0e10cSrcweir                 pAction = "SANE_ACTION_SET_AUTO";break;
155*cdf0e10cSrcweir         }
156*cdf0e10cSrcweir         dbg_msg( "Option: \"%s\" action: %s\n",
157*cdf0e10cSrcweir                  ByteString( GetOptionName( nOption ), gsl_getSystemTextEncoding() ).GetBuffer(),
158*cdf0e10cSrcweir                  pAction );
159*cdf0e10cSrcweir     }
160*cdf0e10cSrcweir #endif
161*cdf0e10cSrcweir //  if( nInfo & ( SANE_INFO_RELOAD_OPTIONS | SANE_INFO_RELOAD_PARAMS ) )
162*cdf0e10cSrcweir     if( nInfo &  SANE_INFO_RELOAD_OPTIONS )
163*cdf0e10cSrcweir         ReloadOptions();
164*cdf0e10cSrcweir     return nStatus;
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir Sane::Sane() :
168*cdf0e10cSrcweir         mppOptions( 0 ),
169*cdf0e10cSrcweir         mnOptions( 0 ),
170*cdf0e10cSrcweir         mnDevice( -1 ),
171*cdf0e10cSrcweir         maHandle( 0 )
172*cdf0e10cSrcweir {
173*cdf0e10cSrcweir     if( ! nRefCount || ! pSaneLib )
174*cdf0e10cSrcweir         Init();
175*cdf0e10cSrcweir     nRefCount++;
176*cdf0e10cSrcweir };
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir Sane::~Sane()
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     if( IsOpen() )
181*cdf0e10cSrcweir         Close();
182*cdf0e10cSrcweir     nRefCount--;
183*cdf0e10cSrcweir     if( ! nRefCount && pSaneLib )
184*cdf0e10cSrcweir         DeInit();
185*cdf0e10cSrcweir }
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir void Sane::Init()
188*cdf0e10cSrcweir {
189*cdf0e10cSrcweir     ::rtl::OUString sSaneLibName( ::rtl::OUString::createFromAscii( "libsane" SAL_DLLEXTENSION ) );
190*cdf0e10cSrcweir     pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
191*cdf0e10cSrcweir     if( ! pSaneLib )
192*cdf0e10cSrcweir     {
193*cdf0e10cSrcweir         sSaneLibName = ::rtl::OUString::createFromAscii( "libsane" SAL_DLLEXTENSION ".1" );
194*cdf0e10cSrcweir         pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
195*cdf0e10cSrcweir     }
196*cdf0e10cSrcweir     // try reasonable places that might not be in the library search path
197*cdf0e10cSrcweir     if( ! pSaneLib )
198*cdf0e10cSrcweir     {
199*cdf0e10cSrcweir         ::rtl::OUString sSaneLibSystemPath( ::rtl::OUString::createFromAscii( "/usr/local/lib/libsane" SAL_DLLEXTENSION ) );
200*cdf0e10cSrcweir         osl_getFileURLFromSystemPath( sSaneLibSystemPath.pData, &sSaneLibName.pData );
201*cdf0e10cSrcweir         pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
202*cdf0e10cSrcweir     }
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     if( pSaneLib )
205*cdf0e10cSrcweir     {
206*cdf0e10cSrcweir         bSaneSymbolLoadFailed = sal_False;
207*cdf0e10cSrcweir         p_init = (SANE_Status(*)(SANE_Int*, SANE_Auth_Callback ))
208*cdf0e10cSrcweir             LoadSymbol( "sane_init" );
209*cdf0e10cSrcweir         p_exit = (void(*)())
210*cdf0e10cSrcweir             LoadSymbol( "sane_exit" );
211*cdf0e10cSrcweir         p_get_devices = (SANE_Status(*)(const SANE_Device***,
212*cdf0e10cSrcweir                                         SANE_Bool ))
213*cdf0e10cSrcweir             LoadSymbol( "sane_get_devices" );
214*cdf0e10cSrcweir         p_open = (SANE_Status(*)(SANE_String_Const, SANE_Handle ))
215*cdf0e10cSrcweir             LoadSymbol( "sane_open" );
216*cdf0e10cSrcweir         p_close = (void(*)(SANE_Handle))
217*cdf0e10cSrcweir             LoadSymbol( "sane_close" );
218*cdf0e10cSrcweir         p_get_option_descriptor = (const SANE_Option_Descriptor*(*)(SANE_Handle,
219*cdf0e10cSrcweir                                                               SANE_Int))
220*cdf0e10cSrcweir             LoadSymbol( "sane_get_option_descriptor" );
221*cdf0e10cSrcweir         p_control_option = (SANE_Status(*)(SANE_Handle, SANE_Int,
222*cdf0e10cSrcweir                                            SANE_Action, void*, SANE_Int*))
223*cdf0e10cSrcweir             LoadSymbol( "sane_control_option" );
224*cdf0e10cSrcweir         p_get_parameters = (SANE_Status(*)(SANE_Handle,SANE_Parameters*))
225*cdf0e10cSrcweir             LoadSymbol( "sane_get_parameters" );
226*cdf0e10cSrcweir         p_start = (SANE_Status(*)(SANE_Handle))
227*cdf0e10cSrcweir             LoadSymbol( "sane_start" );
228*cdf0e10cSrcweir         p_read = (SANE_Status(*)(SANE_Handle, SANE_Byte*,
229*cdf0e10cSrcweir                                  SANE_Int, SANE_Int* ))
230*cdf0e10cSrcweir             LoadSymbol( "sane_read" );
231*cdf0e10cSrcweir         p_cancel = (void(*)(SANE_Handle))
232*cdf0e10cSrcweir             LoadSymbol( "sane_cancel" );
233*cdf0e10cSrcweir         p_set_io_mode = (SANE_Status(*)(SANE_Handle, SANE_Bool))
234*cdf0e10cSrcweir             LoadSymbol( "sane_set_io_mode" );
235*cdf0e10cSrcweir         p_get_select_fd = (SANE_Status(*)(SANE_Handle, SANE_Int*))
236*cdf0e10cSrcweir             LoadSymbol( "sane_get_select_fd" );
237*cdf0e10cSrcweir         p_strstatus = (SANE_String_Const(*)(SANE_Status))
238*cdf0e10cSrcweir             LoadSymbol( "sane_strstatus" );
239*cdf0e10cSrcweir         if( bSaneSymbolLoadFailed )
240*cdf0e10cSrcweir             DeInit();
241*cdf0e10cSrcweir         else
242*cdf0e10cSrcweir         {
243*cdf0e10cSrcweir             SANE_Status nStatus = p_init( &nVersion, 0 );
244*cdf0e10cSrcweir             FAIL_SHUTDOWN_STATE( nStatus, "sane_init", );
245*cdf0e10cSrcweir             nStatus = p_get_devices( (const SANE_Device***)&ppDevices,
246*cdf0e10cSrcweir                                      SANE_FALSE );
247*cdf0e10cSrcweir             FAIL_SHUTDOWN_STATE( nStatus, "sane_get_devices", );
248*cdf0e10cSrcweir             for( nDevices = 0 ; ppDevices[ nDevices ]; nDevices++ ) ;
249*cdf0e10cSrcweir         }
250*cdf0e10cSrcweir     }
251*cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
252*cdf0e10cSrcweir     else
253*cdf0e10cSrcweir         fprintf( stderr, "libsane%s could not be opened: %s\n", SAL_DLLEXTENSION,
254*cdf0e10cSrcweir                  dlerror() );
255*cdf0e10cSrcweir #endif
256*cdf0e10cSrcweir }
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir void Sane::DeInit()
259*cdf0e10cSrcweir {
260*cdf0e10cSrcweir     if( pSaneLib )
261*cdf0e10cSrcweir     {
262*cdf0e10cSrcweir         p_exit();
263*cdf0e10cSrcweir         osl_unloadModule( pSaneLib );
264*cdf0e10cSrcweir         pSaneLib = 0;
265*cdf0e10cSrcweir     }
266*cdf0e10cSrcweir }
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir void Sane::ReloadDevices()
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir     if( IsOpen() )
271*cdf0e10cSrcweir         Close();
272*cdf0e10cSrcweir     DeInit();
273*cdf0e10cSrcweir     Init();
274*cdf0e10cSrcweir }
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir void Sane::ReloadOptions()
277*cdf0e10cSrcweir {
278*cdf0e10cSrcweir     if( ! IsOpen() )
279*cdf0e10cSrcweir         return;
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir     SANE_Option_Descriptor* pZero = (SANE_Option_Descriptor*)
282*cdf0e10cSrcweir         p_get_option_descriptor( maHandle, 0 );
283*cdf0e10cSrcweir     SANE_Word pOptions[2];
284*cdf0e10cSrcweir     SANE_Status nStatus = p_control_option( maHandle, 0, SANE_ACTION_GET_VALUE,
285*cdf0e10cSrcweir                                             (void*)pOptions, NULL );
286*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
287*cdf0e10cSrcweir         fprintf( stderr, "Error: sane driver returned %s while reading number of options !\n", p_strstatus( nStatus ) );
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir     mnOptions = pOptions[ 0 ];
290*cdf0e10cSrcweir     if( (size_t)pZero->size > sizeof( SANE_Word ) )
291*cdf0e10cSrcweir         fprintf( stderr, "driver returned numer of options with larger size tha SANE_Word !!!\n" );
292*cdf0e10cSrcweir     if( mppOptions )
293*cdf0e10cSrcweir         delete [] mppOptions;
294*cdf0e10cSrcweir     mppOptions = (const SANE_Option_Descriptor**)new SANE_Option_Descriptor*[ mnOptions ];
295*cdf0e10cSrcweir     mppOptions[ 0 ] = (SANE_Option_Descriptor*)pZero;
296*cdf0e10cSrcweir     for( int i = 1; i < mnOptions; i++ )
297*cdf0e10cSrcweir         mppOptions[ i ] =  (SANE_Option_Descriptor*)
298*cdf0e10cSrcweir             p_get_option_descriptor( maHandle, i );
299*cdf0e10cSrcweir 
300*cdf0e10cSrcweir     CheckConsistency( NULL, sal_True );
301*cdf0e10cSrcweir 
302*cdf0e10cSrcweir     maReloadOptionsLink.Call( this );
303*cdf0e10cSrcweir }
304*cdf0e10cSrcweir 
305*cdf0e10cSrcweir sal_Bool Sane::Open( const char* name )
306*cdf0e10cSrcweir {
307*cdf0e10cSrcweir     int i;
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir     SANE_Status nStatus = p_open( (SANE_String_Const)name, &maHandle );
310*cdf0e10cSrcweir     FAIL_STATE( nStatus, "sane_open", sal_False );
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir     ReloadOptions();
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir     if( mnDevice == -1 )
315*cdf0e10cSrcweir     {
316*cdf0e10cSrcweir         ByteString aDevice( name );
317*cdf0e10cSrcweir         for( i = 0; i < nDevices; i++ )
318*cdf0e10cSrcweir         {
319*cdf0e10cSrcweir             if( aDevice.Equals( ppDevices[i]->name ) )
320*cdf0e10cSrcweir             {
321*cdf0e10cSrcweir                 mnDevice = i;
322*cdf0e10cSrcweir                 break;
323*cdf0e10cSrcweir             }
324*cdf0e10cSrcweir         }
325*cdf0e10cSrcweir     }
326*cdf0e10cSrcweir 
327*cdf0e10cSrcweir     return sal_True;
328*cdf0e10cSrcweir }
329*cdf0e10cSrcweir 
330*cdf0e10cSrcweir sal_Bool Sane::Open( int n )
331*cdf0e10cSrcweir {
332*cdf0e10cSrcweir     if( n >= 0 && n < nDevices )
333*cdf0e10cSrcweir     {
334*cdf0e10cSrcweir         mnDevice = n;
335*cdf0e10cSrcweir         return Open( (char*)ppDevices[n]->name );
336*cdf0e10cSrcweir     }
337*cdf0e10cSrcweir     return sal_False;
338*cdf0e10cSrcweir }
339*cdf0e10cSrcweir 
340*cdf0e10cSrcweir void Sane::Close()
341*cdf0e10cSrcweir {
342*cdf0e10cSrcweir     if( maHandle )
343*cdf0e10cSrcweir     {
344*cdf0e10cSrcweir         p_close( maHandle );
345*cdf0e10cSrcweir         delete [] mppOptions;
346*cdf0e10cSrcweir         mppOptions = 0;
347*cdf0e10cSrcweir         maHandle = 0;
348*cdf0e10cSrcweir         mnDevice = -1;
349*cdf0e10cSrcweir     }
350*cdf0e10cSrcweir }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir int Sane::GetOptionByName( const char* rName )
353*cdf0e10cSrcweir {
354*cdf0e10cSrcweir     int i;
355*cdf0e10cSrcweir     ByteString aOption( rName );
356*cdf0e10cSrcweir     for( i = 0; i < mnOptions; i++ )
357*cdf0e10cSrcweir     {
358*cdf0e10cSrcweir         if( mppOptions[i]->name && aOption.Equals( mppOptions[i]->name ) )
359*cdf0e10cSrcweir             return i;
360*cdf0e10cSrcweir     }
361*cdf0e10cSrcweir     return -1;
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir sal_Bool Sane::GetOptionValue( int n, sal_Bool& rRet )
365*cdf0e10cSrcweir {
366*cdf0e10cSrcweir     if( ! maHandle  ||  mppOptions[n]->type != SANE_TYPE_BOOL )
367*cdf0e10cSrcweir         return sal_False;
368*cdf0e10cSrcweir     SANE_Word nRet;
369*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, &nRet );
370*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
371*cdf0e10cSrcweir         return sal_False;
372*cdf0e10cSrcweir 
373*cdf0e10cSrcweir     rRet = nRet;
374*cdf0e10cSrcweir     return sal_True;
375*cdf0e10cSrcweir }
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir sal_Bool Sane::GetOptionValue( int n, ByteString& rRet )
378*cdf0e10cSrcweir {
379*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
380*cdf0e10cSrcweir     if( ! maHandle  ||  mppOptions[n]->type != SANE_TYPE_STRING )
381*cdf0e10cSrcweir         return sal_False;
382*cdf0e10cSrcweir     char* pRet = new char[mppOptions[n]->size+1];
383*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet );
384*cdf0e10cSrcweir     if( nStatus == SANE_STATUS_GOOD )
385*cdf0e10cSrcweir     {
386*cdf0e10cSrcweir         bSuccess = sal_True;
387*cdf0e10cSrcweir         rRet = pRet;
388*cdf0e10cSrcweir     }
389*cdf0e10cSrcweir     delete [] pRet;
390*cdf0e10cSrcweir     return bSuccess;
391*cdf0e10cSrcweir }
392*cdf0e10cSrcweir 
393*cdf0e10cSrcweir sal_Bool Sane::GetOptionValue( int n, double& rRet, int nElement )
394*cdf0e10cSrcweir {
395*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
396*cdf0e10cSrcweir 
397*cdf0e10cSrcweir     if( ! maHandle  ||  ( mppOptions[n]->type != SANE_TYPE_INT &&
398*cdf0e10cSrcweir                           mppOptions[n]->type != SANE_TYPE_FIXED ) )
399*cdf0e10cSrcweir         return sal_False;
400*cdf0e10cSrcweir 
401*cdf0e10cSrcweir     SANE_Word* pRet = new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)];
402*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet );
403*cdf0e10cSrcweir     if( nStatus == SANE_STATUS_GOOD )
404*cdf0e10cSrcweir     {
405*cdf0e10cSrcweir         bSuccess = sal_True;
406*cdf0e10cSrcweir         if( mppOptions[n]->type == SANE_TYPE_INT )
407*cdf0e10cSrcweir             rRet = (double)pRet[ nElement ];
408*cdf0e10cSrcweir         else
409*cdf0e10cSrcweir             rRet = SANE_UNFIX( pRet[nElement] );
410*cdf0e10cSrcweir     }
411*cdf0e10cSrcweir     delete [] pRet;
412*cdf0e10cSrcweir     return bSuccess;
413*cdf0e10cSrcweir }
414*cdf0e10cSrcweir 
415*cdf0e10cSrcweir sal_Bool Sane::GetOptionValue( int n, double* pSet )
416*cdf0e10cSrcweir {
417*cdf0e10cSrcweir     if( ! maHandle  || ! ( mppOptions[n]->type == SANE_TYPE_FIXED ||
418*cdf0e10cSrcweir                            mppOptions[n]->type == SANE_TYPE_INT ) )
419*cdf0e10cSrcweir         return sal_False;
420*cdf0e10cSrcweir 
421*cdf0e10cSrcweir     SANE_Word* pFixedSet = new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)];
422*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pFixedSet );
423*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
424*cdf0e10cSrcweir     {
425*cdf0e10cSrcweir         delete [] pFixedSet;
426*cdf0e10cSrcweir         return sal_False;
427*cdf0e10cSrcweir     }
428*cdf0e10cSrcweir     for( size_t i = 0; i <mppOptions[n]->size/sizeof(SANE_Word); i++ )
429*cdf0e10cSrcweir     {
430*cdf0e10cSrcweir         if( mppOptions[n]->type == SANE_TYPE_FIXED )
431*cdf0e10cSrcweir             pSet[i] = SANE_UNFIX( pFixedSet[i] );
432*cdf0e10cSrcweir         else
433*cdf0e10cSrcweir             pSet[i] = (double) pFixedSet[i];
434*cdf0e10cSrcweir     }
435*cdf0e10cSrcweir     delete [] pFixedSet;
436*cdf0e10cSrcweir     return sal_True;
437*cdf0e10cSrcweir }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir sal_Bool Sane::SetOptionValue( int n, sal_Bool bSet )
440*cdf0e10cSrcweir {
441*cdf0e10cSrcweir     if( ! maHandle  ||  mppOptions[n]->type != SANE_TYPE_BOOL )
442*cdf0e10cSrcweir         return sal_False;
443*cdf0e10cSrcweir     SANE_Word nRet = bSet ? SANE_TRUE : SANE_FALSE;
444*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, &nRet );
445*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
446*cdf0e10cSrcweir         return sal_False;
447*cdf0e10cSrcweir     return sal_True;
448*cdf0e10cSrcweir }
449*cdf0e10cSrcweir 
450*cdf0e10cSrcweir sal_Bool Sane::SetOptionValue( int n, const String& rSet )
451*cdf0e10cSrcweir {
452*cdf0e10cSrcweir     if( ! maHandle  ||  mppOptions[n]->type != SANE_TYPE_STRING )
453*cdf0e10cSrcweir         return sal_False;
454*cdf0e10cSrcweir     ByteString aSet( rSet, gsl_getSystemTextEncoding() );
455*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, (void*)aSet.GetBuffer() );
456*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
457*cdf0e10cSrcweir         return sal_False;
458*cdf0e10cSrcweir     return sal_True;
459*cdf0e10cSrcweir }
460*cdf0e10cSrcweir 
461*cdf0e10cSrcweir sal_Bool Sane::SetOptionValue( int n, double fSet, int nElement )
462*cdf0e10cSrcweir {
463*cdf0e10cSrcweir     sal_Bool bSuccess = sal_False;
464*cdf0e10cSrcweir 
465*cdf0e10cSrcweir     if( ! maHandle  ||  ( mppOptions[n]->type != SANE_TYPE_INT &&
466*cdf0e10cSrcweir                           mppOptions[n]->type != SANE_TYPE_FIXED ) )
467*cdf0e10cSrcweir         return sal_False;
468*cdf0e10cSrcweir 
469*cdf0e10cSrcweir     SANE_Status nStatus;
470*cdf0e10cSrcweir     if( mppOptions[n]->size/sizeof(SANE_Word) > 1 )
471*cdf0e10cSrcweir     {
472*cdf0e10cSrcweir         SANE_Word* pSet = new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)];
473*cdf0e10cSrcweir         nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pSet );
474*cdf0e10cSrcweir         if( nStatus == SANE_STATUS_GOOD )
475*cdf0e10cSrcweir         {
476*cdf0e10cSrcweir             pSet[nElement] = mppOptions[n]->type == SANE_TYPE_INT ?
477*cdf0e10cSrcweir                 (SANE_Word)fSet : SANE_FIX( fSet );
478*cdf0e10cSrcweir             nStatus = ControlOption(  n, SANE_ACTION_SET_VALUE, pSet );
479*cdf0e10cSrcweir         }
480*cdf0e10cSrcweir         delete [] pSet;
481*cdf0e10cSrcweir     }
482*cdf0e10cSrcweir     else
483*cdf0e10cSrcweir     {
484*cdf0e10cSrcweir         SANE_Word nSetTo =
485*cdf0e10cSrcweir             mppOptions[n]->type == SANE_TYPE_INT ?
486*cdf0e10cSrcweir             (SANE_Word)fSet : SANE_FIX( fSet );
487*cdf0e10cSrcweir 
488*cdf0e10cSrcweir         nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, &nSetTo );
489*cdf0e10cSrcweir         if( nStatus == SANE_STATUS_GOOD )
490*cdf0e10cSrcweir             bSuccess = sal_True;
491*cdf0e10cSrcweir     }
492*cdf0e10cSrcweir     return bSuccess;
493*cdf0e10cSrcweir }
494*cdf0e10cSrcweir 
495*cdf0e10cSrcweir sal_Bool Sane::SetOptionValue( int n, double* pSet )
496*cdf0e10cSrcweir {
497*cdf0e10cSrcweir     if( ! maHandle  ||  ( mppOptions[n]->type != SANE_TYPE_INT &&
498*cdf0e10cSrcweir                           mppOptions[n]->type != SANE_TYPE_FIXED ) )
499*cdf0e10cSrcweir         return sal_False;
500*cdf0e10cSrcweir     SANE_Word* pFixedSet = new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)];
501*cdf0e10cSrcweir     for( size_t i = 0; i < mppOptions[n]->size/sizeof(SANE_Word); i++ )
502*cdf0e10cSrcweir     {
503*cdf0e10cSrcweir         if( mppOptions[n]->type == SANE_TYPE_FIXED )
504*cdf0e10cSrcweir             pFixedSet[i] = SANE_FIX( pSet[i] );
505*cdf0e10cSrcweir         else
506*cdf0e10cSrcweir             pFixedSet[i] = (SANE_Word)pSet[i];
507*cdf0e10cSrcweir     }
508*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, pFixedSet );
509*cdf0e10cSrcweir     delete [] pFixedSet;
510*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
511*cdf0e10cSrcweir         return sal_False;
512*cdf0e10cSrcweir     return sal_True;
513*cdf0e10cSrcweir }
514*cdf0e10cSrcweir 
515*cdf0e10cSrcweir enum FrameStyleType {
516*cdf0e10cSrcweir     FrameStyle_BW, FrameStyle_Gray, FrameStyle_RGB, FrameStyle_Separated
517*cdf0e10cSrcweir };
518*cdf0e10cSrcweir 
519*cdf0e10cSrcweir #define BYTE_BUFFER_SIZE 32768
520*cdf0e10cSrcweir 
521*cdf0e10cSrcweir static inline sal_uInt8 _ReadValue( FILE* fp, int depth )
522*cdf0e10cSrcweir {
523*cdf0e10cSrcweir     if( depth == 16 )
524*cdf0e10cSrcweir     {
525*cdf0e10cSrcweir         sal_uInt16 nWord;
526*cdf0e10cSrcweir         // data always come in native byte order !
527*cdf0e10cSrcweir         // 16 bits is not really supported by backends as of now
528*cdf0e10cSrcweir         // e.g. UMAX Astra 1200S delivers 16 bit but in BIGENDIAN
529*cdf0e10cSrcweir         // against SANE documentation (xscanimage gets the same result
530*cdf0e10cSrcweir         // as we do
531*cdf0e10cSrcweir         fread( &nWord, 1, 2, fp );
532*cdf0e10cSrcweir         return (sal_uInt8)( nWord / 256 );
533*cdf0e10cSrcweir     }
534*cdf0e10cSrcweir     sal_uInt8 nByte;
535*cdf0e10cSrcweir     fread( &nByte, 1, 1, fp );
536*cdf0e10cSrcweir     return nByte;
537*cdf0e10cSrcweir }
538*cdf0e10cSrcweir 
539*cdf0e10cSrcweir sal_Bool Sane::CheckConsistency( const char* pMes, sal_Bool bInit )
540*cdf0e10cSrcweir {
541*cdf0e10cSrcweir     static SANE_Option_Descriptor** pDescArray = NULL;
542*cdf0e10cSrcweir     static SANE_Option_Descriptor*  pZero = NULL;
543*cdf0e10cSrcweir 
544*cdf0e10cSrcweir     if( bInit )
545*cdf0e10cSrcweir     {
546*cdf0e10cSrcweir         pDescArray = (SANE_Option_Descriptor**)mppOptions;
547*cdf0e10cSrcweir         if( mppOptions )
548*cdf0e10cSrcweir             pZero = (SANE_Option_Descriptor*)mppOptions[0];
549*cdf0e10cSrcweir         return sal_True;
550*cdf0e10cSrcweir     }
551*cdf0e10cSrcweir 
552*cdf0e10cSrcweir     sal_Bool bConsistent = sal_True;
553*cdf0e10cSrcweir 
554*cdf0e10cSrcweir     if( pDescArray != mppOptions )
555*cdf0e10cSrcweir         bConsistent = sal_False;
556*cdf0e10cSrcweir     if( pZero != mppOptions[0] )
557*cdf0e10cSrcweir         bConsistent = sal_False;
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir     if( ! bConsistent )
560*cdf0e10cSrcweir         dbg_msg( "Sane is not consistent. (%s)\n", pMes );
561*cdf0e10cSrcweir 
562*cdf0e10cSrcweir     return bConsistent;
563*cdf0e10cSrcweir }
564*cdf0e10cSrcweir 
565*cdf0e10cSrcweir sal_Bool Sane::Start( BitmapTransporter& rBitmap )
566*cdf0e10cSrcweir {
567*cdf0e10cSrcweir     int nStream = 0, nLine = 0, i = 0;
568*cdf0e10cSrcweir     SANE_Parameters aParams;
569*cdf0e10cSrcweir     FrameStyleType eType = FrameStyle_Gray;
570*cdf0e10cSrcweir     sal_Bool bSuccess = sal_True;
571*cdf0e10cSrcweir     sal_Bool bWidthSet = sal_False;
572*cdf0e10cSrcweir 
573*cdf0e10cSrcweir     if( ! maHandle )
574*cdf0e10cSrcweir         return sal_False;
575*cdf0e10cSrcweir 
576*cdf0e10cSrcweir     int nWidthMM    = 0;
577*cdf0e10cSrcweir     int nHeightMM   = 0;
578*cdf0e10cSrcweir     double fTLx, fTLy, fBRx, fBRy, fResl = 0.0;
579*cdf0e10cSrcweir     int nOption;
580*cdf0e10cSrcweir     if( ( nOption = GetOptionByName( "tl-x" ) ) != -1   &&
581*cdf0e10cSrcweir         GetOptionValue( nOption, fTLx, 0 )              &&
582*cdf0e10cSrcweir         GetOptionUnit( nOption ) == SANE_UNIT_MM )
583*cdf0e10cSrcweir     {
584*cdf0e10cSrcweir         if( ( nOption = GetOptionByName( "br-x" ) ) != -1   &&
585*cdf0e10cSrcweir             GetOptionValue( nOption, fBRx, 0 )              &&
586*cdf0e10cSrcweir             GetOptionUnit( nOption ) == SANE_UNIT_MM )
587*cdf0e10cSrcweir         {
588*cdf0e10cSrcweir             nWidthMM = (int)fabs(fBRx - fTLx);
589*cdf0e10cSrcweir         }
590*cdf0e10cSrcweir     }
591*cdf0e10cSrcweir     if( ( nOption = GetOptionByName( "tl-y" ) ) != -1   &&
592*cdf0e10cSrcweir         GetOptionValue( nOption, fTLy, 0 )              &&
593*cdf0e10cSrcweir         GetOptionUnit( nOption ) == SANE_UNIT_MM )
594*cdf0e10cSrcweir     {
595*cdf0e10cSrcweir         if( ( nOption = GetOptionByName( "br-y" ) ) != -1   &&
596*cdf0e10cSrcweir             GetOptionValue( nOption, fBRy, 0 )              &&
597*cdf0e10cSrcweir             GetOptionUnit( nOption ) == SANE_UNIT_MM )
598*cdf0e10cSrcweir         {
599*cdf0e10cSrcweir             nHeightMM = (int)fabs(fBRy - fTLy);
600*cdf0e10cSrcweir         }
601*cdf0e10cSrcweir     }
602*cdf0e10cSrcweir     if( ( nOption = GetOptionByName( "resolution" ) ) != -1 )
603*cdf0e10cSrcweir         GetOptionValue( nOption, fResl );
604*cdf0e10cSrcweir 
605*cdf0e10cSrcweir     sal_uInt8* pBuffer = NULL;
606*cdf0e10cSrcweir 
607*cdf0e10cSrcweir     SANE_Status nStatus = SANE_STATUS_GOOD;
608*cdf0e10cSrcweir 
609*cdf0e10cSrcweir     rBitmap.lock();
610*cdf0e10cSrcweir     SvMemoryStream& aConverter = rBitmap.getStream();
611*cdf0e10cSrcweir     aConverter.Seek( 0 );
612*cdf0e10cSrcweir     aConverter.SetNumberFormatInt( NUMBERFORMAT_INT_LITTLEENDIAN );
613*cdf0e10cSrcweir 
614*cdf0e10cSrcweir     // write bitmap stream header
615*cdf0e10cSrcweir     aConverter << 'B' << 'M';
616*cdf0e10cSrcweir     aConverter << (sal_uInt32) 0;
617*cdf0e10cSrcweir     aConverter << (sal_uInt32) 0;
618*cdf0e10cSrcweir     aConverter << (sal_uInt32) 60;
619*cdf0e10cSrcweir 
620*cdf0e10cSrcweir     // write BITMAPINFOHEADER
621*cdf0e10cSrcweir     aConverter << (sal_uInt32)40;
622*cdf0e10cSrcweir     aConverter << (sal_uInt32)0; // fill in width later
623*cdf0e10cSrcweir     aConverter << (sal_uInt32)0; // fill in height later
624*cdf0e10cSrcweir     aConverter << (sal_uInt16)1;
625*cdf0e10cSrcweir     // create header for 24 bits
626*cdf0e10cSrcweir     // correct later if necessary
627*cdf0e10cSrcweir     aConverter << (sal_uInt16)24;
628*cdf0e10cSrcweir     aConverter << (sal_uInt32)0;
629*cdf0e10cSrcweir     aConverter << (sal_uInt32)0;
630*cdf0e10cSrcweir     aConverter << (sal_uInt32)0;
631*cdf0e10cSrcweir     aConverter << (sal_uInt32)0;
632*cdf0e10cSrcweir     aConverter << (sal_uInt32)0;
633*cdf0e10cSrcweir     aConverter << (sal_uInt32)0;
634*cdf0e10cSrcweir 
635*cdf0e10cSrcweir     for( nStream=0; nStream < 3 && bSuccess ; nStream++ )
636*cdf0e10cSrcweir     {
637*cdf0e10cSrcweir         nStatus = p_start( maHandle );
638*cdf0e10cSrcweir         DUMP_STATE( nStatus, "sane_start" );
639*cdf0e10cSrcweir         CheckConsistency( "sane_start" );
640*cdf0e10cSrcweir         if( nStatus == SANE_STATUS_GOOD )
641*cdf0e10cSrcweir         {
642*cdf0e10cSrcweir             nStatus = p_get_parameters( maHandle, &aParams );
643*cdf0e10cSrcweir             DUMP_STATE( nStatus, "sane_get_parameters" );
644*cdf0e10cSrcweir             CheckConsistency( "sane_get_parameters" );
645*cdf0e10cSrcweir             if (nStatus != SANE_STATUS_GOOD || aParams.bytes_per_line == 0)
646*cdf0e10cSrcweir             {
647*cdf0e10cSrcweir                 bSuccess = sal_False;
648*cdf0e10cSrcweir                 break;
649*cdf0e10cSrcweir             }
650*cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
651*cdf0e10cSrcweir             const char* ppFormats[] = { "SANE_FRAME_GRAY", "SANE_FRAME_RGB",
652*cdf0e10cSrcweir                                   "SANE_FRAME_RED", "SANE_FRAME_GREEN",
653*cdf0e10cSrcweir                                   "SANE_FRAME_BLUE", "Unknown !!!" };
654*cdf0e10cSrcweir             fprintf( stderr, "Parameters for frame %d:\n", nStream );
655*cdf0e10cSrcweir             if( aParams.format < 0 || aParams.format > 4 )
656*cdf0e10cSrcweir                 aParams.format = (SANE_Frame)5;
657*cdf0e10cSrcweir             fprintf( stderr, "format:           %s\n", ppFormats[ (int)aParams.format ] );
658*cdf0e10cSrcweir             fprintf( stderr, "last_frame:       %s\n", aParams.last_frame ? "TRUE" : "FALSE" );
659*cdf0e10cSrcweir             fprintf( stderr, "depth:            %d\n", (int)aParams.depth );
660*cdf0e10cSrcweir             fprintf( stderr, "pixels_per_line:  %d\n", (int)aParams.pixels_per_line );
661*cdf0e10cSrcweir             fprintf( stderr, "bytes_per_line:   %d\n", (int)aParams.bytes_per_line );
662*cdf0e10cSrcweir #endif
663*cdf0e10cSrcweir             if( ! pBuffer )
664*cdf0e10cSrcweir             {
665*cdf0e10cSrcweir                 pBuffer = new sal_uInt8[ BYTE_BUFFER_SIZE < 4*aParams.bytes_per_line ? 4*aParams.bytes_per_line : BYTE_BUFFER_SIZE ];
666*cdf0e10cSrcweir             }
667*cdf0e10cSrcweir 
668*cdf0e10cSrcweir             if( aParams.last_frame )
669*cdf0e10cSrcweir                 nStream=3;
670*cdf0e10cSrcweir 
671*cdf0e10cSrcweir             switch( aParams.format )
672*cdf0e10cSrcweir             {
673*cdf0e10cSrcweir                 case SANE_FRAME_GRAY:
674*cdf0e10cSrcweir                     eType = FrameStyle_Gray;
675*cdf0e10cSrcweir                     if( aParams.depth == 1 )
676*cdf0e10cSrcweir                         eType = FrameStyle_BW;
677*cdf0e10cSrcweir                     break;
678*cdf0e10cSrcweir                 case SANE_FRAME_RGB:
679*cdf0e10cSrcweir                     eType = FrameStyle_RGB;
680*cdf0e10cSrcweir                     break;
681*cdf0e10cSrcweir                 case SANE_FRAME_RED:
682*cdf0e10cSrcweir                 case SANE_FRAME_GREEN:
683*cdf0e10cSrcweir                 case SANE_FRAME_BLUE:
684*cdf0e10cSrcweir                     eType = FrameStyle_Separated;
685*cdf0e10cSrcweir                     break;
686*cdf0e10cSrcweir                 default:
687*cdf0e10cSrcweir                     fprintf( stderr, "Warning: unknown frame style !!!\n" );
688*cdf0e10cSrcweir             }
689*cdf0e10cSrcweir 
690*cdf0e10cSrcweir             sal_Bool bSynchronousRead = sal_True;
691*cdf0e10cSrcweir 
692*cdf0e10cSrcweir             // should be fail safe, but ... ??
693*cdf0e10cSrcweir             nStatus = p_set_io_mode( maHandle, SANE_FALSE );
694*cdf0e10cSrcweir             CheckConsistency( "sane_set_io_mode" );
695*cdf0e10cSrcweir             if( nStatus != SANE_STATUS_GOOD )
696*cdf0e10cSrcweir             {
697*cdf0e10cSrcweir                 bSynchronousRead = sal_False;
698*cdf0e10cSrcweir                 nStatus = p_set_io_mode( maHandle, SANE_TRUE );
699*cdf0e10cSrcweir                 CheckConsistency( "sane_set_io_mode" );
700*cdf0e10cSrcweir #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
701*cdf0e10cSrcweir                 if( nStatus != SANE_STATUS_GOOD )
702*cdf0e10cSrcweir                     // what ?!?
703*cdf0e10cSrcweir                     fprintf( stderr, "Sane::Start: driver is confused\n" );
704*cdf0e10cSrcweir #endif
705*cdf0e10cSrcweir             }
706*cdf0e10cSrcweir 
707*cdf0e10cSrcweir             SANE_Int nLen=0;
708*cdf0e10cSrcweir             SANE_Int fd = 0;
709*cdf0e10cSrcweir 
710*cdf0e10cSrcweir             if( ! bSynchronousRead )
711*cdf0e10cSrcweir             {
712*cdf0e10cSrcweir                 nStatus = p_get_select_fd( maHandle, &fd );
713*cdf0e10cSrcweir                 DUMP_STATE( nStatus, "sane_get_select_fd" );
714*cdf0e10cSrcweir                 CheckConsistency( "sane_get_select_fd" );
715*cdf0e10cSrcweir                 if( nStatus != SANE_STATUS_GOOD )
716*cdf0e10cSrcweir                     bSynchronousRead = sal_True;
717*cdf0e10cSrcweir             }
718*cdf0e10cSrcweir             FILE* pFrame = tmpfile();
719*cdf0e10cSrcweir             if( ! pFrame )
720*cdf0e10cSrcweir             {
721*cdf0e10cSrcweir                 bSuccess = sal_False;
722*cdf0e10cSrcweir                 break;
723*cdf0e10cSrcweir             }
724*cdf0e10cSrcweir             do {
725*cdf0e10cSrcweir                 if( ! bSynchronousRead )
726*cdf0e10cSrcweir                 {
727*cdf0e10cSrcweir                     fd_set fdset;
728*cdf0e10cSrcweir                     struct timeval tv;
729*cdf0e10cSrcweir 
730*cdf0e10cSrcweir                     FD_ZERO( &fdset );
731*cdf0e10cSrcweir                     FD_SET( (int)fd, &fdset );
732*cdf0e10cSrcweir                     tv.tv_sec = 5;
733*cdf0e10cSrcweir                     tv.tv_usec = 0;
734*cdf0e10cSrcweir                     if( select( fd+1, &fdset, NULL, NULL, &tv ) == 0 )
735*cdf0e10cSrcweir                         fprintf( stderr, "Timout on sane_read descriptor\n" );
736*cdf0e10cSrcweir                 }
737*cdf0e10cSrcweir                 nLen = 0;
738*cdf0e10cSrcweir                 nStatus = p_read( maHandle, pBuffer, BYTE_BUFFER_SIZE, &nLen );
739*cdf0e10cSrcweir                 CheckConsistency( "sane_read" );
740*cdf0e10cSrcweir                 if( nLen && ( nStatus == SANE_STATUS_GOOD ||
741*cdf0e10cSrcweir                               nStatus == SANE_STATUS_EOF ) )
742*cdf0e10cSrcweir                 {
743*cdf0e10cSrcweir                     fwrite( pBuffer, 1, nLen, pFrame );
744*cdf0e10cSrcweir                 }
745*cdf0e10cSrcweir                 else
746*cdf0e10cSrcweir                     DUMP_STATE( nStatus, "sane_read" );
747*cdf0e10cSrcweir             } while( nStatus == SANE_STATUS_GOOD );
748*cdf0e10cSrcweir             if( nStatus != SANE_STATUS_EOF )
749*cdf0e10cSrcweir             {
750*cdf0e10cSrcweir                 fclose( pFrame );
751*cdf0e10cSrcweir                 bSuccess = sal_False;
752*cdf0e10cSrcweir                 break;
753*cdf0e10cSrcweir             }
754*cdf0e10cSrcweir 
755*cdf0e10cSrcweir             int nFrameLength = ftell( pFrame );
756*cdf0e10cSrcweir             fseek( pFrame, 0, SEEK_SET );
757*cdf0e10cSrcweir             sal_uInt32 nWidth = (sal_uInt32) aParams.pixels_per_line;
758*cdf0e10cSrcweir             sal_uInt32 nHeight = (sal_uInt32) (nFrameLength / aParams.bytes_per_line);
759*cdf0e10cSrcweir             if( ! bWidthSet )
760*cdf0e10cSrcweir             {
761*cdf0e10cSrcweir                 if( ! fResl )
762*cdf0e10cSrcweir                     fResl = 300; // if all else fails that's a good guess
763*cdf0e10cSrcweir                 if( ! nWidthMM )
764*cdf0e10cSrcweir                     nWidthMM = (int)(((double)nWidth / fResl) * 25.4);
765*cdf0e10cSrcweir                 if( ! nHeightMM )
766*cdf0e10cSrcweir                     nHeightMM = (int)(((double)nHeight / fResl) * 25.4);
767*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
768*cdf0e10cSrcweir                 fprintf( stderr, "set dimensions to (%d, %d) Pixel, (%d, %d) mm, resolution is %lg\n", (int)nWidth, (int)nHeight, (int)nWidthMM, (int)nHeightMM, fResl );
769*cdf0e10cSrcweir #endif
770*cdf0e10cSrcweir 
771*cdf0e10cSrcweir                 aConverter.Seek( 18 );
772*cdf0e10cSrcweir                 aConverter << (sal_uInt32)nWidth;
773*cdf0e10cSrcweir                 aConverter << (sal_uInt32)nHeight;
774*cdf0e10cSrcweir                 aConverter.Seek( 38 );
775*cdf0e10cSrcweir                 aConverter << (sal_uInt32)(1000*nWidth/nWidthMM);
776*cdf0e10cSrcweir                 aConverter << (sal_uInt32)(1000*nHeight/nHeightMM);
777*cdf0e10cSrcweir                 bWidthSet = sal_True;
778*cdf0e10cSrcweir             }
779*cdf0e10cSrcweir             aConverter.Seek(60);
780*cdf0e10cSrcweir 
781*cdf0e10cSrcweir             if( eType == FrameStyle_BW )
782*cdf0e10cSrcweir             {
783*cdf0e10cSrcweir                 aConverter.Seek( 10 );
784*cdf0e10cSrcweir                 aConverter << (sal_uInt32)64;
785*cdf0e10cSrcweir                 aConverter.Seek( 28 );
786*cdf0e10cSrcweir                 aConverter << (sal_uInt16) 1;
787*cdf0e10cSrcweir                 aConverter.Seek( 54 );
788*cdf0e10cSrcweir                 // write color table
789*cdf0e10cSrcweir                 aConverter << (sal_uInt16)0xffff;
790*cdf0e10cSrcweir                 aConverter << (sal_uInt8)0xff;
791*cdf0e10cSrcweir                 aConverter << (sal_uInt8)0;
792*cdf0e10cSrcweir                 aConverter << (sal_uInt32)0;
793*cdf0e10cSrcweir                 aConverter.Seek( 64 );
794*cdf0e10cSrcweir             }
795*cdf0e10cSrcweir             else if( eType == FrameStyle_Gray )
796*cdf0e10cSrcweir             {
797*cdf0e10cSrcweir                 aConverter.Seek( 10 );
798*cdf0e10cSrcweir                 aConverter << (sal_uInt32)1084;
799*cdf0e10cSrcweir                 aConverter.Seek( 28 );
800*cdf0e10cSrcweir                 aConverter << (sal_uInt16) 8;
801*cdf0e10cSrcweir                 aConverter.Seek( 54 );
802*cdf0e10cSrcweir                 // write color table
803*cdf0e10cSrcweir                 for( nLine = 0; nLine < 256; nLine++ )
804*cdf0e10cSrcweir                 {
805*cdf0e10cSrcweir                     aConverter << (sal_uInt8)nLine;
806*cdf0e10cSrcweir                     aConverter << (sal_uInt8)nLine;
807*cdf0e10cSrcweir                     aConverter << (sal_uInt8)nLine;
808*cdf0e10cSrcweir                     aConverter << (sal_uInt8)0;
809*cdf0e10cSrcweir                 }
810*cdf0e10cSrcweir                 aConverter.Seek( 1084 );
811*cdf0e10cSrcweir             }
812*cdf0e10cSrcweir 
813*cdf0e10cSrcweir             for( nLine = nHeight-1;
814*cdf0e10cSrcweir                  nLine >= 0; nLine-- )
815*cdf0e10cSrcweir             {
816*cdf0e10cSrcweir                 fseek( pFrame, nLine * aParams.bytes_per_line, SEEK_SET );
817*cdf0e10cSrcweir                 if( eType == FrameStyle_BW ||
818*cdf0e10cSrcweir                     ( eType == FrameStyle_Gray && aParams.depth == 8 )
819*cdf0e10cSrcweir                     )
820*cdf0e10cSrcweir                 {
821*cdf0e10cSrcweir                     fread( pBuffer, 1, aParams.bytes_per_line, pFrame );
822*cdf0e10cSrcweir                     aConverter.Write( pBuffer, aParams.bytes_per_line );
823*cdf0e10cSrcweir                 }
824*cdf0e10cSrcweir                 else if( eType == FrameStyle_Gray )
825*cdf0e10cSrcweir                 {
826*cdf0e10cSrcweir                     for( i = 0; i < (aParams.pixels_per_line); i++ )
827*cdf0e10cSrcweir                     {
828*cdf0e10cSrcweir                         sal_uInt8 nGray = _ReadValue( pFrame, aParams.depth );
829*cdf0e10cSrcweir                         aConverter << nGray;
830*cdf0e10cSrcweir                     }
831*cdf0e10cSrcweir                 }
832*cdf0e10cSrcweir                 else if( eType == FrameStyle_RGB )
833*cdf0e10cSrcweir                 {
834*cdf0e10cSrcweir                     for( i = 0; i < (aParams.pixels_per_line); i++ )
835*cdf0e10cSrcweir                     {
836*cdf0e10cSrcweir                         sal_uInt8 nRed, nGreen, nBlue;
837*cdf0e10cSrcweir                         nRed    = _ReadValue( pFrame, aParams.depth );
838*cdf0e10cSrcweir                         nGreen  = _ReadValue( pFrame, aParams.depth );
839*cdf0e10cSrcweir                         nBlue   = _ReadValue( pFrame, aParams.depth );
840*cdf0e10cSrcweir                         aConverter << nBlue;
841*cdf0e10cSrcweir                         aConverter << nGreen;
842*cdf0e10cSrcweir                         aConverter << nRed;
843*cdf0e10cSrcweir                     }
844*cdf0e10cSrcweir                 }
845*cdf0e10cSrcweir                 else if( eType == FrameStyle_Separated )
846*cdf0e10cSrcweir                 {
847*cdf0e10cSrcweir                     for( i = 0; i < (aParams.pixels_per_line); i++ )
848*cdf0e10cSrcweir                     {
849*cdf0e10cSrcweir                         sal_uInt8 nValue = _ReadValue( pFrame, aParams.depth );
850*cdf0e10cSrcweir                         switch( aParams.format )
851*cdf0e10cSrcweir                         {
852*cdf0e10cSrcweir                             case SANE_FRAME_RED:
853*cdf0e10cSrcweir                                 aConverter.SeekRel( 2 );
854*cdf0e10cSrcweir                                 aConverter << nValue;
855*cdf0e10cSrcweir                                 break;
856*cdf0e10cSrcweir                             case SANE_FRAME_GREEN:
857*cdf0e10cSrcweir                                 aConverter.SeekRel( 1 );
858*cdf0e10cSrcweir                                 aConverter << nValue;
859*cdf0e10cSrcweir                                 aConverter.SeekRel( 1 );
860*cdf0e10cSrcweir                                 break;
861*cdf0e10cSrcweir                             case SANE_FRAME_BLUE:
862*cdf0e10cSrcweir                                 aConverter << nValue;
863*cdf0e10cSrcweir                                 aConverter.SeekRel( 2 );
864*cdf0e10cSrcweir                                 break;
865*cdf0e10cSrcweir                             case SANE_FRAME_GRAY:
866*cdf0e10cSrcweir                             case SANE_FRAME_RGB:
867*cdf0e10cSrcweir                                 break;
868*cdf0e10cSrcweir                         }
869*cdf0e10cSrcweir                     }
870*cdf0e10cSrcweir                 }
871*cdf0e10cSrcweir                 int nGap = aConverter.Tell() & 3;
872*cdf0e10cSrcweir                 if( nGap )
873*cdf0e10cSrcweir                     aConverter.SeekRel( 4-nGap );
874*cdf0e10cSrcweir             }
875*cdf0e10cSrcweir             fclose( pFrame ); // deletes tmpfile
876*cdf0e10cSrcweir             if( eType != FrameStyle_Separated )
877*cdf0e10cSrcweir                 break;
878*cdf0e10cSrcweir         }
879*cdf0e10cSrcweir         else
880*cdf0e10cSrcweir             bSuccess = sal_False;
881*cdf0e10cSrcweir     }
882*cdf0e10cSrcweir     // get stream length
883*cdf0e10cSrcweir     aConverter.Seek( STREAM_SEEK_TO_END );
884*cdf0e10cSrcweir     int nPos = aConverter.Tell();
885*cdf0e10cSrcweir 
886*cdf0e10cSrcweir     aConverter.Seek( 2 );
887*cdf0e10cSrcweir     aConverter << (sal_uInt32) nPos+1;
888*cdf0e10cSrcweir     aConverter.Seek( 0 );
889*cdf0e10cSrcweir 
890*cdf0e10cSrcweir     rBitmap.unlock();
891*cdf0e10cSrcweir 
892*cdf0e10cSrcweir     if( bSuccess )
893*cdf0e10cSrcweir     {
894*cdf0e10cSrcweir         // only cancel a successful operation
895*cdf0e10cSrcweir         // sane disrupts memory else
896*cdf0e10cSrcweir         p_cancel( maHandle );
897*cdf0e10cSrcweir         CheckConsistency( "sane_cancel" );
898*cdf0e10cSrcweir     }
899*cdf0e10cSrcweir     if( pBuffer )
900*cdf0e10cSrcweir         delete [] pBuffer;
901*cdf0e10cSrcweir 
902*cdf0e10cSrcweir     ReloadOptions();
903*cdf0e10cSrcweir 
904*cdf0e10cSrcweir 
905*cdf0e10cSrcweir     dbg_msg( "Sane::Start returns with %s\n", bSuccess ? "TRUE" : "FALSE" );
906*cdf0e10cSrcweir 
907*cdf0e10cSrcweir     return bSuccess;
908*cdf0e10cSrcweir }
909*cdf0e10cSrcweir 
910*cdf0e10cSrcweir int Sane::GetRange( int n, double*& rpDouble )
911*cdf0e10cSrcweir {
912*cdf0e10cSrcweir     if( mppOptions[n]->constraint_type != SANE_CONSTRAINT_RANGE &&
913*cdf0e10cSrcweir         mppOptions[n]->constraint_type != SANE_CONSTRAINT_WORD_LIST )
914*cdf0e10cSrcweir     {
915*cdf0e10cSrcweir         return -1;
916*cdf0e10cSrcweir     }
917*cdf0e10cSrcweir 
918*cdf0e10cSrcweir     rpDouble = 0;
919*cdf0e10cSrcweir     int nItems, i;
920*cdf0e10cSrcweir     sal_Bool bIsFixed = mppOptions[n]->type == SANE_TYPE_FIXED ? sal_True : sal_False;
921*cdf0e10cSrcweir 
922*cdf0e10cSrcweir     dbg_msg( "Sane::GetRange of option %s ", mppOptions[n]->name );
923*cdf0e10cSrcweir     if(mppOptions[n]->constraint_type == SANE_CONSTRAINT_RANGE )
924*cdf0e10cSrcweir     {
925*cdf0e10cSrcweir         double fMin, fMax, fQuant;
926*cdf0e10cSrcweir         if( bIsFixed )
927*cdf0e10cSrcweir         {
928*cdf0e10cSrcweir             fMin = SANE_UNFIX( mppOptions[n]->constraint.range->min );
929*cdf0e10cSrcweir             fMax = SANE_UNFIX( mppOptions[n]->constraint.range->max );
930*cdf0e10cSrcweir             fQuant = SANE_UNFIX( mppOptions[n]->constraint.range->quant );
931*cdf0e10cSrcweir         }
932*cdf0e10cSrcweir         else
933*cdf0e10cSrcweir         {
934*cdf0e10cSrcweir             fMin = (double)mppOptions[n]->constraint.range->min;
935*cdf0e10cSrcweir             fMax = (double)mppOptions[n]->constraint.range->max;
936*cdf0e10cSrcweir             fQuant = (double)mppOptions[n]->constraint.range->quant;
937*cdf0e10cSrcweir         }
938*cdf0e10cSrcweir         if( fQuant != 0.0 )
939*cdf0e10cSrcweir         {
940*cdf0e10cSrcweir             dbg_msg( "quantum range [ %lg ; %lg ; %lg ]\n",
941*cdf0e10cSrcweir                      fMin, fQuant, fMax );
942*cdf0e10cSrcweir             nItems = (int)((fMax - fMin)/fQuant)+1;
943*cdf0e10cSrcweir             rpDouble = new double[ nItems ];
944*cdf0e10cSrcweir             double fValue = fMin;
945*cdf0e10cSrcweir             for( i = 0; i < nItems; i++, fValue += fQuant )
946*cdf0e10cSrcweir                 rpDouble[i] = fValue;
947*cdf0e10cSrcweir             rpDouble[ nItems-1 ] = fMax;
948*cdf0e10cSrcweir             return nItems;
949*cdf0e10cSrcweir         }
950*cdf0e10cSrcweir         else
951*cdf0e10cSrcweir         {
952*cdf0e10cSrcweir             dbg_msg( "normal range [ %lg %lg ]\n",
953*cdf0e10cSrcweir                      fMin, fMax );
954*cdf0e10cSrcweir             rpDouble = new double[2];
955*cdf0e10cSrcweir             rpDouble[0] = fMin;
956*cdf0e10cSrcweir             rpDouble[1] = fMax;
957*cdf0e10cSrcweir             return 0;
958*cdf0e10cSrcweir         }
959*cdf0e10cSrcweir     }
960*cdf0e10cSrcweir     else
961*cdf0e10cSrcweir     {
962*cdf0e10cSrcweir         nItems = mppOptions[n]->constraint.word_list[0];
963*cdf0e10cSrcweir         rpDouble = new double[nItems];
964*cdf0e10cSrcweir         for( i=0; i<nItems; i++ )
965*cdf0e10cSrcweir         {
966*cdf0e10cSrcweir             rpDouble[i] = bIsFixed ?
967*cdf0e10cSrcweir                 SANE_UNFIX( mppOptions[n]->constraint.word_list[i+1] ) :
968*cdf0e10cSrcweir                 (double)mppOptions[n]->constraint.word_list[i+1];
969*cdf0e10cSrcweir         }
970*cdf0e10cSrcweir         dbg_msg( "wordlist [ %lg ... %lg ]\n",
971*cdf0e10cSrcweir                  rpDouble[ 0 ], rpDouble[ nItems-1 ] );
972*cdf0e10cSrcweir         return nItems;
973*cdf0e10cSrcweir     }
974*cdf0e10cSrcweir }
975*cdf0e10cSrcweir 
976*cdf0e10cSrcweir static const char *ppUnits[] = {
977*cdf0e10cSrcweir     "",
978*cdf0e10cSrcweir     "[Pixel]",
979*cdf0e10cSrcweir     "[Bit]",
980*cdf0e10cSrcweir     "[mm]",
981*cdf0e10cSrcweir     "[DPI]",
982*cdf0e10cSrcweir     "[%]",
983*cdf0e10cSrcweir     "[usec]"
984*cdf0e10cSrcweir };
985*cdf0e10cSrcweir 
986*cdf0e10cSrcweir String Sane::GetOptionUnitName( int n )
987*cdf0e10cSrcweir {
988*cdf0e10cSrcweir     String aText;
989*cdf0e10cSrcweir     SANE_Unit nUnit = mppOptions[n]->unit;
990*cdf0e10cSrcweir     size_t nUnitAsSize = (size_t)nUnit;
991*cdf0e10cSrcweir     if( nUnitAsSize > sizeof( ppUnits )/sizeof( ppUnits[0] ) )
992*cdf0e10cSrcweir         aText = String::CreateFromAscii( "[unknown units]" );
993*cdf0e10cSrcweir     else
994*cdf0e10cSrcweir         aText = String( ppUnits[ nUnit ], gsl_getSystemTextEncoding() );
995*cdf0e10cSrcweir     return aText;
996*cdf0e10cSrcweir }
997*cdf0e10cSrcweir 
998*cdf0e10cSrcweir sal_Bool Sane::ActivateButtonOption( int n )
999*cdf0e10cSrcweir {
1000*cdf0e10cSrcweir     SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, NULL );
1001*cdf0e10cSrcweir     if( nStatus != SANE_STATUS_GOOD )
1002*cdf0e10cSrcweir         return sal_False;
1003*cdf0e10cSrcweir     return sal_True;
1004*cdf0e10cSrcweir }
1005