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 .
23 #include <sal/log.hxx>
24 #include <tools/stream.hxx>
25 #include <unotools/tempfile.hxx>
31 #include <sys/types.h>
32 #include <sal/config.h>
33 #include <sal/macros.h>
34 #include <boost/scoped_array.hpp>
36 #if (OSL_DEBUG_LEVEL > 1) || 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 inline void dbg_msg( const char* pString
, ... )
44 #if (OSL_DEBUG_LEVEL > 1) || 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
= 0;
80 SANE_Int
Sane::nVersion
= 0;
81 SANE_Device
** Sane::ppDevices
= 0;
82 int Sane::nDevices
= 0;
84 SANE_Status (*Sane::p_init
)( SANE_Int
*,
85 SANE_Auth_Callback
) = 0;
86 void (*Sane::p_exit
)() = 0;
87 SANE_Status (*Sane::p_get_devices
)( const SANE_Device
***,
89 SANE_Status (*Sane::p_open
)( SANE_String_Const
, SANE_Handle
) = 0;
90 void (*Sane::p_close
)( SANE_Handle
) = 0;
91 const SANE_Option_Descriptor
* (*Sane::p_get_option_descriptor
)(
92 SANE_Handle
, SANE_Int
) = 0;
93 SANE_Status (*Sane::p_control_option
)( SANE_Handle
, SANE_Int
,
96 SANE_Status (*Sane::p_get_parameters
)( SANE_Handle
,
97 SANE_Parameters
* ) = 0;
98 SANE_Status (*Sane::p_start
)( SANE_Handle
) = 0;
99 SANE_Status (*Sane::p_read
)( SANE_Handle
, SANE_Byte
*, SANE_Int
,
101 void (*Sane::p_cancel
)( SANE_Handle
) = 0;
102 SANE_Status (*Sane::p_set_io_mode
)( SANE_Handle
, SANE_Bool
) = 0;
103 SANE_Status (*Sane::p_get_select_fd
)( SANE_Handle
, SANE_Int
* ) = 0;
104 SANE_String_Const (*Sane::p_strstatus
)( SANE_Status
) = 0;
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
, (SANE_Int
)nOption
,
126 nAction
, pData
, &nInfo
);
127 DUMP_STATE( nStatus
, "sane_control_option" );
128 #if OSL_DEBUG_LEVEL > 1
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
)
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
, 0 );
228 FAIL_SHUTDOWN_STATE( nStatus
, "sane_init", );
229 nStatus
= p_get_devices( (const SANE_Device
***)&ppDevices
,
231 FAIL_SHUTDOWN_STATE( nStatus
, "sane_get_devices", );
232 for( nDevices
= 0 ; ppDevices
[ nDevices
]; nDevices
++ ) ;
235 #if (OSL_DEBUG_LEVEL > 1) || 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 (void*)pOptions
, NULL
);
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( (size_t)pZero
->size
> sizeof( SANE_Word
) )
274 fprintf( stderr
, "driver returned numer of options with larger size tha SANE_Word !!!\n" );
276 delete [] mppOptions
;
277 mppOptions
= 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( NULL
, true );
284 maReloadOptionsLink
.Call( this );
287 bool Sane::Open( const char* name
)
289 SANE_Status nStatus
= p_open( (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
.equals( ppDevices
[i
]->name
) )
310 bool Sane::Open( int n
)
312 if( n
>= 0 && n
< nDevices
)
315 return Open( ppDevices
[n
]->name
);
325 delete [] mppOptions
;
332 int Sane::GetOptionByName( const char* rName
)
335 OString
aOption( rName
);
336 for( i
= 0; i
< mnOptions
; i
++ )
338 if( mppOptions
[i
]->name
&& aOption
.equals( mppOptions
[i
]->name
) )
344 bool Sane::GetOptionValue( int n
, bool& rRet
)
346 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
349 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, &nRet
);
350 if( nStatus
!= SANE_STATUS_GOOD
)
357 bool Sane::GetOptionValue( int n
, OString
& rRet
)
359 bool bSuccess
= false;
360 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_STRING
)
362 boost::scoped_array
<char> pRet(new char[mppOptions
[n
]->size
+1]);
363 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pRet
.get() );
364 if( nStatus
== SANE_STATUS_GOOD
)
372 bool Sane::GetOptionValue( int n
, double& rRet
, int nElement
)
374 bool bSuccess
= false;
376 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_INT
&&
377 mppOptions
[n
]->type
!= SANE_TYPE_FIXED
) )
380 boost::scoped_array
<SANE_Word
> pRet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
381 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pRet
.get() );
382 if( nStatus
== SANE_STATUS_GOOD
)
385 if( mppOptions
[n
]->type
== SANE_TYPE_INT
)
386 rRet
= (double)pRet
[ nElement
];
388 rRet
= SANE_UNFIX( pRet
[nElement
] );
393 bool Sane::GetOptionValue( int n
, double* pSet
)
395 if( ! maHandle
|| ! ( mppOptions
[n
]->type
== SANE_TYPE_FIXED
||
396 mppOptions
[n
]->type
== SANE_TYPE_INT
) )
399 boost::scoped_array
<SANE_Word
> pFixedSet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
400 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pFixedSet
.get() );
401 if( nStatus
!= SANE_STATUS_GOOD
)
403 for( size_t i
= 0; i
<mppOptions
[n
]->size
/sizeof(SANE_Word
); i
++ )
405 if( mppOptions
[n
]->type
== SANE_TYPE_FIXED
)
406 pSet
[i
] = SANE_UNFIX( pFixedSet
[i
] );
408 pSet
[i
] = (double) pFixedSet
[i
];
413 bool Sane::SetOptionValue( int n
, bool bSet
)
415 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_BOOL
)
417 SANE_Word nRet
= bSet
? SANE_TRUE
: SANE_FALSE
;
418 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, &nRet
);
419 if( nStatus
!= SANE_STATUS_GOOD
)
424 bool Sane::SetOptionValue( int n
, const OUString
& rSet
)
426 if( ! maHandle
|| mppOptions
[n
]->type
!= SANE_TYPE_STRING
)
428 OString
aSet(OUStringToOString(rSet
, osl_getThreadTextEncoding()));
429 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, (void*)aSet
.getStr() );
430 if( nStatus
!= SANE_STATUS_GOOD
)
435 bool Sane::SetOptionValue( int n
, double fSet
, int nElement
)
437 bool bSuccess
= false;
439 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_INT
&&
440 mppOptions
[n
]->type
!= SANE_TYPE_FIXED
) )
444 if( mppOptions
[n
]->size
/sizeof(SANE_Word
) > 1 )
446 boost::scoped_array
<SANE_Word
> pSet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
447 nStatus
= ControlOption( n
, SANE_ACTION_GET_VALUE
, pSet
.get() );
448 if( nStatus
== SANE_STATUS_GOOD
)
450 pSet
[nElement
] = mppOptions
[n
]->type
== SANE_TYPE_INT
?
451 (SANE_Word
)fSet
: SANE_FIX( fSet
);
452 nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, pSet
.get() );
458 mppOptions
[n
]->type
== SANE_TYPE_INT
?
459 (SANE_Word
)fSet
: SANE_FIX( fSet
);
461 nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, &nSetTo
);
462 if( nStatus
== SANE_STATUS_GOOD
)
468 bool Sane::SetOptionValue( int n
, double* pSet
)
470 if( ! maHandle
|| ( mppOptions
[n
]->type
!= SANE_TYPE_INT
&&
471 mppOptions
[n
]->type
!= SANE_TYPE_FIXED
) )
473 boost::scoped_array
<SANE_Word
> pFixedSet(new SANE_Word
[mppOptions
[n
]->size
/sizeof(SANE_Word
)]);
474 for( size_t i
= 0; i
< mppOptions
[n
]->size
/sizeof(SANE_Word
); i
++ )
476 if( mppOptions
[n
]->type
== SANE_TYPE_FIXED
)
477 pFixedSet
[i
] = SANE_FIX( pSet
[i
] );
479 pFixedSet
[i
] = (SANE_Word
)pSet
[i
];
481 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, pFixedSet
.get() );
482 if( nStatus
!= SANE_STATUS_GOOD
)
487 enum FrameStyleType
{
488 FrameStyle_BW
, FrameStyle_Gray
, FrameStyle_RGB
, FrameStyle_Separated
491 #define BYTE_BUFFER_SIZE 32768
493 static inline sal_uInt8
_ReadValue( FILE* fp
, int depth
)
498 // data always come in native byte order !
499 // 16 bits is not really supported by backends as of now
500 // e.g. UMAX Astra 1200S delivers 16 bit but in BIGENDIAN
501 // against SANE documentation (xscanimage gets the same result
503 size_t items_read
= fread( &nWord
, 1, 2, fp
);
507 SAL_WARN( "extensions.scanner", "short read, abandoning" );
511 return (sal_uInt8
)( nWord
/ 256 );
514 size_t items_read
= fread( &nByte
, 1, 1, fp
);
517 SAL_WARN( "extensions.scanner", "short read, abandoning" );
523 bool Sane::CheckConsistency( const char* pMes
, bool bInit
)
525 static const SANE_Option_Descriptor
** pDescArray
= NULL
;
526 static const SANE_Option_Descriptor
* pZero
= NULL
;
530 pDescArray
= mppOptions
;
532 pZero
= mppOptions
[0];
536 bool bConsistent
= true;
538 if( pDescArray
!= mppOptions
)
540 if( pZero
!= mppOptions
[0] )
544 dbg_msg( "Sane is not consistent. (%s)\n", pMes
);
549 bool Sane::Start( BitmapTransporter
& rBitmap
)
551 int nStream
= 0, nLine
= 0, i
= 0;
552 SANE_Parameters aParams
;
553 FrameStyleType eType
= FrameStyle_Gray
;
554 bool bSuccess
= true;
555 bool bWidthSet
= false;
562 double fTLx
, fTLy
, fResl
= 0.0;
564 if( ( nOption
= GetOptionByName( "tl-x" ) ) != -1 &&
565 GetOptionValue( nOption
, fTLx
, 0 ) &&
566 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
569 if( ( nOption
= GetOptionByName( "br-x" ) ) != -1 &&
570 GetOptionValue( nOption
, fBRx
, 0 ) &&
571 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
573 nWidthMM
= (int)fabs(fBRx
- fTLx
);
576 if( ( nOption
= GetOptionByName( "tl-y" ) ) != -1 &&
577 GetOptionValue( nOption
, fTLy
, 0 ) &&
578 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
581 if( ( nOption
= GetOptionByName( "br-y" ) ) != -1 &&
582 GetOptionValue( nOption
, fBRy
, 0 ) &&
583 GetOptionUnit( nOption
) == SANE_UNIT_MM
)
585 nHeightMM
= (int)fabs(fBRy
- fTLy
);
588 if( ( nOption
= GetOptionByName( "resolution" ) ) != -1 )
589 (void)GetOptionValue( nOption
, fResl
);
591 sal_uInt8
* pBuffer
= NULL
;
593 SANE_Status nStatus
= SANE_STATUS_GOOD
;
596 SvMemoryStream
& aConverter
= rBitmap
.getStream();
597 aConverter
.Seek( 0 );
598 aConverter
.SetEndian( SvStreamEndian::LITTLE
);
600 // write bitmap stream header
601 aConverter
.WriteChar( 'B' ).WriteChar( 'M' );
602 aConverter
.WriteUInt32( 0 );
603 aConverter
.WriteUInt32( 0 );
604 aConverter
.WriteUInt32( 60 );
606 // write BITMAPINFOHEADER
607 aConverter
.WriteUInt32( 40 );
608 aConverter
.WriteUInt32( 0 ); // fill in width later
609 aConverter
.WriteUInt32( 0 ); // fill in height later
610 aConverter
.WriteUInt16( 1 );
611 // create header for 24 bits
612 // correct later if necessary
613 aConverter
.WriteUInt16( 24 );
614 aConverter
.WriteUInt32( 0 );
615 aConverter
.WriteUInt32( 0 );
616 aConverter
.WriteUInt32( 0 );
617 aConverter
.WriteUInt32( 0 );
618 aConverter
.WriteUInt32( 0 );
619 aConverter
.WriteUInt32( 0 );
621 for( nStream
=0; nStream
< 3 && bSuccess
; nStream
++ )
623 nStatus
= p_start( maHandle
);
624 DUMP_STATE( nStatus
, "sane_start" );
625 CheckConsistency( "sane_start" );
626 if( nStatus
== SANE_STATUS_GOOD
)
628 nStatus
= p_get_parameters( maHandle
, &aParams
);
629 DUMP_STATE( nStatus
, "sane_get_parameters" );
630 CheckConsistency( "sane_get_parameters" );
631 if (nStatus
!= SANE_STATUS_GOOD
|| aParams
.bytes_per_line
== 0)
636 #if (OSL_DEBUG_LEVEL > 1) || defined DBG_UTIL
637 const char* ppFormats
[] = { "SANE_FRAME_GRAY", "SANE_FRAME_RGB",
638 "SANE_FRAME_RED", "SANE_FRAME_GREEN",
639 "SANE_FRAME_BLUE", "Unknown !!!" };
640 fprintf( stderr
, "Parameters for frame %d:\n", nStream
);
641 if( aParams
.format
< 0 || aParams
.format
> 4 )
642 aParams
.format
= (SANE_Frame
)5;
643 fprintf( stderr
, "format: %s\n", ppFormats
[ (int)aParams
.format
] );
644 fprintf( stderr
, "last_frame: %s\n", aParams
.last_frame
? "TRUE" : "FALSE" );
645 fprintf( stderr
, "depth: %d\n", (int)aParams
.depth
);
646 fprintf( stderr
, "pixels_per_line: %d\n", (int)aParams
.pixels_per_line
);
647 fprintf( stderr
, "bytes_per_line: %d\n", (int)aParams
.bytes_per_line
);
651 pBuffer
= new sal_uInt8
[ BYTE_BUFFER_SIZE
< 4*aParams
.bytes_per_line
? 4*aParams
.bytes_per_line
: BYTE_BUFFER_SIZE
];
654 if( aParams
.last_frame
)
657 switch( aParams
.format
)
659 case SANE_FRAME_GRAY
:
660 eType
= FrameStyle_Gray
;
661 if( aParams
.depth
== 1 )
662 eType
= FrameStyle_BW
;
665 eType
= FrameStyle_RGB
;
668 case SANE_FRAME_GREEN
:
669 case SANE_FRAME_BLUE
:
670 eType
= FrameStyle_Separated
;
673 fprintf( stderr
, "Warning: unknown frame style !!!\n" );
676 bool bSynchronousRead
= true;
678 // should be fail safe, but ... ??
679 nStatus
= p_set_io_mode( maHandle
, SANE_FALSE
);
680 CheckConsistency( "sane_set_io_mode" );
681 if( nStatus
!= SANE_STATUS_GOOD
)
683 bSynchronousRead
= false;
684 nStatus
= p_set_io_mode( maHandle
, SANE_TRUE
);
685 CheckConsistency( "sane_set_io_mode" );
686 SAL_WARN_IF(nStatus
!= SANE_STATUS_GOOD
, "extensions.scanner", "driver is confused" ); (void)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::TempFile aFrame
;
701 aFrame
.EnableKillingFile();
702 FILE* pFrame
= fopen(OUStringToOString(aFrame
.GetFileName(), osl_getThreadTextEncoding()).getStr(), "w+b");
709 if( ! bSynchronousRead
)
715 FD_SET( (int)fd
, &fdset
);
718 if( select( fd
+1, &fdset
, NULL
, NULL
, &tv
) == 0 )
719 fprintf( stderr
, "Timout on sane_read descriptor\n" );
722 nStatus
= p_read( maHandle
, pBuffer
, 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
, 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
= (sal_uInt32
) aParams
.pixels_per_line
;
744 sal_uInt32 nHeight
= (sal_uInt32
) (nFrameLength
/ aParams
.bytes_per_line
);
748 fResl
= 300; // if all else fails that's a good guess
750 nWidthMM
= (int)(((double)nWidth
/ fResl
) * 25.4);
752 nHeightMM
= (int)(((double)nHeight
/ fResl
) * 25.4);
753 #if OSL_DEBUG_LEVEL > 1
754 fprintf( stderr
, "set dimensions to (%d, %d) Pixel, (%d, %d) mm, resolution is %lg\n", (int)nWidth
, (int)nHeight
, (int)nWidthMM
, (int)nHeightMM
, 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 fseek( pFrame
, nLine
* aParams
.bytes_per_line
, SEEK_SET
);
802 if( eType
== FrameStyle_BW
||
803 ( eType
== FrameStyle_Gray
&& aParams
.depth
== 8 )
806 SANE_Int items_read
= fread( pBuffer
, 1, aParams
.bytes_per_line
, pFrame
);
807 if (items_read
!= aParams
.bytes_per_line
)
809 SAL_WARN( "extensions.scanner", "short read, padding with zeros" );
810 memset(pBuffer
+ items_read
, 0, aParams
.bytes_per_line
- items_read
);
812 aConverter
.Write( pBuffer
, aParams
.bytes_per_line
);
814 else if( eType
== FrameStyle_Gray
)
816 for( i
= 0; i
< (aParams
.pixels_per_line
); i
++ )
818 sal_uInt8 nGray
= _ReadValue( pFrame
, aParams
.depth
);
819 aConverter
.WriteUChar( nGray
);
822 else if( eType
== FrameStyle_RGB
)
824 for( i
= 0; i
< (aParams
.pixels_per_line
); i
++ )
826 sal_uInt8 nRed
, nGreen
, nBlue
;
827 nRed
= _ReadValue( pFrame
, aParams
.depth
);
828 nGreen
= _ReadValue( pFrame
, aParams
.depth
);
829 nBlue
= _ReadValue( pFrame
, aParams
.depth
);
830 aConverter
.WriteUChar( nBlue
);
831 aConverter
.WriteUChar( nGreen
);
832 aConverter
.WriteUChar( nRed
);
835 else if( eType
== FrameStyle_Separated
)
837 for( i
= 0; i
< (aParams
.pixels_per_line
); i
++ )
839 sal_uInt8 nValue
= _ReadValue( pFrame
, aParams
.depth
);
840 switch( aParams
.format
)
843 aConverter
.SeekRel( 2 );
844 aConverter
.WriteUChar( nValue
);
846 case SANE_FRAME_GREEN
:
847 aConverter
.SeekRel( 1 );
848 aConverter
.WriteUChar( nValue
);
849 aConverter
.SeekRel( 1 );
851 case SANE_FRAME_BLUE
:
852 aConverter
.WriteUChar( nValue
);
853 aConverter
.SeekRel( 2 );
855 case SANE_FRAME_GRAY
:
861 int nGap
= aConverter
.Tell() & 3;
863 aConverter
.SeekRel( 4-nGap
);
865 fclose( pFrame
); // deletes tmpfile
866 if( eType
!= FrameStyle_Separated
)
873 aConverter
.Seek( STREAM_SEEK_TO_END
);
874 int nPos
= aConverter
.Tell();
876 aConverter
.Seek( 2 );
877 aConverter
.WriteUInt32( nPos
+1 );
878 aConverter
.Seek( 0 );
884 // only cancel a successful operation
885 // sane disrupts memory else
886 p_cancel( maHandle
);
887 CheckConsistency( "sane_cancel" );
895 dbg_msg( "Sane::Start returns with %s\n", bSuccess
? "TRUE" : "FALSE" );
900 int Sane::GetRange( int n
, double*& rpDouble
)
902 if( mppOptions
[n
]->constraint_type
!= SANE_CONSTRAINT_RANGE
&&
903 mppOptions
[n
]->constraint_type
!= SANE_CONSTRAINT_WORD_LIST
)
910 bool bIsFixed
= mppOptions
[n
]->type
== SANE_TYPE_FIXED
;
912 dbg_msg( "Sane::GetRange of option %s ", mppOptions
[n
]->name
);
913 if(mppOptions
[n
]->constraint_type
== SANE_CONSTRAINT_RANGE
)
915 double fMin
, fMax
, fQuant
;
918 fMin
= SANE_UNFIX( mppOptions
[n
]->constraint
.range
->min
);
919 fMax
= SANE_UNFIX( mppOptions
[n
]->constraint
.range
->max
);
920 fQuant
= SANE_UNFIX( mppOptions
[n
]->constraint
.range
->quant
);
924 fMin
= (double)mppOptions
[n
]->constraint
.range
->min
;
925 fMax
= (double)mppOptions
[n
]->constraint
.range
->max
;
926 fQuant
= (double)mppOptions
[n
]->constraint
.range
->quant
;
930 dbg_msg( "quantum range [ %lg ; %lg ; %lg ]\n",
931 fMin
, fQuant
, fMax
);
932 nItems
= (int)((fMax
- fMin
)/fQuant
)+1;
933 rpDouble
= new double[ nItems
];
934 double fValue
= fMin
;
935 for( i
= 0; i
< nItems
; i
++, fValue
+= fQuant
)
936 rpDouble
[i
] = fValue
;
937 rpDouble
[ nItems
-1 ] = fMax
;
942 dbg_msg( "normal range [ %lg %lg ]\n",
944 rpDouble
= new double[2];
952 nItems
= mppOptions
[n
]->constraint
.word_list
[0];
953 rpDouble
= new double[nItems
];
954 for( i
=0; i
<nItems
; i
++ )
956 rpDouble
[i
] = bIsFixed
?
957 SANE_UNFIX( mppOptions
[n
]->constraint
.word_list
[i
+1] ) :
958 (double)mppOptions
[n
]->constraint
.word_list
[i
+1];
960 dbg_msg( "wordlist [ %lg ... %lg ]\n",
961 rpDouble
[ 0 ], rpDouble
[ nItems
-1 ] );
966 static const char *ppUnits
[] = {
976 OUString
Sane::GetOptionUnitName( int n
)
979 SANE_Unit nUnit
= mppOptions
[n
]->unit
;
980 size_t nUnitAsSize
= (size_t)nUnit
;
981 if (nUnitAsSize
>= SAL_N_ELEMENTS( ppUnits
))
982 aText
= "[unknown units]";
984 aText
= OUString( ppUnits
[ nUnit
], strlen(ppUnits
[ nUnit
]), osl_getThreadTextEncoding() );
988 bool Sane::ActivateButtonOption( int n
)
990 SANE_Status nStatus
= ControlOption( n
, SANE_ACTION_SET_VALUE
, NULL
);
991 if( nStatus
!= SANE_STATUS_GOOD
)
996 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */