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 fprintf( stderr
, "Could not load symbol %s\n",
115 bSaneSymbolLoadFailed
= true;
120 SANE_Status
Sane::ControlOption( int nOption
, SANE_Action nAction
,
125 SANE_Status nStatus
= p_control_option( maHandle
, static_cast<SANE_Int
>(nOption
),
126 nAction
, pData
, &nInfo
);
127 DUMP_STATE( nStatus
, "sane_control_option" );
128 #if OSL_DEBUG_LEVEL > 0
129 if( nStatus
!= SANE_STATUS_GOOD
)
131 const char* pAction
= "Unknown";
134 case SANE_ACTION_GET_VALUE
:
135 pAction
= "SANE_ACTION_GET_VALUE";break;
136 case SANE_ACTION_SET_VALUE
:
137 pAction
= "SANE_ACTION_SET_VALUE";break;
138 case SANE_ACTION_SET_AUTO
:
139 pAction
= "SANE_ACTION_SET_AUTO";break;
141 dbg_msg( "Option: \"%s\" action: %s\n",
142 OUStringToOString(GetOptionName(nOption
), osl_getThreadTextEncoding()).getStr(),
146 if( nInfo
& SANE_INFO_RELOAD_OPTIONS
)
156 if( ! nRefCount
|| ! pSaneLib
)
166 if( ! nRefCount
&& pSaneLib
)
172 #ifndef DISABLE_DYNLOADING
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 #ifndef DISABLE_DYNLOADING
248 osl_unloadModule( pSaneLib
);
254 void Sane::ReloadDevices()
262 void Sane::ReloadOptions()
267 const SANE_Option_Descriptor
* pZero
= p_get_option_descriptor( maHandle
, 0 );
268 SANE_Word pOptions
[2];
269 SANE_Status nStatus
= p_control_option( maHandle
, 0, SANE_ACTION_GET_VALUE
,
270 static_cast<void*>(pOptions
), nullptr );
271 if( nStatus
!= SANE_STATUS_GOOD
)
272 fprintf( stderr
, "Error: sane driver returned %s while reading number of options !\n", p_strstatus( nStatus
) );
274 mnOptions
= pOptions
[ 0 ];
275 if( o3tl::make_unsigned(pZero
->size
) > sizeof( SANE_Word
) )
276 fprintf( stderr
, "driver returned number of options with larger size than SANE_Word!!!\n" );
277 mppOptions
.reset(new const SANE_Option_Descriptor
*[ mnOptions
]);
278 mppOptions
[ 0 ] = pZero
;
279 for( int i
= 1; i
< mnOptions
; i
++ )
280 mppOptions
[ i
] = p_get_option_descriptor( maHandle
, i
);
282 CheckConsistency( nullptr, true );
284 maReloadOptionsLink
.Call( *this );
287 bool Sane::Open( const char* name
)
289 SANE_Status nStatus
= p_open( reinterpret_cast<SANE_String_Const
>(name
), &maHandle
);
290 FAIL_STATE( nStatus
, "sane_open", false );
296 OString
aDevice( name
);
297 for( int i
= 0; i
< nDevices
; i
++ )
299 if( aDevice
== ppDevices
[i
]->name
)
310 bool Sane::Open( int n
)
312 if( n
>= 0 && n
< nDevices
)
315 return Open( ppDevices
[n
]->name
);
331 int Sane::GetOptionByName( const char* rName
)
334 OString
aOption( rName
);
335 for( i
= 0; i
< mnOptions
; i
++ )
337 if( mppOptions
[i
]->name
&& aOption
== mppOptions
[i
]->name
)
343 bool Sane::GetOptionValue( int n
, bool& rRet
)
345 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
348 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, &nRet
);
349 if( nStatus
!= SANE_STATUS_GOOD
)
356 bool Sane::GetOptionValue( int n
, OString
& rRet
)
358 bool bSuccess
= false;
359 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_STRING
)
361 std::unique_ptr
<char[]> pRet(new char[mppOptions
[n
]->size
+1]);
362 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pRet
.get() );
363 if( nStatus
== SANE_STATUS_GOOD
)
371 bool Sane::GetOptionValue( int n
, double& rRet
, int nElement
)
373 bool bSuccess
= false;
375 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_INT
&&
376 mppOptions
[n
]->type
!= SANE_TYPE_FIXED
) )
379 std::unique_ptr
<SANE_Word
[]> pRet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
380 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pRet
.get() );
381 if( nStatus
== SANE_STATUS_GOOD
)
384 if( mppOptions
[n
]->type
== SANE_TYPE_INT
)
385 rRet
= static_cast<double>(pRet
[ nElement
]);
387 rRet
= SANE_UNFIX( pRet
[nElement
] );
392 bool Sane::GetOptionValue( int n
, double* pSet
)
394 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_FIXED
&&
395 mppOptions
[n
]->type
!= SANE_TYPE_INT
) )
398 std::unique_ptr
<SANE_Word
[]> pFixedSet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
399 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pFixedSet
.get() );
400 if( nStatus
!= SANE_STATUS_GOOD
)
402 for( size_t i
= 0; i
<mppOptions
[n
]->size
/sizeof(SANE_Word
); i
++ )
404 if( mppOptions
[n
]->type
== SANE_TYPE_FIXED
)
405 pSet
[i
] = SANE_UNFIX( pFixedSet
[i
] );
407 pSet
[i
] = static_cast<double>(pFixedSet
[i
]);
412 void Sane::SetOptionValue( int n
, bool bSet
)
414 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
416 SANE_Word nRet
= bSet
? SANE_TRUE
: SANE_FALSE
;
417 ControlOption( n
, SANE_ACTION_SET_VALUE
, &nRet
);
420 void Sane::SetOptionValue( int n
, std::u16string_view rSet
)
422 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_STRING
)
424 OString
aSet(OUStringToOString(rSet
, osl_getThreadTextEncoding()));
425 ControlOption( n
, SANE_ACTION_SET_VALUE
, const_cast<char *>(aSet
.getStr()) );
428 void Sane::SetOptionValue( int n
, double fSet
, int nElement
)
430 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_INT
&&
431 mppOptions
[n
]->type
!= SANE_TYPE_FIXED
) )
434 if( mppOptions
[n
]->size
/sizeof(SANE_Word
) > 1 )
436 std::unique_ptr
<SANE_Word
[]> pSet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
437 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pSet
.get() );
438 if( nStatus
== SANE_STATUS_GOOD
)
440 pSet
[nElement
] = mppOptions
[n
]->type
== SANE_TYPE_INT
?
441 static_cast<SANE_Word
>(fSet
) : SANE_FIX( fSet
);
442 ControlOption( n
, SANE_ACTION_SET_VALUE
, pSet
.get() );
448 mppOptions
[n
]->type
== SANE_TYPE_INT
?
449 static_cast<SANE_Word
>(fSet
) : SANE_FIX( fSet
);
451 ControlOption( n
, SANE_ACTION_SET_VALUE
, &nSetTo
);
455 void Sane::SetOptionValue( int n
, double const * pSet
)
457 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_INT
&&
458 mppOptions
[n
]->type
!= SANE_TYPE_FIXED
) )
460 std::unique_ptr
<SANE_Word
[]> pFixedSet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
461 for( size_t i
= 0; i
< mppOptions
[n
]->size
/sizeof(SANE_Word
); i
++ )
463 if( mppOptions
[n
]->type
== SANE_TYPE_FIXED
)
464 pFixedSet
[i
] = SANE_FIX( pSet
[i
] );
466 pFixedSet
[i
] = static_cast<SANE_Word
>(pSet
[i
]);
468 ControlOption( n
, SANE_ACTION_SET_VALUE
, pFixedSet
.get() );
473 enum FrameStyleType
{
474 FrameStyle_BW
, FrameStyle_Gray
, FrameStyle_RGB
, FrameStyle_Separated
479 #define BYTE_BUFFER_SIZE 32768
481 static sal_uInt8
ReadValue( FILE* fp
, int depth
)
486 // data always come in native byte order !
487 // 16 bits is not really supported by backends as of now
488 // e.g. UMAX Astra 1200S delivers 16 bit but in BIGENDIAN
489 // against SANE documentation (xscanimage gets the same result
491 size_t items_read
= fread( &nWord
, 1, 2, fp
);
495 SAL_WARN( "extensions.scanner", "short read, abandoning" );
499 return static_cast<sal_uInt8
>( nWord
/ 256 );
502 size_t items_read
= fread( &nByte
, 1, 1, fp
);
505 SAL_WARN( "extensions.scanner", "short read, abandoning" );
511 bool Sane::CheckConsistency( const char* pMes
, bool bInit
)
513 static const SANE_Option_Descriptor
** pDescArray
= nullptr;
514 static const SANE_Option_Descriptor
* pZero
= nullptr;
518 pDescArray
= mppOptions
.get();
520 pZero
= mppOptions
[0];
524 bool bConsistent
= true;
526 if( pDescArray
!= mppOptions
.get() )
528 if( pZero
!= mppOptions
[0] )
532 dbg_msg( "Sane is not consistent. (%s)\n", pMes
);
537 bool Sane::Start( BitmapTransporter
& rBitmap
)
539 int nStream
= 0, nLine
= 0, i
= 0;
540 SANE_Parameters aParams
;
541 FrameStyleType eType
= FrameStyle_Gray
;
542 bool bSuccess
= true;
543 bool bWidthSet
= false;
550 double fTLx
, fTLy
, fResl
= 0.0;
552 nOption
= GetOptionByName( "tl-x" );
554 GetOptionValue( nOption
, fTLx
) &&
555 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
558 nOption
= GetOptionByName( "br-x" );
560 GetOptionValue( nOption
, fBRx
) &&
561 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
563 nWidthMM
= static_cast<int>(fabs(fBRx
- fTLx
));
566 nOption
= GetOptionByName( "tl-y" );
568 GetOptionValue( nOption
, fTLy
) &&
569 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
572 nOption
= GetOptionByName( "br-y" );
574 GetOptionValue( nOption
, fBRy
) &&
575 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
577 nHeightMM
= static_cast<int>(fabs(fBRy
- fTLy
));
580 if( ( nOption
= GetOptionByName( "resolution" ) ) != -1 )
581 (void)GetOptionValue( nOption
, fResl
);
583 std::unique_ptr
<sal_uInt8
[]> pBuffer
;
585 SANE_Status nStatus
= SANE_STATUS_GOOD
;
588 SvMemoryStream
& aConverter
= rBitmap
.getStream();
589 aConverter
.Seek( 0 );
590 aConverter
.SetEndian( SvStreamEndian::LITTLE
);
592 // write bitmap stream header
593 aConverter
.WriteChar( 'B' ).WriteChar( 'M' );
594 aConverter
.WriteUInt32( 0 );
595 aConverter
.WriteUInt32( 0 );
596 aConverter
.WriteUInt32( 60 );
598 // write BITMAPINFOHEADER
599 aConverter
.WriteUInt32( 40 );
600 aConverter
.WriteUInt32( 0 ); // fill in width later
601 aConverter
.WriteUInt32( 0 ); // fill in height later
602 aConverter
.WriteUInt16( 1 );
603 // create header for 24 bits
604 // correct later if necessary
605 aConverter
.WriteUInt16( 24 );
606 aConverter
.WriteUInt32( 0 );
607 aConverter
.WriteUInt32( 0 );
608 aConverter
.WriteUInt32( 0 );
609 aConverter
.WriteUInt32( 0 );
610 aConverter
.WriteUInt32( 0 );
611 aConverter
.WriteUInt32( 0 );
613 for( nStream
=0; nStream
< 3 && bSuccess
; nStream
++ )
615 nStatus
= p_start( maHandle
);
616 DUMP_STATE( nStatus
, "sane_start" );
617 CheckConsistency( "sane_start" );
618 if( nStatus
== SANE_STATUS_GOOD
)
620 nStatus
= p_get_parameters( maHandle
, &aParams
);
621 DUMP_STATE( nStatus
, "sane_get_parameters" );
622 CheckConsistency( "sane_get_parameters" );
623 if (nStatus
!= SANE_STATUS_GOOD
|| aParams
.bytes_per_line
== 0)
628 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
629 const char* const ppFormats
[] = { "SANE_FRAME_GRAY", "SANE_FRAME_RGB",
630 "SANE_FRAME_RED", "SANE_FRAME_GREEN",
631 "SANE_FRAME_BLUE", "Unknown !!!" };
632 fprintf( stderr
, "Parameters for frame %d:\n", nStream
);
634 typename
std::make_unsigned
<
635 typename
std::underlying_type
<SANE_Frame
>::type
>::type
>(
639 aParams
.format
= SANE_Frame(5);
641 fprintf( stderr
, "format: %s\n", ppFormats
[ static_cast<int>(aParams
.format
) ] );
642 fprintf( stderr
, "last_frame: %s\n", aParams
.last_frame
? "TRUE" : "FALSE" );
643 fprintf( stderr
, "depth: %d\n", static_cast<int>(aParams
.depth
) );
644 fprintf( stderr
, "pixels_per_line: %d\n", static_cast<int>(aParams
.pixels_per_line
) );
645 fprintf( stderr
, "bytes_per_line: %d\n", static_cast<int>(aParams
.bytes_per_line
) );
649 pBuffer
.reset(new sal_uInt8
[ BYTE_BUFFER_SIZE
< 4*aParams
.bytes_per_line
? 4*aParams
.bytes_per_line
: BYTE_BUFFER_SIZE
]);
652 if( aParams
.last_frame
)
655 switch( aParams
.format
)
657 case SANE_FRAME_GRAY
:
658 eType
= FrameStyle_Gray
;
659 if( aParams
.depth
== 1 )
660 eType
= FrameStyle_BW
;
663 eType
= FrameStyle_RGB
;
666 case SANE_FRAME_GREEN
:
667 case SANE_FRAME_BLUE
:
668 eType
= FrameStyle_Separated
;
671 fprintf( stderr
, "Warning: unknown frame style !!!\n" );
674 bool bSynchronousRead
= true;
676 // should be fail safe, but ... ??
677 nStatus
= p_set_io_mode( maHandle
, SANE_FALSE
);
678 CheckConsistency( "sane_set_io_mode" );
679 if( nStatus
!= SANE_STATUS_GOOD
)
681 bSynchronousRead
= false;
682 nStatus
= p_set_io_mode(maHandle
, SANE_TRUE
);
683 CheckConsistency( "sane_set_io_mode" );
684 if (nStatus
!= SANE_STATUS_GOOD
)
686 SAL_WARN("extensions.scanner", "SANE driver status is: " << nStatus
);
693 if( ! bSynchronousRead
)
695 nStatus
= p_get_select_fd( maHandle
, &fd
);
696 DUMP_STATE( nStatus
, "sane_get_select_fd" );
697 CheckConsistency( "sane_get_select_fd" );
698 if( nStatus
!= SANE_STATUS_GOOD
)
699 bSynchronousRead
= true;
701 utl::TempFileNamed aFrame
;
702 aFrame
.EnableKillingFile();
703 FILE* pFrame
= fopen(OUStringToOString(aFrame
.GetFileName(), osl_getThreadTextEncoding()).getStr(), "w+b");
710 if( ! bSynchronousRead
)
716 FD_SET( static_cast<int>(fd
), &fdset
);
719 if( select( fd
+1, &fdset
, nullptr, nullptr, &tv
) == 0 )
720 fprintf( stderr
, "Timeout on sane_read descriptor\n" );
723 nStatus
= p_read( maHandle
, pBuffer
.get(), BYTE_BUFFER_SIZE
, &nLen
);
724 CheckConsistency( "sane_read" );
725 if( nLen
&& ( nStatus
== SANE_STATUS_GOOD
||
726 nStatus
== SANE_STATUS_EOF
) )
728 bSuccess
= (static_cast<size_t>(nLen
) == fwrite( pBuffer
.get(), 1, nLen
, pFrame
));
733 DUMP_STATE( nStatus
, "sane_read" );
734 } while( nStatus
== SANE_STATUS_GOOD
);
735 if (nStatus
!= SANE_STATUS_EOF
|| !bSuccess
)
742 int nFrameLength
= ftell( pFrame
);
743 fseek( pFrame
, 0, SEEK_SET
);
744 sal_uInt32 nWidth
= static_cast<sal_uInt32
>(aParams
.pixels_per_line
);
745 sal_uInt32 nHeight
= static_cast<sal_uInt32
>(nFrameLength
/ aParams
.bytes_per_line
);
749 fResl
= 300; // if all else fails that's a good guess
751 nWidthMM
= static_cast<int>((static_cast<double>(nWidth
) / fResl
) * 25.4);
753 nHeightMM
= static_cast<int>((static_cast<double>(nHeight
) / fResl
) * 25.4);
754 SAL_INFO("extensions.scanner", "set dimensions to(" << nWidth
<< ", " << nHeight
<< ") Pixel, (" << nWidthMM
<< ", " << nHeightMM
<<
755 ") mm, resolution is " << fResl
);
757 aConverter
.Seek( 18 );
758 aConverter
.WriteUInt32( nWidth
);
759 aConverter
.WriteUInt32( nHeight
);
760 aConverter
.Seek( 38 );
761 aConverter
.WriteUInt32( 1000*nWidth
/nWidthMM
);
762 aConverter
.WriteUInt32( 1000*nHeight
/nHeightMM
);
767 if( eType
== FrameStyle_BW
)
769 aConverter
.Seek( 10 );
770 aConverter
.WriteUInt32( 64 );
771 aConverter
.Seek( 28 );
772 aConverter
.WriteUInt16( 1 );
773 aConverter
.Seek( 54 );
775 aConverter
.WriteUInt16( 0xffff );
776 aConverter
.WriteUChar( 0xff );
777 aConverter
.WriteUChar( 0 );
778 aConverter
.WriteUInt32( 0 );
779 aConverter
.Seek( 64 );
781 else if( eType
== FrameStyle_Gray
)
783 aConverter
.Seek( 10 );
784 aConverter
.WriteUInt32( 1084 );
785 aConverter
.Seek( 28 );
786 aConverter
.WriteUInt16( 8 );
787 aConverter
.Seek( 54 );
789 for( nLine
= 0; nLine
< 256; nLine
++ )
791 aConverter
.WriteUChar( nLine
);
792 aConverter
.WriteUChar( nLine
);
793 aConverter
.WriteUChar( nLine
);
794 aConverter
.WriteUChar( 0 );
796 aConverter
.Seek( 1084 );
799 for (nLine
= nHeight
-1; nLine
>= 0; --nLine
)
801 if (fseek(pFrame
, nLine
* aParams
.bytes_per_line
, SEEK_SET
) == -1)
806 if( eType
== FrameStyle_BW
||
807 ( eType
== FrameStyle_Gray
&& aParams
.depth
== 8 )
810 SANE_Int items_read
= fread( pBuffer
.get(), 1, aParams
.bytes_per_line
, pFrame
);
811 if (items_read
!= aParams
.bytes_per_line
)
813 SAL_WARN( "extensions.scanner", "short read, padding with zeros" );
814 memset(pBuffer
.get() + items_read
, 0, aParams
.bytes_per_line
- items_read
);
816 aConverter
.WriteBytes(pBuffer
.get(), aParams
.bytes_per_line
);
818 else if( eType
== FrameStyle_Gray
)
820 for( i
= 0; i
< (aParams
.pixels_per_line
); i
++ )
822 sal_uInt8 nGray
= ReadValue( pFrame
, aParams
.depth
);
823 aConverter
.WriteUChar( nGray
);
826 else if( eType
== FrameStyle_RGB
)
828 for( i
= 0; i
< (aParams
.pixels_per_line
); i
++ )
830 sal_uInt8 nRed
, nGreen
, nBlue
;
831 nRed
= ReadValue( pFrame
, aParams
.depth
);
832 nGreen
= ReadValue( pFrame
, aParams
.depth
);
833 nBlue
= ReadValue( pFrame
, aParams
.depth
);
834 aConverter
.WriteUChar( nBlue
);
835 aConverter
.WriteUChar( nGreen
);
836 aConverter
.WriteUChar( nRed
);
839 else if( eType
== FrameStyle_Separated
)
841 for( i
= 0; i
< (aParams
.pixels_per_line
); i
++ )
843 sal_uInt8 nValue
= ReadValue( pFrame
, aParams
.depth
);
844 switch( aParams
.format
)
847 aConverter
.SeekRel( 2 );
848 aConverter
.WriteUChar( nValue
);
850 case SANE_FRAME_GREEN
:
851 aConverter
.SeekRel( 1 );
852 aConverter
.WriteUChar( nValue
);
853 aConverter
.SeekRel( 1 );
855 case SANE_FRAME_BLUE
:
856 aConverter
.WriteUChar( nValue
);
857 aConverter
.SeekRel( 2 );
859 case SANE_FRAME_GRAY
:
865 int nGap
= aConverter
.Tell() & 3;
867 aConverter
.SeekRel( 4-nGap
);
869 fclose( pFrame
); // deletes tmpfile
870 if( eType
!= FrameStyle_Separated
)
877 int nPos
= aConverter
.TellEnd();
879 aConverter
.Seek( 2 );
880 aConverter
.WriteUInt32( nPos
+1 );
881 aConverter
.Seek( 0 );
887 // only cancel a successful operation
888 // sane disrupts memory else
889 p_cancel( maHandle
);
890 CheckConsistency( "sane_cancel" );
897 dbg_msg( "Sane::Start returns with %s\n", bSuccess
? "TRUE" : "FALSE" );
902 int Sane::GetRange( int n
, std::unique_ptr
<double[]>& rpDouble
)
904 if( mppOptions
[n
]->constraint_type
!= SANE_CONSTRAINT_RANGE
&&
905 mppOptions
[n
]->constraint_type
!= SANE_CONSTRAINT_WORD_LIST
)
912 bool bIsFixed
= mppOptions
[n
]->type
== SANE_TYPE_FIXED
;
914 dbg_msg( "Sane::GetRange of option %s ", mppOptions
[n
]->name
);
915 if(mppOptions
[n
]->constraint_type
== SANE_CONSTRAINT_RANGE
)
917 double fMin
, fMax
, fQuant
;
920 fMin
= SANE_UNFIX( mppOptions
[n
]->constraint
.range
->min
);
921 fMax
= SANE_UNFIX( mppOptions
[n
]->constraint
.range
->max
);
922 fQuant
= SANE_UNFIX( mppOptions
[n
]->constraint
.range
->quant
);
926 fMin
= static_cast<double>(mppOptions
[n
]->constraint
.range
->min
);
927 fMax
= static_cast<double>(mppOptions
[n
]->constraint
.range
->max
);
928 fQuant
= static_cast<double>(mppOptions
[n
]->constraint
.range
->quant
);
932 dbg_msg( "quantum range [ %lg ; %lg ; %lg ]\n",
933 fMin
, fQuant
, fMax
);
934 nItems
= static_cast<int>((fMax
- fMin
)/fQuant
)+1;
935 rpDouble
.reset(new double[ nItems
]);
936 double fValue
= fMin
;
937 for( i
= 0; i
< nItems
; i
++, fValue
+= fQuant
)
938 rpDouble
[i
] = fValue
;
939 rpDouble
[ nItems
-1 ] = fMax
;
944 dbg_msg( "normal range [ %lg %lg ]\n",
946 rpDouble
.reset(new double[2]);
954 nItems
= mppOptions
[n
]->constraint
.word_list
[0];
955 rpDouble
.reset(new double[nItems
]);
956 for( i
=0; i
<nItems
; i
++ )
958 rpDouble
[i
] = bIsFixed
?
959 SANE_UNFIX( mppOptions
[n
]->constraint
.word_list
[i
+1] ) :
960 static_cast<double>(mppOptions
[n
]->constraint
.word_list
[i
+1]);
962 dbg_msg( "wordlist [ %lg ... %lg ]\n",
963 rpDouble
[ 0 ], rpDouble
[ nItems
-1 ] );
968 static const char *ppUnits
[] = {
978 OUString
Sane::GetOptionUnitName( int n
)
981 SANE_Unit nUnit
= mppOptions
[n
]->unit
;
982 size_t nUnitAsSize
= static_cast<size_t>(nUnit
);
983 if (nUnitAsSize
>= SAL_N_ELEMENTS( ppUnits
))
984 aText
= "[unknown units]";
986 aText
= OUString( ppUnits
[ nUnit
], strlen(ppUnits
[ nUnit
]), osl_getThreadTextEncoding() );
990 bool Sane::ActivateButtonOption( int n
)
992 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, nullptr );
993 return nStatus
== SANE_STATUS_GOOD
;
996 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */