bump product version to 6.3.0.0.beta1
[LibreOffice.git] / extensions / source / scanner / sane.cxx
blob47055a2b09682309f27f2a85ec294a1a122fc363
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 <type_traits>
22 #include <math.h>
23 #include <osl/file.h>
24 #include <sal/log.hxx>
25 #include <tools/stream.hxx>
26 #include <unotools/tempfile.hxx>
27 #include "sane.hxx"
28 #include <dlfcn.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sal/config.h>
34 #include <sal/macros.h>
35 #include <memory>
37 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
38 #include <stdarg.h>
39 #define dump_state( a, b, c, d ) fprintf( stderr, a, b, c, d );
40 #else
41 #define dump_state( a, b, c, d ) ;
42 #endif
43 static void dbg_msg( const char* pString, ... )
45 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
46 va_list ap;
47 va_start( ap, pString );
48 vfprintf( stderr, pString, ap );
49 va_end( ap );
50 #else
51 (void)pString;
52 #endif
55 #define FAIL_SHUTDOWN_STATE( x, y, z ) \
56 if( x != SANE_STATUS_GOOD ) \
57 { \
58 dump_state( "%s returned error %d (%s)\n", \
59 y, x, p_strstatus( x ) ); \
60 DeInit(); \
61 return z; \
64 #define FAIL_STATE( x, y, z ) \
65 if( x != SANE_STATUS_GOOD ) \
66 { \
67 dump_state( "%s returned error %d (%s)\n", \
68 y, x, p_strstatus( x ) ); \
69 return z; \
72 #define DUMP_STATE( x, y ) \
73 if( x != SANE_STATUS_GOOD ) \
74 { \
75 dump_state( "%s returned error %d (%s)\n", \
76 y, x, p_strstatus( x ) ); \
79 int Sane::nRefCount = 0;
80 oslModule Sane::pSaneLib = nullptr;
81 SANE_Int Sane::nVersion = 0;
82 SANE_Device** Sane::ppDevices = nullptr;
83 int Sane::nDevices = 0;
85 SANE_Status (*Sane::p_init)( SANE_Int*,
86 SANE_Auth_Callback ) = nullptr;
87 void (*Sane::p_exit)() = nullptr;
88 SANE_Status (*Sane::p_get_devices)( const SANE_Device***,
89 SANE_Bool ) = nullptr;
90 SANE_Status (*Sane::p_open)( SANE_String_Const, SANE_Handle ) = nullptr;
91 void (*Sane::p_close)( SANE_Handle ) = nullptr;
92 const SANE_Option_Descriptor* (*Sane::p_get_option_descriptor)(
93 SANE_Handle, SANE_Int ) = nullptr;
94 SANE_Status (*Sane::p_control_option)( SANE_Handle, SANE_Int,
95 SANE_Action, void*,
96 SANE_Int* ) = nullptr;
97 SANE_Status (*Sane::p_get_parameters)( SANE_Handle,
98 SANE_Parameters* ) = nullptr;
99 SANE_Status (*Sane::p_start)( SANE_Handle ) = nullptr;
100 SANE_Status (*Sane::p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
101 SANE_Int* ) = nullptr;
102 void (*Sane::p_cancel)( SANE_Handle ) = nullptr;
103 SANE_Status (*Sane::p_set_io_mode)( SANE_Handle, SANE_Bool ) = nullptr;
104 SANE_Status (*Sane::p_get_select_fd)( SANE_Handle, SANE_Int* ) = nullptr;
105 SANE_String_Const (*Sane::p_strstatus)( SANE_Status ) = nullptr;
107 static bool bSaneSymbolLoadFailed = false;
109 inline oslGenericFunction Sane::LoadSymbol( const char* pSymbolname )
111 oslGenericFunction pFunction = osl_getAsciiFunctionSymbol( pSaneLib, pSymbolname );
112 if( ! pFunction )
114 fprintf( stderr, "Could not load symbol %s\n",
115 pSymbolname );
116 bSaneSymbolLoadFailed = true;
118 return pFunction;
121 SANE_Status Sane::ControlOption( int nOption, SANE_Action nAction,
122 void* pData )
124 SANE_Int nInfo = 0;
126 SANE_Status nStatus = p_control_option( maHandle, static_cast<SANE_Int>(nOption),
127 nAction, pData, &nInfo );
128 DUMP_STATE( nStatus, "sane_control_option" );
129 #if OSL_DEBUG_LEVEL > 0
130 if( nStatus != SANE_STATUS_GOOD )
132 const char* pAction = "Unknown";
133 switch( nAction )
135 case SANE_ACTION_GET_VALUE:
136 pAction = "SANE_ACTION_GET_VALUE";break;
137 case SANE_ACTION_SET_VALUE:
138 pAction = "SANE_ACTION_SET_VALUE";break;
139 case SANE_ACTION_SET_AUTO:
140 pAction = "SANE_ACTION_SET_AUTO";break;
142 dbg_msg( "Option: \"%s\" action: %s\n",
143 OUStringToOString(GetOptionName(nOption), osl_getThreadTextEncoding()).getStr(),
144 pAction );
146 #endif
147 if( nInfo & SANE_INFO_RELOAD_OPTIONS )
148 ReloadOptions();
149 return nStatus;
152 Sane::Sane() :
153 mnOptions( 0 ),
154 mnDevice( -1 ),
155 maHandle( nullptr )
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, nullptr );
228 FAIL_SHUTDOWN_STATE( nStatus, "sane_init", );
229 nStatus = p_get_devices( const_cast<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 > 0) || 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 = nullptr;
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 static_cast<void*>(pOptions), nullptr );
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( static_cast<size_t>(pZero->size) > sizeof( SANE_Word ) )
274 fprintf( stderr, "driver returned number of options with larger size than SANE_Word!!!\n" );
275 mppOptions.reset(new const SANE_Option_Descriptor*[ mnOptions ]);
276 mppOptions[ 0 ] = pZero;
277 for( int i = 1; i < mnOptions; i++ )
278 mppOptions[ i ] = p_get_option_descriptor( maHandle, i );
280 CheckConsistency( nullptr, true );
282 maReloadOptionsLink.Call( *this );
285 bool Sane::Open( const char* name )
287 SANE_Status nStatus = p_open( reinterpret_cast<SANE_String_Const>(name), &maHandle );
288 FAIL_STATE( nStatus, "sane_open", false );
290 ReloadOptions();
292 if( mnDevice == -1 )
294 OString aDevice( name );
295 for( int i = 0; i < nDevices; i++ )
297 if( aDevice == ppDevices[i]->name )
299 mnDevice = i;
300 break;
305 return true;
308 bool Sane::Open( int n )
310 if( n >= 0 && n < nDevices )
312 mnDevice = n;
313 return Open( ppDevices[n]->name );
315 return false;
318 void Sane::Close()
320 if( maHandle )
322 p_close( maHandle );
323 mppOptions.reset();
324 maHandle = nullptr;
325 mnDevice = -1;
329 int Sane::GetOptionByName( const char* rName )
331 int i;
332 OString aOption( rName );
333 for( i = 0; i < mnOptions; i++ )
335 if( mppOptions[i]->name && aOption == mppOptions[i]->name )
336 return i;
338 return -1;
341 bool Sane::GetOptionValue( int n, bool& rRet )
343 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
344 return false;
345 SANE_Word nRet;
346 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, &nRet );
347 if( nStatus != SANE_STATUS_GOOD )
348 return false;
350 rRet = nRet;
351 return true;
354 bool Sane::GetOptionValue( int n, OString& rRet )
356 bool bSuccess = false;
357 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
358 return false;
359 std::unique_ptr<char[]> pRet(new char[mppOptions[n]->size+1]);
360 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet.get() );
361 if( nStatus == SANE_STATUS_GOOD )
363 bSuccess = true;
364 rRet = pRet.get();
366 return bSuccess;
369 bool Sane::GetOptionValue( int n, double& rRet, int nElement )
371 bool bSuccess = false;
373 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
374 mppOptions[n]->type != SANE_TYPE_FIXED ) )
375 return false;
377 std::unique_ptr<SANE_Word[]> pRet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
378 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pRet.get() );
379 if( nStatus == SANE_STATUS_GOOD )
381 bSuccess = true;
382 if( mppOptions[n]->type == SANE_TYPE_INT )
383 rRet = static_cast<double>(pRet[ nElement ]);
384 else
385 rRet = SANE_UNFIX( pRet[nElement] );
387 return bSuccess;
390 bool Sane::GetOptionValue( int n, double* pSet )
392 if( ! maHandle || ! ( mppOptions[n]->type == SANE_TYPE_FIXED ||
393 mppOptions[n]->type == SANE_TYPE_INT ) )
394 return false;
396 std::unique_ptr<SANE_Word[]> pFixedSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
397 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pFixedSet.get() );
398 if( nStatus != SANE_STATUS_GOOD )
399 return false;
400 for( size_t i = 0; i <mppOptions[n]->size/sizeof(SANE_Word); i++ )
402 if( mppOptions[n]->type == SANE_TYPE_FIXED )
403 pSet[i] = SANE_UNFIX( pFixedSet[i] );
404 else
405 pSet[i] = static_cast<double>(pFixedSet[i]);
407 return true;
410 void Sane::SetOptionValue( int n, bool bSet )
412 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
413 return;
414 SANE_Word nRet = bSet ? SANE_TRUE : SANE_FALSE;
415 ControlOption( n, SANE_ACTION_SET_VALUE, &nRet );
418 void Sane::SetOptionValue( int n, const OUString& rSet )
420 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
421 return;
422 OString aSet(OUStringToOString(rSet, osl_getThreadTextEncoding()));
423 ControlOption( n, SANE_ACTION_SET_VALUE, const_cast<char *>(aSet.getStr()) );
426 void Sane::SetOptionValue( int n, double fSet, int nElement )
428 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
429 mppOptions[n]->type != SANE_TYPE_FIXED ) )
430 return;
432 if( mppOptions[n]->size/sizeof(SANE_Word) > 1 )
434 std::unique_ptr<SANE_Word[]> pSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
435 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, pSet.get() );
436 if( nStatus == SANE_STATUS_GOOD )
438 pSet[nElement] = mppOptions[n]->type == SANE_TYPE_INT ?
439 static_cast<SANE_Word>(fSet) : SANE_FIX( fSet );
440 ControlOption( n, SANE_ACTION_SET_VALUE, pSet.get() );
443 else
445 SANE_Word nSetTo =
446 mppOptions[n]->type == SANE_TYPE_INT ?
447 static_cast<SANE_Word>(fSet) : SANE_FIX( fSet );
449 ControlOption( n, SANE_ACTION_SET_VALUE, &nSetTo );
453 void Sane::SetOptionValue( int n, double const * pSet )
455 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
456 mppOptions[n]->type != SANE_TYPE_FIXED ) )
457 return;
458 std::unique_ptr<SANE_Word[]> pFixedSet(new SANE_Word[mppOptions[n]->size/sizeof(SANE_Word)]);
459 for( size_t i = 0; i < mppOptions[n]->size/sizeof(SANE_Word); i++ )
461 if( mppOptions[n]->type == SANE_TYPE_FIXED )
462 pFixedSet[i] = SANE_FIX( pSet[i] );
463 else
464 pFixedSet[i] = static_cast<SANE_Word>(pSet[i]);
466 ControlOption( n, SANE_ACTION_SET_VALUE, pFixedSet.get() );
469 enum FrameStyleType {
470 FrameStyle_BW, FrameStyle_Gray, FrameStyle_RGB, FrameStyle_Separated
473 #define BYTE_BUFFER_SIZE 32768
475 static sal_uInt8 ReadValue( FILE* fp, int depth )
477 if( depth == 16 )
479 sal_uInt16 nWord;
480 // data always come in native byte order !
481 // 16 bits is not really supported by backends as of now
482 // e.g. UMAX Astra 1200S delivers 16 bit but in BIGENDIAN
483 // against SANE documentation (xscanimage gets the same result
484 // as we do
485 size_t items_read = fread( &nWord, 1, 2, fp );
487 if (items_read != 2)
489 SAL_WARN( "extensions.scanner", "short read, abandoning" );
490 return 0;
493 return static_cast<sal_uInt8>( nWord / 256 );
495 sal_uInt8 nByte;
496 size_t items_read = fread( &nByte, 1, 1, fp );
497 if (items_read != 1)
499 SAL_WARN( "extensions.scanner", "short read, abandoning" );
500 return 0;
502 return nByte;
505 bool Sane::CheckConsistency( const char* pMes, bool bInit )
507 static const SANE_Option_Descriptor** pDescArray = nullptr;
508 static const SANE_Option_Descriptor* pZero = nullptr;
510 if( bInit )
512 pDescArray = mppOptions.get();
513 if( mppOptions )
514 pZero = mppOptions[0];
515 return true;
518 bool bConsistent = true;
520 if( pDescArray != mppOptions.get() )
521 bConsistent = false;
522 if( pZero != mppOptions[0] )
523 bConsistent = false;
525 if( ! bConsistent )
526 dbg_msg( "Sane is not consistent. (%s)\n", pMes );
528 return bConsistent;
531 bool Sane::Start( BitmapTransporter& rBitmap )
533 int nStream = 0, nLine = 0, i = 0;
534 SANE_Parameters aParams;
535 FrameStyleType eType = FrameStyle_Gray;
536 bool bSuccess = true;
537 bool bWidthSet = false;
539 if( ! maHandle )
540 return false;
542 int nWidthMM = 0;
543 int nHeightMM = 0;
544 double fTLx, fTLy, fResl = 0.0;
545 int nOption;
546 if( ( nOption = GetOptionByName( "tl-x" ) ) != -1 &&
547 GetOptionValue( nOption, fTLx ) &&
548 GetOptionUnit( nOption ) == SANE_UNIT_MM )
550 double fBRx;
551 if( ( nOption = GetOptionByName( "br-x" ) ) != -1 &&
552 GetOptionValue( nOption, fBRx ) &&
553 GetOptionUnit( nOption ) == SANE_UNIT_MM )
555 nWidthMM = static_cast<int>(fabs(fBRx - fTLx));
558 if( ( nOption = GetOptionByName( "tl-y" ) ) != -1 &&
559 GetOptionValue( nOption, fTLy ) &&
560 GetOptionUnit( nOption ) == SANE_UNIT_MM )
562 double fBRy;
563 if( ( nOption = GetOptionByName( "br-y" ) ) != -1 &&
564 GetOptionValue( nOption, fBRy ) &&
565 GetOptionUnit( nOption ) == SANE_UNIT_MM )
567 nHeightMM = static_cast<int>(fabs(fBRy - fTLy));
570 if( ( nOption = GetOptionByName( "resolution" ) ) != -1 )
571 (void)GetOptionValue( nOption, fResl );
573 std::unique_ptr<sal_uInt8[]> pBuffer;
575 SANE_Status nStatus = SANE_STATUS_GOOD;
577 rBitmap.lock();
578 SvMemoryStream& aConverter = rBitmap.getStream();
579 aConverter.Seek( 0 );
580 aConverter.SetEndian( SvStreamEndian::LITTLE );
582 // write bitmap stream header
583 aConverter.WriteChar( 'B' ).WriteChar( 'M' );
584 aConverter.WriteUInt32( 0 );
585 aConverter.WriteUInt32( 0 );
586 aConverter.WriteUInt32( 60 );
588 // write BITMAPINFOHEADER
589 aConverter.WriteUInt32( 40 );
590 aConverter.WriteUInt32( 0 ); // fill in width later
591 aConverter.WriteUInt32( 0 ); // fill in height later
592 aConverter.WriteUInt16( 1 );
593 // create header for 24 bits
594 // correct later if necessary
595 aConverter.WriteUInt16( 24 );
596 aConverter.WriteUInt32( 0 );
597 aConverter.WriteUInt32( 0 );
598 aConverter.WriteUInt32( 0 );
599 aConverter.WriteUInt32( 0 );
600 aConverter.WriteUInt32( 0 );
601 aConverter.WriteUInt32( 0 );
603 for( nStream=0; nStream < 3 && bSuccess ; nStream++ )
605 nStatus = p_start( maHandle );
606 DUMP_STATE( nStatus, "sane_start" );
607 CheckConsistency( "sane_start" );
608 if( nStatus == SANE_STATUS_GOOD )
610 nStatus = p_get_parameters( maHandle, &aParams );
611 DUMP_STATE( nStatus, "sane_get_parameters" );
612 CheckConsistency( "sane_get_parameters" );
613 if (nStatus != SANE_STATUS_GOOD || aParams.bytes_per_line == 0)
615 bSuccess = false;
616 break;
618 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
619 const char* const ppFormats[] = { "SANE_FRAME_GRAY", "SANE_FRAME_RGB",
620 "SANE_FRAME_RED", "SANE_FRAME_GREEN",
621 "SANE_FRAME_BLUE", "Unknown !!!" };
622 fprintf( stderr, "Parameters for frame %d:\n", nStream );
623 if( static_cast<
624 typename std::make_unsigned<
625 typename std::underlying_type<SANE_Frame>::type>::type>(
626 aParams.format)
627 > 4 )
629 aParams.format = SANE_Frame(5);
631 fprintf( stderr, "format: %s\n", ppFormats[ static_cast<int>(aParams.format) ] );
632 fprintf( stderr, "last_frame: %s\n", aParams.last_frame ? "TRUE" : "FALSE" );
633 fprintf( stderr, "depth: %d\n", static_cast<int>(aParams.depth) );
634 fprintf( stderr, "pixels_per_line: %d\n", static_cast<int>(aParams.pixels_per_line) );
635 fprintf( stderr, "bytes_per_line: %d\n", static_cast<int>(aParams.bytes_per_line) );
636 #endif
637 if( ! pBuffer )
639 pBuffer.reset(new sal_uInt8[ BYTE_BUFFER_SIZE < 4*aParams.bytes_per_line ? 4*aParams.bytes_per_line : BYTE_BUFFER_SIZE ]);
642 if( aParams.last_frame )
643 nStream=3;
645 switch( aParams.format )
647 case SANE_FRAME_GRAY:
648 eType = FrameStyle_Gray;
649 if( aParams.depth == 1 )
650 eType = FrameStyle_BW;
651 break;
652 case SANE_FRAME_RGB:
653 eType = FrameStyle_RGB;
654 break;
655 case SANE_FRAME_RED:
656 case SANE_FRAME_GREEN:
657 case SANE_FRAME_BLUE:
658 eType = FrameStyle_Separated;
659 break;
660 default:
661 fprintf( stderr, "Warning: unknown frame style !!!\n" );
664 bool bSynchronousRead = true;
666 // should be fail safe, but ... ??
667 nStatus = p_set_io_mode( maHandle, SANE_FALSE );
668 CheckConsistency( "sane_set_io_mode" );
669 if( nStatus != SANE_STATUS_GOOD )
671 bSynchronousRead = false;
672 nStatus = p_set_io_mode(maHandle, SANE_TRUE);
673 CheckConsistency( "sane_set_io_mode" );
674 if (nStatus != SANE_STATUS_GOOD)
676 SAL_WARN("extensions.scanner", "SANE driver status is: " << nStatus);
680 SANE_Int nLen=0;
681 SANE_Int fd = 0;
683 if( ! bSynchronousRead )
685 nStatus = p_get_select_fd( maHandle, &fd );
686 DUMP_STATE( nStatus, "sane_get_select_fd" );
687 CheckConsistency( "sane_get_select_fd" );
688 if( nStatus != SANE_STATUS_GOOD )
689 bSynchronousRead = true;
691 utl::TempFile aFrame;
692 aFrame.EnableKillingFile();
693 FILE* pFrame = fopen(OUStringToOString(aFrame.GetFileName(), osl_getThreadTextEncoding()).getStr(), "w+b");
694 if( ! pFrame )
696 bSuccess = false;
697 break;
699 do {
700 if( ! bSynchronousRead )
702 fd_set fdset;
703 struct timeval tv;
705 FD_ZERO( &fdset );
706 FD_SET( static_cast<int>(fd), &fdset );
707 tv.tv_sec = 5;
708 tv.tv_usec = 0;
709 if( select( fd+1, &fdset, nullptr, nullptr, &tv ) == 0 )
710 fprintf( stderr, "Timeout on sane_read descriptor\n" );
712 nLen = 0;
713 nStatus = p_read( maHandle, pBuffer.get(), BYTE_BUFFER_SIZE, &nLen );
714 CheckConsistency( "sane_read" );
715 if( nLen && ( nStatus == SANE_STATUS_GOOD ||
716 nStatus == SANE_STATUS_EOF ) )
718 bSuccess = (static_cast<size_t>(nLen) == fwrite( pBuffer.get(), 1, nLen, pFrame ));
719 if (!bSuccess)
720 break;
722 else
723 DUMP_STATE( nStatus, "sane_read" );
724 } while( nStatus == SANE_STATUS_GOOD );
725 if (nStatus != SANE_STATUS_EOF || !bSuccess)
727 fclose( pFrame );
728 bSuccess = false;
729 break;
732 int nFrameLength = ftell( pFrame );
733 fseek( pFrame, 0, SEEK_SET );
734 sal_uInt32 nWidth = static_cast<sal_uInt32>(aParams.pixels_per_line);
735 sal_uInt32 nHeight = static_cast<sal_uInt32>(nFrameLength / aParams.bytes_per_line);
736 if( ! bWidthSet )
738 if( ! fResl )
739 fResl = 300; // if all else fails that's a good guess
740 if( ! nWidthMM )
741 nWidthMM = static_cast<int>((static_cast<double>(nWidth) / fResl) * 25.4);
742 if( ! nHeightMM )
743 nHeightMM = static_cast<int>((static_cast<double>(nHeight) / fResl) * 25.4);
744 SAL_INFO("extensions.scanner", "set dimensions to(" << nWidth << ", " << nHeight << ") Pixel, (" << nWidthMM << ", " << nHeightMM <<
745 ") mm, resolution is " << fResl);
747 aConverter.Seek( 18 );
748 aConverter.WriteUInt32( nWidth );
749 aConverter.WriteUInt32( nHeight );
750 aConverter.Seek( 38 );
751 aConverter.WriteUInt32( 1000*nWidth/nWidthMM );
752 aConverter.WriteUInt32( 1000*nHeight/nHeightMM );
753 bWidthSet = true;
755 aConverter.Seek(60);
757 if( eType == FrameStyle_BW )
759 aConverter.Seek( 10 );
760 aConverter.WriteUInt32( 64 );
761 aConverter.Seek( 28 );
762 aConverter.WriteUInt16( 1 );
763 aConverter.Seek( 54 );
764 // write color table
765 aConverter.WriteUInt16( 0xffff );
766 aConverter.WriteUChar( 0xff );
767 aConverter.WriteUChar( 0 );
768 aConverter.WriteUInt32( 0 );
769 aConverter.Seek( 64 );
771 else if( eType == FrameStyle_Gray )
773 aConverter.Seek( 10 );
774 aConverter.WriteUInt32( 1084 );
775 aConverter.Seek( 28 );
776 aConverter.WriteUInt16( 8 );
777 aConverter.Seek( 54 );
778 // write color table
779 for( nLine = 0; nLine < 256; nLine++ )
781 aConverter.WriteUChar( nLine );
782 aConverter.WriteUChar( nLine );
783 aConverter.WriteUChar( nLine );
784 aConverter.WriteUChar( 0 );
786 aConverter.Seek( 1084 );
789 for (nLine = nHeight-1; nLine >= 0; --nLine)
791 if (fseek(pFrame, nLine * aParams.bytes_per_line, SEEK_SET) == -1)
793 bSuccess = false;
794 break;
796 if( eType == FrameStyle_BW ||
797 ( eType == FrameStyle_Gray && aParams.depth == 8 )
800 SANE_Int items_read = fread( pBuffer.get(), 1, aParams.bytes_per_line, pFrame );
801 if (items_read != aParams.bytes_per_line)
803 SAL_WARN( "extensions.scanner", "short read, padding with zeros" );
804 memset(pBuffer.get() + items_read, 0, aParams.bytes_per_line - items_read);
806 aConverter.WriteBytes(pBuffer.get(), aParams.bytes_per_line);
808 else if( eType == FrameStyle_Gray )
810 for( i = 0; i < (aParams.pixels_per_line); i++ )
812 sal_uInt8 nGray = ReadValue( pFrame, aParams.depth );
813 aConverter.WriteUChar( nGray );
816 else if( eType == FrameStyle_RGB )
818 for( i = 0; i < (aParams.pixels_per_line); i++ )
820 sal_uInt8 nRed, nGreen, nBlue;
821 nRed = ReadValue( pFrame, aParams.depth );
822 nGreen = ReadValue( pFrame, aParams.depth );
823 nBlue = ReadValue( pFrame, aParams.depth );
824 aConverter.WriteUChar( nBlue );
825 aConverter.WriteUChar( nGreen );
826 aConverter.WriteUChar( nRed );
829 else if( eType == FrameStyle_Separated )
831 for( i = 0; i < (aParams.pixels_per_line); i++ )
833 sal_uInt8 nValue = ReadValue( pFrame, aParams.depth );
834 switch( aParams.format )
836 case SANE_FRAME_RED:
837 aConverter.SeekRel( 2 );
838 aConverter.WriteUChar( nValue );
839 break;
840 case SANE_FRAME_GREEN:
841 aConverter.SeekRel( 1 );
842 aConverter.WriteUChar( nValue );
843 aConverter.SeekRel( 1 );
844 break;
845 case SANE_FRAME_BLUE:
846 aConverter.WriteUChar( nValue );
847 aConverter.SeekRel( 2 );
848 break;
849 case SANE_FRAME_GRAY:
850 case SANE_FRAME_RGB:
851 break;
855 int nGap = aConverter.Tell() & 3;
856 if (nGap)
857 aConverter.SeekRel( 4-nGap );
859 fclose( pFrame ); // deletes tmpfile
860 if( eType != FrameStyle_Separated )
861 break;
863 else
864 bSuccess = false;
866 // get stream length
867 int nPos = aConverter.TellEnd();
869 aConverter.Seek( 2 );
870 aConverter.WriteUInt32( nPos+1 );
871 aConverter.Seek( 0 );
873 rBitmap.unlock();
875 if( bSuccess )
877 // only cancel a successful operation
878 // sane disrupts memory else
879 p_cancel( maHandle );
880 CheckConsistency( "sane_cancel" );
882 pBuffer.reset();
884 ReloadOptions();
887 dbg_msg( "Sane::Start returns with %s\n", bSuccess ? "TRUE" : "FALSE" );
889 return bSuccess;
892 int Sane::GetRange( int n, std::unique_ptr<double[]>& rpDouble )
894 if( mppOptions[n]->constraint_type != SANE_CONSTRAINT_RANGE &&
895 mppOptions[n]->constraint_type != SANE_CONSTRAINT_WORD_LIST )
897 return -1;
900 rpDouble = nullptr;
901 int nItems, i;
902 bool bIsFixed = mppOptions[n]->type == SANE_TYPE_FIXED;
904 dbg_msg( "Sane::GetRange of option %s ", mppOptions[n]->name );
905 if(mppOptions[n]->constraint_type == SANE_CONSTRAINT_RANGE )
907 double fMin, fMax, fQuant;
908 if( bIsFixed )
910 fMin = SANE_UNFIX( mppOptions[n]->constraint.range->min );
911 fMax = SANE_UNFIX( mppOptions[n]->constraint.range->max );
912 fQuant = SANE_UNFIX( mppOptions[n]->constraint.range->quant );
914 else
916 fMin = static_cast<double>(mppOptions[n]->constraint.range->min);
917 fMax = static_cast<double>(mppOptions[n]->constraint.range->max);
918 fQuant = static_cast<double>(mppOptions[n]->constraint.range->quant);
920 if( fQuant != 0.0 )
922 dbg_msg( "quantum range [ %lg ; %lg ; %lg ]\n",
923 fMin, fQuant, fMax );
924 nItems = static_cast<int>((fMax - fMin)/fQuant)+1;
925 rpDouble.reset(new double[ nItems ]);
926 double fValue = fMin;
927 for( i = 0; i < nItems; i++, fValue += fQuant )
928 rpDouble[i] = fValue;
929 rpDouble[ nItems-1 ] = fMax;
930 return nItems;
932 else
934 dbg_msg( "normal range [ %lg %lg ]\n",
935 fMin, fMax );
936 rpDouble.reset(new double[2]);
937 rpDouble[0] = fMin;
938 rpDouble[1] = fMax;
939 return 0;
942 else
944 nItems = mppOptions[n]->constraint.word_list[0];
945 rpDouble.reset(new double[nItems]);
946 for( i=0; i<nItems; i++ )
948 rpDouble[i] = bIsFixed ?
949 SANE_UNFIX( mppOptions[n]->constraint.word_list[i+1] ) :
950 static_cast<double>(mppOptions[n]->constraint.word_list[i+1]);
952 dbg_msg( "wordlist [ %lg ... %lg ]\n",
953 rpDouble[ 0 ], rpDouble[ nItems-1 ] );
954 return nItems;
958 static const char *ppUnits[] = {
960 "[Pixel]",
961 "[Bit]",
962 "[mm]",
963 "[DPI]",
964 "[%]",
965 "[usec]"
968 OUString Sane::GetOptionUnitName( int n )
970 OUString aText;
971 SANE_Unit nUnit = mppOptions[n]->unit;
972 size_t nUnitAsSize = static_cast<size_t>(nUnit);
973 if (nUnitAsSize >= SAL_N_ELEMENTS( ppUnits ))
974 aText = "[unknown units]";
975 else
976 aText = OUString( ppUnits[ nUnit ], strlen(ppUnits[ nUnit ]), osl_getThreadTextEncoding() );
977 return aText;
980 bool Sane::ActivateButtonOption( int n )
982 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, nullptr );
983 return nStatus == SANE_STATUS_GOOD;
986 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */