Line data Source code
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 "imgprod.hxx"
21 : #include "services.hxx"
22 :
23 : #include <vcl/bmpacc.hxx>
24 : #include <vcl/cvtgrf.hxx>
25 : #include <vcl/svapp.hxx>
26 : #include <unotools/ucbstreamhelper.hxx>
27 : #include <vcl/graphicfilter.hxx>
28 : #include <cppuhelper/queryinterface.hxx>
29 : #include <com/sun/star/io/XInputStream.hpp>
30 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 :
32 : #include "svtools/imageresourceaccess.hxx"
33 : #include <comphelper/processfactory.hxx>
34 :
35 : // - ImgProdLockBytes -
36 :
37 :
38 : class ImgProdLockBytes : public SvLockBytes
39 : {
40 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xStmRef;
41 : ::com::sun::star::uno::Sequence<sal_Int8> maSeq;
42 :
43 : public:
44 :
45 : ImgProdLockBytes( SvStream* pStm, bool bOwner );
46 : ImgProdLockBytes( ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > & rStreamRef );
47 : virtual ~ImgProdLockBytes();
48 :
49 : virtual ErrCode ReadAt( sal_uInt64 nPos, void* pBuffer, sal_Size nCount, sal_Size * pRead ) const SAL_OVERRIDE;
50 : virtual ErrCode WriteAt( sal_uInt64 nPos, const void* pBuffer, sal_Size nCount, sal_Size * pWritten ) SAL_OVERRIDE;
51 : virtual ErrCode Flush() const SAL_OVERRIDE;
52 : virtual ErrCode SetSize( sal_uInt64 nSize ) SAL_OVERRIDE;
53 : virtual ErrCode Stat( SvLockBytesStat*, SvLockBytesStatFlag ) const SAL_OVERRIDE;
54 : };
55 :
56 :
57 :
58 0 : ImgProdLockBytes::ImgProdLockBytes( SvStream* pStm, bool bOwner ) :
59 0 : SvLockBytes( pStm, bOwner )
60 : {
61 0 : }
62 :
63 :
64 :
65 1 : ImgProdLockBytes::ImgProdLockBytes( ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > & rStmRef ) :
66 1 : xStmRef( rStmRef )
67 : {
68 1 : if( xStmRef.is() )
69 : {
70 1 : const sal_uInt32 nBytesToRead = 65535;
71 : sal_uInt32 nRead;
72 :
73 1 : do
74 : {
75 1 : ::com::sun::star::uno::Sequence< sal_Int8 > aReadSeq;
76 :
77 1 : nRead = xStmRef->readSomeBytes( aReadSeq, nBytesToRead );
78 :
79 1 : if( nRead )
80 : {
81 1 : const sal_uInt32 nOldLength = maSeq.getLength();
82 1 : maSeq.realloc( nOldLength + nRead );
83 1 : memcpy( maSeq.getArray() + nOldLength, aReadSeq.getConstArray(), aReadSeq.getLength() );
84 1 : }
85 : }
86 : while( nBytesToRead == nRead );
87 : }
88 1 : }
89 :
90 :
91 :
92 3 : ImgProdLockBytes::~ImgProdLockBytes()
93 : {
94 3 : }
95 :
96 4 : ErrCode ImgProdLockBytes::ReadAt(sal_uInt64 const nPos,
97 : void* pBuffer, sal_Size nCount, sal_Size * pRead) const
98 : {
99 4 : if( GetStream() )
100 : {
101 0 : const_cast<SvStream*>(GetStream())->ResetError();
102 0 : const ErrCode nErr = SvLockBytes::ReadAt( nPos, pBuffer, nCount, pRead );
103 0 : const_cast<SvStream*>(GetStream())->ResetError();
104 0 : return nErr;
105 : }
106 : else
107 : {
108 4 : const sal_Size nSeqLen = maSeq.getLength();
109 4 : ErrCode nErr = ERRCODE_NONE;
110 :
111 4 : if( nPos < nSeqLen )
112 : {
113 4 : if( ( nPos + nCount ) > nSeqLen )
114 1 : nCount = nSeqLen - nPos;
115 :
116 4 : memcpy( pBuffer, maSeq.getConstArray() + nPos, nCount );
117 4 : *pRead = nCount;
118 : }
119 : else
120 0 : *pRead = 0UL;
121 :
122 4 : return nErr;
123 : }
124 : }
125 :
126 0 : ErrCode ImgProdLockBytes::WriteAt(sal_uInt64 const nPos,
127 : const void* pBuffer, sal_Size nCount, sal_Size * pWritten)
128 : {
129 0 : if( GetStream() )
130 0 : return SvLockBytes::WriteAt( nPos, pBuffer, nCount, pWritten );
131 : else
132 : {
133 : DBG_ASSERT( xStmRef.is(), "ImgProdLockBytes::WriteAt: xInputStream has no reference..." );
134 0 : return ERRCODE_IO_CANTWRITE;
135 : }
136 : }
137 :
138 :
139 :
140 1 : ErrCode ImgProdLockBytes::Flush() const
141 : {
142 1 : return ERRCODE_NONE;
143 : }
144 :
145 :
146 :
147 0 : ErrCode ImgProdLockBytes::SetSize(sal_uInt64 const nSize)
148 : {
149 0 : if( GetStream() )
150 0 : return SvLockBytes::SetSize( nSize );
151 : else
152 : {
153 : OSL_FAIL( "ImgProdLockBytes::SetSize not supported for xInputStream..." );
154 0 : return ERRCODE_IO_CANTWRITE;
155 : }
156 : }
157 :
158 :
159 :
160 2 : ErrCode ImgProdLockBytes::Stat( SvLockBytesStat* pStat, SvLockBytesStatFlag eFlag ) const
161 : {
162 2 : if( GetStream() )
163 0 : return SvLockBytes::Stat( pStat, eFlag );
164 : else
165 : {
166 : DBG_ASSERT( xStmRef.is(), "ImgProdLockBytes::Stat: xInputStream has no reference..." );
167 2 : pStat->nSize = maSeq.getLength();
168 2 : return ERRCODE_NONE;
169 : }
170 : }
171 :
172 : // - ImageProducer -
173 78 : ImageProducer::ImageProducer()
174 : : mpStm(NULL)
175 : , mnTransIndex(0)
176 78 : , mbConsInit(false)
177 : {
178 78 : mpGraphic = new Graphic;
179 78 : }
180 :
181 216 : ImageProducer::~ImageProducer()
182 : {
183 72 : delete mpGraphic;
184 72 : mpGraphic = NULL;
185 :
186 72 : delete mpStm;
187 72 : mpStm = NULL;
188 144 : }
189 :
190 : // ::com::sun::star::uno::XInterface
191 1 : ::com::sun::star::uno::Any ImageProducer::queryInterface( const ::com::sun::star::uno::Type & rType ) throw(::com::sun::star::uno::RuntimeException, std::exception)
192 : {
193 : ::com::sun::star::uno::Any aRet = ::cppu::queryInterface( rType,
194 : (static_cast< ::com::sun::star::lang::XInitialization* >(this)),
195 1 : (static_cast< ::com::sun::star::awt::XImageProducer* >(this)) );
196 1 : return (aRet.hasValue() ? aRet : OWeakObject::queryInterface( rType ));
197 : }
198 :
199 :
200 :
201 0 : void ImageProducer::addConsumer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer >& rxConsumer )
202 : throw(::com::sun::star::uno::RuntimeException,
203 : std::exception)
204 : {
205 : DBG_ASSERT( rxConsumer.is(), "::AddConsumer(...): No consumer referenced!" );
206 0 : if( rxConsumer.is() )
207 0 : maConsList.push_back( new ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer > ( rxConsumer ));
208 0 : }
209 :
210 :
211 :
212 0 : void ImageProducer::removeConsumer( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XImageConsumer >& rxConsumer ) throw(::com::sun::star::uno::RuntimeException, std::exception)
213 : {
214 0 : ConsumerList_t::reverse_iterator riter = std::find(maConsList.rbegin(),maConsList.rend(),rxConsumer);
215 :
216 0 : if (riter != maConsList.rend())
217 0 : maConsList.erase(riter.base()-1);
218 0 : }
219 :
220 :
221 :
222 1 : void ImageProducer::SetImage( const OUString& rPath )
223 : {
224 1 : maURL = rPath;
225 1 : mpGraphic->Clear();
226 1 : mbConsInit = false;
227 1 : delete mpStm;
228 :
229 1 : if ( ::svt::GraphicAccess::isSupportedURL( maURL ) )
230 : {
231 0 : mpStm = ::svt::GraphicAccess::getImageStream( ::comphelper::getProcessComponentContext(), maURL );
232 : }
233 1 : else if( !maURL.isEmpty() )
234 : {
235 0 : SvStream* pIStm = ::utl::UcbStreamHelper::CreateStream( maURL, STREAM_STD_READ );
236 0 : mpStm = pIStm ? new SvStream( new ImgProdLockBytes( pIStm, true ) ) : NULL;
237 : }
238 : else
239 1 : mpStm = NULL;
240 1 : }
241 :
242 :
243 :
244 0 : void ImageProducer::SetImage( SvStream& rStm )
245 : {
246 0 : maURL.clear();
247 0 : mpGraphic->Clear();
248 0 : mbConsInit = false;
249 :
250 0 : delete mpStm;
251 0 : mpStm = new SvStream( new ImgProdLockBytes( &rStm, false ) );
252 0 : }
253 :
254 :
255 :
256 6 : void ImageProducer::setImage( ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > & rInputStmRef )
257 : {
258 6 : maURL.clear();
259 6 : mpGraphic->Clear();
260 6 : mbConsInit = false;
261 6 : delete mpStm;
262 :
263 6 : if( rInputStmRef.is() )
264 1 : mpStm = new SvStream( new ImgProdLockBytes( rInputStmRef ) );
265 : else
266 5 : mpStm = NULL;
267 6 : }
268 :
269 :
270 :
271 0 : void ImageProducer::NewDataAvailable()
272 : {
273 0 : if( ( GRAPHIC_NONE == mpGraphic->GetType() ) || mpGraphic->GetContext() )
274 0 : startProduction();
275 0 : }
276 :
277 :
278 :
279 6 : void ImageProducer::startProduction() throw(::com::sun::star::uno::RuntimeException, std::exception)
280 : {
281 6 : if( !maConsList.empty() || maDoneHdl.IsSet() )
282 : {
283 6 : bool bNotifyEmptyGraphics = false;
284 :
285 : // valid stream or filled graphic? => update consumers
286 6 : if( mpStm || ( mpGraphic->GetType() != GRAPHIC_NONE ) )
287 : {
288 : // if we already have a graphic, we don't have to import again;
289 : // graphic is cleared if a new Stream is set
290 1 : if( ( mpGraphic->GetType() == GRAPHIC_NONE ) || mpGraphic->GetContext() )
291 : {
292 1 : if ( ImplImportGraphic( *mpGraphic ) && maDoneHdl.IsSet() )
293 1 : maDoneHdl.Call( mpGraphic );
294 : }
295 :
296 1 : if( mpGraphic->GetType() != GRAPHIC_NONE )
297 1 : ImplUpdateData( *mpGraphic );
298 : else
299 0 : bNotifyEmptyGraphics = true;
300 : }
301 : else
302 5 : bNotifyEmptyGraphics = true;
303 :
304 6 : if ( bNotifyEmptyGraphics )
305 : {
306 : // reset image
307 : // create temporary list to hold interfaces
308 5 : ConsumerList_t aTmp = maConsList;
309 :
310 : // iterate through interfaces
311 5 : for( ConsumerList_t::iterator iter = aTmp.begin(); iter != aTmp.end(); ++iter )
312 : {
313 0 : (*iter)->init( 0, 0 );
314 0 : (*iter)->complete( ::com::sun::star::awt::ImageStatus::IMAGESTATUS_STATICIMAGEDONE, this );
315 : }
316 :
317 5 : if ( maDoneHdl.IsSet() )
318 5 : maDoneHdl.Call( NULL );
319 : }
320 : }
321 6 : }
322 :
323 :
324 :
325 1 : bool ImageProducer::ImplImportGraphic( Graphic& rGraphic )
326 : {
327 1 : if (!mpStm)
328 0 : return false;
329 :
330 1 : if( ERRCODE_IO_PENDING == mpStm->GetError() )
331 0 : mpStm->ResetError();
332 :
333 1 : mpStm->Seek( 0UL );
334 :
335 1 : bool bRet = GraphicConverter::Import( *mpStm, rGraphic ) == ERRCODE_NONE;
336 :
337 1 : if( ERRCODE_IO_PENDING == mpStm->GetError() )
338 0 : mpStm->ResetError();
339 :
340 1 : return bRet;
341 : }
342 :
343 :
344 :
345 1 : void ImageProducer::ImplUpdateData( const Graphic& rGraphic )
346 : {
347 1 : ImplInitConsumer( rGraphic );
348 :
349 1 : if( mbConsInit && !maConsList.empty() )
350 : {
351 : // create temporary list to hold interfaces
352 0 : ConsumerList_t aTmp = maConsList;
353 :
354 0 : ImplUpdateConsumer( rGraphic );
355 0 : mbConsInit = false;
356 :
357 : // iterate through interfaces
358 0 : for( ConsumerList_t::iterator iter = aTmp.begin(); iter != aTmp.end(); ++iter )
359 0 : (*iter)->complete( ::com::sun::star::awt::ImageStatus::IMAGESTATUS_STATICIMAGEDONE, this );
360 : }
361 1 : }
362 :
363 :
364 :
365 1 : void ImageProducer::ImplInitConsumer( const Graphic& rGraphic )
366 : {
367 1 : Bitmap aBmp( rGraphic.GetBitmapEx().GetBitmap() );
368 1 : BitmapReadAccess* pBmpAcc = aBmp.AcquireReadAccess();
369 :
370 1 : if( pBmpAcc )
371 : {
372 1 : sal_uInt16 nPalCount = 0;
373 1 : sal_uInt32 nRMask = 0;
374 1 : sal_uInt32 nGMask = 0;
375 1 : sal_uInt32 nBMask = 0;
376 1 : sal_uInt32 nAMask = 0;
377 1 : ::com::sun::star::uno::Sequence< sal_Int32 > aRGBPal;
378 :
379 1 : if( pBmpAcc->HasPalette() )
380 : {
381 1 : nPalCount = pBmpAcc->GetPaletteEntryCount();
382 :
383 1 : if( nPalCount )
384 : {
385 1 : aRGBPal = ::com::sun::star::uno::Sequence< sal_Int32 >( nPalCount + 1 );
386 :
387 1 : sal_Int32* pTmp = aRGBPal.getArray();
388 :
389 257 : for( sal_uInt32 i = 0; i < nPalCount; i++, pTmp++ )
390 : {
391 256 : const BitmapColor& rCol = pBmpAcc->GetPaletteColor( (sal_uInt16) i );
392 :
393 256 : *pTmp = ( (sal_Int32) rCol.GetRed() ) << (sal_Int32)(24L);
394 256 : *pTmp |= ( (sal_Int32) rCol.GetGreen() ) << (sal_Int32)(16L);
395 256 : *pTmp |= ( (sal_Int32) rCol.GetBlue() ) << (sal_Int32)(8L);
396 256 : *pTmp |= (sal_Int32)(0x000000ffL);
397 : }
398 :
399 1 : if( rGraphic.IsTransparent() )
400 : {
401 : // append transparent entry
402 1 : *pTmp = (sal_Int32)(0xffffff00L);
403 1 : mnTransIndex = nPalCount;
404 1 : nPalCount++;
405 : }
406 : else
407 0 : mnTransIndex = 0;
408 :
409 : }
410 : }
411 : else
412 : {
413 0 : nRMask = 0xff000000UL;
414 0 : nGMask = 0x00ff0000UL;
415 0 : nBMask = 0x0000ff00UL;
416 0 : nAMask = 0x000000ffUL;
417 : }
418 :
419 : // create temporary list to hold interfaces
420 2 : ConsumerList_t aTmp = maConsList;
421 :
422 : // iterate through interfaces
423 1 : for( ConsumerList_t::iterator iter = aTmp.begin(); iter != aTmp.end(); ++iter)
424 : {
425 0 : (*iter)->init( pBmpAcc->Width(), pBmpAcc->Height() );
426 0 : (*iter)->setColorModel( pBmpAcc->GetBitCount(),aRGBPal, nRMask, nGMask, nBMask, nAMask );
427 : }
428 :
429 1 : Bitmap::ReleaseAccess( pBmpAcc );
430 2 : mbConsInit = true;
431 1 : }
432 1 : }
433 :
434 :
435 :
436 0 : void ImageProducer::ImplUpdateConsumer( const Graphic& rGraphic )
437 : {
438 0 : BitmapEx aBmpEx( rGraphic.GetBitmapEx() );
439 0 : Bitmap aBmp( aBmpEx.GetBitmap() );
440 0 : BitmapReadAccess* pBmpAcc = aBmp.AcquireReadAccess();
441 :
442 0 : if( pBmpAcc )
443 : {
444 0 : Bitmap aMask( aBmpEx.GetMask() );
445 0 : BitmapReadAccess* pMskAcc = !!aMask ? aMask.AcquireReadAccess() : NULL;
446 0 : const long nWidth = pBmpAcc->Width();
447 0 : const long nHeight = pBmpAcc->Height();
448 0 : const long nStartX = 0L;
449 0 : const long nEndX = nWidth - 1L;
450 0 : const long nStartY = 0L;
451 0 : const long nEndY = nHeight - 1L;
452 0 : const long nPartWidth = nEndX - nStartX + 1;
453 0 : const long nPartHeight = nEndY - nStartY + 1;
454 :
455 0 : if( !pMskAcc )
456 : {
457 0 : aMask = Bitmap( aBmp.GetSizePixel(), 1 );
458 0 : aMask.Erase( COL_BLACK );
459 0 : pMskAcc = aMask.AcquireReadAccess();
460 : }
461 :
462 : // create temporary list to hold interfaces
463 0 : ConsumerList_t aTmp = maConsList;
464 :
465 0 : if( pBmpAcc->HasPalette() )
466 : {
467 0 : const BitmapColor aWhite( pMskAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
468 :
469 0 : if( mnTransIndex < 256 )
470 : {
471 0 : ::com::sun::star::uno::Sequence<sal_Int8> aData( nPartWidth * nPartHeight );
472 0 : sal_Int8* pTmp = aData.getArray();
473 :
474 0 : for( long nY = nStartY; nY <= nEndY; nY++ )
475 : {
476 0 : for( long nX = nStartX; nX <= nEndX; nX++ )
477 : {
478 0 : if( pMskAcc->GetPixel( nY, nX ) == aWhite )
479 : *pTmp++ = sal::static_int_cast< sal_Int8 >(
480 0 : mnTransIndex );
481 : else
482 0 : *pTmp++ = pBmpAcc->GetPixel( nY, nX ).GetIndex();
483 : }
484 : }
485 :
486 : // iterate through interfaces
487 0 : for (ConsumerList_t::iterator iter = aTmp.begin(); iter != aTmp.end(); ++iter)
488 0 : (*iter)->setPixelsByBytes( nStartX, nStartY, nPartWidth, nPartHeight, aData, 0UL, nPartWidth );
489 : }
490 : else
491 : {
492 0 : ::com::sun::star::uno::Sequence<sal_Int32> aData( nPartWidth * nPartHeight );
493 0 : sal_Int32* pTmp = aData.getArray();
494 :
495 0 : for( long nY = nStartY; nY <= nEndY; nY++ )
496 : {
497 0 : for( long nX = nStartX; nX <= nEndX; nX++ )
498 : {
499 0 : if( pMskAcc->GetPixel( nY, nX ) == aWhite )
500 0 : *pTmp++ = mnTransIndex;
501 : else
502 0 : *pTmp++ = pBmpAcc->GetPixel( nY, nX ).GetIndex();
503 : }
504 : }
505 :
506 : // iterate through interfaces
507 0 : for (ConsumerList_t::iterator iter = aTmp.begin(); iter != aTmp.end(); ++iter)
508 0 : (*iter)->setPixelsByLongs( nStartX, nStartY, nPartWidth, nPartHeight, aData, 0UL, nPartWidth );
509 0 : }
510 : }
511 : else
512 : {
513 0 : ::com::sun::star::uno::Sequence<sal_Int32> aData( nPartWidth * nPartHeight );
514 0 : const BitmapColor aWhite( pMskAcc->GetBestMatchingColor( Color( COL_WHITE ) ) );
515 0 : sal_Int32* pTmp = aData.getArray();
516 :
517 0 : for( long nY = nStartY; nY <= nEndY; nY++ )
518 : {
519 0 : for( long nX = nStartX; nX <= nEndX; nX++, pTmp++ )
520 : {
521 0 : const BitmapColor aCol( pBmpAcc->GetPixel( nY, nX ) );
522 :
523 0 : *pTmp = ( (sal_Int32) aCol.GetRed() ) << (sal_Int32)(24L);
524 0 : *pTmp |= ( (sal_Int32) aCol.GetGreen() ) << (sal_Int32)(16L);
525 0 : *pTmp |= ( (sal_Int32) aCol.GetBlue() ) << (sal_Int32)(8L);
526 :
527 0 : if( pMskAcc->GetPixel( nY, nX ) != aWhite )
528 0 : *pTmp |= 0x000000ffUL;
529 0 : }
530 : }
531 :
532 : // iterate through interfaces
533 0 : for (ConsumerList_t::iterator iter = aTmp.begin(); iter != aTmp.end(); ++iter)
534 0 : (*iter)->setPixelsByLongs( nStartX, nStartY, nPartWidth, nPartHeight, aData, 0UL, nPartWidth );
535 : }
536 :
537 0 : Bitmap::ReleaseAccess( pBmpAcc );
538 0 : Bitmap::ReleaseAccess( pMskAcc );
539 0 : }
540 0 : }
541 :
542 0 : void ImageProducer::initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw (::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException, std::exception)
543 : {
544 0 : if ( aArguments.getLength() == 1 )
545 : {
546 0 : ::com::sun::star::uno::Any aArg = aArguments.getConstArray()[0];
547 0 : OUString aURL;
548 0 : if ( aArg >>= aURL )
549 : {
550 0 : SetImage( aURL );
551 0 : }
552 : }
553 0 : }
554 :
555 : extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
556 0 : com_sun_star_form_ImageProducer_get_implementation(::com::sun::star::uno::XComponentContext*,
557 : ::com::sun::star::uno::Sequence<css::uno::Any> const &)
558 : {
559 0 : return cppu::acquire(new ImageProducer());
560 138 : }
561 :
562 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|