LibreOffice Module extensions (master) 1
sane.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <type_traits>
21#include <math.h>
22
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>
35
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
42static void dbg_msg( const char* pString, ... )
43{
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
52}
53
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; \
61 }
62
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; \
69 }
70
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 ) ); \
76 }
77
78int Sane::nRefCount = 0;
79oslModule Sane::pSaneLib = nullptr;
80SANE_Int Sane::nVersion = 0;
81SANE_Device** Sane::ppDevices = nullptr;
82int Sane::nDevices = 0;
83
84SANE_Status (*Sane::p_init)( SANE_Int*,
85 SANE_Auth_Callback ) = nullptr;
86void (*Sane::p_exit)() = nullptr;
87SANE_Status (*Sane::p_get_devices)( const SANE_Device***,
88 SANE_Bool ) = nullptr;
89SANE_Status (*Sane::p_open)( SANE_String_Const, SANE_Handle ) = nullptr;
90void (*Sane::p_close)( SANE_Handle ) = nullptr;
91const SANE_Option_Descriptor* (*Sane::p_get_option_descriptor)(
92 SANE_Handle, SANE_Int ) = nullptr;
93SANE_Status (*Sane::p_control_option)( SANE_Handle, SANE_Int,
94 SANE_Action, void*,
95 SANE_Int* ) = nullptr;
96SANE_Status (*Sane::p_get_parameters)( SANE_Handle,
97 SANE_Parameters* ) = nullptr;
98SANE_Status (*Sane::p_start)( SANE_Handle ) = nullptr;
99SANE_Status (*Sane::p_read)( SANE_Handle, SANE_Byte*, SANE_Int,
100 SANE_Int* ) = nullptr;
101void (*Sane::p_cancel)( SANE_Handle ) = nullptr;
102SANE_Status (*Sane::p_set_io_mode)( SANE_Handle, SANE_Bool ) = nullptr;
103SANE_Status (*Sane::p_get_select_fd)( SANE_Handle, SANE_Int* ) = nullptr;
104SANE_String_Const (*Sane::p_strstatus)( SANE_Status ) = nullptr;
105
106static bool bSaneSymbolLoadFailed = false;
107
108inline oslGenericFunction Sane::LoadSymbol( const char* pSymbolname )
109{
110 oslGenericFunction pFunction = osl_getAsciiFunctionSymbol( pSaneLib, pSymbolname );
111 if( ! pFunction )
112 {
113 fprintf( stderr, "Could not load symbol %s\n",
114 pSymbolname );
116 }
117 return pFunction;
118}
119
120SANE_Status Sane::ControlOption( int nOption, SANE_Action nAction,
121 void* pData )
122{
123 SANE_Int nInfo = 0;
124
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 )
130 {
131 const char* pAction = "Unknown";
132 switch( nAction )
133 {
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;
140 }
141 dbg_msg( "Option: \"%s\" action: %s\n",
142 OUStringToOString(GetOptionName(nOption), osl_getThreadTextEncoding()).getStr(),
143 pAction );
144 }
145#endif
146 if( nInfo & SANE_INFO_RELOAD_OPTIONS )
148 return nStatus;
149}
150
152 mnOptions( 0 ),
153 mnDevice( -1 ),
154 maHandle( nullptr )
155{
156 if( ! nRefCount || ! pSaneLib )
157 Init();
158 nRefCount++;
159};
160
162{
163 if( IsOpen() )
164 Close();
165 nRefCount--;
166 if( ! nRefCount && pSaneLib )
167 DeInit();
168}
169
171{
172#ifndef DISABLE_DYNLOADING
173 OUString sSaneLibName( "libsane" SAL_DLLEXTENSION );
174 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
175 if( ! pSaneLib )
176 {
177 sSaneLibName = "libsane" SAL_DLLEXTENSION ".1";
178 pSaneLib = osl_loadModule( sSaneLibName.pData, SAL_LOADMODULE_LAZY );
179 }
180 // try reasonable places that might not be in the library search path
181 if( ! pSaneLib )
182 {
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 );
186 }
187#endif
188 if( pSaneLib )
189 {
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" ));
224 DeInit();
225 else
226 {
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++ ) ;
233 }
234 }
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
240}
241
243{
244 if( pSaneLib )
245 {
246 p_exit();
247#ifndef DISABLE_DYNLOADING
248 osl_unloadModule( pSaneLib );
249#endif
250 pSaneLib = nullptr;
251 }
252}
253
255{
256 if( IsOpen() )
257 Close();
258 DeInit();
259 Init();
260}
261
263{
264 if( ! IsOpen() )
265 return;
266
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 ) );
273
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++ )
281
282 CheckConsistency( nullptr, true );
283
284 maReloadOptionsLink.Call( *this );
285}
286
287bool Sane::Open( const char* name )
288{
289 SANE_Status nStatus = p_open( reinterpret_cast<SANE_String_Const>(name), &maHandle );
290 FAIL_STATE( nStatus, "sane_open", false );
291
293
294 if( mnDevice == -1 )
295 {
296 OString aDevice( name );
297 for( int i = 0; i < nDevices; i++ )
298 {
299 if( aDevice == ppDevices[i]->name )
300 {
301 mnDevice = i;
302 break;
303 }
304 }
305 }
306
307 return true;
308}
309
310bool Sane::Open( int n )
311{
312 if( n >= 0 && n < nDevices )
313 {
314 mnDevice = n;
315 return Open( ppDevices[n]->name );
316 }
317 return false;
318}
319
321{
322 if( maHandle )
323 {
324 p_close( maHandle );
325 mppOptions.reset();
326 maHandle = nullptr;
327 mnDevice = -1;
328 }
329}
330
331int Sane::GetOptionByName( const char* rName )
332{
333 int i;
334 OString aOption( rName );
335 for( i = 0; i < mnOptions; i++ )
336 {
337 if( mppOptions[i]->name && aOption == mppOptions[i]->name )
338 return i;
339 }
340 return -1;
341}
342
343bool Sane::GetOptionValue( int n, bool& rRet )
344{
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;
351
352 rRet = nRet;
353 return true;
354}
355
356bool Sane::GetOptionValue( int n, OString& rRet )
357{
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 )
364 {
365 bSuccess = true;
366 rRet = pRet.get();
367 }
368 return bSuccess;
369}
370
371bool Sane::GetOptionValue( int n, double& rRet, int nElement )
372{
373 bool bSuccess = false;
374
375 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
376 mppOptions[n]->type != SANE_TYPE_FIXED ) )
377 return false;
378
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 )
382 {
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] );
388 }
389 return bSuccess;
390}
391
392bool Sane::GetOptionValue( int n, double* pSet )
393{
394 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_FIXED &&
395 mppOptions[n]->type != SANE_TYPE_INT ) )
396 return false;
397
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++ )
403 {
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]);
408 }
409 return true;
410}
411
412void Sane::SetOptionValue( int n, bool bSet )
413{
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 );
418}
419
420void Sane::SetOptionValue( int n, std::u16string_view rSet )
421{
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()) );
426}
427
428void Sane::SetOptionValue( int n, double fSet, int nElement )
429{
430 if( ! maHandle || ( mppOptions[n]->type != SANE_TYPE_INT &&
431 mppOptions[n]->type != SANE_TYPE_FIXED ) )
432 return;
433
434 if( mppOptions[n]->size/sizeof(SANE_Word) > 1 )
435 {
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 )
439 {
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() );
443 }
444 }
445 else
446 {
447 SANE_Word nSetTo =
448 mppOptions[n]->type == SANE_TYPE_INT ?
449 static_cast<SANE_Word>(fSet) : SANE_FIX( fSet );
450
451 ControlOption( n, SANE_ACTION_SET_VALUE, &nSetTo );
452 }
453}
454
455void Sane::SetOptionValue( int n, double const * pSet )
456{
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++ )
462 {
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]);
467 }
468 ControlOption( n, SANE_ACTION_SET_VALUE, pFixedSet.get() );
469}
470
471namespace {
472
473enum FrameStyleType {
474 FrameStyle_BW, FrameStyle_Gray, FrameStyle_RGB, FrameStyle_Separated
475};
476
477}
478
479#define BYTE_BUFFER_SIZE 32768
480
481static sal_uInt8 ReadValue( FILE* fp, int depth )
482{
483 if( depth == 16 )
484 {
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 );
492
493 if (items_read != 2)
494 {
495 SAL_WARN( "extensions.scanner", "short read, abandoning" );
496 return 0;
497 }
498
499 return static_cast<sal_uInt8>( nWord / 256 );
500 }
501 sal_uInt8 nByte;
502 size_t items_read = fread( &nByte, 1, 1, fp );
503 if (items_read != 1)
504 {
505 SAL_WARN( "extensions.scanner", "short read, abandoning" );
506 return 0;
507 }
508 return nByte;
509}
510
511bool Sane::CheckConsistency( const char* pMes, bool bInit )
512{
513 static const SANE_Option_Descriptor** pDescArray = nullptr;
514 static const SANE_Option_Descriptor* pZero = nullptr;
515
516 if( bInit )
517 {
518 pDescArray = mppOptions.get();
519 if( mppOptions )
520 pZero = mppOptions[0];
521 return true;
522 }
523
524 bool bConsistent = true;
525
526 if( pDescArray != mppOptions.get() )
527 bConsistent = false;
528 if( pZero != mppOptions[0] )
529 bConsistent = false;
530
531 if( ! bConsistent )
532 dbg_msg( "Sane is not consistent. (%s)\n", pMes );
533
534 return bConsistent;
535}
536
538{
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;
544
545 if( ! maHandle )
546 return false;
547
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 )
556 {
557 double fBRx;
558 nOption = GetOptionByName( "br-x" );
559 if( nOption != -1 &&
560 GetOptionValue( nOption, fBRx ) &&
561 GetOptionUnit( nOption ) == SANE_UNIT_MM )
562 {
563 nWidthMM = static_cast<int>(fabs(fBRx - fTLx));
564 }
565 }
566 nOption = GetOptionByName( "tl-y" );
567 if( nOption != -1 &&
568 GetOptionValue( nOption, fTLy ) &&
569 GetOptionUnit( nOption ) == SANE_UNIT_MM )
570 {
571 double fBRy;
572 nOption = GetOptionByName( "br-y" );
573 if( nOption != -1 &&
574 GetOptionValue( nOption, fBRy ) &&
575 GetOptionUnit( nOption ) == SANE_UNIT_MM )
576 {
577 nHeightMM = static_cast<int>(fabs(fBRy - fTLy));
578 }
579 }
580 if( ( nOption = GetOptionByName( "resolution" ) ) != -1 )
581 (void)GetOptionValue( nOption, fResl );
582
583 std::unique_ptr<sal_uInt8[]> pBuffer;
584
585 SANE_Status nStatus = SANE_STATUS_GOOD;
586
587 rBitmap.lock();
588 SvMemoryStream& aConverter = rBitmap.getStream();
589 aConverter.Seek( 0 );
590 aConverter.SetEndian( SvStreamEndian::LITTLE );
591
592 // write bitmap stream header
593 aConverter.WriteChar( 'B' ).WriteChar( 'M' );
594 aConverter.WriteUInt32( 0 );
595 aConverter.WriteUInt32( 0 );
596 aConverter.WriteUInt32( 60 );
597
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 );
612
613 for( nStream=0; nStream < 3 && bSuccess ; nStream++ )
614 {
615 nStatus = p_start( maHandle );
616 DUMP_STATE( nStatus, "sane_start" );
617 CheckConsistency( "sane_start" );
618 if( nStatus == SANE_STATUS_GOOD )
619 {
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)
624 {
625 bSuccess = false;
626 break;
627 }
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 )
638 {
639 aParams.format = SANE_Frame(5);
640 }
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 )
648 {
649 pBuffer.reset(new sal_uInt8[ BYTE_BUFFER_SIZE < 4*aParams.bytes_per_line ? 4*aParams.bytes_per_line : BYTE_BUFFER_SIZE ]);
650 }
651
652 if( aParams.last_frame )
653 nStream=3;
654
655 switch( aParams.format )
656 {
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" );
672 }
673
674 bool bSynchronousRead = true;
675
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 )
680 {
681 bSynchronousRead = false;
682 nStatus = p_set_io_mode(maHandle, SANE_TRUE);
683 CheckConsistency( "sane_set_io_mode" );
684 if (nStatus != SANE_STATUS_GOOD)
685 {
686 SAL_WARN("extensions.scanner", "SANE driver status is: " << nStatus);
687 }
688 }
689
690 SANE_Int nLen=0;
691 SANE_Int fd = 0;
692
693 if( ! bSynchronousRead )
694 {
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;
700 }
701 utl::TempFileNamed aFrame;
702 aFrame.EnableKillingFile();
703 FILE* pFrame = fopen(OUStringToOString(aFrame.GetFileName(), osl_getThreadTextEncoding()).getStr(), "w+b");
704 if( ! pFrame )
705 {
706 bSuccess = false;
707 break;
708 }
709 do {
710 if( ! bSynchronousRead )
711 {
712 fd_set fdset;
713 struct timeval tv;
714
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" );
721 }
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 ) )
727 {
728 bSuccess = (static_cast<size_t>(nLen) == fwrite( pBuffer.get(), 1, nLen, pFrame ));
729 if (!bSuccess)
730 break;
731 }
732 else
733 DUMP_STATE( nStatus, "sane_read" );
734 } while( nStatus == SANE_STATUS_GOOD );
735 if (nStatus != SANE_STATUS_EOF || !bSuccess)
736 {
737 fclose( pFrame );
738 bSuccess = false;
739 break;
740 }
741
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 )
747 {
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);
756
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;
764 }
765 aConverter.Seek(60);
766
767 if( eType == FrameStyle_BW )
768 {
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 );
780 }
781 else if( eType == FrameStyle_Gray )
782 {
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++ )
790 {
791 aConverter.WriteUChar( nLine );
792 aConverter.WriteUChar( nLine );
793 aConverter.WriteUChar( nLine );
794 aConverter.WriteUChar( 0 );
795 }
796 aConverter.Seek( 1084 );
797 }
798
799 for (nLine = nHeight-1; nLine >= 0; --nLine)
800 {
801 if (fseek(pFrame, nLine * aParams.bytes_per_line, SEEK_SET) == -1)
802 {
803 bSuccess = false;
804 break;
805 }
806 if( eType == FrameStyle_BW ||
807 ( eType == FrameStyle_Gray && aParams.depth == 8 )
808 )
809 {
810 SANE_Int items_read = fread( pBuffer.get(), 1, aParams.bytes_per_line, pFrame );
811 if (items_read != aParams.bytes_per_line)
812 {
813 SAL_WARN( "extensions.scanner", "short read, padding with zeros" );
814 memset(pBuffer.get() + items_read, 0, aParams.bytes_per_line - items_read);
815 }
816 aConverter.WriteBytes(pBuffer.get(), aParams.bytes_per_line);
817 }
818 else if( eType == FrameStyle_Gray )
819 {
820 for( i = 0; i < (aParams.pixels_per_line); i++ )
821 {
822 sal_uInt8 nGray = ReadValue( pFrame, aParams.depth );
823 aConverter.WriteUChar( nGray );
824 }
825 }
826 else if( eType == FrameStyle_RGB )
827 {
828 for( i = 0; i < (aParams.pixels_per_line); i++ )
829 {
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 );
837 }
838 }
839 else if( eType == FrameStyle_Separated )
840 {
841 for( i = 0; i < (aParams.pixels_per_line); i++ )
842 {
843 sal_uInt8 nValue = ReadValue( pFrame, aParams.depth );
844 switch( aParams.format )
845 {
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;
862 }
863 }
864 }
865 int nGap = aConverter.Tell() & 3;
866 if (nGap)
867 aConverter.SeekRel( 4-nGap );
868 }
869 fclose( pFrame ); // deletes tmpfile
870 if( eType != FrameStyle_Separated )
871 break;
872 }
873 else
874 bSuccess = false;
875 }
876 // get stream length
877 int nPos = aConverter.TellEnd();
878
879 aConverter.Seek( 2 );
880 aConverter.WriteUInt32( nPos+1 );
881 aConverter.Seek( 0 );
882
883 rBitmap.unlock();
884
885 if( bSuccess )
886 {
887 // only cancel a successful operation
888 // sane disrupts memory else
890 CheckConsistency( "sane_cancel" );
891 }
892 pBuffer.reset();
893
895
896
897 dbg_msg( "Sane::Start returns with %s\n", bSuccess ? "TRUE" : "FALSE" );
898
899 return bSuccess;
900}
901
902int Sane::GetRange( int n, std::unique_ptr<double[]>& rpDouble )
903{
904 if( mppOptions[n]->constraint_type != SANE_CONSTRAINT_RANGE &&
905 mppOptions[n]->constraint_type != SANE_CONSTRAINT_WORD_LIST )
906 {
907 return -1;
908 }
909
910 rpDouble = nullptr;
911 int nItems, i;
912 bool bIsFixed = mppOptions[n]->type == SANE_TYPE_FIXED;
913
914 dbg_msg( "Sane::GetRange of option %s ", mppOptions[n]->name );
915 if(mppOptions[n]->constraint_type == SANE_CONSTRAINT_RANGE )
916 {
917 double fMin, fMax, fQuant;
918 if( bIsFixed )
919 {
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 );
923 }
924 else
925 {
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);
929 }
930 if( fQuant != 0.0 )
931 {
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;
941 }
942 else
943 {
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;
950 }
951 }
952 else
953 {
954 nItems = mppOptions[n]->constraint.word_list[0];
955 rpDouble.reset(new double[nItems]);
956 for( i=0; i<nItems; i++ )
957 {
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]);
961 }
962 dbg_msg( "wordlist [ %lg ... %lg ]\n",
963 rpDouble[ 0 ], rpDouble[ nItems-1 ] );
964 return nItems;
965 }
966}
967
968static const char *ppUnits[] = {
969 "",
970 "[Pixel]",
971 "[Bit]",
972 "[mm]",
973 "[DPI]",
974 "[%]",
975 "[usec]"
976};
977
978OUString Sane::GetOptionUnitName( int n )
979{
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;
988}
989
991{
992 SANE_Status nStatus = ControlOption( n, SANE_ACTION_SET_VALUE, nullptr );
993 return nStatus == SANE_STATUS_GOOD;
994}
995
996/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void lock()
Definition: sane.hxx:53
void unlock()
Definition: sane.hxx:54
SvMemoryStream & getStream()
Definition: sane.hxx:55
SANE_Unit GetOptionUnit(int n)
Definition: sane.hxx:132
static SANE_Status(* p_get_devices)(const SANE_Device ***, SANE_Bool)
Definition: sane.hxx:68
int mnDevice
Definition: sane.hxx:93
int mnOptions
Definition: sane.hxx:92
bool CheckConsistency(const char *, bool bInit=false)
Definition: sane.cxx:511
bool GetOptionValue(int, bool &)
Definition: sane.cxx:343
bool IsOpen() const
Definition: sane.hxx:113
static SANE_Status(* p_get_select_fd)(SANE_Handle, SANE_Int *)
Definition: sane.hxx:84
bool ActivateButtonOption(int)
Definition: sane.cxx:990
void ReloadOptions()
Definition: sane.cxx:262
OUString GetOptionUnitName(int n)
Definition: sane.cxx:978
void Close()
Definition: sane.cxx:320
Link< Sane &, void > maReloadOptionsLink
Definition: sane.hxx:96
int GetOptionByName(const char *)
Definition: sane.cxx:331
static SANE_Status(* p_start)(SANE_Handle)
Definition: sane.hxx:79
bool Start(BitmapTransporter &)
Definition: sane.cxx:537
static const SANE_Option_Descriptor *(* p_get_option_descriptor)(SANE_Handle, SANE_Int)
Definition: sane.hxx:72
OUString GetOptionName(int n)
Definition: sane.hxx:126
int GetRange(int, std::unique_ptr< double[]> &)
Definition: sane.cxx:902
static SANE_Status(* p_read)(SANE_Handle, SANE_Byte *, SANE_Int, SANE_Int *)
Definition: sane.hxx:80
~Sane()
Definition: sane.cxx:161
static void(* p_exit)()
Definition: sane.hxx:67
static SANE_Status(* p_init)(SANE_Int *, SANE_Auth_Callback)
Definition: sane.hxx:65
static int nRefCount
Definition: sane.hxx:62
static SANE_Device ** ppDevices
Definition: sane.hxx:88
static void(* p_close)(SANE_Handle)
Definition: sane.hxx:71
bool Open(const char *)
Definition: sane.cxx:287
static SANE_Status(* p_control_option)(SANE_Handle, SANE_Int, SANE_Action, void *, SANE_Int *)
Definition: sane.hxx:74
SANE_Handle maHandle
Definition: sane.hxx:94
static void DeInit()
Definition: sane.cxx:242
std::unique_ptr< const SANE_Option_Descriptor *[]> mppOptions
Definition: sane.hxx:91
static SANE_Int nVersion
Definition: sane.hxx:87
SANE_Status ControlOption(int, SANE_Action, void *)
Definition: sane.cxx:120
static int nDevices
Definition: sane.hxx:89
static SANE_String_Const(* p_strstatus)(SANE_Status)
Definition: sane.hxx:85
static SANE_Status(* p_set_io_mode)(SANE_Handle, SANE_Bool)
Definition: sane.hxx:83
void ReloadDevices()
Definition: sane.cxx:254
static SANE_Status(* p_get_parameters)(SANE_Handle, SANE_Parameters *)
Definition: sane.hxx:77
static SANE_Status(* p_open)(SANE_String_Const, SANE_Handle)
Definition: sane.hxx:70
static oslGenericFunction LoadSymbol(const char *)
Definition: sane.cxx:108
void SetOptionValue(int, bool)
Definition: sane.cxx:412
static void(* p_cancel)(SANE_Handle)
Definition: sane.hxx:82
static oslModule pSaneLib
Definition: sane.hxx:63
Sane()
Definition: sane.cxx:151
static void Init()
Definition: sane.cxx:170
virtual sal_uInt64 TellEnd() override
sal_uInt64 Tell() const
void SetEndian(SvStreamEndian SvStreamEndian)
std::size_t WriteBytes(const void *pData, std::size_t nSize)
SvStream & WriteUChar(unsigned char nChar)
SvStream & WriteUInt16(sal_uInt16 nUInt16)
SvStream & WriteUInt32(sal_uInt32 nUInt32)
sal_uInt64 Seek(sal_uInt64 nPos)
SvStream & WriteChar(char nChar)
sal_uInt64 SeekRel(sal_Int64 nPos)
void EnableKillingFile(bool bEnable=true)
OUString GetFileName() const
#define SAL_DLLEXTENSION
DocumentType eType
sal_Int16 nValue
const char * name
sal_Int64 n
sal_uInt16 nPos
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
#define SAL_N_ELEMENTS(arr)
std::unique_ptr< sal_Int32[]> pData
size
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
OString OUStringToOString(std::u16string_view str, ConnectionSettings const *settings)
static sal_uInt8 ReadValue(FILE *fp, int depth)
Definition: sane.cxx:481
#define FAIL_SHUTDOWN_STATE(x, y, z)
Definition: sane.cxx:54
static const char * ppUnits[]
Definition: sane.cxx:968
static void dbg_msg(const char *pString,...)
Definition: sane.cxx:42
#define FAIL_STATE(x, y, z)
Definition: sane.cxx:63
#define BYTE_BUFFER_SIZE
Definition: sane.cxx:479
static bool bSaneSymbolLoadFailed
Definition: sane.cxx:106
#define DUMP_STATE(x, y)
Definition: sane.cxx:71
static SfxItemSet & rSet
unsigned char sal_uInt8
ResultType type