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 : #include "services.hxx"
23 :
24 : #include <vcl/bmpacc.hxx>
25 : #include <vcl/cvtgrf.hxx>
26 : #include <vcl/svapp.hxx>
27 : #include <unotools/ucbstreamhelper.hxx>
28 : #include <vcl/graphicfilter.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 :
36 : // - ImgProdLockBytes -
37 :
38 :
39 : class ImgProdLockBytes : public SvLockBytes
40 : {
41 : ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > xStmRef;
42 : ::com::sun::star::uno::Sequence<sal_Int8> maSeq;
43 :
44 : public:
45 :
46 : ImgProdLockBytes( SvStream* pStm, 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, bool bOwner ) :
60 0 : SvLockBytes( pStm, bOwner )
61 : {
62 0 : }
63 :
64 :
65 :
66 2 : ImgProdLockBytes::ImgProdLockBytes( ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > & rStmRef ) :
67 2 : xStmRef( rStmRef )
68 : {
69 2 : if( xStmRef.is() )
70 : {
71 2 : const sal_uInt32 nBytesToRead = 65535;
72 : sal_uInt32 nRead;
73 :
74 2 : do
75 : {
76 2 : ::com::sun::star::uno::Sequence< sal_Int8 > aReadSeq;
77 :
78 2 : nRead = xStmRef->readSomeBytes( aReadSeq, nBytesToRead );
79 :
80 2 : if( nRead )
81 : {
82 2 : const sal_uInt32 nOldLength = maSeq.getLength();
83 2 : maSeq.realloc( nOldLength + nRead );
84 2 : memcpy( maSeq.getArray() + nOldLength, aReadSeq.getConstArray(), aReadSeq.getLength() );
85 2 : }
86 : }
87 : while( nBytesToRead == nRead );
88 : }
89 2 : }
90 :
91 :
92 :
93 4 : ImgProdLockBytes::~ImgProdLockBytes()
94 : {
95 4 : }
96 :
97 8 : ErrCode ImgProdLockBytes::ReadAt(sal_uInt64 const nPos,
98 : void* pBuffer, sal_Size nCount, sal_Size * pRead) const
99 : {
100 8 : 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 8 : const sal_Size nSeqLen = maSeq.getLength();
110 8 : ErrCode nErr = ERRCODE_NONE;
111 :
112 8 : if( nPos < nSeqLen )
113 : {
114 8 : if( ( nPos + nCount ) > nSeqLen )
115 2 : nCount = nSeqLen - nPos;
116 :
117 8 : memcpy( pBuffer, maSeq.getConstArray() + nPos, nCount );
118 8 : *pRead = nCount;
119 : }
120 : else
121 0 : *pRead = 0UL;
122 :
123 8 : 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 2 : ErrCode ImgProdLockBytes::Flush() const
142 : {
143 2 : 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 2 : ErrCode ImgProdLockBytes::Stat( SvLockBytesStat* pStat, SvLockBytesStatFlag eFlag ) const
162 : {
163 2 : if( GetStream() )
164 0 : return SvLockBytes::Stat( pStat, eFlag );
165 : else
166 : {
167 : DBG_ASSERT( xStmRef.is(), "ImgProdLockBytes::Stat: xInputStream has no reference..." );
168 2 : pStat->nSize = maSeq.getLength();
169 2 : return ERRCODE_NONE;
170 : }
171 : }
172 :
173 : // - ImageProducer -
174 144 : ImageProducer::ImageProducer()
175 : : mpStm(NULL)
176 : , mnTransIndex(0)
177 144 : , mbConsInit(false)
178 : {
179 144 : mpGraphic = new Graphic;
180 144 : }
181 :
182 396 : ImageProducer::~ImageProducer()
183 : {
184 132 : delete mpGraphic;
185 132 : mpGraphic = NULL;
186 :
187 132 : delete mpStm;
188 132 : mpStm = NULL;
189 264 : }
190 :
191 : // ::com::sun::star::uno::XInterface
192 2 : ::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 2 : (static_cast< ::com::sun::star::awt::XImageProducer* >(this)) );
197 2 : 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 2 : void ImageProducer::SetImage( const OUString& rPath )
224 : {
225 2 : maURL = rPath;
226 2 : mpGraphic->Clear();
227 2 : mbConsInit = false;
228 2 : delete mpStm;
229 :
230 2 : if ( ::svt::GraphicAccess::isSupportedURL( maURL ) )
231 : {
232 0 : mpStm = ::svt::GraphicAccess::getImageStream( ::comphelper::getProcessComponentContext(), maURL );
233 : }
234 2 : 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, true ) ) : NULL;
238 : }
239 : else
240 2 : mpStm = NULL;
241 2 : }
242 :
243 :
244 :
245 0 : void ImageProducer::SetImage( SvStream& rStm )
246 : {
247 0 : maURL = OUString();
248 0 : mpGraphic->Clear();
249 0 : mbConsInit = false;
250 :
251 0 : delete mpStm;
252 0 : mpStm = new SvStream( new ImgProdLockBytes( &rStm, false ) );
253 0 : }
254 :
255 :
256 :
257 12 : void ImageProducer::setImage( ::com::sun::star::uno::Reference< ::com::sun::star::io::XInputStream > & rInputStmRef )
258 : {
259 12 : maURL = OUString();
260 12 : mpGraphic->Clear();
261 12 : mbConsInit = false;
262 12 : delete mpStm;
263 :
264 12 : if( rInputStmRef.is() )
265 2 : mpStm = new SvStream( new ImgProdLockBytes( rInputStmRef ) );
266 : else
267 10 : mpStm = NULL;
268 12 : }
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 12 : void ImageProducer::startProduction() throw(::com::sun::star::uno::RuntimeException, std::exception)
281 : {
282 12 : if( !maConsList.empty() || maDoneHdl.IsSet() )
283 : {
284 12 : bool bNotifyEmptyGraphics = false;
285 :
286 : // valid stream or filled graphic? => update consumers
287 12 : 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 2 : if( ( mpGraphic->GetType() == GRAPHIC_NONE ) || mpGraphic->GetContext() )
292 : {
293 2 : if ( ImplImportGraphic( *mpGraphic ) && maDoneHdl.IsSet() )
294 2 : maDoneHdl.Call( mpGraphic );
295 : }
296 :
297 2 : if( mpGraphic->GetType() != GRAPHIC_NONE )
298 2 : ImplUpdateData( *mpGraphic );
299 : else
300 0 : bNotifyEmptyGraphics = true;
301 : }
302 : else
303 10 : bNotifyEmptyGraphics = true;
304 :
305 12 : if ( bNotifyEmptyGraphics )
306 : {
307 : // reset image
308 : // create temporary list to hold interfaces
309 10 : ConsumerList_t aTmp = maConsList;
310 :
311 : // iterate through interfaces
312 10 : 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 10 : if ( maDoneHdl.IsSet() )
319 10 : maDoneHdl.Call( NULL );
320 : }
321 : }
322 12 : }
323 :
324 :
325 :
326 2 : bool ImageProducer::ImplImportGraphic( Graphic& rGraphic )
327 : {
328 2 : if (!mpStm)
329 0 : return false;
330 :
331 2 : if( ERRCODE_IO_PENDING == mpStm->GetError() )
332 0 : mpStm->ResetError();
333 :
334 2 : mpStm->Seek( 0UL );
335 :
336 2 : bool bRet = GraphicConverter::Import( *mpStm, rGraphic ) == ERRCODE_NONE;
337 :
338 2 : if( ERRCODE_IO_PENDING == mpStm->GetError() )
339 0 : mpStm->ResetError();
340 :
341 2 : return bRet;
342 : }
343 :
344 :
345 :
346 2 : void ImageProducer::ImplUpdateData( const Graphic& rGraphic )
347 : {
348 2 : ImplInitConsumer( rGraphic );
349 :
350 2 : 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 = 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 2 : }
363 :
364 :
365 :
366 2 : void ImageProducer::ImplInitConsumer( const Graphic& rGraphic )
367 : {
368 2 : Bitmap aBmp( rGraphic.GetBitmapEx().GetBitmap() );
369 2 : BitmapReadAccess* pBmpAcc = aBmp.AcquireReadAccess();
370 :
371 2 : if( pBmpAcc )
372 : {
373 2 : sal_uInt16 nPalCount = 0;
374 2 : sal_uInt32 nRMask = 0;
375 2 : sal_uInt32 nGMask = 0;
376 2 : sal_uInt32 nBMask = 0;
377 2 : sal_uInt32 nAMask = 0;
378 2 : ::com::sun::star::uno::Sequence< sal_Int32 > aRGBPal;
379 :
380 2 : if( pBmpAcc->HasPalette() )
381 : {
382 2 : nPalCount = pBmpAcc->GetPaletteEntryCount();
383 :
384 2 : if( nPalCount )
385 : {
386 2 : aRGBPal = ::com::sun::star::uno::Sequence< sal_Int32 >( nPalCount + 1 );
387 :
388 2 : sal_Int32* pTmp = aRGBPal.getArray();
389 :
390 514 : for( sal_uInt32 i = 0; i < nPalCount; i++, pTmp++ )
391 : {
392 512 : const BitmapColor& rCol = pBmpAcc->GetPaletteColor( (sal_uInt16) i );
393 :
394 512 : *pTmp = ( (sal_Int32) rCol.GetRed() ) << (sal_Int32)(24L);
395 512 : *pTmp |= ( (sal_Int32) rCol.GetGreen() ) << (sal_Int32)(16L);
396 512 : *pTmp |= ( (sal_Int32) rCol.GetBlue() ) << (sal_Int32)(8L);
397 512 : *pTmp |= (sal_Int32)(0x000000ffL);
398 : }
399 :
400 2 : if( rGraphic.IsTransparent() )
401 : {
402 : // append transparent entry
403 2 : *pTmp = (sal_Int32)(0xffffff00L);
404 2 : mnTransIndex = nPalCount;
405 2 : 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 4 : ConsumerList_t aTmp = maConsList;
422 :
423 : // iterate through interfaces
424 2 : 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 2 : aBmp.ReleaseAccess( pBmpAcc );
431 4 : mbConsInit = true;
432 2 : }
433 2 : }
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 192 : } // namespace frm
566 :
567 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|