1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
23 #include <o3tl/safeint.hxx>
25 #include <sal/log.hxx>
26 #include <tools/stream.hxx>
27 #include <unotools/tempfile.hxx>
32 #include <sal/config.h>
33 #include <sal/macros.h>
36 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
38 #define dump_state( a, b, c, d ) fprintf( stderr, a, b, c, d );
40 #define dump_state( a, b, c, d ) ;
42 static void dbg_msg( const char* pString
, ... )
44 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
46 va_start( ap
, pString
);
47 vfprintf( stderr
, pString
, ap
);
54 #define FAIL_SHUTDOWN_STATE( x, y, z ) \
55 if( x != SANE_STATUS_GOOD ) \
57 dump_state( "%s returned error %d (%s)\n", \
58 y, x, p_strstatus( x ) ); \
63 #define FAIL_STATE( x, y, z ) \
64 if( x != SANE_STATUS_GOOD ) \
66 dump_state( "%s returned error %d (%s)\n", \
67 y, x, p_strstatus( x ) ); \
71 #define DUMP_STATE( x, y ) \
72 if( x != SANE_STATUS_GOOD ) \
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
,
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
);
113 SAL_WARN( "extensions.scanner", "Could not load symbol " << pSymbolname
);
114 bSaneSymbolLoadFailed
= true;
119 SANE_Status
Sane::ControlOption( int nOption
, SANE_Action nAction
,
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";
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(),
145 if( nInfo
& SANE_INFO_RELOAD_OPTIONS
)
155 if( ! nRefCount
|| ! pSaneLib
)
165 if( ! nRefCount
&& pSaneLib
)
171 #ifndef DISABLE_DYNLOADING
172 OUString
sSaneLibName( u
"libsane" SAL_DLLEXTENSION
""_ustr
);
173 pSaneLib
= osl_loadModule( sSaneLibName
.pData
, SAL_LOADMODULE_LAZY
);
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
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
);
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
***,
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
,
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
)
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
),
230 FAIL_SHUTDOWN_STATE( nStatus
, "sane_get_devices", );
231 for( nDevices
= 0 ; ppDevices
[ nDevices
]; nDevices
++ ) ;
234 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
236 fprintf( stderr
, "libsane%s could not be opened: %s\n", SAL_DLLEXTENSION
,
246 #ifndef DISABLE_DYNLOADING
247 osl_unloadModule( pSaneLib
);
253 void Sane::ReloadDevices()
261 void Sane::ReloadOptions()
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 );
295 OString
aDevice( name
);
296 for( int i
= 0; i
< nDevices
; i
++ )
298 if( aDevice
== ppDevices
[i
]->name
)
309 bool Sane::Open( int n
)
311 if( n
>= 0 && n
< nDevices
)
314 return Open( ppDevices
[n
]->name
);
330 int Sane::GetOptionByName( const char* rName
)
333 OString
aOption( rName
);
334 for( i
= 0; i
< mnOptions
; i
++ )
336 if( mppOptions
[i
]->name
&& aOption
== mppOptions
[i
]->name
)
342 bool Sane::GetOptionValue( int n
, bool& rRet
)
344 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
347 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, &nRet
);
348 if( nStatus
!= SANE_STATUS_GOOD
)
355 bool Sane::GetOptionValue( int n
, OString
& rRet
)
357 bool bSuccess
= false;
358 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_STRING
)
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
)
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
) )
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
)
383 if( mppOptions
[n
]->type
== SANE_TYPE_INT
)
384 rRet
= static_cast<double>(pRet
[ nElement
]);
386 rRet
= SANE_UNFIX( pRet
[nElement
] );
391 bool Sane::GetOptionValue( int n
, double* pSet
)
393 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_FIXED
&&
394 mppOptions
[n
]->type
!= SANE_TYPE_INT
) )
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
)
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
] );
406 pSet
[i
] = static_cast<double>(pFixedSet
[i
]);
411 void Sane::SetOptionValue( int n
, bool bSet
)
413 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
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
)
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
) )
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() );
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
) )
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
] );
465 pFixedSet
[i
] = static_cast<SANE_Word
>(pSet
[i
]);
467 ControlOption( n
, SANE_ACTION_SET_VALUE
, pFixedSet
.get() );
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
)
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
490 size_t items_read
= fread( &nWord
, 1, 2, fp
);
494 SAL_WARN( "extensions.scanner", "short read, abandoning" );
498 return static_cast<sal_uInt8
>( nWord
/ 256 );
501 size_t items_read
= fread( &nByte
, 1, 1, fp
);
504 SAL_WARN( "extensions.scanner", "short read, abandoning" );
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;
517 pDescArray
= mppOptions
.get();
519 pZero
= mppOptions
[0];
523 bool bConsistent
= true;
525 if( pDescArray
!= mppOptions
.get() )
527 if( pZero
!= mppOptions
[0] )
531 dbg_msg( "Sane is not consistent. (%s)\n", pMes
);
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;
549 double fTLx
, fTLy
, fResl
= 0.0;
551 nOption
= GetOptionByName( "tl-x" );
553 GetOptionValue( nOption
, fTLx
) &&
554 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
557 nOption
= GetOptionByName( "br-x" );
559 GetOptionValue( nOption
, fBRx
) &&
560 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
562 nWidthMM
= static_cast<int>(fabs(fBRx
- fTLx
));
565 nOption
= GetOptionByName( "tl-y" );
567 GetOptionValue( nOption
, fTLy
) &&
568 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
571 nOption
= GetOptionByName( "br-y" );
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
;
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)
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
);
633 typename
std::make_unsigned
<
634 typename
std::underlying_type
<SANE_Frame
>::type
>::type
>(
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
) );
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
)
654 switch( aParams
.format
)
656 case SANE_FRAME_GRAY
:
657 eType
= FrameStyle_Gray
;
658 if( aParams
.depth
== 1 )
659 eType
= FrameStyle_BW
;
662 eType
= FrameStyle_RGB
;
665 case SANE_FRAME_GREEN
:
666 case SANE_FRAME_BLUE
:
667 eType
= FrameStyle_Separated
;
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
);
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");
709 if( ! bSynchronousRead
)
715 FD_SET( static_cast<int>(fd
), &fdset
);
718 if( select( fd
+1, &fdset
, nullptr, nullptr, &tv
) == 0 )
719 SAL_WARN( "extensions.scanner", "Timeout on sane_read descriptor" );
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
));
732 DUMP_STATE( nStatus
, "sane_read" );
733 } while( nStatus
== SANE_STATUS_GOOD
);
734 if (nStatus
!= SANE_STATUS_EOF
|| !bSuccess
)
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
);
748 fResl
= 300; // if all else fails that's a good guess
750 nWidthMM
= static_cast<int>((static_cast<double>(nWidth
) / fResl
) * 25.4);
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
);
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 );
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 );
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)
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
)
846 aConverter
.SeekRel( 2 );
847 aConverter
.WriteUChar( nValue
);
849 case SANE_FRAME_GREEN
:
850 aConverter
.SeekRel( 1 );
851 aConverter
.WriteUChar( nValue
);
852 aConverter
.SeekRel( 1 );
854 case SANE_FRAME_BLUE
:
855 aConverter
.WriteUChar( nValue
);
856 aConverter
.SeekRel( 2 );
858 case SANE_FRAME_GRAY
:
864 int nGap
= aConverter
.Tell() & 3;
866 aConverter
.SeekRel( 4-nGap
);
868 fclose( pFrame
); // deletes tmpfile
869 if( eType
!= FrameStyle_Separated
)
876 int nPos
= aConverter
.TellEnd();
878 aConverter
.Seek( 2 );
879 aConverter
.WriteUInt32( nPos
+1 );
880 aConverter
.Seek( 0 );
886 // only cancel a successful operation
887 // sane disrupts memory else
888 p_cancel( maHandle
);
889 CheckConsistency( "sane_cancel" );
896 dbg_msg( "Sane::Start returns with %s\n", bSuccess
? "TRUE" : "FALSE" );
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
)
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
;
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
);
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
);
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
;
943 dbg_msg( "normal range [ %lg %lg ]\n",
945 rpDouble
.reset(new double[2]);
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 ] );
967 static const char *ppUnits
[] = {
977 OUString
Sane::GetOptionUnitName( int n
)
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]";
985 aText
= OUString( ppUnits
[ nUnit
], strlen(ppUnits
[ nUnit
]), osl_getThreadTextEncoding() );
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: */