1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <tools/stream.hxx>
#include <tools/debug.hxx>
#include <vcl/bitmapaccess.hxx>
#include <vcl/graph.hxx>
#include <vcl/outdev.hxx>
#include <vcl/FilterConfigItem.hxx>
#include <com/sun/star/task/XStatusIndicator.hpp>
#include "giflzwc.hxx"
#include <memory>
namespace {
class GIFWriter
{
Bitmap aAccBmp;
SvStream& m_rGIF;
BitmapReadAccess* m_pAcc;
sal_uInt32 nMinPercent;
sal_uInt32 nMaxPercent;
sal_uInt32 nLastPercent;
long nActX;
long nActY;
sal_Int32 nInterlaced;
bool bStatus;
bool bTransparent;
void MayCallback(sal_uInt32 nPercent);
void WriteSignature( bool bGIF89a );
void WriteGlobalHeader( const Size& rSize );
void WriteLoopExtension( const Animation& rAnimation );
void WriteLogSizeExtension( const Size& rSize100 );
void WriteImageExtension( long nTimer, Disposal eDisposal );
void WriteLocalHeader();
void WritePalette();
void WriteAccess();
void WriteTerminator();
bool CreateAccess( const BitmapEx& rBmpEx );
void DestroyAccess();
void WriteAnimation( const Animation& rAnimation );
void WriteBitmapEx( const BitmapEx& rBmpEx, const Point& rPoint, bool bExtended,
long nTimer = 0, Disposal eDisposal = Disposal::Not );
css::uno::Reference< css::task::XStatusIndicator > xStatusIndicator;
public:
explicit GIFWriter(SvStream &rStream);
bool WriteGIF( const Graphic& rGraphic, FilterConfigItem* pConfigItem );
};
}
GIFWriter::GIFWriter(SvStream &rStream)
: m_rGIF(rStream)
, m_pAcc(nullptr)
, nMinPercent(0)
, nMaxPercent(0)
, nLastPercent(0)
, nActX(0)
, nActY(0)
, nInterlaced(0)
, bStatus(false)
, bTransparent(false)
{
}
bool GIFWriter::WriteGIF(const Graphic& rGraphic, FilterConfigItem* pFilterConfigItem)
{
if ( pFilterConfigItem )
{
xStatusIndicator = pFilterConfigItem->GetStatusIndicator();
if ( xStatusIndicator.is() )
{
xStatusIndicator->start( OUString(), 100 );
}
}
Size aSize100;
const MapMode aMap( rGraphic.GetPrefMapMode() );
bool bLogSize = ( aMap.GetMapUnit() != MapUnit::MapPixel );
if( bLogSize )
aSize100 = OutputDevice::LogicToLogic(rGraphic.GetPrefSize(), aMap, MapMode(MapUnit::Map100thMM));
bStatus = true;
nLastPercent = 0;
nInterlaced = 0;
m_pAcc = nullptr;
if ( pFilterConfigItem )
nInterlaced = pFilterConfigItem->ReadInt32( "Interlaced", 0 );
m_rGIF.SetEndian( SvStreamEndian::LITTLE );
if( rGraphic.IsAnimated() )
{
const Animation& rAnimation = rGraphic.GetAnimation();
WriteSignature( true );
if ( bStatus )
{
WriteGlobalHeader( rAnimation.GetDisplaySizePixel() );
if( bStatus )
{
WriteLoopExtension( rAnimation );
if( bStatus )
WriteAnimation( rAnimation );
}
}
}
else
{
const bool bGrafTrans = rGraphic.IsTransparent();
BitmapEx aBmpEx = rGraphic.GetBitmapEx();
nMinPercent = 0;
nMaxPercent = 100;
WriteSignature( bGrafTrans || bLogSize );
if( bStatus )
{
WriteGlobalHeader( aBmpEx.GetSizePixel() );
if( bStatus )
WriteBitmapEx( aBmpEx, Point(), bGrafTrans );
}
}
if( bStatus )
{
if( bLogSize )
WriteLogSizeExtension( aSize100 );
WriteTerminator();
}
if ( xStatusIndicator.is() )
xStatusIndicator->end();
return bStatus;
}
void GIFWriter::WriteBitmapEx( const BitmapEx& rBmpEx, const Point& rPoint,
bool bExtended, long nTimer, Disposal eDisposal )
{
if( !CreateAccess( rBmpEx ) )
return;
nActX = rPoint.X();
nActY = rPoint.Y();
if( bExtended )
WriteImageExtension( nTimer, eDisposal );
if( bStatus )
{
WriteLocalHeader();
if( bStatus )
{
WritePalette();
if( bStatus )
WriteAccess();
}
}
DestroyAccess();
}
void GIFWriter::WriteAnimation( const Animation& rAnimation )
{
const sal_uInt16 nCount = rAnimation.Count();
if( !nCount )
return;
const double fStep = 100. / nCount;
nMinPercent = 0;
nMaxPercent = static_cast<sal_uInt32>(fStep);
for( sal_uInt16 i = 0; i < nCount; i++ )
{
const AnimationBitmap& rAnimationBitmap = rAnimation.Get( i );
WriteBitmapEx(rAnimationBitmap.maBitmapEx, rAnimationBitmap.maPositionPixel, true,
rAnimationBitmap.mnWait, rAnimationBitmap.meDisposal );
nMinPercent = nMaxPercent;
nMaxPercent = static_cast<sal_uInt32>(nMaxPercent + fStep);
}
}
void GIFWriter::MayCallback(sal_uInt32 nPercent)
{
if ( xStatusIndicator.is() )
{
if( nPercent >= nLastPercent + 3 )
{
nLastPercent = nPercent;
if ( nPercent <= 100 )
xStatusIndicator->setValue( nPercent );
}
}
}
bool GIFWriter::CreateAccess( const BitmapEx& rBmpEx )
{
if( bStatus )
{
Bitmap aMask( rBmpEx.GetMask() );
aAccBmp = rBmpEx.GetBitmap();
bTransparent = false;
if( !!aMask )
{
if( aAccBmp.Convert( BmpConversion::N8BitTrans ) )
{
aMask.Convert( BmpConversion::N1BitThreshold );
aAccBmp.Replace( aMask, BMP_COL_TRANS );
bTransparent = true;
}
else
aAccBmp.Convert( BmpConversion::N8BitColors );
}
else
aAccBmp.Convert( BmpConversion::N8BitColors );
m_pAcc = aAccBmp.AcquireReadAccess();
if( !m_pAcc )
bStatus = false;
}
return bStatus;
}
void GIFWriter::DestroyAccess()
{
Bitmap::ReleaseAccess( m_pAcc );
m_pAcc = nullptr;
}
void GIFWriter::WriteSignature( bool bGIF89a )
{
if( bStatus )
{
m_rGIF.WriteBytes(bGIF89a ? "GIF89a" : "GIF87a" , 6);
if( m_rGIF.GetError() )
bStatus = false;
}
}
void GIFWriter::WriteGlobalHeader( const Size& rSize )
{
if( !bStatus )
return;
// 256 colors
const sal_uInt16 nWidth = static_cast<sal_uInt16>(rSize.Width());
const sal_uInt16 nHeight = static_cast<sal_uInt16>(rSize.Height());
const sal_uInt8 cFlags = 128 | ( 7 << 4 );
// write values
m_rGIF.WriteUInt16( nWidth );
m_rGIF.WriteUInt16( nHeight );
m_rGIF.WriteUChar( cFlags );
m_rGIF.WriteUChar( 0x00 );
m_rGIF.WriteUChar( 0x00 );
// write dummy palette with two entries (black/white);
// we do this only because of a bug in Photoshop, since those can't
// read pictures without a global color palette
m_rGIF.WriteUInt16( 0 );
m_rGIF.WriteUInt16( 255 );
m_rGIF.WriteUInt16( 65535 );
if( m_rGIF.GetError() )
bStatus = false;
}
void GIFWriter::WriteLoopExtension( const Animation& rAnimation )
{
DBG_ASSERT( rAnimation.Count() > 0, "Animation has no bitmaps!" );
sal_uInt16 nLoopCount = static_cast<sal_uInt16>(rAnimation.GetLoopCount());
// if only one run should take place
// the LoopExtension won't be written
// The default in this case is a single run
if( nLoopCount == 1 )
return;
// Netscape interprets the LoopCount
// as the sole number of _repetitions_
if( nLoopCount )
nLoopCount--;
const sal_uInt8 cLoByte = static_cast<sal_uInt8>(nLoopCount);
const sal_uInt8 cHiByte = static_cast<sal_uInt8>( nLoopCount >> 8 );
m_rGIF.WriteUChar( 0x21 );
m_rGIF.WriteUChar( 0xff );
m_rGIF.WriteUChar( 0x0b );
m_rGIF.WriteBytes( "NETSCAPE2.0", 11 );
m_rGIF.WriteUChar( 0x03 );
m_rGIF.WriteUChar( 0x01 );
m_rGIF.WriteUChar( cLoByte );
m_rGIF.WriteUChar( cHiByte );
m_rGIF.WriteUChar( 0x00 );
}
void GIFWriter::WriteLogSizeExtension( const Size& rSize100 )
{
// writer PrefSize in 100th-mm as ApplicationExtension
if( rSize100.Width() && rSize100.Height() )
{
m_rGIF.WriteUChar( 0x21 );
m_rGIF.WriteUChar( 0xff );
m_rGIF.WriteUChar( 0x0b );
m_rGIF.WriteBytes( "STARDIV 5.0", 11 );
m_rGIF.WriteUChar( 0x09 );
m_rGIF.WriteUChar( 0x01 );
m_rGIF.WriteUInt32( rSize100.Width() );
m_rGIF.WriteUInt32( rSize100.Height() );
m_rGIF.WriteUChar( 0x00 );
}
}
void GIFWriter::WriteImageExtension( long nTimer, Disposal eDisposal )
{
if( !bStatus )
return;
const sal_uInt16 nDelay = static_cast<sal_uInt16>(nTimer);
sal_uInt8 cFlags = 0;
// set Transparency-Flag
if( bTransparent )
cFlags |= 1;
// set Disposal-value
if( eDisposal == Disposal::Back )
cFlags |= ( 2 << 2 );
else if( eDisposal == Disposal::Previous )
cFlags |= ( 3 << 2 );
m_rGIF.WriteUChar( 0x21 );
m_rGIF.WriteUChar( 0xf9 );
m_rGIF.WriteUChar( 0x04 );
m_rGIF.WriteUChar( cFlags );
m_rGIF.WriteUInt16( nDelay );
m_rGIF.WriteUChar( m_pAcc->GetBestPaletteIndex( BMP_COL_TRANS ) );
m_rGIF.WriteUChar( 0x00 );
if( m_rGIF.GetError() )
bStatus = false;
}
void GIFWriter::WriteLocalHeader()
{
if( !bStatus )
return;
const sal_uInt16 nPosX = static_cast<sal_uInt16>(nActX);
const sal_uInt16 nPosY = static_cast<sal_uInt16>(nActY);
const sal_uInt16 nWidth = static_cast<sal_uInt16>(m_pAcc->Width());
const sal_uInt16 nHeight = static_cast<sal_uInt16>(m_pAcc->Height());
sal_uInt8 cFlags = static_cast<sal_uInt8>( m_pAcc->GetBitCount() - 1 );
// set Interlaced-Flag
if( nInterlaced )
cFlags |= 0x40;
// set Flag for the local color palette
cFlags |= 0x80;
m_rGIF.WriteUChar( 0x2c );
m_rGIF.WriteUInt16( nPosX );
m_rGIF.WriteUInt16( nPosY );
m_rGIF.WriteUInt16( nWidth );
m_rGIF.WriteUInt16( nHeight );
m_rGIF.WriteUChar( cFlags );
if( m_rGIF.GetError() )
bStatus = false;
}
void GIFWriter::WritePalette()
{
if( !(bStatus && m_pAcc->HasPalette()) )
return;
const sal_uInt16 nCount = m_pAcc->GetPaletteEntryCount();
const sal_uInt16 nMaxCount = ( 1 << m_pAcc->GetBitCount() );
for ( sal_uInt16 i = 0; i < nCount; i++ )
{
const BitmapColor& rColor = m_pAcc->GetPaletteColor( i );
m_rGIF.WriteUChar( rColor.GetRed() );
m_rGIF.WriteUChar( rColor.GetGreen() );
m_rGIF.WriteUChar( rColor.GetBlue() );
}
// fill up the rest with 0
if( nCount < nMaxCount )
m_rGIF.SeekRel( ( nMaxCount - nCount ) * 3 );
if( m_rGIF.GetError() )
bStatus = false;
}
void GIFWriter::WriteAccess()
{
GIFLZWCompressor aCompressor;
const long nWidth = m_pAcc->Width();
const long nHeight = m_pAcc->Height();
std::unique_ptr<sal_uInt8[]> pBuffer;
bool bNative = m_pAcc->GetScanlineFormat() == ScanlineFormat::N8BitPal;
if( !bNative )
pBuffer.reset(new sal_uInt8[ nWidth ]);
if( !(bStatus && ( 8 == m_pAcc->GetBitCount() ) && m_pAcc->HasPalette()) )
return;
aCompressor.StartCompression( m_rGIF, m_pAcc->GetBitCount() );
long nY, nT;
for( long i = 0; i < nHeight; ++i )
{
if( nInterlaced )
{
nY = i << 3;
if( nY >= nHeight )
{
nT = i - ( ( nHeight + 7 ) >> 3 );
nY= ( nT << 3 ) + 4;
if( nY >= nHeight )
{
nT -= ( nHeight + 3 ) >> 3;
nY = ( nT << 2 ) + 2;
if ( nY >= nHeight )
{
nT -= ( ( nHeight + 1 ) >> 2 );
nY = ( nT << 1 ) + 1;
}
}
}
}
else
nY = i;
if( bNative )
aCompressor.Compress( m_pAcc->GetScanline( nY ), nWidth );
else
{
Scanline pScanline = m_pAcc->GetScanline( nY );
for( long nX = 0; nX < nWidth; nX++ )
pBuffer[ nX ] = m_pAcc->GetIndexFromData( pScanline, nX );
aCompressor.Compress( pBuffer.get(), nWidth );
}
if ( m_rGIF.GetError() )
bStatus = false;
MayCallback( nMinPercent + ( nMaxPercent - nMinPercent ) * i / nHeight );
if( !bStatus )
break;
}
aCompressor.EndCompression();
if ( m_rGIF.GetError() )
bStatus = false;
}
void GIFWriter::WriteTerminator()
{
if( bStatus )
{
m_rGIF.WriteUChar( 0x3b );
if( m_rGIF.GetError() )
bStatus = false;
}
}
extern "C" SAL_DLLPUBLIC_EXPORT bool
egiGraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem )<--- Parameter 'rGraphic' can be declared with const
{
GIFWriter aWriter(rStream);
return aWriter.WriteGIF(rGraphic, pConfigItem);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|