1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_extensions.hxx"
30*cdf0e10cSrcweir #include <scanner.hxx>
31*cdf0e10cSrcweir #include <sanedlg.hxx>
32*cdf0e10cSrcweir #include <vos/thread.hxx>
33*cdf0e10cSrcweir #include <tools/list.hxx>
34*cdf0e10cSrcweir #include <boost/shared_ptr.hpp>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
37*cdf0e10cSrcweir #include <stdio.h>
38*cdf0e10cSrcweir #endif
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir BitmapTransporter::BitmapTransporter()
41*cdf0e10cSrcweir {
42*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
43*cdf0e10cSrcweir     fprintf( stderr, "BitmapTransporter\n" );
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir }
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir BitmapTransporter::~BitmapTransporter()
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
50*cdf0e10cSrcweir     fprintf( stderr, "~BitmapTransporter\n" );
51*cdf0e10cSrcweir #endif
52*cdf0e10cSrcweir }
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir // -----------------------------------------------------------------------------
55*cdf0e10cSrcweir 
56*cdf0e10cSrcweir ANY SAL_CALL BitmapTransporter::queryInterface( const Type& rType ) throw( RuntimeException )
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir 	const ANY aRet( cppu::queryInterface( rType, static_cast< AWT::XBitmap* >( this ) ) );
59*cdf0e10cSrcweir 
60*cdf0e10cSrcweir 	return( aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ) );
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir 
63*cdf0e10cSrcweir // -----------------------------------------------------------------------------
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir AWT::Size BitmapTransporter::getSize() throw()
66*cdf0e10cSrcweir {
67*cdf0e10cSrcweir 	vos::OGuard	aGuard( m_aProtector );
68*cdf0e10cSrcweir 	int			nPreviousPos = m_aStream.Tell();
69*cdf0e10cSrcweir 	AWT::Size	aRet;
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir 	// ensure that there is at least a header
72*cdf0e10cSrcweir 	m_aStream.Seek( STREAM_SEEK_TO_END );
73*cdf0e10cSrcweir 	int nLen = m_aStream.Tell();
74*cdf0e10cSrcweir 	if( nLen > 15 )
75*cdf0e10cSrcweir 	{
76*cdf0e10cSrcweir 		m_aStream.Seek( 4 );
77*cdf0e10cSrcweir 		m_aStream >> aRet.Width >> aRet.Height;
78*cdf0e10cSrcweir 	}
79*cdf0e10cSrcweir 	else
80*cdf0e10cSrcweir 		aRet.Width = aRet.Height = 0;
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir 	m_aStream.Seek( nPreviousPos );
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir 	return aRet;
85*cdf0e10cSrcweir }
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir // -----------------------------------------------------------------------------
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir SEQ( sal_Int8 ) BitmapTransporter::getDIB() throw()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 	vos::OGuard aGuard( m_aProtector );
92*cdf0e10cSrcweir 	int			nPreviousPos = m_aStream.Tell();
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	// create return value
95*cdf0e10cSrcweir 	m_aStream.Seek( STREAM_SEEK_TO_END );
96*cdf0e10cSrcweir 	int nBytes = m_aStream.Tell();
97*cdf0e10cSrcweir 	m_aStream.Seek( 0 );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 	SEQ( sal_Int8 ) aValue( nBytes );
100*cdf0e10cSrcweir 	m_aStream.Read( aValue.getArray(), nBytes );
101*cdf0e10cSrcweir 	m_aStream.Seek( nPreviousPos );
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 	return aValue;
104*cdf0e10cSrcweir }
105*cdf0e10cSrcweir 
106*cdf0e10cSrcweir // --------------
107*cdf0e10cSrcweir // - SaneHolder -
108*cdf0e10cSrcweir // --------------
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir struct SaneHolder
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir 	Sane				m_aSane;
113*cdf0e10cSrcweir 	REF( AWT::XBitmap )	m_xBitmap;
114*cdf0e10cSrcweir 	vos::OMutex			m_aProtector;
115*cdf0e10cSrcweir 	ScanError			m_nError;
116*cdf0e10cSrcweir     bool				m_bBusy;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir     SaneHolder() : m_nError(ScanError_ScanErrorNone), m_bBusy(false) {}
119*cdf0e10cSrcweir };
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir namespace
122*cdf0e10cSrcweir {
123*cdf0e10cSrcweir     typedef std::vector< boost::shared_ptr<SaneHolder> > sanevec;
124*cdf0e10cSrcweir     class allSanes
125*cdf0e10cSrcweir     {
126*cdf0e10cSrcweir     private:
127*cdf0e10cSrcweir         int mnRefCount;
128*cdf0e10cSrcweir     public:
129*cdf0e10cSrcweir         sanevec m_aSanes;
130*cdf0e10cSrcweir         allSanes() : mnRefCount(0) {}
131*cdf0e10cSrcweir         void acquire();
132*cdf0e10cSrcweir         void release();
133*cdf0e10cSrcweir     };
134*cdf0e10cSrcweir 
135*cdf0e10cSrcweir     void allSanes::acquire()
136*cdf0e10cSrcweir     {
137*cdf0e10cSrcweir         ++mnRefCount;
138*cdf0e10cSrcweir     }
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir     void allSanes::release()
141*cdf0e10cSrcweir     {
142*cdf0e10cSrcweir         // was unused, now because of i99835: "Scanning interface not SANE API
143*cdf0e10cSrcweir         // compliant" destroy all SaneHolder to get Sane Dtor called
144*cdf0e10cSrcweir         --mnRefCount;
145*cdf0e10cSrcweir         if (!mnRefCount)
146*cdf0e10cSrcweir             m_aSanes.clear();
147*cdf0e10cSrcweir     }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir     struct theSaneProtector : public rtl::Static<vos::OMutex, theSaneProtector> {};
150*cdf0e10cSrcweir     struct theSanes : public rtl::Static<allSanes, theSanes> {};
151*cdf0e10cSrcweir }
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir // -----------------
154*cdf0e10cSrcweir // - ScannerThread -
155*cdf0e10cSrcweir // -----------------
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir class ScannerThread : public vos::OThread
158*cdf0e10cSrcweir {
159*cdf0e10cSrcweir 	boost::shared_ptr<SaneHolder>				m_pHolder;
160*cdf0e10cSrcweir 	REF( com::sun::star::lang::XEventListener )	m_xListener;
161*cdf0e10cSrcweir 	ScannerManager*								m_pManager; // just for the disposing call
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir public:
164*cdf0e10cSrcweir 	virtual void run();
165*cdf0e10cSrcweir 	virtual void onTerminated() { delete this; }
166*cdf0e10cSrcweir public:
167*cdf0e10cSrcweir 	ScannerThread( boost::shared_ptr<SaneHolder> pHolder,
168*cdf0e10cSrcweir 				   const REF( com::sun::star::lang::XEventListener )& listener,
169*cdf0e10cSrcweir 				   ScannerManager* pManager );
170*cdf0e10cSrcweir 	virtual ~ScannerThread();
171*cdf0e10cSrcweir };
172*cdf0e10cSrcweir 
173*cdf0e10cSrcweir // -----------------------------------------------------------------------------
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir ScannerThread::ScannerThread(
176*cdf0e10cSrcweir                              boost::shared_ptr<SaneHolder> pHolder,
177*cdf0e10cSrcweir                              const REF( com::sun::star::lang::XEventListener )& listener,
178*cdf0e10cSrcweir                              ScannerManager* pManager )
179*cdf0e10cSrcweir         : m_pHolder( pHolder ), m_xListener( listener ), m_pManager( pManager )
180*cdf0e10cSrcweir {
181*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
182*cdf0e10cSrcweir     fprintf( stderr, "ScannerThread\n" );
183*cdf0e10cSrcweir #endif
184*cdf0e10cSrcweir }
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir ScannerThread::~ScannerThread()
187*cdf0e10cSrcweir {
188*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
189*cdf0e10cSrcweir     fprintf( stderr, "~ScannerThread\n" );
190*cdf0e10cSrcweir #endif
191*cdf0e10cSrcweir }
192*cdf0e10cSrcweir 
193*cdf0e10cSrcweir void ScannerThread::run()
194*cdf0e10cSrcweir {
195*cdf0e10cSrcweir 	vos::OGuard			aGuard( m_pHolder->m_aProtector );
196*cdf0e10cSrcweir     BitmapTransporter*	pTransporter = new BitmapTransporter;
197*cdf0e10cSrcweir 	REF( XInterface )	aIf( static_cast< OWeakObject* >( pTransporter ) );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	m_pHolder->m_xBitmap = REF( AWT::XBitmap )( aIf, UNO_QUERY );
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     m_pHolder->m_bBusy = true;
202*cdf0e10cSrcweir 	if( m_pHolder->m_aSane.IsOpen() )
203*cdf0e10cSrcweir 	{
204*cdf0e10cSrcweir 		int nOption = m_pHolder->m_aSane.GetOptionByName( "preview" );
205*cdf0e10cSrcweir 		if( nOption != -1 )
206*cdf0e10cSrcweir 			m_pHolder->m_aSane.SetOptionValue( nOption, (sal_Bool)sal_False );
207*cdf0e10cSrcweir 
208*cdf0e10cSrcweir 		m_pHolder->m_nError =
209*cdf0e10cSrcweir 			m_pHolder->m_aSane.Start( *pTransporter ) ?
210*cdf0e10cSrcweir 			ScanError_ScanErrorNone : ScanError_ScanCanceled;
211*cdf0e10cSrcweir 	}
212*cdf0e10cSrcweir 	else
213*cdf0e10cSrcweir 		m_pHolder->m_nError = ScanError_ScannerNotAvailable;
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir     REF( XInterface ) xXInterface( static_cast< OWeakObject* >( m_pManager ) );
217*cdf0e10cSrcweir 	m_xListener->disposing( com::sun::star::lang::EventObject(xXInterface) );
218*cdf0e10cSrcweir     m_pHolder->m_bBusy = false;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir // ------------------
222*cdf0e10cSrcweir // - ScannerManager -
223*cdf0e10cSrcweir // ------------------
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir void ScannerManager::AcquireData()
226*cdf0e10cSrcweir {
227*cdf0e10cSrcweir     vos::OGuard aGuard( theSaneProtector::get() );
228*cdf0e10cSrcweir     theSanes::get().acquire();
229*cdf0e10cSrcweir }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir void ScannerManager::ReleaseData()
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir     vos::OGuard aGuard( theSaneProtector::get() );
234*cdf0e10cSrcweir     theSanes::get().release();
235*cdf0e10cSrcweir }
236*cdf0e10cSrcweir 
237*cdf0e10cSrcweir // -----------------------------------------------------------------------------
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir AWT::Size ScannerManager::getSize() throw()
240*cdf0e10cSrcweir {
241*cdf0e10cSrcweir 	AWT::Size aRet;
242*cdf0e10cSrcweir 	aRet.Width = aRet.Height = 0;
243*cdf0e10cSrcweir 	return aRet;
244*cdf0e10cSrcweir }
245*cdf0e10cSrcweir 
246*cdf0e10cSrcweir // -----------------------------------------------------------------------------
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir SEQ( sal_Int8 ) ScannerManager::getDIB() throw()
249*cdf0e10cSrcweir {
250*cdf0e10cSrcweir 	return SEQ( sal_Int8 )();
251*cdf0e10cSrcweir }
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir // -----------------------------------------------------------------------------
254*cdf0e10cSrcweir 
255*cdf0e10cSrcweir SEQ( ScannerContext ) ScannerManager::getAvailableScanners() throw()
256*cdf0e10cSrcweir {
257*cdf0e10cSrcweir 	vos::OGuard aGuard( theSaneProtector::get() );
258*cdf0e10cSrcweir 	sanevec &rSanes = theSanes::get().m_aSanes;
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir 	if( rSanes.empty() )
261*cdf0e10cSrcweir 	{
262*cdf0e10cSrcweir 		boost::shared_ptr<SaneHolder> pSaneHolder(new SaneHolder);
263*cdf0e10cSrcweir 		if( Sane::IsSane() )
264*cdf0e10cSrcweir 			rSanes.push_back( pSaneHolder );
265*cdf0e10cSrcweir 	}
266*cdf0e10cSrcweir 
267*cdf0e10cSrcweir 	if( Sane::IsSane() )
268*cdf0e10cSrcweir 	{
269*cdf0e10cSrcweir 		SEQ( ScannerContext ) aRet(1);
270*cdf0e10cSrcweir 		aRet.getArray()[0].ScannerName		= ::rtl::OUString::createFromAscii( "SANE" );
271*cdf0e10cSrcweir 		aRet.getArray()[0].InternalData		= 0;
272*cdf0e10cSrcweir 		return aRet;
273*cdf0e10cSrcweir 	}
274*cdf0e10cSrcweir 
275*cdf0e10cSrcweir 	return SEQ( ScannerContext )();
276*cdf0e10cSrcweir }
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir // -----------------------------------------------------------------------------
279*cdf0e10cSrcweir 
280*cdf0e10cSrcweir sal_Bool ScannerManager::configureScanner( ScannerContext& scanner_context ) throw( ScannerException )
281*cdf0e10cSrcweir {
282*cdf0e10cSrcweir 	vos::OGuard aGuard( theSaneProtector::get() );
283*cdf0e10cSrcweir 	sanevec &rSanes = theSanes::get().m_aSanes;
284*cdf0e10cSrcweir 
285*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
286*cdf0e10cSrcweir     fprintf( stderr, "ScannerManager::configureScanner\n" );
287*cdf0e10cSrcweir #endif
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir 	if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
290*cdf0e10cSrcweir 		throw ScannerException(
291*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "Scanner does not exist" ),
292*cdf0e10cSrcweir 			REF( XScannerManager )( this ),
293*cdf0e10cSrcweir 			ScanError_InvalidContext
294*cdf0e10cSrcweir 			);
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir     boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
297*cdf0e10cSrcweir     if( pHolder->m_bBusy )
298*cdf0e10cSrcweir 		throw ScannerException(
299*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "Scanner is busy" ),
300*cdf0e10cSrcweir 			REF( XScannerManager )( this ),
301*cdf0e10cSrcweir 			ScanError_ScanInProgress
302*cdf0e10cSrcweir 			);
303*cdf0e10cSrcweir 
304*cdf0e10cSrcweir     pHolder->m_bBusy = true;
305*cdf0e10cSrcweir 	SaneDlg aDlg( NULL, pHolder->m_aSane );
306*cdf0e10cSrcweir     sal_Bool bRet = (sal_Bool)aDlg.Execute();
307*cdf0e10cSrcweir     pHolder->m_bBusy = false;
308*cdf0e10cSrcweir 
309*cdf0e10cSrcweir 	return bRet;
310*cdf0e10cSrcweir }
311*cdf0e10cSrcweir 
312*cdf0e10cSrcweir // -----------------------------------------------------------------------------
313*cdf0e10cSrcweir 
314*cdf0e10cSrcweir void ScannerManager::startScan( const ScannerContext& scanner_context,
315*cdf0e10cSrcweir 								const REF( com::sun::star::lang::XEventListener )& listener ) throw( ScannerException )
316*cdf0e10cSrcweir {
317*cdf0e10cSrcweir 	vos::OGuard aGuard( theSaneProtector::get() );
318*cdf0e10cSrcweir 	sanevec &rSanes = theSanes::get().m_aSanes;
319*cdf0e10cSrcweir 
320*cdf0e10cSrcweir #if OSL_DEBUG_LEVEL > 1
321*cdf0e10cSrcweir     fprintf( stderr, "ScannerManager::startScan\n" );
322*cdf0e10cSrcweir #endif
323*cdf0e10cSrcweir 
324*cdf0e10cSrcweir 	if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
325*cdf0e10cSrcweir 		throw ScannerException(
326*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "Scanner does not exist" ),
327*cdf0e10cSrcweir 			REF( XScannerManager )( this ),
328*cdf0e10cSrcweir 			ScanError_InvalidContext
329*cdf0e10cSrcweir 			);
330*cdf0e10cSrcweir 	boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
331*cdf0e10cSrcweir     if( pHolder->m_bBusy )
332*cdf0e10cSrcweir 		throw ScannerException(
333*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "Scanner is busy" ),
334*cdf0e10cSrcweir 			REF( XScannerManager )( this ),
335*cdf0e10cSrcweir 			ScanError_ScanInProgress
336*cdf0e10cSrcweir 			);
337*cdf0e10cSrcweir     pHolder->m_bBusy = true;
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir 	ScannerThread* pThread = new ScannerThread( pHolder, listener, this );
340*cdf0e10cSrcweir 	pThread->create();
341*cdf0e10cSrcweir }
342*cdf0e10cSrcweir 
343*cdf0e10cSrcweir // -----------------------------------------------------------------------------
344*cdf0e10cSrcweir 
345*cdf0e10cSrcweir ScanError ScannerManager::getError( const ScannerContext& scanner_context ) throw( ScannerException )
346*cdf0e10cSrcweir {
347*cdf0e10cSrcweir 	vos::OGuard aGuard( theSaneProtector::get() );
348*cdf0e10cSrcweir 	sanevec &rSanes = theSanes::get().m_aSanes;
349*cdf0e10cSrcweir 
350*cdf0e10cSrcweir 	if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
351*cdf0e10cSrcweir 		throw ScannerException(
352*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "Scanner does not exist" ),
353*cdf0e10cSrcweir 			REF( XScannerManager )( this ),
354*cdf0e10cSrcweir 			ScanError_InvalidContext
355*cdf0e10cSrcweir 			);
356*cdf0e10cSrcweir 
357*cdf0e10cSrcweir 	boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
358*cdf0e10cSrcweir 
359*cdf0e10cSrcweir 	return pHolder->m_nError;
360*cdf0e10cSrcweir }
361*cdf0e10cSrcweir 
362*cdf0e10cSrcweir // -----------------------------------------------------------------------------
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir REF( AWT::XBitmap ) ScannerManager::getBitmap( const ScannerContext& scanner_context ) throw( ScannerException )
365*cdf0e10cSrcweir {
366*cdf0e10cSrcweir 	vos::OGuard aGuard( theSaneProtector::get() );
367*cdf0e10cSrcweir 	sanevec &rSanes = theSanes::get().m_aSanes;
368*cdf0e10cSrcweir 
369*cdf0e10cSrcweir 	if( scanner_context.InternalData < 0 || (sal_uLong)scanner_context.InternalData >= rSanes.size() )
370*cdf0e10cSrcweir 		throw ScannerException(
371*cdf0e10cSrcweir 			::rtl::OUString::createFromAscii( "Scanner does not exist" ),
372*cdf0e10cSrcweir 			REF( XScannerManager )( this ),
373*cdf0e10cSrcweir 			ScanError_InvalidContext
374*cdf0e10cSrcweir 			);
375*cdf0e10cSrcweir 	boost::shared_ptr<SaneHolder> pHolder = rSanes[scanner_context.InternalData];
376*cdf0e10cSrcweir 
377*cdf0e10cSrcweir 	vos::OGuard aProtGuard( pHolder->m_aProtector );
378*cdf0e10cSrcweir 
379*cdf0e10cSrcweir 	REF( AWT::XBitmap ) xRet( pHolder->m_xBitmap );
380*cdf0e10cSrcweir 	pHolder->m_xBitmap = REF( AWT::XBitmap )();
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir 	return xRet;
383*cdf0e10cSrcweir }
384