bump product version to 7.2.5.1
[LibreOffice.git] / vcl / source / filter / egif / egif.cxx
blob660ebf95333c66c921af92037bb3a6017c82c8d0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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"
29 #include <memory>
30 #include <filter/GifWriter.hxx>
32 namespace {
34 class GIFWriter
36 Bitmap aAccBmp;
37 SvStream& m_rGIF;
38 BitmapReadAccess* m_pAcc;
39 sal_uInt32 nMinPercent;
40 sal_uInt32 nMaxPercent;
41 sal_uInt32 nLastPercent;
42 tools::Long nActX;
43 tools::Long nActY;
44 sal_Int32 nInterlaced;
45 bool bStatus;
46 bool bTransparent;
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();
55 void WritePalette();
56 void WriteAccess();
57 void WriteTerminator();
59 bool CreateAccess( const BitmapEx& rBmpEx );
60 void DestroyAccess();
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;
68 public:
70 explicit GIFWriter(SvStream &rStream);
72 bool WriteGIF( const Graphic& rGraphic, FilterConfigItem* pConfigItem );
77 GIFWriter::GIFWriter(SvStream &rStream)
78 : m_rGIF(rStream)
79 , m_pAcc(nullptr)
80 , nMinPercent(0)
81 , nMaxPercent(0)
82 , nLastPercent(0)
83 , nActX(0)
84 , nActY(0)
85 , nInterlaced(0)
86 , bStatus(false)
87 , bTransparent(false)
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 );
103 Size aSize100;
104 const MapMode aMap( rGraphic.GetPrefMapMode() );
105 bool bLogSize = ( aMap.GetMapUnit() != MapUnit::MapPixel );
107 if( bLogSize )
108 aSize100 = OutputDevice::LogicToLogic(rGraphic.GetPrefSize(), aMap, MapMode(MapUnit::Map100thMM));
110 bStatus = true;
111 nLastPercent = 0;
112 nInterlaced = 0;
113 m_pAcc = nullptr;
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 );
126 if ( bStatus )
128 WriteGlobalHeader( rAnimation.GetDisplaySizePixel() );
130 if( bStatus )
132 WriteLoopExtension( rAnimation );
134 if( bStatus )
135 WriteAnimation( rAnimation );
139 else
141 const bool bGrafTrans = rGraphic.IsTransparent();
143 BitmapEx aBmpEx = rGraphic.GetBitmapEx();
145 nMinPercent = 0;
146 nMaxPercent = 100;
148 WriteSignature( bGrafTrans || bLogSize );
150 if( bStatus )
152 WriteGlobalHeader( aBmpEx.GetSizePixel() );
154 if( bStatus )
155 WriteBitmapEx( aBmpEx, Point(), bGrafTrans );
159 if( bStatus )
161 if( bLogSize )
162 WriteLogSizeExtension( aSize100 );
164 WriteTerminator();
167 if ( xStatusIndicator.is() )
168 xStatusIndicator->end();
170 return bStatus;
174 void GIFWriter::WriteBitmapEx( const BitmapEx& rBmpEx, const Point& rPoint,
175 bool bExtended, tools::Long nTimer, Disposal eDisposal )
177 if( !CreateAccess( rBmpEx ) )
178 return;
180 nActX = rPoint.X();
181 nActY = rPoint.Y();
183 if( bExtended )
184 WriteImageExtension( nTimer, eDisposal );
186 if( bStatus )
188 WriteLocalHeader();
190 if( bStatus )
192 WritePalette();
194 if( bStatus )
195 WriteAccess();
199 DestroyAccess();
203 void GIFWriter::WriteAnimation( const Animation& rAnimation )
205 const sal_uInt16 nCount = rAnimation.Count();
207 if( !nCount )
208 return;
210 const double fStep = 100. / nCount;
212 nMinPercent = 0;
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 )
243 if( bStatus )
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 );
256 bTransparent = true;
258 else
259 aAccBmp.Convert( BmpConversion::N8BitColors );
261 else
262 aAccBmp.Convert( BmpConversion::N8BitColors );
264 m_pAcc = aAccBmp.AcquireReadAccess();
266 if( !m_pAcc )
267 bStatus = false;
270 return bStatus;
274 void GIFWriter::DestroyAccess()
276 Bitmap::ReleaseAccess( m_pAcc );
277 m_pAcc = nullptr;
281 void GIFWriter::WriteSignature( bool bGIF89a )
283 if( bStatus )
285 m_rGIF.WriteBytes(bGIF89a ? "GIF89a" : "GIF87a" , 6);
287 if( m_rGIF.GetError() )
288 bStatus = false;
293 void GIFWriter::WriteGlobalHeader( const Size& rSize )
295 if( !bStatus )
296 return;
298 // 256 colors
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 );
303 // write values
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() )
318 bStatus = false;
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 )
332 return;
334 // Netscape interprets the LoopCount
335 // as the sole number of _repetitions_
336 if( nLoopCount )
337 nLoopCount--;
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 )
374 if( !bStatus )
375 return;
377 const sal_uInt16 nDelay = static_cast<sal_uInt16>(nTimer);
378 sal_uInt8 cFlags = 0;
380 // set Transparency-Flag
381 if( bTransparent )
382 cFlags |= 1;
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() )
399 bStatus = false;
403 void GIFWriter::WriteLocalHeader()
405 if( !bStatus )
406 return;
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
415 if( nInterlaced )
416 cFlags |= 0x40;
418 // set Flag for the local color palette
419 cFlags |= 0x80;
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() )
429 bStatus = false;
433 void GIFWriter::WritePalette()
435 if( !(bStatus && m_pAcc->HasPalette()) )
436 return;
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() )
455 bStatus = false;
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;
467 if( !bNative )
468 pBuffer.reset(new sal_uInt8[ nWidth ]);
470 if( !(bStatus && ( 8 == m_pAcc->GetBitCount() ) && m_pAcc->HasPalette()) )
471 return;
473 aCompressor.StartCompression( m_rGIF, m_pAcc->GetBitCount() );
475 tools::Long nY, nT;
477 for( tools::Long i = 0; i < nHeight; ++i )
479 if( nInterlaced )
481 nY = i << 3;
483 if( nY >= nHeight )
485 nT = i - ( ( nHeight + 7 ) >> 3 );
486 nY= ( nT << 3 ) + 4;
488 if( nY >= nHeight )
490 nT -= ( nHeight + 3 ) >> 3;
491 nY = ( nT << 2 ) + 2;
493 if ( nY >= nHeight )
495 nT -= ( ( nHeight + 1 ) >> 2 );
496 nY = ( nT << 1 ) + 1;
501 else
502 nY = i;
504 if( bNative )
505 aCompressor.Compress( m_pAcc->GetScanline( nY ), nWidth );
506 else
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() )
516 bStatus = false;
518 MayCallback( nMinPercent + ( nMaxPercent - nMinPercent ) * i / nHeight );
520 if( !bStatus )
521 break;
524 aCompressor.EndCompression();
526 if ( m_rGIF.GetError() )
527 bStatus = false;
531 void GIFWriter::WriteTerminator()
533 if( bStatus )
535 m_rGIF.WriteUChar( 0x3b );
537 if( m_rGIF.GetError() )
538 bStatus = false;
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: */