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