Avoid potential negative array index access to cached text.
[LibreOffice.git] / extensions / source / scanner / sane.cxx
blob0e92a87964338d21fdb52f7ee14f78c8d7f9565e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <type_traits>
21 #include <math.h>
23 #include <o3tl/safeint.hxx>
24 #include <osl/file.h>
25 #include <sal/log.hxx>
26 #include <tools/stream.hxx>
27 #include <unotools/tempfile.hxx>
28 #include "sane.hxx"
29 #include <dlfcn.h>
30 #include <stdio.h>
31 #include <sys/time.h>
32 #include <sal/config.h>
33 #include <sal/macros.h>
34 #include <memory>
36 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
37 #include <stdarg.h>
38 #define dump_state( a, b, c, d ) fprintf( stderr, a, b, c, d );
39 #else
40 #define dump_state( a, b, c, d ) ;
41 #endif
42 static void dbg_msg( const char* pString, ... )
44 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
45 va_list ap;
46 va_start( ap, pString );
47 vfprintf( stderr, pString, ap );
48 va_end( ap );
49 #else
50 (void)pString;
51 #endif
54 #define FAIL_SHUTDOWN_STATE( x, y, z ) \
55 if( x != SANE_STATUS_GOOD ) \
56 { \
57 dump_state( "%s returned error %d (%s)\n", \
58 y, x, p_strstatus( x ) ); \
59 DeInit(); \
60 return z; \
63 #define FAIL_STATE( x, y, z ) \
64 if( x != SANE_STATUS_GOOD ) \
65 { \
66 dump_state( "%s returned error %d (%s)\n", \
67 y, x, p_strstatus( x ) ); \
68 return z; \
71 #define DUMP_STATE( x, y ) \
72 if( x != SANE_STATUS_GOOD ) \
73 { \
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,
94 SANE_Action, void*,
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 );
111 if( ! pFunction )
113 fprintf( stderr, "Could not load symbol %s\n",
114 pSymbolname );
115 bSaneSymbolLoadFailed = true;
117 return pFunction;
120 SANE_Status Sane::ControlOption( int nOption, SANE_Action nAction,
121 void* pData )
123 SANE_Int nInfo = 0;
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";
132 switch( nAction )
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(),
143 pAction );
145 #endif
146 if( nInfo & SANE_INFO_RELOAD_OPTIONS )
147 ReloadOptions();
148 return nStatus;
151 Sane::Sane() :
152 mnOptions( 0 ),
153 mnDevice( -1 ),
154 maHandle( nullptr )
156 if( ! nRefCount || ! pSaneLib )
157 Init();
158 nRefCount++;
161 Sane::~Sane()
163 if( IsOpen() )
164 Close();
165 nRefCount--;
166 if( ! nRefCount && pSaneLib )
167 DeInit();
170 void Sane::Init()
172 #ifndef DISABLE_DYNLOADING
173 OUString sSaneLibName( "libsane" SAL_DLLEXTENSION );
174 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
175 if( ! pSaneLib )
177 sSaneLibName = "libsane" SAL_DLLEXTENSION ".1";
178 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
180 // try reasonable places that might not be in the library search path
181 if( ! pSaneLib )
183 OUString sSaneLibSystemPath( "/usr/local/lib/libsane" SAL_DLLEXTENSION );
184 osl_getFileURLFromSystemPath( sSaneLibSystemPath.pData, &sSaneLibName.pData );
185 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
187 #endif
188 if( pSaneLib )
190 bSaneSymbolLoadFailed = false;
191 p_init = reinterpret_cast<SANE_Status(*)(SANE_Int*, SANE_Auth_Callback )>(
192 LoadSymbol( "sane_init" ));
193 p_exit = reinterpret_cast<void(*)()>(
194 LoadSymbol( "sane_exit" ));
195 p_get_devices = reinterpret_cast<SANE_Status(*)(const SANE_Device***,
196 SANE_Bool )>(
197 LoadSymbol( "sane_get_devices" ));
198 p_open = reinterpret_cast<SANE_Status(*)(SANE_String_Const, SANE_Handle )>(
199 LoadSymbol( "sane_open" ));
200 p_close = reinterpret_cast<void(*)(SANE_Handle)>(
201 LoadSymbol( "sane_close" ));
202 p_get_option_descriptor = reinterpret_cast<const SANE_Option_Descriptor*(*)(SANE_Handle,
203 SANE_Int)>(
204 LoadSymbol( "sane_get_option_descriptor" ));
205 p_control_option = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Int,
206 SANE_Action, void*, SANE_Int*)>(
207 LoadSymbol( "sane_control_option" ));
208 p_get_parameters = reinterpret_cast<SANE_Status(*)(SANE_Handle,SANE_Parameters*)>(
209 LoadSymbol( "sane_get_parameters" ));
210 p_start = reinterpret_cast<SANE_Status(*)(SANE_Handle)>(
211 LoadSymbol( "sane_start" ));
212 p_read = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Byte*,
213 SANE_Int, SANE_Int* )>(
214 LoadSymbol( "sane_read" ));
215 p_cancel = reinterpret_cast<void(*)(SANE_Handle)>(
216 LoadSymbol( "sane_cancel" ));
217 p_set_io_mode = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Bool)>(
218 LoadSymbol( "sane_set_io_mode" ));
219 p_get_select_fd = reinterpret_cast<SANE_Status(*)(SANE_Handle, SANE_Int*)>(
220 LoadSymbol( "sane_get_select_fd" ));
221 p_strstatus = reinterpret_cast<SANE_String_Const(*)(SANE_Status)>(
222 LoadSymbol( "sane_strstatus" ));
223 if( bSaneSymbolLoadFailed )
224 DeInit();
225 else
227 SANE_Status nStatus = p_init( &nVersion, nullptr );
228 FAIL_SHUTDOWN_STATE( nStatus, "sane_init", );
229 nStatus = p_get_devices( const_cast<const SANE_Device***>(&ppDevices),
230 SANE_FALSE );
231 FAIL_SHUTDOWN_STATE( nStatus, "sane_get_devices", );
232 for( nDevices = 0 ; ppDevices[ nDevices ]; nDevices++ ) ;
235 #if (OSL_DEBUG_LEVEL > 0) || defined DBG_UTIL
236 else
237 fprintf( stderr, "libsane%s could not be opened: %s\n", SAL_DLLEXTENSION,
238 dlerror() );
239 #endif
242 void Sane::DeInit()
244 if( pSaneLib )
246 p_exit();
247 #ifndef DISABLE_DYNLOADING
248 osl_unloadModule( pSaneLib );
249 #endif
250 pSaneLib = nullptr;
254 void Sane::ReloadDevices()
256 if( IsOpen() )
257 Close();
258 DeInit();
259 Init();
262 void Sane::ReloadOptions()
264 if( ! IsOpen() )
265 return;
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 );
292 ReloadOptions();
294 if( mnDevice == -1 )
296 OString aDevice( name );
297 for( int i = 0; i < nDevices; i++ )
299 if( aDevice == ppDevices[i]->name )
301 mnDevice = i;
302 break;
307 return true;
310 bool Sane::Open( int n )
312 if( n >= 0 && n < nDevices )
314 mnDevice = n;
315 return Open( ppDevices[n]->name );
317 return false;
320 void Sane::Close()
322 if( maHandle )
324 p_close( maHandle );
325 mppOptions.reset();
326 maHandle = nullptr;
327 mnDevice = -1;
331 int Sane::GetOptionByName( const char* rName )
333 int i;
334 OString aOption( rName );
335 for( i = 0; i < mnOptions; i++ )
337 if( mppOptions[i]->name && aOption == mppOptions[i]->name )
338 return i;
340 return -1;
343 bool Sane::GetOptionValue( int n, bool& rRet )
345 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
346 return false;
347 SANE_Word nRet;
348 SANE_Status nStatus = ControlOption( n, SANE_ACTION_GET_VALUE, &nRet );
349 if( nStatus != SANE_STATUS_GOOD )
350 return false;
352 rRet = nRet;
353 return true;
356 bool Sane::GetOptionValue( int n, OString& rRet )
358 bool bSuccess = false;
359 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_STRING )
360 return false;
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 )
365 bSuccess = true;
366 rRet = pRet.get();
368 return bSuccess;
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 ) )
377 return false;
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 )
383 bSuccess = true;
384 if( mppOptions[n]->type == SANE_TYPE_INT )
385 rRet = static_cast<double>(pRet[ nElement ]);
386 else
387 rRet = SANE_UNFIX( pRet[nElement] );
389 return bSuccess;
392 bool Sane::GetOptionValue( int n, double* pSet )
394 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_FIXED &&
395 mppOptions[n]->type != SANE_TYPE_INT ) )
396 return false;
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 )
401 return false;
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] );
406 else
407 pSet[i] = static_cast<double>(pFixedSet[i]);
409 return true;
412 void Sane::SetOptionValue( int n, bool bSet )
414 if( ! maHandle || mppOptions[n]->type != SANE_TYPE_BOOL )
415 return;
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 )
423 return;
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 ) )
432 return;
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() );
445 else
447 SANE_Word nSetTo =
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 ) )
459 return;
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] );
465 else
466 pFixedSet[i] = static_cast<SANE_Word>(pSet[i]);
468 ControlOption( n, SANE_ACTION_SET_VALUE, pFixedSet.get() );
471 namespace {
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 )
483 if( depth == 16 )
485 sal_uInt16 nWord;
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
490 // as we do
491 size_t items_read = fread( &nWord, 1, 2, fp );
493 if (items_read != 2)
495 SAL_WARN( "extensions.scanner", "short read, abandoning" );
496 return 0;
499 return static_cast<sal_uInt8>( nWord / 256 );
501 sal_uInt8 nByte;
502 size_t items_read = fread( &nByte, 1, 1, fp );
503 if (items_read != 1)
505 SAL_WARN( "extensions.scanner", "short read, abandoning" );
506 return 0;
508 return nByte;
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;
516 if( bInit )
518 pDescArray = mppOptions.get();
519 if( mppOptions )
520 pZero = mppOptions[0];
521 return true;
524 bool bConsistent = true;
526 if( pDescArray != mppOptions.get() )
527 bConsistent = false;
528 if( pZero != mppOptions[0] )
529 bConsistent = false;
531 if( ! bConsistent )
532 dbg_msg( "Sane is not consistent. (%s)\n", pMes );
534 return bConsistent;
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;
545 if( ! maHandle )
546 return false;
548 int nWidthMM = 0;
549 int nHeightMM = 0;
550 double fTLx, fTLy, fResl = 0.0;
551 int nOption;
552 nOption = GetOptionByName( "tl-x" );
553 if( nOption != -1 &&
554 GetOptionValue( nOption, fTLx ) &&
555 GetOptionUnit( nOption ) == SANE_UNIT_MM )
557 double fBRx;
558 nOption = GetOptionByName( "br-x" );
559 if( nOption != -1 &&
560 GetOptionValue( nOption, fBRx ) &&
561 GetOptionUnit( nOption ) == SANE_UNIT_MM )
563 nWidthMM = static_cast<int>(fabs(fBRx - fTLx));
566 nOption = GetOptionByName( "tl-y" );
567 if( nOption != -1 &&
568 GetOptionValue( nOption, fTLy ) &&
569 GetOptionUnit( nOption ) == SANE_UNIT_MM )
571 double fBRy;
572 nOption = GetOptionByName( "br-y" );
573 if( nOption != -1 &&
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;
587 rBitmap.lock();
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)
625 bSuccess = false;
626 break;
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 );
633 if( static_cast<
634 typename std::make_unsigned<
635 typename std::underlying_type<SANE_Frame>::type>::type>(
636 aParams.format)
637 > 4 )
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) );
646 #endif
647 if( ! pBuffer )
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 )
653 nStream=3;
655 switch( aParams.format )
657 case SANE_FRAME_GRAY:
658 eType = FrameStyle_Gray;
659 if( aParams.depth == 1 )
660 eType = FrameStyle_BW;
661 break;
662 case SANE_FRAME_RGB:
663 eType = FrameStyle_RGB;
664 break;
665 case SANE_FRAME_RED:
666 case SANE_FRAME_GREEN:
667 case SANE_FRAME_BLUE:
668 eType = FrameStyle_Separated;
669 break;
670 default:
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);
690 SANE_Int nLen=0;
691 SANE_Int fd = 0;
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");
704 if( ! pFrame )
706 bSuccess = false;
707 break;
709 do {
710 if( ! bSynchronousRead )
712 fd_set fdset;
713 struct timeval tv;
715 FD_ZERO( &fdset );
716 FD_SET( static_cast<int>(fd), &fdset );
717 tv.tv_sec = 5;
718 tv.tv_usec = 0;
719 if( select( fd+1, &fdset, nullptr, nullptr, &tv ) == 0 )
720 fprintf( stderr, "Timeout on sane_read descriptor\n" );
722 nLen = 0;
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 ));
729 if (!bSuccess)
730 break;
732 else
733 DUMP_STATE( nStatus, "sane_read" );
734 } while( nStatus == SANE_STATUS_GOOD );
735 if (nStatus != SANE_STATUS_EOF || !bSuccess)
737 fclose( pFrame );
738 bSuccess = false;
739 break;
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);
746 if( ! bWidthSet )
748 if( ! fResl )
749 fResl = 300; // if all else fails that's a good guess
750 if( ! nWidthMM )
751 nWidthMM = static_cast<int>((static_cast<double>(nWidth) / fResl) * 25.4);
752 if( ! nHeightMM )
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 );
763 bWidthSet = true;
765 aConverter.Seek(60);
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 );
774 // write color table
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 );
788 // write color table
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)
803 bSuccess = false;
804 break;
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 )
846 case SANE_FRAME_RED:
847 aConverter.SeekRel( 2 );
848 aConverter.WriteUChar( nValue );
849 break;
850 case SANE_FRAME_GREEN:
851 aConverter.SeekRel( 1 );
852 aConverter.WriteUChar( nValue );
853 aConverter.SeekRel( 1 );
854 break;
855 case SANE_FRAME_BLUE:
856 aConverter.WriteUChar( nValue );
857 aConverter.SeekRel( 2 );
858 break;
859 case SANE_FRAME_GRAY:
860 case SANE_FRAME_RGB:
861 break;
865 int nGap = aConverter.Tell() & 3;
866 if (nGap)
867 aConverter.SeekRel( 4-nGap );
869 fclose( pFrame ); // deletes tmpfile
870 if( eType != FrameStyle_Separated )
871 break;
873 else
874 bSuccess = false;
876 // get stream length
877 int nPos = aConverter.TellEnd();
879 aConverter.Seek( 2 );
880 aConverter.WriteUInt32( nPos+1 );
881 aConverter.Seek( 0 );
883 rBitmap.unlock();
885 if( bSuccess )
887 // only cancel a successful operation
888 // sane disrupts memory else
889 p_cancel( maHandle );
890 CheckConsistency( "sane_cancel" );
892 pBuffer.reset();
894 ReloadOptions();
897 dbg_msg( "Sane::Start returns with %s\n", bSuccess ? "TRUE" : "FALSE" );
899 return bSuccess;
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 )
907 return -1;
910 rpDouble = nullptr;
911 int nItems, i;
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;
918 if( bIsFixed )
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 );
924 else
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);
930 if( fQuant != 0.0 )
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;
940 return nItems;
942 else
944 dbg_msg( "normal range [ %lg %lg ]\n",
945 fMin, fMax );
946 rpDouble.reset(new double[2]);
947 rpDouble[0] = fMin;
948 rpDouble[1] = fMax;
949 return 0;
952 else
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 ] );
964 return nItems;
968 static const char *ppUnits[] = {
970 "[Pixel]",
971 "[Bit]",
972 "[mm]",
973 "[DPI]",
974 "[%]",
975 "[usec]"
978 OUString Sane::GetOptionUnitName( int n )
980 OUString aText;
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]";
985 else
986 aText = OUString( ppUnits[ nUnit ], strlen(ppUnits[ nUnit ]), osl_getThreadTextEncoding() );
987 return aText;
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: */