tdf#130857 qt weld: Support mail merge "Server Auth" dialog
[LibreOffice.git] / extensions / source / scanner / sane.cxx
blobe1fde5e5213c958c35e9c9765dd0fc79431dc1e2
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 <type_traits>
21 #include <math.h>
23 #include <o3tl/safeint.hxx>
24 #include <osl/file.h>
25 #include <sal/log.hxx>
26 #include <tools/stream.hxx>
27 #include <unotools/tempfile.hxx>
28 #include "sane.hxx"
29 #include <dlfcn.h>
30 #include <stdio.h>
31 #include <sys/time.h>
32 #include <sal/config.h>
33 #include <sal/macros.h>
34 #include <memory>
36 #if (OSL_DEBUG_LEVEL > 0) || 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 static void dbg_msg( const char* pString, ... )
44 #if (OSL_DEBUG_LEVEL > 0) || 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 = nullptr;
80 SANE_Int Sane::nVersion = 0;
81 SANE_Device** Sane::ppDevices = nullptr;
82 int Sane::nDevices = 0;
84 SANE_Status (*Sane::p_init)( SANE_Int*,
85 SANE_Auth_Callback ) = nullptr;
86 void (*Sane::p_exit)() = nullptr;
87 SANE_Status (*Sane::p_get_devices)( const SANE_Device***,
88 SANE_Bool ) = nullptr;
89 SANE_Status (*Sane::p_open)( SANE_String_Const, SANE_Handle ) = nullptr;
90 void (*Sane::p_close)( SANE_Handle ) = nullptr;
91 const SANE_Option_Descriptor* (*Sane::p_get_option_descriptor)(
92 SANE_Handle, SANE_Int ) = nullptr;
93 SANE_Status (*Sane::p_control_option)( SANE_Handle, SANE_Int,
94 SANE_Action, void*,
95 SANE_Int* ) = nullptr;
96 SANE_Status (*Sane::p_get_parameters)( SANE_Handle,
97 SANE_Parameters* ) = nullptr;
98 SANE_Status (*Sane::p_start)( SANE_Handle ) = nullptr;
99 SANE_Status (*Sane::p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
100 SANE_Int* ) = nullptr;
101 void (*Sane::p_cancel)( SANE_Handle ) = nullptr;
102 SANE_Status (*Sane::p_set_io_mode)( SANE_Handle, SANE_Bool ) = nullptr;
103 SANE_Status (*Sane::p_get_select_fd)( SANE_Handle, SANE_Int* ) = nullptr;
104 SANE_String_Const (*Sane::p_strstatus)( SANE_Status ) = nullptr;
106 static bool bSaneSymbolLoadFailed = false;
108 inline oslGenericFunction Sane::LoadSymbol( const char* pSymbolname )
110 oslGenericFunction pFunction = osl_getAsciiFunctionSymbol( pSaneLib, pSymbolname );
111 if( ! pFunction )
113 SAL_WARN( "extensions.scanner", "Could not load symbol " << pSymbolname );
114 bSaneSymbolLoadFailed = true;
116 return pFunction;
119 SANE_Status Sane::ControlOption( int nOption, SANE_Action nAction,
120 void* pData )
122 SANE_Int nInfo = 0;
124 SANE_Status nStatus = p_control_option( maHandle, static_cast<SANE_Int>(nOption),
125 nAction, pData, &nInfo );
126 DUMP_STATE( nStatus, "sane_control_option" );
127 #if OSL_DEBUG_LEVEL > 0
128 if( nStatus != SANE_STATUS_GOOD )
130 const char* pAction = "Unknown";
131 switch( nAction )
133 case SANE_ACTION_GET_VALUE:
134 pAction = "SANE_ACTION_GET_VALUE";break;
135 case SANE_ACTION_SET_VALUE:
136 pAction = "SANE_ACTION_SET_VALUE";break;
137 case SANE_ACTION_SET_AUTO:
138 pAction = "SANE_ACTION_SET_AUTO";break;
140 dbg_msg( "Option: \"%s\" action: %s\n",
141 OUStringToOString(GetOptionName(nOption), osl_getThreadTextEncoding()).getStr(),
142 pAction );
144 #endif
145 if( nInfo & SANE_INFO_RELOAD_OPTIONS )
146 ReloadOptions();
147 return nStatus;
150 Sane::Sane() :
151 mnOptions( 0 ),
152 mnDevice( -1 ),
153 maHandle( nullptr )
155 if( ! nRefCount || ! pSaneLib )
156 Init();
157 nRefCount++;
160 Sane::~Sane()
162 if( IsOpen() )
163 Close();
164 nRefCount--;
165 if( ! nRefCount && pSaneLib )
166 DeInit();
169 void Sane::Init()
171 #ifndef DISABLE_DYNLOADING
172 OUString sSaneLibName( u"libsane" SAL_DLLEXTENSION ""_ustr );
173 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
174 if( ! pSaneLib )
176 sSaneLibName = "libsane" SAL_DLLEXTENSION ".1";
177 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
179 // try reasonable places that might not be in the library search path
180 if( ! pSaneLib )
182 OUString sSaneLibSystemPath( u"/usr/local/lib/libsane" SAL_DLLEXTENSION ""_ustr );
183 osl_getFileURLFromSystemPath( sSaneLibSystemPath.pData, &sSaneLibName.pData );
184 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
186 #endif
187 if( pSaneLib )
189 bSaneSymbolLoadFailed = false;
190 p_init = reinterpret_cast<SANE_Status(*)(SANE_Int*, SANE_Auth_Callback )>(
191 LoadSymbol( "sane_init" ));
192 p_exit = reinterpret_cast<void(*)()>(
193 LoadSymbol( "sane_exit" ));
194 p_get_devices = reinterpret_cast<SANE_Status(*)(const SANE_Device***,
195 SANE_Bool )>(
196 LoadSymbol( "sane_get_devices" ));
197 p_open = reinterpret_cast<SANE_Status(*)(SANE_String_Const, SANE_Handle )>(
198 LoadSymbol( "sane_open" ));
199 p_close = reinterpret_cast<void(*)(SANE_Handle)>(
200 LoadSymbol( "sane_close" ));
201 p_get_option_descriptor = reinterpret_cast<const SANE_Option_Descriptor*(*)(SANE_Handle,
202 SANE_Int)>(
203 LoadSymbol( "sane_get_option_descriptor" ));
204 p_control_option = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Int,
205 SANE_Action, void*, SANE_Int*)>(
206 LoadSymbol( "sane_control_option" ));
207 p_get_parameters = reinterpret_cast<SANE_Status(*)(SANE_Handle,SANE_Parameters*)>(
208 LoadSymbol( "sane_get_parameters" ));
209 p_start = reinterpret_cast<SANE_Status(*)(SANE_Handle)>(
210 LoadSymbol( "sane_start" ));
211 p_read = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Byte*,
212 SANE_Int, SANE_Int* )>(
213 LoadSymbol( "sane_read" ));
214 p_cancel = reinterpret_cast<void(*)(SANE_Handle)>(
215 LoadSymbol( "sane_cancel" ));
216 p_set_io_mode = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Bool)>(
217 LoadSymbol( "sane_set_io_mode" ));
218 p_get_select_fd = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Int*)>(
219 LoadSymbol( "sane_get_select_fd" ));
220 p_strstatus = reinterpret_cast<SANE_String_Const(*)(SANE_Status)>(
221 LoadSymbol( "sane_strstatus" ));
222 if( bSaneSymbolLoadFailed )
223 DeInit();
224 else
226 SANE_Status nStatus = p_init( &nVersion, nullptr );
227 FAIL_SHUTDOWN_STATE( nStatus, "sane_init", );
228 nStatus = p_get_devices( const_cast<const SANE_Device***>(&ppDevices),
229 SANE_FALSE );
230 FAIL_SHUTDOWN_STATE( nStatus, "sane_get_devices", );
231 for( nDevices = 0 ; ppDevices[ nDevices ]; nDevices++ ) ;
234 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
235 else
236 fprintf( stderr, "libsane%s could not be opened: %s\n", SAL_DLLEXTENSION,
237 dlerror() );
238 #endif
241 void Sane::DeInit()
243 if( pSaneLib )
245 p_exit();
246 #ifndef DISABLE_DYNLOADING
247 osl_unloadModule( pSaneLib );
248 #endif
249 pSaneLib = nullptr;
253 void Sane::ReloadDevices()
255 if( IsOpen() )
256 Close();
257 DeInit();
258 Init();
261 void Sane::ReloadOptions()
263 if( ! IsOpen() )
264 return;
266 const SANE_Option_Descriptor* pZero = p_get_option_descriptor( maHandle, 0 );
267 SANE_Word pOptions[2];
268 SANE_Status nStatus = p_control_option( maHandle, 0, SANE_ACTION_GET_VALUE,
269 static_cast<void*>(pOptions), nullptr );
270 if( nStatus != SANE_STATUS_GOOD )
271 SAL_WARN( "extensions.scanner", "Error: sane driver returned " << p_strstatus( nStatus ) << " while reading number of options !" );
273 mnOptions = pOptions[ 0 ];
274 if( o3tl::make_unsigned(pZero->size) > sizeof( SANE_Word ) )
275 SAL_WARN( "extensions.scanner", "driver returned number of options with larger size than SANE_Word!!!" );
276 mppOptions.reset(new const SANE_Option_Descriptor*[ mnOptions ]);
277 mppOptions[ 0 ] = pZero;
278 for( int i = 1; i < mnOptions; i++ )
279 mppOptions[ i ] = p_get_option_descriptor( maHandle, i );
281 CheckConsistency( nullptr, true );
283 maReloadOptionsLink.Call( *this );
286 bool Sane::Open( const char* name )
288 SANE_Status nStatus = p_open( reinterpret_cast<SANE_String_Const>(name), &maHandle );
289 FAIL_STATE( nStatus, "sane_open", false );
291 ReloadOptions();
293 if( mnDevice == -1 )
295 OString aDevice( name );
296 for( int i = 0; i < nDevices; i++ )
298 if( aDevice == ppDevices[i]->name )
300 mnDevice = i;
301 break;
306 return true;
309 bool Sane::Open( int n )
311 if( n >= 0 && n < nDevices )
313 mnDevice = n;
314 return Open( ppDevices[n]->name );
316 return false;
319 void Sane::Close()
321 if( maHandle )
323 p_close( maHandle );
324 mppOptions.reset();
325 maHandle = nullptr;
326 mnDevice = -1;
330 int Sane::GetOptionByName( const char* rName )
332 int i;
333 OString aOption( rName );
334 for( i = 0; i < mnOptions; i++ )
336 if( mppOptions[i]->name && aOption == mppOptions[i]->name )
337 return i;
339 return -1;
342 bool Sane::GetOptionValue( int n, bool& rRet )
344 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
345 return false;
346 SANE_Word nRet;
347 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, &nRet );
348 if( nStatus != SANE_STATUS_GOOD )
349 return false;
351 rRet = nRet;
352 return true;
355 bool Sane::GetOptionValue( int n, OString& rRet )
357 bool bSuccess = false;
358 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
359 return false;
360 std::unique_ptr<char[]> pRet(new char[mppOptions[n]->size+1]);
361 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet.get() );
362 if( nStatus == SANE_STATUS_GOOD )
364 bSuccess = true;
365 rRet = pRet.get();
367 return bSuccess;
370 bool Sane::GetOptionValue( int n, double& rRet, int nElement )
372 bool bSuccess = false;
374 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
375 mppOptions[n]->type != SANE_TYPE_FIXED ) )
376 return false;
378 std::unique_ptr<SANE_Word[]> pRet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
379 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet.get() );
380 if( nStatus == SANE_STATUS_GOOD )
382 bSuccess = true;
383 if( mppOptions[n]->type == SANE_TYPE_INT )
384 rRet = static_cast<double>(pRet[ nElement ]);
385 else
386 rRet = SANE_UNFIX( pRet[nElement] );
388 return bSuccess;
391 bool Sane::GetOptionValue( int n, double* pSet )
393 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_FIXED &&
394 mppOptions[n]->type != SANE_TYPE_INT ) )
395 return false;
397 std::unique_ptr<SANE_Word[]> pFixedSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
398 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pFixedSet.get() );
399 if( nStatus != SANE_STATUS_GOOD )
400 return false;
401 for( size_t i = 0; i <mppOptions[n]->size/sizeof(SANE_Word); i++ )
403 if( mppOptions[n]->type == SANE_TYPE_FIXED )
404 pSet[i] = SANE_UNFIX( pFixedSet[i] );
405 else
406 pSet[i] = static_cast<double>(pFixedSet[i]);
408 return true;
411 void Sane::SetOptionValue( int n, bool bSet )
413 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
414 return;
415 SANE_Word nRet = bSet ? SANE_TRUE : SANE_FALSE;
416 ControlOption( n, SANE_ACTION_SET_VALUE, &nRet );
419 void Sane::SetOptionValue( int n, std::u16string_view rSet )
421 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
422 return;
423 OString aSet(OUStringToOString(rSet, osl_getThreadTextEncoding()));
424 ControlOption( n, SANE_ACTION_SET_VALUE, const_cast<char *>(aSet.getStr()) );
427 void Sane::SetOptionValue( int n, double fSet, int nElement )
429 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
430 mppOptions[n]->type != SANE_TYPE_FIXED ) )
431 return;
433 if( mppOptions[n]->size/sizeof(SANE_Word) > 1 )
435 std::unique_ptr<SANE_Word[]> pSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
436 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pSet.get() );
437 if( nStatus == SANE_STATUS_GOOD )
439 pSet[nElement] = mppOptions[n]->type == SANE_TYPE_INT ?
440 static_cast<SANE_Word>(fSet) : SANE_FIX( fSet );
441 ControlOption( n, SANE_ACTION_SET_VALUE, pSet.get() );
444 else
446 SANE_Word nSetTo =
447 mppOptions[n]->type == SANE_TYPE_INT ?
448 static_cast<SANE_Word>(fSet) : SANE_FIX( fSet );
450 ControlOption( n, SANE_ACTION_SET_VALUE, &nSetTo );
454 void Sane::SetOptionValue( int n, double const * pSet )
456 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
457 mppOptions[n]->type != SANE_TYPE_FIXED ) )
458 return;
459 std::unique_ptr<SANE_Word[]> pFixedSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
460 for( size_t i = 0; i < mppOptions[n]->size/sizeof(SANE_Word); i++ )
462 if( mppOptions[n]->type == SANE_TYPE_FIXED )
463 pFixedSet[i] = SANE_FIX( pSet[i] );
464 else
465 pFixedSet[i] = static_cast<SANE_Word>(pSet[i]);
467 ControlOption( n, SANE_ACTION_SET_VALUE, pFixedSet.get() );
470 namespace {
472 enum FrameStyleType {
473 FrameStyle_BW, FrameStyle_Gray, FrameStyle_RGB, FrameStyle_Separated
478 #define BYTE_BUFFER_SIZE 32768
480 static sal_uInt8 ReadValue( FILE* fp, int depth )
482 if( depth == 16 )
484 sal_uInt16 nWord;
485 // data always come in native byte order !
486 // 16 bits is not really supported by backends as of now
487 // e.g. UMAX Astra 1200S delivers 16 bit but in BIGENDIAN
488 // against SANE documentation (xscanimage gets the same result
489 // as we do
490 size_t items_read = fread( &nWord, 1, 2, fp );
492 if (items_read != 2)
494 SAL_WARN( "extensions.scanner", "short read, abandoning" );
495 return 0;
498 return static_cast<sal_uInt8>( nWord / 256 );
500 sal_uInt8 nByte;
501 size_t items_read = fread( &nByte, 1, 1, fp );
502 if (items_read != 1)
504 SAL_WARN( "extensions.scanner", "short read, abandoning" );
505 return 0;
507 return nByte;
510 bool Sane::CheckConsistency( const char* pMes, bool bInit )
512 static const SANE_Option_Descriptor** pDescArray = nullptr;
513 static const SANE_Option_Descriptor* pZero = nullptr;
515 if( bInit )
517 pDescArray = mppOptions.get();
518 if( mppOptions )
519 pZero = mppOptions[0];
520 return true;
523 bool bConsistent = true;
525 if( pDescArray != mppOptions.get() )
526 bConsistent = false;
527 if( pZero != mppOptions[0] )
528 bConsistent = false;
530 if( ! bConsistent )
531 dbg_msg( "Sane is not consistent. (%s)\n", pMes );
533 return bConsistent;
536 bool Sane::Start( BitmapTransporter& rBitmap )
538 int nStream = 0, nLine = 0, i = 0;
539 SANE_Parameters aParams;
540 FrameStyleType eType = FrameStyle_Gray;
541 bool bSuccess = true;
542 bool bWidthSet = false;
544 if( ! maHandle )
545 return false;
547 int nWidthMM = 0;
548 int nHeightMM = 0;
549 double fTLx, fTLy, fResl = 0.0;
550 int nOption;
551 nOption = GetOptionByName( "tl-x" );
552 if( nOption != -1 &&
553 GetOptionValue( nOption, fTLx ) &&
554 GetOptionUnit( nOption ) == SANE_UNIT_MM )
556 double fBRx;
557 nOption = GetOptionByName( "br-x" );
558 if( nOption != -1 &&
559 GetOptionValue( nOption, fBRx ) &&
560 GetOptionUnit( nOption ) == SANE_UNIT_MM )
562 nWidthMM = static_cast<int>(fabs(fBRx - fTLx));
565 nOption = GetOptionByName( "tl-y" );
566 if( nOption != -1 &&
567 GetOptionValue( nOption, fTLy ) &&
568 GetOptionUnit( nOption ) == SANE_UNIT_MM )
570 double fBRy;
571 nOption = GetOptionByName( "br-y" );
572 if( nOption != -1 &&
573 GetOptionValue( nOption, fBRy ) &&
574 GetOptionUnit( nOption ) == SANE_UNIT_MM )
576 nHeightMM = static_cast<int>(fabs(fBRy - fTLy));
579 if( ( nOption = GetOptionByName( "resolution" ) ) != -1 )
580 (void)GetOptionValue( nOption, fResl );
582 std::unique_ptr<sal_uInt8[]> pBuffer;
584 SANE_Status nStatus = SANE_STATUS_GOOD;
586 rBitmap.lock();
587 SvMemoryStream& aConverter = rBitmap.getStream();
588 aConverter.Seek( 0 );
589 aConverter.SetEndian( SvStreamEndian::LITTLE );
591 // write bitmap stream header
592 aConverter.WriteChar( 'B' ).WriteChar( 'M' );
593 aConverter.WriteUInt32( 0 );
594 aConverter.WriteUInt32( 0 );
595 aConverter.WriteUInt32( 60 );
597 // write BITMAPINFOHEADER
598 aConverter.WriteUInt32( 40 );
599 aConverter.WriteUInt32( 0 ); // fill in width later
600 aConverter.WriteUInt32( 0 ); // fill in height later
601 aConverter.WriteUInt16( 1 );
602 // create header for 24 bits
603 // correct later if necessary
604 aConverter.WriteUInt16( 24 );
605 aConverter.WriteUInt32( 0 );
606 aConverter.WriteUInt32( 0 );
607 aConverter.WriteUInt32( 0 );
608 aConverter.WriteUInt32( 0 );
609 aConverter.WriteUInt32( 0 );
610 aConverter.WriteUInt32( 0 );
612 for( nStream=0; nStream < 3 && bSuccess ; nStream++ )
614 nStatus = p_start( maHandle );
615 DUMP_STATE( nStatus, "sane_start" );
616 CheckConsistency( "sane_start" );
617 if( nStatus == SANE_STATUS_GOOD )
619 nStatus = p_get_parameters( maHandle, &aParams );
620 DUMP_STATE( nStatus, "sane_get_parameters" );
621 CheckConsistency( "sane_get_parameters" );
622 if (nStatus != SANE_STATUS_GOOD || aParams.bytes_per_line == 0)
624 bSuccess = false;
625 break;
627 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
628 const char* const ppFormats[] = { "SANE_FRAME_GRAY", "SANE_FRAME_RGB",
629 "SANE_FRAME_RED", "SANE_FRAME_GREEN",
630 "SANE_FRAME_BLUE", "Unknown !!!" };
631 fprintf( stderr, "Parameters for frame %d:\n", nStream );
632 if( static_cast<
633 typename std::make_unsigned<
634 typename std::underlying_type<SANE_Frame>::type>::type>(
635 aParams.format)
636 > 4 )
638 aParams.format = SANE_Frame(5);
640 fprintf( stderr, "format: %s\n", ppFormats[ static_cast<int>(aParams.format) ] );
641 fprintf( stderr, "last_frame: %s\n", aParams.last_frame ? "TRUE" : "FALSE" );
642 fprintf( stderr, "depth: %d\n", static_cast<int>(aParams.depth) );
643 fprintf( stderr, "pixels_per_line: %d\n", static_cast<int>(aParams.pixels_per_line) );
644 fprintf( stderr, "bytes_per_line: %d\n", static_cast<int>(aParams.bytes_per_line) );
645 #endif
646 if( ! pBuffer )
648 pBuffer.reset(new sal_uInt8[ BYTE_BUFFER_SIZE < 4*aParams.bytes_per_line ? 4*aParams.bytes_per_line : BYTE_BUFFER_SIZE ]);
651 if( aParams.last_frame )
652 nStream=3;
654 switch( aParams.format )
656 case SANE_FRAME_GRAY:
657 eType = FrameStyle_Gray;
658 if( aParams.depth == 1 )
659 eType = FrameStyle_BW;
660 break;
661 case SANE_FRAME_RGB:
662 eType = FrameStyle_RGB;
663 break;
664 case SANE_FRAME_RED:
665 case SANE_FRAME_GREEN:
666 case SANE_FRAME_BLUE:
667 eType = FrameStyle_Separated;
668 break;
669 default:
670 SAL_WARN( "extensions.scanner", "Unknown frame style" );
673 bool bSynchronousRead = true;
675 // should be fail safe, but ... ??
676 nStatus = p_set_io_mode( maHandle, SANE_FALSE );
677 CheckConsistency( "sane_set_io_mode" );
678 if( nStatus != SANE_STATUS_GOOD )
680 bSynchronousRead = false;
681 nStatus = p_set_io_mode(maHandle, SANE_TRUE);
682 CheckConsistency( "sane_set_io_mode" );
683 if (nStatus != SANE_STATUS_GOOD)
685 SAL_WARN("extensions.scanner", "SANE driver status is: " << 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::TempFileNamed 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( static_cast<int>(fd), &fdset );
716 tv.tv_sec = 5;
717 tv.tv_usec = 0;
718 if( select( fd+1, &fdset, nullptr, nullptr, &tv ) == 0 )
719 SAL_WARN( "extensions.scanner", "Timeout on sane_read descriptor" );
721 nLen = 0;
722 nStatus = p_read( maHandle, pBuffer.get(), 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.get(), 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 = static_cast<sal_uInt32>(aParams.pixels_per_line);
744 sal_uInt32 nHeight = static_cast<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 = static_cast<int>((static_cast<double>(nWidth) / fResl) * 25.4);
751 if( ! nHeightMM )
752 nHeightMM = static_cast<int>((static_cast<double>(nHeight) / fResl) * 25.4);
753 SAL_INFO("extensions.scanner", "set dimensions to(" << nWidth << ", " << nHeight << ") Pixel, (" << nWidthMM << ", " << nHeightMM <<
754 ") mm, resolution is " << fResl);
756 aConverter.Seek( 18 );
757 aConverter.WriteUInt32( nWidth );
758 aConverter.WriteUInt32( nHeight );
759 aConverter.Seek( 38 );
760 aConverter.WriteUInt32( 1000*nWidth/nWidthMM );
761 aConverter.WriteUInt32( 1000*nHeight/nHeightMM );
762 bWidthSet = true;
764 aConverter.Seek(60);
766 if( eType == FrameStyle_BW )
768 aConverter.Seek( 10 );
769 aConverter.WriteUInt32( 64 );
770 aConverter.Seek( 28 );
771 aConverter.WriteUInt16( 1 );
772 aConverter.Seek( 54 );
773 // write color table
774 aConverter.WriteUInt16( 0xffff );
775 aConverter.WriteUChar( 0xff );
776 aConverter.WriteUChar( 0 );
777 aConverter.WriteUInt32( 0 );
778 aConverter.Seek( 64 );
780 else if( eType == FrameStyle_Gray )
782 aConverter.Seek( 10 );
783 aConverter.WriteUInt32( 1084 );
784 aConverter.Seek( 28 );
785 aConverter.WriteUInt16( 8 );
786 aConverter.Seek( 54 );
787 // write color table
788 for( nLine = 0; nLine < 256; nLine++ )
790 aConverter.WriteUChar( nLine );
791 aConverter.WriteUChar( nLine );
792 aConverter.WriteUChar( nLine );
793 aConverter.WriteUChar( 0 );
795 aConverter.Seek( 1084 );
798 for (nLine = nHeight-1; nLine >= 0; --nLine)
800 if (fseek(pFrame, nLine * aParams.bytes_per_line, SEEK_SET) == -1)
802 bSuccess = false;
803 break;
805 if( eType == FrameStyle_BW ||
806 ( eType == FrameStyle_Gray && aParams.depth == 8 )
809 SANE_Int items_read = fread( pBuffer.get(), 1, aParams.bytes_per_line, pFrame );
810 if (items_read != aParams.bytes_per_line)
812 SAL_WARN( "extensions.scanner", "short read, padding with zeros" );
813 memset(pBuffer.get() + items_read, 0, aParams.bytes_per_line - items_read);
815 aConverter.WriteBytes(pBuffer.get(), aParams.bytes_per_line);
817 else if( eType == FrameStyle_Gray )
819 for( i = 0; i < (aParams.pixels_per_line); i++ )
821 sal_uInt8 nGray = ReadValue( pFrame, aParams.depth );
822 aConverter.WriteUChar( nGray );
825 else if( eType == FrameStyle_RGB )
827 for( i = 0; i < (aParams.pixels_per_line); i++ )
829 sal_uInt8 nRed, nGreen, nBlue;
830 nRed = ReadValue( pFrame, aParams.depth );
831 nGreen = ReadValue( pFrame, aParams.depth );
832 nBlue = ReadValue( pFrame, aParams.depth );
833 aConverter.WriteUChar( nBlue );
834 aConverter.WriteUChar( nGreen );
835 aConverter.WriteUChar( nRed );
838 else if( eType == FrameStyle_Separated )
840 for( i = 0; i < (aParams.pixels_per_line); i++ )
842 sal_uInt8 nValue = ReadValue( pFrame, aParams.depth );
843 switch( aParams.format )
845 case SANE_FRAME_RED:
846 aConverter.SeekRel( 2 );
847 aConverter.WriteUChar( nValue );
848 break;
849 case SANE_FRAME_GREEN:
850 aConverter.SeekRel( 1 );
851 aConverter.WriteUChar( nValue );
852 aConverter.SeekRel( 1 );
853 break;
854 case SANE_FRAME_BLUE:
855 aConverter.WriteUChar( nValue );
856 aConverter.SeekRel( 2 );
857 break;
858 case SANE_FRAME_GRAY:
859 case SANE_FRAME_RGB:
860 break;
864 int nGap = aConverter.Tell() & 3;
865 if (nGap)
866 aConverter.SeekRel( 4-nGap );
868 fclose( pFrame ); // deletes tmpfile
869 if( eType != FrameStyle_Separated )
870 break;
872 else
873 bSuccess = false;
875 // get stream length
876 int nPos = aConverter.TellEnd();
878 aConverter.Seek( 2 );
879 aConverter.WriteUInt32( nPos+1 );
880 aConverter.Seek( 0 );
882 rBitmap.unlock();
884 if( bSuccess )
886 // only cancel a successful operation
887 // sane disrupts memory else
888 p_cancel( maHandle );
889 CheckConsistency( "sane_cancel" );
891 pBuffer.reset();
893 ReloadOptions();
896 dbg_msg( "Sane::Start returns with %s\n", bSuccess ? "TRUE" : "FALSE" );
898 return bSuccess;
901 int Sane::GetRange( int n, std::unique_ptr<double[]>& rpDouble )
903 if( mppOptions[n]->constraint_type != SANE_CONSTRAINT_RANGE &&
904 mppOptions[n]->constraint_type != SANE_CONSTRAINT_WORD_LIST )
906 return -1;
909 rpDouble = nullptr;
910 int nItems, i;
911 bool bIsFixed = mppOptions[n]->type == SANE_TYPE_FIXED;
913 dbg_msg( "Sane::GetRange of option %s ", mppOptions[n]->name );
914 if(mppOptions[n]->constraint_type == SANE_CONSTRAINT_RANGE )
916 double fMin, fMax, fQuant;
917 if( bIsFixed )
919 fMin = SANE_UNFIX( mppOptions[n]->constraint.range->min );
920 fMax = SANE_UNFIX( mppOptions[n]->constraint.range->max );
921 fQuant = SANE_UNFIX( mppOptions[n]->constraint.range->quant );
923 else
925 fMin = static_cast<double>(mppOptions[n]->constraint.range->min);
926 fMax = static_cast<double>(mppOptions[n]->constraint.range->max);
927 fQuant = static_cast<double>(mppOptions[n]->constraint.range->quant);
929 if( fQuant != 0.0 )
931 dbg_msg( "quantum range [ %lg ; %lg ; %lg ]\n",
932 fMin, fQuant, fMax );
933 nItems = static_cast<int>((fMax - fMin)/fQuant)+1;
934 rpDouble.reset(new double[ nItems ]);
935 double fValue = fMin;
936 for( i = 0; i < nItems; i++, fValue += fQuant )
937 rpDouble[i] = fValue;
938 rpDouble[ nItems-1 ] = fMax;
939 return nItems;
941 else
943 dbg_msg( "normal range [ %lg %lg ]\n",
944 fMin, fMax );
945 rpDouble.reset(new double[2]);
946 rpDouble[0] = fMin;
947 rpDouble[1] = fMax;
948 return 0;
951 else
953 nItems = mppOptions[n]->constraint.word_list[0];
954 rpDouble.reset(new double[nItems]);
955 for( i=0; i<nItems; i++ )
957 rpDouble[i] = bIsFixed ?
958 SANE_UNFIX( mppOptions[n]->constraint.word_list[i+1] ) :
959 static_cast<double>(mppOptions[n]->constraint.word_list[i+1]);
961 dbg_msg( "wordlist [ %lg ... %lg ]\n",
962 rpDouble[ 0 ], rpDouble[ nItems-1 ] );
963 return nItems;
967 static const char *ppUnits[] = {
969 "[Pixel]",
970 "[Bit]",
971 "[mm]",
972 "[DPI]",
973 "[%]",
974 "[usec]"
977 OUString Sane::GetOptionUnitName( int n )
979 OUString aText;
980 SANE_Unit nUnit = mppOptions[n]->unit;
981 size_t nUnitAsSize = static_cast<size_t>(nUnit);
982 if (nUnitAsSize >= SAL_N_ELEMENTS( ppUnits ))
983 aText = "[unknown units]";
984 else
985 aText = OUString( ppUnits[ nUnit ], strlen(ppUnits[ nUnit ]), osl_getThreadTextEncoding() );
986 return aText;
989 bool Sane::ActivateButtonOption( int n )
991 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, nullptr );
992 return nStatus == SANE_STATUS_GOOD;
995 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */