bump product version to 5.0.4.1
[LibreOffice.git] / extensions / source / scanner / sane.cxx
blob2df5bc75b2ce2001d5bd71a4731d69b4f83fca70
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <cstdarg>
21 #include <math.h>
22 #include <osl/file.h>
23 #include <sal/log.hxx>
24 #include <tools/stream.hxx>
25 #include <unotools/tempfile.hxx>
26 #include <sane.hxx>
27 #include <dlfcn.h>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <sal/config.h>
33 #include <sal/macros.h>
34 #include <boost/scoped_array.hpp>
36 #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
37 #include <stdarg.h>
38 #define dump_state( a, b, c, d ) fprintf( stderr, a, b, c, d );
39 #else
40 #define dump_state( a, b, c, d ) ;
41 #endif
42 inline void dbg_msg( const char* pString, ... )
44 #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
45 va_list ap;
46 va_start( ap, pString );
47 vfprintf( stderr, pString, ap );
48 va_end( ap );
49 #else
50 (void)pString;
51 #endif
54 #define FAIL_SHUTDOWN_STATE( x, y, z ) \
55 if( x != SANE_STATUS_GOOD ) \
56 { \
57 dump_state( "%s returned error %d (%s)\n", \
58 y, x, p_strstatus( x ) ); \
59 DeInit(); \
60 return z; \
63 #define FAIL_STATE( x, y, z ) \
64 if( x != SANE_STATUS_GOOD ) \
65 { \
66 dump_state( "%s returned error %d (%s)\n", \
67 y, x, p_strstatus( x ) ); \
68 return z; \
71 #define DUMP_STATE( x, y ) \
72 if( x != SANE_STATUS_GOOD ) \
73 { \
74 dump_state( "%s returned error %d (%s)\n", \
75 y, x, p_strstatus( x ) ); \
78 int Sane::nRefCount = 0;
79 oslModule Sane::pSaneLib = 0;
80 SANE_Int Sane::nVersion = 0;
81 SANE_Device** Sane::ppDevices = 0;
82 int Sane::nDevices = 0;
84 SANE_Status (*Sane::p_init)( SANE_Int*,
85 SANE_Auth_Callback ) = 0;
86 void (*Sane::p_exit)() = 0;
87 SANE_Status (*Sane::p_get_devices)( const SANE_Device***,
88 SANE_Bool ) = 0;
89 SANE_Status (*Sane::p_open)( SANE_String_Const, SANE_Handle ) = 0;
90 void (*Sane::p_close)( SANE_Handle ) = 0;
91 const SANE_Option_Descriptor* (*Sane::p_get_option_descriptor)(
92 SANE_Handle, SANE_Int ) = 0;
93 SANE_Status (*Sane::p_control_option)( SANE_Handle, SANE_Int,
94 SANE_Action, void*,
95 SANE_Int* ) = 0;
96 SANE_Status (*Sane::p_get_parameters)( SANE_Handle,
97 SANE_Parameters* ) = 0;
98 SANE_Status (*Sane::p_start)( SANE_Handle ) = 0;
99 SANE_Status (*Sane::p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
100 SANE_Int* ) = 0;
101 void (*Sane::p_cancel)( SANE_Handle ) = 0;
102 SANE_Status (*Sane::p_set_io_mode)( SANE_Handle, SANE_Bool ) = 0;
103 SANE_Status (*Sane::p_get_select_fd)( SANE_Handle, SANE_Int* ) = 0;
104 SANE_String_Const (*Sane::p_strstatus)( SANE_Status ) = 0;
106 static bool bSaneSymbolLoadFailed = false;
108 inline oslGenericFunction Sane::LoadSymbol( const char* pSymbolname )
110 oslGenericFunction pFunction = osl_getAsciiFunctionSymbol( pSaneLib, pSymbolname );
111 if( ! pFunction )
113 fprintf( stderr, "Could not load symbol %s\n",
114 pSymbolname );
115 bSaneSymbolLoadFailed = true;
117 return pFunction;
120 SANE_Status Sane::ControlOption( int nOption, SANE_Action nAction,
121 void* pData )
123 SANE_Int nInfo = 0;
125 SANE_Status nStatus = p_control_option( maHandle, (SANE_Int)nOption,
126 nAction, pData, &nInfo );
127 DUMP_STATE( nStatus, "sane_control_option" );
128 #if OSL_DEBUG_LEVEL > 1
129 if( nStatus != SANE_STATUS_GOOD )
131 const char* pAction = "Unknown";
132 switch( nAction )
134 case SANE_ACTION_GET_VALUE:
135 pAction = "SANE_ACTION_GET_VALUE";break;
136 case SANE_ACTION_SET_VALUE:
137 pAction = "SANE_ACTION_SET_VALUE";break;
138 case SANE_ACTION_SET_AUTO:
139 pAction = "SANE_ACTION_SET_AUTO";break;
141 dbg_msg( "Option: \"%s\" action: %s\n",
142 OUStringToOString(GetOptionName(nOption), osl_getThreadTextEncoding()).getStr(),
143 pAction );
145 #endif
146 if( nInfo & SANE_INFO_RELOAD_OPTIONS )
147 ReloadOptions();
148 return nStatus;
151 Sane::Sane() :
152 mppOptions( 0 ),
153 mnOptions( 0 ),
154 mnDevice( -1 ),
155 maHandle( 0 )
157 if( ! nRefCount || ! pSaneLib )
158 Init();
159 nRefCount++;
162 Sane::~Sane()
164 if( IsOpen() )
165 Close();
166 nRefCount--;
167 if( ! nRefCount && pSaneLib )
168 DeInit();
171 void Sane::Init()
173 OUString sSaneLibName( "libsane" SAL_DLLEXTENSION );
174 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
175 if( ! pSaneLib )
177 sSaneLibName = "libsane" SAL_DLLEXTENSION ".1";
178 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
180 // try reasonable places that might not be in the library search path
181 if( ! pSaneLib )
183 OUString sSaneLibSystemPath( "/usr/local/lib/libsane" SAL_DLLEXTENSION );
184 osl_getFileURLFromSystemPath( sSaneLibSystemPath.pData, &sSaneLibName.pData );
185 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
188 if( pSaneLib )
190 bSaneSymbolLoadFailed = false;
191 p_init = reinterpret_cast<SANE_Status(*)(SANE_Int*, SANE_Auth_Callback )>(
192 LoadSymbol( "sane_init" ));
193 p_exit = reinterpret_cast<void(*)()>(
194 LoadSymbol( "sane_exit" ));
195 p_get_devices = reinterpret_cast<SANE_Status(*)(const SANE_Device***,
196 SANE_Bool )>(
197 LoadSymbol( "sane_get_devices" ));
198 p_open = reinterpret_cast<SANE_Status(*)(SANE_String_Const, SANE_Handle )>(
199 LoadSymbol( "sane_open" ));
200 p_close = reinterpret_cast<void(*)(SANE_Handle)>(
201 LoadSymbol( "sane_close" ));
202 p_get_option_descriptor = reinterpret_cast<const SANE_Option_Descriptor*(*)(SANE_Handle,
203 SANE_Int)>(
204 LoadSymbol( "sane_get_option_descriptor" ));
205 p_control_option = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Int,
206 SANE_Action, void*, SANE_Int*)>(
207 LoadSymbol( "sane_control_option" ));
208 p_get_parameters = reinterpret_cast<SANE_Status(*)(SANE_Handle,SANE_Parameters*)>(
209 LoadSymbol( "sane_get_parameters" ));
210 p_start = reinterpret_cast<SANE_Status(*)(SANE_Handle)>(
211 LoadSymbol( "sane_start" ));
212 p_read = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Byte*,
213 SANE_Int, SANE_Int* )>(
214 LoadSymbol( "sane_read" ));
215 p_cancel = reinterpret_cast<void(*)(SANE_Handle)>(
216 LoadSymbol( "sane_cancel" ));
217 p_set_io_mode = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Bool)>(
218 LoadSymbol( "sane_set_io_mode" ));
219 p_get_select_fd = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Int*)>(
220 LoadSymbol( "sane_get_select_fd" ));
221 p_strstatus = reinterpret_cast<SANE_String_Const(*)(SANE_Status)>(
222 LoadSymbol( "sane_strstatus" ));
223 if( bSaneSymbolLoadFailed )
224 DeInit();
225 else
227 SANE_Status nStatus = p_init( &nVersion, 0 );
228 FAIL_SHUTDOWN_STATE( nStatus, "sane_init", );
229 nStatus = p_get_devices( (const SANE_Device***)&ppDevices,
230 SANE_FALSE );
231 FAIL_SHUTDOWN_STATE( nStatus, "sane_get_devices", );
232 for( nDevices = 0 ; ppDevices[ nDevices ]; nDevices++ ) ;
235 #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
236 else
237 fprintf( stderr, "libsane%s could not be opened: %s\n", SAL_DLLEXTENSION,
238 dlerror() );
239 #endif
242 void Sane::DeInit()
244 if( pSaneLib )
246 p_exit();
247 osl_unloadModule( pSaneLib );
248 pSaneLib = 0;
252 void Sane::ReloadDevices()
254 if( IsOpen() )
255 Close();
256 DeInit();
257 Init();
260 void Sane::ReloadOptions()
262 if( ! IsOpen() )
263 return;
265 const SANE_Option_Descriptor* pZero = p_get_option_descriptor( maHandle, 0 );
266 SANE_Word pOptions[2];
267 SANE_Status nStatus = p_control_option( maHandle, 0, SANE_ACTION_GET_VALUE,
268 (void*)pOptions, NULL );
269 if( nStatus != SANE_STATUS_GOOD )
270 fprintf( stderr, "Error: sane driver returned %s while reading number of options !\n", p_strstatus( nStatus ) );
272 mnOptions = pOptions[ 0 ];
273 if( (size_t)pZero->size > sizeof( SANE_Word ) )
274 fprintf( stderr, "driver returned numer of options with larger size tha SANE_Word !!!\n" );
275 if( mppOptions )
276 delete [] mppOptions;
277 mppOptions = new const SANE_Option_Descriptor*[ mnOptions ];
278 mppOptions[ 0 ] = pZero;
279 for( int i = 1; i < mnOptions; i++ )
280 mppOptions[ i ] = p_get_option_descriptor( maHandle, i );
282 CheckConsistency( NULL, true );
284 maReloadOptionsLink.Call( this );
287 bool Sane::Open( const char* name )
289 SANE_Status nStatus = p_open( (SANE_String_Const)name, &maHandle );
290 FAIL_STATE( nStatus, "sane_open", false );
292 ReloadOptions();
294 if( mnDevice == -1 )
296 OString aDevice( name );
297 for( int i = 0; i < nDevices; i++ )
299 if( aDevice.equals( ppDevices[i]->name ) )
301 mnDevice = i;
302 break;
307 return true;
310 bool Sane::Open( int n )
312 if( n >= 0 && n < nDevices )
314 mnDevice = n;
315 return Open( ppDevices[n]->name );
317 return false;
320 void Sane::Close()
322 if( maHandle )
324 p_close( maHandle );
325 delete [] mppOptions;
326 mppOptions = 0;
327 maHandle = 0;
328 mnDevice = -1;
332 int Sane::GetOptionByName( const char* rName )
334 int i;
335 OString aOption( rName );
336 for( i = 0; i < mnOptions; i++ )
338 if( mppOptions[i]->name && aOption.equals( mppOptions[i]->name ) )
339 return i;
341 return -1;
344 bool Sane::GetOptionValue( int n, bool& rRet )
346 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
347 return false;
348 SANE_Word nRet;
349 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, &nRet );
350 if( nStatus != SANE_STATUS_GOOD )
351 return false;
353 rRet = nRet;
354 return true;
357 bool Sane::GetOptionValue( int n, OString& rRet )
359 bool bSuccess = false;
360 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
361 return false;
362 boost::scoped_array<char> pRet(new char[mppOptions[n]->size+1]);
363 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet.get() );
364 if( nStatus == SANE_STATUS_GOOD )
366 bSuccess = true;
367 rRet = pRet.get();
369 return bSuccess;
372 bool Sane::GetOptionValue( int n, double& rRet, int nElement )
374 bool bSuccess = false;
376 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
377 mppOptions[n]->type != SANE_TYPE_FIXED ) )
378 return false;
380 boost::scoped_array<SANE_Word> pRet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
381 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet.get() );
382 if( nStatus == SANE_STATUS_GOOD )
384 bSuccess = true;
385 if( mppOptions[n]->type == SANE_TYPE_INT )
386 rRet = (double)pRet[ nElement ];
387 else
388 rRet = SANE_UNFIX( pRet[nElement] );
390 return bSuccess;
393 bool Sane::GetOptionValue( int n, double* pSet )
395 if( ! maHandle || ! ( mppOptions[n]->type == SANE_TYPE_FIXED ||
396 mppOptions[n]->type == SANE_TYPE_INT ) )
397 return false;
399 boost::scoped_array<SANE_Word> pFixedSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
400 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pFixedSet.get() );
401 if( nStatus != SANE_STATUS_GOOD )
402 return false;
403 for( size_t i = 0; i <mppOptions[n]->size/sizeof(SANE_Word); i++ )
405 if( mppOptions[n]->type == SANE_TYPE_FIXED )
406 pSet[i] = SANE_UNFIX( pFixedSet[i] );
407 else
408 pSet[i] = (double) pFixedSet[i];
410 return true;
413 bool Sane::SetOptionValue( int n, bool bSet )
415 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
416 return false;
417 SANE_Word nRet = bSet ? SANE_TRUE : SANE_FALSE;
418 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, &nRet );
419 if( nStatus != SANE_STATUS_GOOD )
420 return false;
421 return true;
424 bool Sane::SetOptionValue( int n, const OUString& rSet )
426 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
427 return false;
428 OString aSet(OUStringToOString(rSet, osl_getThreadTextEncoding()));
429 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, (void*)aSet.getStr() );
430 if( nStatus != SANE_STATUS_GOOD )
431 return false;
432 return true;
435 bool Sane::SetOptionValue( int n, double fSet, int nElement )
437 bool bSuccess = false;
439 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
440 mppOptions[n]->type != SANE_TYPE_FIXED ) )
441 return false;
443 SANE_Status nStatus;
444 if( mppOptions[n]->size/sizeof(SANE_Word) > 1 )
446 boost::scoped_array<SANE_Word> pSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
447 nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pSet.get() );
448 if( nStatus == SANE_STATUS_GOOD )
450 pSet[nElement] = mppOptions[n]->type == SANE_TYPE_INT ?
451 (SANE_Word)fSet : SANE_FIX( fSet );
452 nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, pSet.get() );
455 else
457 SANE_Word nSetTo =
458 mppOptions[n]->type == SANE_TYPE_INT ?
459 (SANE_Word)fSet : SANE_FIX( fSet );
461 nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, &nSetTo );
462 if( nStatus == SANE_STATUS_GOOD )
463 bSuccess = true;
465 return bSuccess;
468 bool Sane::SetOptionValue( int n, double* pSet )
470 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
471 mppOptions[n]->type != SANE_TYPE_FIXED ) )
472 return false;
473 boost::scoped_array<SANE_Word> pFixedSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
474 for( size_t i = 0; i < mppOptions[n]->size/sizeof(SANE_Word); i++ )
476 if( mppOptions[n]->type == SANE_TYPE_FIXED )
477 pFixedSet[i] = SANE_FIX( pSet[i] );
478 else
479 pFixedSet[i] = (SANE_Word)pSet[i];
481 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, pFixedSet.get() );
482 if( nStatus != SANE_STATUS_GOOD )
483 return false;
484 return true;
487 enum FrameStyleType {
488 FrameStyle_BW, FrameStyle_Gray, FrameStyle_RGB, FrameStyle_Separated
491 #define BYTE_BUFFER_SIZE 32768
493 static inline sal_uInt8 _ReadValue( FILE* fp, int depth )
495 if( depth == 16 )
497 sal_uInt16 nWord;
498 // data always come in native byte order !
499 // 16 bits is not really supported by backends as of now
500 // e.g. UMAX Astra 1200S delivers 16 bit but in BIGENDIAN
501 // against SANE documentation (xscanimage gets the same result
502 // as we do
503 size_t items_read = fread( &nWord, 1, 2, fp );
505 if (items_read != 2)
507 SAL_WARN( "extensions.scanner", "short read, abandoning" );
508 return 0;
511 return (sal_uInt8)( nWord / 256 );
513 sal_uInt8 nByte;
514 size_t items_read = fread( &nByte, 1, 1, fp );
515 if (items_read != 1)
517 SAL_WARN( "extensions.scanner", "short read, abandoning" );
518 return 0;
520 return nByte;
523 bool Sane::CheckConsistency( const char* pMes, bool bInit )
525 static const SANE_Option_Descriptor** pDescArray = NULL;
526 static const SANE_Option_Descriptor* pZero = NULL;
528 if( bInit )
530 pDescArray = mppOptions;
531 if( mppOptions )
532 pZero = mppOptions[0];
533 return true;
536 bool bConsistent = true;
538 if( pDescArray != mppOptions )
539 bConsistent = false;
540 if( pZero != mppOptions[0] )
541 bConsistent = false;
543 if( ! bConsistent )
544 dbg_msg( "Sane is not consistent. (%s)\n", pMes );
546 return bConsistent;
549 bool Sane::Start( BitmapTransporter& rBitmap )
551 int nStream = 0, nLine = 0, i = 0;
552 SANE_Parameters aParams;
553 FrameStyleType eType = FrameStyle_Gray;
554 bool bSuccess = true;
555 bool bWidthSet = false;
557 if( ! maHandle )
558 return false;
560 int nWidthMM = 0;
561 int nHeightMM = 0;
562 double fTLx, fTLy, fResl = 0.0;
563 int nOption;
564 if( ( nOption = GetOptionByName( "tl-x" ) ) != -1 &&
565 GetOptionValue( nOption, fTLx, 0 ) &&
566 GetOptionUnit( nOption ) == SANE_UNIT_MM )
568 double fBRx;
569 if( ( nOption = GetOptionByName( "br-x" ) ) != -1 &&
570 GetOptionValue( nOption, fBRx, 0 ) &&
571 GetOptionUnit( nOption ) == SANE_UNIT_MM )
573 nWidthMM = (int)fabs(fBRx - fTLx);
576 if( ( nOption = GetOptionByName( "tl-y" ) ) != -1 &&
577 GetOptionValue( nOption, fTLy, 0 ) &&
578 GetOptionUnit( nOption ) == SANE_UNIT_MM )
580 double fBRy;
581 if( ( nOption = GetOptionByName( "br-y" ) ) != -1 &&
582 GetOptionValue( nOption, fBRy, 0 ) &&
583 GetOptionUnit( nOption ) == SANE_UNIT_MM )
585 nHeightMM = (int)fabs(fBRy - fTLy);
588 if( ( nOption = GetOptionByName( "resolution" ) ) != -1 )
589 (void)GetOptionValue( nOption, fResl );
591 sal_uInt8* pBuffer = NULL;
593 SANE_Status nStatus = SANE_STATUS_GOOD;
595 rBitmap.lock();
596 SvMemoryStream& aConverter = rBitmap.getStream();
597 aConverter.Seek( 0 );
598 aConverter.SetEndian( SvStreamEndian::LITTLE );
600 // write bitmap stream header
601 aConverter.WriteChar( 'B' ).WriteChar( 'M' );
602 aConverter.WriteUInt32( 0 );
603 aConverter.WriteUInt32( 0 );
604 aConverter.WriteUInt32( 60 );
606 // write BITMAPINFOHEADER
607 aConverter.WriteUInt32( 40 );
608 aConverter.WriteUInt32( 0 ); // fill in width later
609 aConverter.WriteUInt32( 0 ); // fill in height later
610 aConverter.WriteUInt16( 1 );
611 // create header for 24 bits
612 // correct later if necessary
613 aConverter.WriteUInt16( 24 );
614 aConverter.WriteUInt32( 0 );
615 aConverter.WriteUInt32( 0 );
616 aConverter.WriteUInt32( 0 );
617 aConverter.WriteUInt32( 0 );
618 aConverter.WriteUInt32( 0 );
619 aConverter.WriteUInt32( 0 );
621 for( nStream=0; nStream < 3 && bSuccess ; nStream++ )
623 nStatus = p_start( maHandle );
624 DUMP_STATE( nStatus, "sane_start" );
625 CheckConsistency( "sane_start" );
626 if( nStatus == SANE_STATUS_GOOD )
628 nStatus = p_get_parameters( maHandle, &aParams );
629 DUMP_STATE( nStatus, "sane_get_parameters" );
630 CheckConsistency( "sane_get_parameters" );
631 if (nStatus != SANE_STATUS_GOOD || aParams.bytes_per_line == 0)
633 bSuccess = false;
634 break;
636 #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
637 const char* ppFormats[] = { "SANE_FRAME_GRAY", "SANE_FRAME_RGB",
638 "SANE_FRAME_RED", "SANE_FRAME_GREEN",
639 "SANE_FRAME_BLUE", "Unknown !!!" };
640 fprintf( stderr, "Parameters for frame %d:\n", nStream );
641 if( aParams.format < 0 || aParams.format > 4 )
642 aParams.format = (SANE_Frame)5;
643 fprintf( stderr, "format: %s\n", ppFormats[ (int)aParams.format ] );
644 fprintf( stderr, "last_frame: %s\n", aParams.last_frame ? "TRUE" : "FALSE" );
645 fprintf( stderr, "depth: %d\n", (int)aParams.depth );
646 fprintf( stderr, "pixels_per_line: %d\n", (int)aParams.pixels_per_line );
647 fprintf( stderr, "bytes_per_line: %d\n", (int)aParams.bytes_per_line );
648 #endif
649 if( ! pBuffer )
651 pBuffer = new sal_uInt8[ BYTE_BUFFER_SIZE < 4*aParams.bytes_per_line ? 4*aParams.bytes_per_line : BYTE_BUFFER_SIZE ];
654 if( aParams.last_frame )
655 nStream=3;
657 switch( aParams.format )
659 case SANE_FRAME_GRAY:
660 eType = FrameStyle_Gray;
661 if( aParams.depth == 1 )
662 eType = FrameStyle_BW;
663 break;
664 case SANE_FRAME_RGB:
665 eType = FrameStyle_RGB;
666 break;
667 case SANE_FRAME_RED:
668 case SANE_FRAME_GREEN:
669 case SANE_FRAME_BLUE:
670 eType = FrameStyle_Separated;
671 break;
672 default:
673 fprintf( stderr, "Warning: unknown frame style !!!\n" );
676 bool bSynchronousRead = true;
678 // should be fail safe, but ... ??
679 nStatus = p_set_io_mode( maHandle, SANE_FALSE );
680 CheckConsistency( "sane_set_io_mode" );
681 if( nStatus != SANE_STATUS_GOOD )
683 bSynchronousRead = false;
684 nStatus = p_set_io_mode( maHandle, SANE_TRUE );
685 CheckConsistency( "sane_set_io_mode" );
686 SAL_WARN_IF(nStatus != SANE_STATUS_GOOD, "extensions.scanner", "driver is confused" ); (void)nStatus;
689 SANE_Int nLen=0;
690 SANE_Int fd = 0;
692 if( ! bSynchronousRead )
694 nStatus = p_get_select_fd( maHandle, &fd );
695 DUMP_STATE( nStatus, "sane_get_select_fd" );
696 CheckConsistency( "sane_get_select_fd" );
697 if( nStatus != SANE_STATUS_GOOD )
698 bSynchronousRead = true;
700 utl::TempFile aFrame;
701 aFrame.EnableKillingFile();
702 FILE* pFrame = fopen(OUStringToOString(aFrame.GetFileName(), osl_getThreadTextEncoding()).getStr(), "w+b");
703 if( ! pFrame )
705 bSuccess = false;
706 break;
708 do {
709 if( ! bSynchronousRead )
711 fd_set fdset;
712 struct timeval tv;
714 FD_ZERO( &fdset );
715 FD_SET( (int)fd, &fdset );
716 tv.tv_sec = 5;
717 tv.tv_usec = 0;
718 if( select( fd+1, &fdset, NULL, NULL, &tv ) == 0 )
719 fprintf( stderr, "Timout on sane_read descriptor\n" );
721 nLen = 0;
722 nStatus = p_read( maHandle, pBuffer, BYTE_BUFFER_SIZE, &nLen );
723 CheckConsistency( "sane_read" );
724 if( nLen && ( nStatus == SANE_STATUS_GOOD ||
725 nStatus == SANE_STATUS_EOF ) )
727 bSuccess = (static_cast<size_t>(nLen) == fwrite( pBuffer, 1, nLen, pFrame ));
728 if (!bSuccess)
729 break;
731 else
732 DUMP_STATE( nStatus, "sane_read" );
733 } while( nStatus == SANE_STATUS_GOOD );
734 if (nStatus != SANE_STATUS_EOF || !bSuccess)
736 fclose( pFrame );
737 bSuccess = false;
738 break;
741 int nFrameLength = ftell( pFrame );
742 fseek( pFrame, 0, SEEK_SET );
743 sal_uInt32 nWidth = (sal_uInt32) aParams.pixels_per_line;
744 sal_uInt32 nHeight = (sal_uInt32) (nFrameLength / aParams.bytes_per_line);
745 if( ! bWidthSet )
747 if( ! fResl )
748 fResl = 300; // if all else fails that's a good guess
749 if( ! nWidthMM )
750 nWidthMM = (int)(((double)nWidth / fResl) * 25.4);
751 if( ! nHeightMM )
752 nHeightMM = (int)(((double)nHeight / fResl) * 25.4);
753 #if OSL_DEBUG_LEVEL > 1
754 fprintf( stderr, "set dimensions to (%d, %d) Pixel, (%d, %d) mm, resolution is %lg\n", (int)nWidth, (int)nHeight, (int)nWidthMM, (int)nHeightMM, fResl );
755 #endif
757 aConverter.Seek( 18 );
758 aConverter.WriteUInt32( nWidth );
759 aConverter.WriteUInt32( nHeight );
760 aConverter.Seek( 38 );
761 aConverter.WriteUInt32( 1000*nWidth/nWidthMM );
762 aConverter.WriteUInt32( 1000*nHeight/nHeightMM );
763 bWidthSet = true;
765 aConverter.Seek(60);
767 if( eType == FrameStyle_BW )
769 aConverter.Seek( 10 );
770 aConverter.WriteUInt32( 64 );
771 aConverter.Seek( 28 );
772 aConverter.WriteUInt16( 1 );
773 aConverter.Seek( 54 );
774 // write color table
775 aConverter.WriteUInt16( 0xffff );
776 aConverter.WriteUChar( 0xff );
777 aConverter.WriteUChar( 0 );
778 aConverter.WriteUInt32( 0 );
779 aConverter.Seek( 64 );
781 else if( eType == FrameStyle_Gray )
783 aConverter.Seek( 10 );
784 aConverter.WriteUInt32( 1084 );
785 aConverter.Seek( 28 );
786 aConverter.WriteUInt16( 8 );
787 aConverter.Seek( 54 );
788 // write color table
789 for( nLine = 0; nLine < 256; nLine++ )
791 aConverter.WriteUChar( nLine );
792 aConverter.WriteUChar( nLine );
793 aConverter.WriteUChar( nLine );
794 aConverter.WriteUChar( 0 );
796 aConverter.Seek( 1084 );
799 for (nLine = nHeight-1; nLine >= 0; --nLine)
801 fseek( pFrame, nLine * aParams.bytes_per_line, SEEK_SET );
802 if( eType == FrameStyle_BW ||
803 ( eType == FrameStyle_Gray && aParams.depth == 8 )
806 SANE_Int items_read = fread( pBuffer, 1, aParams.bytes_per_line, pFrame );
807 if (items_read != aParams.bytes_per_line)
809 SAL_WARN( "extensions.scanner", "short read, padding with zeros" );
810 memset(pBuffer + items_read, 0, aParams.bytes_per_line - items_read);
812 aConverter.Write( pBuffer, aParams.bytes_per_line );
814 else if( eType == FrameStyle_Gray )
816 for( i = 0; i < (aParams.pixels_per_line); i++ )
818 sal_uInt8 nGray = _ReadValue( pFrame, aParams.depth );
819 aConverter.WriteUChar( nGray );
822 else if( eType == FrameStyle_RGB )
824 for( i = 0; i < (aParams.pixels_per_line); i++ )
826 sal_uInt8 nRed, nGreen, nBlue;
827 nRed = _ReadValue( pFrame, aParams.depth );
828 nGreen = _ReadValue( pFrame, aParams.depth );
829 nBlue = _ReadValue( pFrame, aParams.depth );
830 aConverter.WriteUChar( nBlue );
831 aConverter.WriteUChar( nGreen );
832 aConverter.WriteUChar( nRed );
835 else if( eType == FrameStyle_Separated )
837 for( i = 0; i < (aParams.pixels_per_line); i++ )
839 sal_uInt8 nValue = _ReadValue( pFrame, aParams.depth );
840 switch( aParams.format )
842 case SANE_FRAME_RED:
843 aConverter.SeekRel( 2 );
844 aConverter.WriteUChar( nValue );
845 break;
846 case SANE_FRAME_GREEN:
847 aConverter.SeekRel( 1 );
848 aConverter.WriteUChar( nValue );
849 aConverter.SeekRel( 1 );
850 break;
851 case SANE_FRAME_BLUE:
852 aConverter.WriteUChar( nValue );
853 aConverter.SeekRel( 2 );
854 break;
855 case SANE_FRAME_GRAY:
856 case SANE_FRAME_RGB:
857 break;
861 int nGap = aConverter.Tell() & 3;
862 if( nGap )
863 aConverter.SeekRel( 4-nGap );
865 fclose( pFrame ); // deletes tmpfile
866 if( eType != FrameStyle_Separated )
867 break;
869 else
870 bSuccess = false;
872 // get stream length
873 aConverter.Seek( STREAM_SEEK_TO_END );
874 int nPos = aConverter.Tell();
876 aConverter.Seek( 2 );
877 aConverter.WriteUInt32( nPos+1 );
878 aConverter.Seek( 0 );
880 rBitmap.unlock();
882 if( bSuccess )
884 // only cancel a successful operation
885 // sane disrupts memory else
886 p_cancel( maHandle );
887 CheckConsistency( "sane_cancel" );
889 if( pBuffer )
890 delete [] pBuffer;
892 ReloadOptions();
895 dbg_msg( "Sane::Start returns with %s\n", bSuccess ? "TRUE" : "FALSE" );
897 return bSuccess;
900 int Sane::GetRange( int n, double*& rpDouble )
902 if( mppOptions[n]->constraint_type != SANE_CONSTRAINT_RANGE &&
903 mppOptions[n]->constraint_type != SANE_CONSTRAINT_WORD_LIST )
905 return -1;
908 rpDouble = 0;
909 int nItems, i;
910 bool bIsFixed = mppOptions[n]->type == SANE_TYPE_FIXED;
912 dbg_msg( "Sane::GetRange of option %s ", mppOptions[n]->name );
913 if(mppOptions[n]->constraint_type == SANE_CONSTRAINT_RANGE )
915 double fMin, fMax, fQuant;
916 if( bIsFixed )
918 fMin = SANE_UNFIX( mppOptions[n]->constraint.range->min );
919 fMax = SANE_UNFIX( mppOptions[n]->constraint.range->max );
920 fQuant = SANE_UNFIX( mppOptions[n]->constraint.range->quant );
922 else
924 fMin = (double)mppOptions[n]->constraint.range->min;
925 fMax = (double)mppOptions[n]->constraint.range->max;
926 fQuant = (double)mppOptions[n]->constraint.range->quant;
928 if( fQuant != 0.0 )
930 dbg_msg( "quantum range [ %lg ; %lg ; %lg ]\n",
931 fMin, fQuant, fMax );
932 nItems = (int)((fMax - fMin)/fQuant)+1;
933 rpDouble = new double[ nItems ];
934 double fValue = fMin;
935 for( i = 0; i < nItems; i++, fValue += fQuant )
936 rpDouble[i] = fValue;
937 rpDouble[ nItems-1 ] = fMax;
938 return nItems;
940 else
942 dbg_msg( "normal range [ %lg %lg ]\n",
943 fMin, fMax );
944 rpDouble = new double[2];
945 rpDouble[0] = fMin;
946 rpDouble[1] = fMax;
947 return 0;
950 else
952 nItems = mppOptions[n]->constraint.word_list[0];
953 rpDouble = new double[nItems];
954 for( i=0; i<nItems; i++ )
956 rpDouble[i] = bIsFixed ?
957 SANE_UNFIX( mppOptions[n]->constraint.word_list[i+1] ) :
958 (double)mppOptions[n]->constraint.word_list[i+1];
960 dbg_msg( "wordlist [ %lg ... %lg ]\n",
961 rpDouble[ 0 ], rpDouble[ nItems-1 ] );
962 return nItems;
966 static const char *ppUnits[] = {
968 "[Pixel]",
969 "[Bit]",
970 "[mm]",
971 "[DPI]",
972 "[%]",
973 "[usec]"
976 OUString Sane::GetOptionUnitName( int n )
978 OUString aText;
979 SANE_Unit nUnit = mppOptions[n]->unit;
980 size_t nUnitAsSize = (size_t)nUnit;
981 if (nUnitAsSize >= SAL_N_ELEMENTS( ppUnits ))
982 aText = "[unknown units]";
983 else
984 aText = OUString( ppUnits[ nUnit ], strlen(ppUnits[ nUnit ]), osl_getThreadTextEncoding() );
985 return aText;
988 bool Sane::ActivateButtonOption( int n )
990 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, NULL );
991 if( nStatus != SANE_STATUS_GOOD )
992 return false;
993 return true;
996 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */