1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 #include <tools/stream.hxx>
22 #include <tools/debug.hxx>
23 #include <vcl/BitmapReadAccess.hxx>
24 #include <vcl/graph.hxx>
25 #include <vcl/outdev.hxx>
26 #include <vcl/FilterConfigItem.hxx>
27 #include <com/sun/star/task/XStatusIndicator.hpp>
28 #include "giflzwc.hxx"
30 #include <filter/GifWriter.hxx>
38 BitmapReadAccess
* m_pAcc
;
39 sal_uInt32 nMinPercent
;
40 sal_uInt32 nMaxPercent
;
41 sal_uInt32 nLastPercent
;
44 sal_Int32 nInterlaced
;
48 void MayCallback(sal_uInt32 nPercent
);
49 void WriteSignature( bool bGIF89a
);
50 void WriteGlobalHeader( const Size
& rSize
);
51 void WriteLoopExtension( const Animation
& rAnimation
);
52 void WriteLogSizeExtension( const Size
& rSize100
);
53 void WriteImageExtension( tools::Long nTimer
, Disposal eDisposal
);
54 void WriteLocalHeader();
57 void WriteTerminator();
59 bool CreateAccess( const BitmapEx
& rBmpEx
);
62 void WriteAnimation( const Animation
& rAnimation
);
63 void WriteBitmapEx( const BitmapEx
& rBmpEx
, const Point
& rPoint
, bool bExtended
,
64 tools::Long nTimer
= 0, Disposal eDisposal
= Disposal::Not
);
66 css::uno::Reference
< css::task::XStatusIndicator
> xStatusIndicator
;
70 explicit GIFWriter(SvStream
&rStream
);
72 bool WriteGIF( const Graphic
& rGraphic
, FilterConfigItem
* pConfigItem
);
77 GIFWriter::GIFWriter(SvStream
&rStream
)
92 bool GIFWriter::WriteGIF(const Graphic
& rGraphic
, FilterConfigItem
* pFilterConfigItem
)
94 if ( pFilterConfigItem
)
96 xStatusIndicator
= pFilterConfigItem
->GetStatusIndicator();
97 if ( xStatusIndicator
.is() )
99 xStatusIndicator
->start( OUString(), 100 );
104 const MapMode
aMap( rGraphic
.GetPrefMapMode() );
105 bool bLogSize
= ( aMap
.GetMapUnit() != MapUnit::MapPixel
);
108 aSize100
= OutputDevice::LogicToLogic(rGraphic
.GetPrefSize(), aMap
, MapMode(MapUnit::Map100thMM
));
115 if ( pFilterConfigItem
)
116 nInterlaced
= pFilterConfigItem
->ReadInt32( "Interlaced", 0 );
118 m_rGIF
.SetEndian( SvStreamEndian::LITTLE
);
120 if( rGraphic
.IsAnimated() )
122 const Animation
& rAnimation
= rGraphic
.GetAnimation();
124 WriteSignature( true );
128 WriteGlobalHeader( rAnimation
.GetDisplaySizePixel() );
132 WriteLoopExtension( rAnimation
);
135 WriteAnimation( rAnimation
);
141 const bool bGrafTrans
= rGraphic
.IsTransparent();
143 BitmapEx aBmpEx
= rGraphic
.GetBitmapEx();
148 WriteSignature( bGrafTrans
|| bLogSize
);
152 WriteGlobalHeader( aBmpEx
.GetSizePixel() );
155 WriteBitmapEx( aBmpEx
, Point(), bGrafTrans
);
162 WriteLogSizeExtension( aSize100
);
167 if ( xStatusIndicator
.is() )
168 xStatusIndicator
->end();
174 void GIFWriter::WriteBitmapEx( const BitmapEx
& rBmpEx
, const Point
& rPoint
,
175 bool bExtended
, tools::Long nTimer
, Disposal eDisposal
)
177 if( !CreateAccess( rBmpEx
) )
184 WriteImageExtension( nTimer
, eDisposal
);
203 void GIFWriter::WriteAnimation( const Animation
& rAnimation
)
205 const sal_uInt16 nCount
= rAnimation
.Count();
210 const double fStep
= 100. / nCount
;
213 nMaxPercent
= static_cast<sal_uInt32
>(fStep
);
215 for( sal_uInt16 i
= 0; i
< nCount
; i
++ )
217 const AnimationBitmap
& rAnimationBitmap
= rAnimation
.Get( i
);
219 WriteBitmapEx(rAnimationBitmap
.maBitmapEx
, rAnimationBitmap
.maPositionPixel
, true,
220 rAnimationBitmap
.mnWait
, rAnimationBitmap
.meDisposal
);
221 nMinPercent
= nMaxPercent
;
222 nMaxPercent
= static_cast<sal_uInt32
>(nMaxPercent
+ fStep
);
227 void GIFWriter::MayCallback(sal_uInt32 nPercent
)
229 if ( xStatusIndicator
.is() )
231 if( nPercent
>= nLastPercent
+ 3 )
233 nLastPercent
= nPercent
;
234 if ( nPercent
<= 100 )
235 xStatusIndicator
->setValue( nPercent
);
241 bool GIFWriter::CreateAccess( const BitmapEx
& rBmpEx
)
245 Bitmap
aMask( rBmpEx
.GetAlpha() );
247 aAccBmp
= rBmpEx
.GetBitmap();
248 bTransparent
= false;
250 if( !aMask
.IsEmpty() )
252 if( aAccBmp
.Convert( BmpConversion::N8BitTrans
) )
254 aMask
.Convert( BmpConversion::N1BitThreshold
);
255 aAccBmp
.Replace( aMask
, BMP_COL_TRANS
);
259 aAccBmp
.Convert( BmpConversion::N8BitColors
);
262 aAccBmp
.Convert( BmpConversion::N8BitColors
);
264 m_pAcc
= aAccBmp
.AcquireReadAccess();
274 void GIFWriter::DestroyAccess()
276 Bitmap::ReleaseAccess( m_pAcc
);
281 void GIFWriter::WriteSignature( bool bGIF89a
)
285 m_rGIF
.WriteBytes(bGIF89a
? "GIF89a" : "GIF87a" , 6);
287 if( m_rGIF
.GetError() )
293 void GIFWriter::WriteGlobalHeader( const Size
& rSize
)
299 const sal_uInt16 nWidth
= static_cast<sal_uInt16
>(rSize
.Width());
300 const sal_uInt16 nHeight
= static_cast<sal_uInt16
>(rSize
.Height());
301 const sal_uInt8 cFlags
= 128 | ( 7 << 4 );
304 m_rGIF
.WriteUInt16( nWidth
);
305 m_rGIF
.WriteUInt16( nHeight
);
306 m_rGIF
.WriteUChar( cFlags
);
307 m_rGIF
.WriteUChar( 0x00 );
308 m_rGIF
.WriteUChar( 0x00 );
310 // write dummy palette with two entries (black/white);
311 // we do this only because of a bug in Photoshop, since those can't
312 // read pictures without a global color palette
313 m_rGIF
.WriteUInt16( 0 );
314 m_rGIF
.WriteUInt16( 255 );
315 m_rGIF
.WriteUInt16( 65535 );
317 if( m_rGIF
.GetError() )
322 void GIFWriter::WriteLoopExtension( const Animation
& rAnimation
)
324 DBG_ASSERT( rAnimation
.Count() > 0, "Animation has no bitmaps!" );
326 sal_uInt16 nLoopCount
= static_cast<sal_uInt16
>(rAnimation
.GetLoopCount());
328 // if only one run should take place
329 // the LoopExtension won't be written
330 // The default in this case is a single run
331 if( nLoopCount
== 1 )
334 // Netscape interprets the LoopCount
335 // as the sole number of _repetitions_
339 const sal_uInt8 cLoByte
= static_cast<sal_uInt8
>(nLoopCount
);
340 const sal_uInt8 cHiByte
= static_cast<sal_uInt8
>( nLoopCount
>> 8 );
342 m_rGIF
.WriteUChar( 0x21 );
343 m_rGIF
.WriteUChar( 0xff );
344 m_rGIF
.WriteUChar( 0x0b );
345 m_rGIF
.WriteBytes( "NETSCAPE2.0", 11 );
346 m_rGIF
.WriteUChar( 0x03 );
347 m_rGIF
.WriteUChar( 0x01 );
348 m_rGIF
.WriteUChar( cLoByte
);
349 m_rGIF
.WriteUChar( cHiByte
);
350 m_rGIF
.WriteUChar( 0x00 );
354 void GIFWriter::WriteLogSizeExtension( const Size
& rSize100
)
356 // writer PrefSize in 100th-mm as ApplicationExtension
357 if( rSize100
.Width() && rSize100
.Height() )
359 m_rGIF
.WriteUChar( 0x21 );
360 m_rGIF
.WriteUChar( 0xff );
361 m_rGIF
.WriteUChar( 0x0b );
362 m_rGIF
.WriteBytes( "STARDIV 5.0", 11 );
363 m_rGIF
.WriteUChar( 0x09 );
364 m_rGIF
.WriteUChar( 0x01 );
365 m_rGIF
.WriteUInt32( rSize100
.Width() );
366 m_rGIF
.WriteUInt32( rSize100
.Height() );
367 m_rGIF
.WriteUChar( 0x00 );
372 void GIFWriter::WriteImageExtension( tools::Long nTimer
, Disposal eDisposal
)
377 const sal_uInt16 nDelay
= static_cast<sal_uInt16
>(nTimer
);
378 sal_uInt8 cFlags
= 0;
380 // set Transparency-Flag
384 // set Disposal-value
385 if( eDisposal
== Disposal::Back
)
386 cFlags
|= ( 2 << 2 );
387 else if( eDisposal
== Disposal::Previous
)
388 cFlags
|= ( 3 << 2 );
390 m_rGIF
.WriteUChar( 0x21 );
391 m_rGIF
.WriteUChar( 0xf9 );
392 m_rGIF
.WriteUChar( 0x04 );
393 m_rGIF
.WriteUChar( cFlags
);
394 m_rGIF
.WriteUInt16( nDelay
);
395 m_rGIF
.WriteUChar( m_pAcc
->GetBestPaletteIndex( BMP_COL_TRANS
) );
396 m_rGIF
.WriteUChar( 0x00 );
398 if( m_rGIF
.GetError() )
403 void GIFWriter::WriteLocalHeader()
408 const sal_uInt16 nPosX
= static_cast<sal_uInt16
>(nActX
);
409 const sal_uInt16 nPosY
= static_cast<sal_uInt16
>(nActY
);
410 const sal_uInt16 nWidth
= static_cast<sal_uInt16
>(m_pAcc
->Width());
411 const sal_uInt16 nHeight
= static_cast<sal_uInt16
>(m_pAcc
->Height());
412 sal_uInt8 cFlags
= static_cast<sal_uInt8
>( m_pAcc
->GetBitCount() - 1 );
414 // set Interlaced-Flag
418 // set Flag for the local color palette
421 m_rGIF
.WriteUChar( 0x2c );
422 m_rGIF
.WriteUInt16( nPosX
);
423 m_rGIF
.WriteUInt16( nPosY
);
424 m_rGIF
.WriteUInt16( nWidth
);
425 m_rGIF
.WriteUInt16( nHeight
);
426 m_rGIF
.WriteUChar( cFlags
);
428 if( m_rGIF
.GetError() )
433 void GIFWriter::WritePalette()
435 if( !(bStatus
&& m_pAcc
->HasPalette()) )
438 const sal_uInt16 nCount
= m_pAcc
->GetPaletteEntryCount();
439 const sal_uInt16 nMaxCount
= ( 1 << m_pAcc
->GetBitCount() );
441 for ( sal_uInt16 i
= 0; i
< nCount
; i
++ )
443 const BitmapColor
& rColor
= m_pAcc
->GetPaletteColor( i
);
445 m_rGIF
.WriteUChar( rColor
.GetRed() );
446 m_rGIF
.WriteUChar( rColor
.GetGreen() );
447 m_rGIF
.WriteUChar( rColor
.GetBlue() );
450 // fill up the rest with 0
451 if( nCount
< nMaxCount
)
452 m_rGIF
.SeekRel( ( nMaxCount
- nCount
) * 3 );
454 if( m_rGIF
.GetError() )
459 void GIFWriter::WriteAccess()
461 GIFLZWCompressor aCompressor
;
462 const tools::Long nWidth
= m_pAcc
->Width();
463 const tools::Long nHeight
= m_pAcc
->Height();
464 std::unique_ptr
<sal_uInt8
[]> pBuffer
;
465 bool bNative
= m_pAcc
->GetScanlineFormat() == ScanlineFormat::N8BitPal
;
468 pBuffer
.reset(new sal_uInt8
[ nWidth
]);
470 if( !(bStatus
&& ( 8 == m_pAcc
->GetBitCount() ) && m_pAcc
->HasPalette()) )
473 aCompressor
.StartCompression( m_rGIF
, m_pAcc
->GetBitCount() );
477 for( tools::Long i
= 0; i
< nHeight
; ++i
)
485 nT
= i
- ( ( nHeight
+ 7 ) >> 3 );
490 nT
-= ( nHeight
+ 3 ) >> 3;
491 nY
= ( nT
<< 2 ) + 2;
495 nT
-= ( ( nHeight
+ 1 ) >> 2 );
496 nY
= ( nT
<< 1 ) + 1;
505 aCompressor
.Compress( m_pAcc
->GetScanline( nY
), nWidth
);
508 Scanline pScanline
= m_pAcc
->GetScanline( nY
);
509 for( tools::Long nX
= 0; nX
< nWidth
; nX
++ )
510 pBuffer
[ nX
] = m_pAcc
->GetIndexFromData( pScanline
, nX
);
512 aCompressor
.Compress( pBuffer
.get(), nWidth
);
515 if ( m_rGIF
.GetError() )
518 MayCallback( nMinPercent
+ ( nMaxPercent
- nMinPercent
) * i
/ nHeight
);
524 aCompressor
.EndCompression();
526 if ( m_rGIF
.GetError() )
531 void GIFWriter::WriteTerminator()
535 m_rGIF
.WriteUChar( 0x3b );
537 if( m_rGIF
.GetError() )
543 bool ExportGifGraphic(SvStream
& rStream
, Graphic
& rGraphic
, FilterConfigItem
* pConfigItem
)
545 GIFWriter
aWriter(rStream
);
546 return aWriter
.WriteGIF(rGraphic
, pConfigItem
);
549 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */