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 .
21 #include <type_traits>
24 #include <sal/log.hxx>
25 #include <tools/stream.hxx>
26 #include <unotools/tempfile.hxx>
32 #include <sys/types.h>
33 #include <sal/config.h>
34 #include <sal/macros.h>
37 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
39 #define dump_state( a, b, c, d ) fprintf( stderr, a, b, c, d );
41 #define dump_state( a, b, c, d ) ;
43 static void dbg_msg( const char* pString
, ... )
45 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
47 va_start( ap
, pString
);
48 vfprintf( stderr
, pString
, ap
);
55 #define FAIL_SHUTDOWN_STATE( x, y, z ) \
56 if( x != SANE_STATUS_GOOD ) \
58 dump_state( "%s returned error %d (%s)\n", \
59 y, x, p_strstatus( x ) ); \
64 #define FAIL_STATE( x, y, z ) \
65 if( x != SANE_STATUS_GOOD ) \
67 dump_state( "%s returned error %d (%s)\n", \
68 y, x, p_strstatus( x ) ); \
72 #define DUMP_STATE( x, y ) \
73 if( x != SANE_STATUS_GOOD ) \
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
,
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
);
114 fprintf( stderr
, "Could not load symbol %s\n",
116 bSaneSymbolLoadFailed
= true;
121 SANE_Status
Sane::ControlOption( int nOption
, SANE_Action nAction
,
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";
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(),
147 if( nInfo
& SANE_INFO_RELOAD_OPTIONS
)
157 if( ! nRefCount
|| ! pSaneLib
)
167 if( ! nRefCount
&& pSaneLib
)
173 OUString
sSaneLibName( "libsane" SAL_DLLEXTENSION
);
174 pSaneLib
= osl_loadModule( sSaneLibName
.pData
, SAL_LOADMODULE_LAZY
);
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
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
);
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
***,
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
,
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
)
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
),
231 FAIL_SHUTDOWN_STATE( nStatus
, "sane_get_devices", );
232 for( nDevices
= 0 ; ppDevices
[ nDevices
]; nDevices
++ ) ;
235 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
237 fprintf( stderr
, "libsane%s could not be opened: %s\n", SAL_DLLEXTENSION
,
247 osl_unloadModule( pSaneLib
);
252 void Sane::ReloadDevices()
260 void Sane::ReloadOptions()
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 );
294 OString
aDevice( name
);
295 for( int i
= 0; i
< nDevices
; i
++ )
297 if( aDevice
== ppDevices
[i
]->name
)
308 bool Sane::Open( int n
)
310 if( n
>= 0 && n
< nDevices
)
313 return Open( ppDevices
[n
]->name
);
329 int Sane::GetOptionByName( const char* rName
)
332 OString
aOption( rName
);
333 for( i
= 0; i
< mnOptions
; i
++ )
335 if( mppOptions
[i
]->name
&& aOption
== mppOptions
[i
]->name
)
341 bool Sane::GetOptionValue( int n
, bool& rRet
)
343 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
346 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, &nRet
);
347 if( nStatus
!= SANE_STATUS_GOOD
)
354 bool Sane::GetOptionValue( int n
, OString
& rRet
)
356 bool bSuccess
= false;
357 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_STRING
)
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
)
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
) )
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
)
382 if( mppOptions
[n
]->type
== SANE_TYPE_INT
)
383 rRet
= static_cast<double>(pRet
[ nElement
]);
385 rRet
= SANE_UNFIX( pRet
[nElement
] );
390 bool Sane::GetOptionValue( int n
, double* pSet
)
392 if( ! maHandle
|| ! ( mppOptions
[n
]->type
== SANE_TYPE_FIXED
||
393 mppOptions
[n
]->type
== SANE_TYPE_INT
) )
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
)
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
] );
405 pSet
[i
] = static_cast<double>(pFixedSet
[i
]);
410 void Sane::SetOptionValue( int n
, bool bSet
)
412 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
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
)
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
) )
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() );
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
) )
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
] );
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
)
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
485 size_t items_read
= fread( &nWord
, 1, 2, fp
);
489 SAL_WARN( "extensions.scanner", "short read, abandoning" );
493 return static_cast<sal_uInt8
>( nWord
/ 256 );
496 size_t items_read
= fread( &nByte
, 1, 1, fp
);
499 SAL_WARN( "extensions.scanner", "short read, abandoning" );
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;
512 pDescArray
= mppOptions
.get();
514 pZero
= mppOptions
[0];
518 bool bConsistent
= true;
520 if( pDescArray
!= mppOptions
.get() )
522 if( pZero
!= mppOptions
[0] )
526 dbg_msg( "Sane is not consistent. (%s)\n", pMes
);
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;
544 double fTLx
, fTLy
, fResl
= 0.0;
546 if( ( nOption
= GetOptionByName( "tl-x" ) ) != -1 &&
547 GetOptionValue( nOption
, fTLx
) &&
548 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
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
)
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
;
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)
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
);
624 typename
std::make_unsigned
<
625 typename
std::underlying_type
<SANE_Frame
>::type
>::type
>(
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
) );
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
)
645 switch( aParams
.format
)
647 case SANE_FRAME_GRAY
:
648 eType
= FrameStyle_Gray
;
649 if( aParams
.depth
== 1 )
650 eType
= FrameStyle_BW
;
653 eType
= FrameStyle_RGB
;
656 case SANE_FRAME_GREEN
:
657 case SANE_FRAME_BLUE
:
658 eType
= FrameStyle_Separated
;
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
);
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");
700 if( ! bSynchronousRead
)
706 FD_SET( static_cast<int>(fd
), &fdset
);
709 if( select( fd
+1, &fdset
, nullptr, nullptr, &tv
) == 0 )
710 fprintf( stderr
, "Timeout on sane_read descriptor\n" );
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
));
723 DUMP_STATE( nStatus
, "sane_read" );
724 } while( nStatus
== SANE_STATUS_GOOD
);
725 if (nStatus
!= SANE_STATUS_EOF
|| !bSuccess
)
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
);
739 fResl
= 300; // if all else fails that's a good guess
741 nWidthMM
= static_cast<int>((static_cast<double>(nWidth
) / fResl
) * 25.4);
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
);
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 );
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 );
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)
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
)
837 aConverter
.SeekRel( 2 );
838 aConverter
.WriteUChar( nValue
);
840 case SANE_FRAME_GREEN
:
841 aConverter
.SeekRel( 1 );
842 aConverter
.WriteUChar( nValue
);
843 aConverter
.SeekRel( 1 );
845 case SANE_FRAME_BLUE
:
846 aConverter
.WriteUChar( nValue
);
847 aConverter
.SeekRel( 2 );
849 case SANE_FRAME_GRAY
:
855 int nGap
= aConverter
.Tell() & 3;
857 aConverter
.SeekRel( 4-nGap
);
859 fclose( pFrame
); // deletes tmpfile
860 if( eType
!= FrameStyle_Separated
)
867 int nPos
= aConverter
.TellEnd();
869 aConverter
.Seek( 2 );
870 aConverter
.WriteUInt32( nPos
+1 );
871 aConverter
.Seek( 0 );
877 // only cancel a successful operation
878 // sane disrupts memory else
879 p_cancel( maHandle
);
880 CheckConsistency( "sane_cancel" );
887 dbg_msg( "Sane::Start returns with %s\n", bSuccess
? "TRUE" : "FALSE" );
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
)
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
;
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
);
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
);
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
;
934 dbg_msg( "normal range [ %lg %lg ]\n",
936 rpDouble
.reset(new double[2]);
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 ] );
958 static const char *ppUnits
[] = {
968 OUString
Sane::GetOptionUnitName( int n
)
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]";
976 aText
= OUString( ppUnits
[ nUnit
], strlen(ppUnits
[ nUnit
]), osl_getThreadTextEncoding() );
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: */