28#define NO_PENDING( rStm ) ( ( rStm ).GetError() != ERRCODE_IO_PENDING )
34 GLOBAL_HEADER_READING,
62 sal_uInt64 nAnimationByteSize;
63 sal_uInt64 nAnimationMinFileData;
69 std::vector<sal_uInt8> aSrcBuf;
70 std::unique_ptr<GIFLZWDecompressor> pDecomp;
75 sal_uInt64 nMaxStreamData;
76 sal_uInt32 nLogWidth100;
77 sal_uInt32 nLogHeight100;
79 sal_uInt16 nGlobalWidth;
80 sal_uInt16 nGlobalHeight;
81 sal_uInt16 nImageWidth;
82 sal_uInt16 nImageHeight;
83 sal_uInt16 nImagePosX;
84 sal_uInt16 nImagePosY;
87 sal_uInt16 nLastImageY;
88 sal_uInt16 nLastInterCount;
104 void ClearImageExtensions();
106 bool ReadGlobalHeader();
107 bool ReadExtension();
108 bool ReadLocalHeader();
111 void CreateNewBitmaps();
117 bool ReadIsAnimated();
118 void GetLogicSize(
Size& rLogicSize);
119 Graphic GetIntermediateGraphic();
121 explicit GIFReader(
SvStream& rStm );
126GIFReader::GIFReader(
SvStream& rStm )
127 : nAnimationByteSize(0)
128 , nAnimationMinFileData(0)
133 , nLastPos ( rStm.Tell() )
134 , nMaxStreamData( rStm.remainingSize() )
136 , nLogHeight100 ( 0 )
138 , nGlobalHeight ( 0 )
146 , nLastInterCount ( 0 )
148 , eActAction ( GLOBAL_HEADER_READING )
150 , bGCTransparent ( false )
151 , bInterlaced ( false)
152 , bOverreadBlock ( false )
153 , bImGraphicReady ( false )
154 , bGlobalPalette ( false )
155 , nBackgroundColor ( 0 )
156 , nGCTransparentIndex ( 0 )
158 , cNonTransIndex1 ( 0 )
160 maUpperName =
"SVIGIF";
162 ClearImageExtensions();
165void GIFReader::ClearImageExtensions()
167 nGCDisposalMethod = 0;
168 bGCTransparent =
false;
173 bool bWatchForBackgroundColor)
175 const Size aSize(nWidth, nHeight);
177 sal_uInt64 nCombinedPixSize = nWidth * nHeight;
179 nCombinedPixSize += (nCombinedPixSize/8);
186 sal_uInt64 nMinFileData = nWidth * nHeight / 2560;
188 nMinFileData += nAnimationMinFileData;
189 nCombinedPixSize += nAnimationByteSize;
191 if (nMaxStreamData < nMinFileData)
194 SAL_WARN(
"vcl.filter",
"in gif frame index " << aAnimation.Count() <<
" gif claims dimensions " << nWidth <<
" x " << nHeight <<
195 " but filesize of " << nMaxStreamData <<
" is surely insufficiently large to fill all frame images");
206 if (nCombinedPixSize >= SAL_MAX_INT32/3*2)
212 if (!aSize.Width() || !aSize.Height())
220 const Color aWhite(COL_WHITE);
224 if (!aAnimation.Count())
231 cTransIndex1 =
static_cast<sal_uInt8>(pAcc1->GetBestPaletteIndex(aWhite));
232 cNonTransIndex1 = cTransIndex1 ? 0 : 1;
244 if (!aBmp8.IsEmpty() && bWatchForBackgroundColor && aAnimation.Count())
245 aBmp8.Erase((*pPal)[nBackgroundColor]);
247 aBmp8.Erase(COL_WHITE);
250 bStatus = bool(pAcc8);
254bool GIFReader::ReadGlobalHeader()
259 auto nRead = rIStm.ReadBytes(pBuf, 6);
263 if( !strcmp( pBuf,
"GIF87a" ) || !strcmp( pBuf,
"GIF89a" ) )
265 nRead = rIStm.ReadBytes(pBuf, 7);
279 bGlobalPalette = ( nRF & 0x80 );
282 ReadPaletteEntries( &aGPalette,
sal_uLong(1) << ( ( nRF & 7 ) + 1 ) );
284 nBackgroundColor = 0;
300 const sal_uInt64 nMaxPossible = rIStm.remainingSize();
301 if (nLen > nMaxPossible)
303 std::unique_ptr<sal_uInt8[]> pBuf(
new sal_uInt8[ nLen ]);
304 std::size_t nRead = rIStm.ReadBytes(pBuf.get(), nLen);
330bool GIFReader::ReadExtension()
336 rIStm.ReadUChar( cFunction );
339 bool bOverreadDataBlocks =
false;
342 rIStm.ReadUChar( cSize );
349 rIStm.ReadUChar(cFlags);
350 rIStm.ReadUInt16(nTimer);
351 rIStm.ReadUChar(nGCTransparentIndex);
353 rIStm.ReadUChar(cByte);
357 nGCDisposalMethod = ( cFlags >> 2) & 7;
358 bGCTransparent = ( cFlags & 1 );
359 bStatus = ( cSize == 4 ) && ( cByte == 0 );
371 bOverreadDataBlocks =
true;
378 rIStm.ReadUChar( cSize );
381 if( aAppId ==
"NETSCAPE" && aAppCode ==
"2.0" && cSize == 3 )
384 rIStm.ReadUChar( cByte );
389 rIStm.ReadUChar( cByte );
391 rIStm.ReadUChar( cByte );
392 nLoops |= (
static_cast<sal_uInt16
>(cByte) << 8 );
393 rIStm.ReadUChar( cByte );
395 bStatus = ( cByte == 0 );
397 bOverreadDataBlocks =
false;
408 else if ( aAppId ==
"STARDIV " && aAppCode ==
"5.0" && cSize == 9 )
411 rIStm.ReadUChar( cByte );
416 rIStm.ReadUInt32( nLogWidth100 ).ReadUInt32( nLogHeight100 );
417 rIStm.ReadUChar( cByte );
418 bStatus = ( cByte == 0 );
420 bOverreadDataBlocks =
false;
433 bOverreadDataBlocks =
true;
438 if ( bOverreadDataBlocks )
441 while( cSize && bStatus && !rIStm.eof() )
443 sal_uInt16
nCount =
static_cast<sal_uInt16
>(cSize) + 1;
444 const sal_uInt64 nMaxPossible = rIStm.remainingSize();
445 if (nCount > nMaxPossible)
449 rIStm.SeekRel( nCount - 1 );
452 std::size_t nRead = rIStm.ReadBytes(&cSize, 1);
466bool GIFReader::ReadLocalHeader()
471 std::size_t nRead = rIStm.ReadBytes(pBuf, 9);
486 bInterlaced = ( ( nFlags & 0x40 ) == 0x40 );
493 ReadPaletteEntries( pPal,
sal_uLong(1) << ( (nFlags & 7 ) + 1 ) );
503 CreateBitmaps( nImageWidth, nImageHeight, pPal, bGlobalPalette && ( pPal == &aGPalette ) );
516 rIStm.ReadUChar( cBlockSize );
522 if ( cBlockSize == 0 )
526 rIStm.ReadBytes( aSrcBuf.data(), cBlockSize );
536 sal_uInt8* pTarget = pDecomp->DecompressBlock( aSrcBuf.data(), cBlockSize, nRead, bEOI );
538 nRet = ( bEOI ? 3 : 1 );
540 if( nRead && !bOverreadBlock )
541 FillImages( pTarget, nRead );
543 std::free( pTarget );
556 if( nImageX >= nImageWidth )
563 if( nLastInterCount )
570 if( ( nMinY > nLastImageY ) && ( nLastImageY < ( nImageHeight - 1 ) ) )
572 sal_uInt8* pScanline8 = pAcc8->GetScanline( nYAcc );
573 sal_uInt32 nSize8 = pAcc8->GetScanlineSize();
575 sal_uInt32 nSize1 = 0;
579 pScanline1 = pAcc1->GetScanline( nYAcc );
580 nSize1 = pAcc1->GetScanlineSize();
585 memcpy( pAcc8->GetScanline( j ), pScanline8, nSize8 );
588 memcpy( pAcc1->GetScanline( j ), pScanline1, nSize1 );
593 nT1 = ( ++nImageY ) << 3;
596 if( nT1 >= nImageHeight )
598 tools::Long nT2 = nImageY - ( ( nImageHeight + 7 ) >> 3 );
599 nT1 = ( nT2 << 3 ) + 4;
602 if( nT1 >= nImageHeight )
604 nT2 -= ( nImageHeight + 3 ) >> 3;
605 nT1 = ( nT2 << 2 ) + 2;
608 if( nT1 >= nImageHeight )
610 nT2 -= ( nImageHeight + 1 ) >> 2;
611 nT1 = ( nT2 << 1 ) + 1;
617 nLastImageY =
static_cast<sal_uInt16
>(nT1);
622 nLastImageY = ++nImageY;
630 if( nImageY < nImageHeight )
636 if( cTmp == nGCTransparentIndex )
637 pAcc1->SetPixelIndex( nYAcc, nImageX++, cTransIndex1 );
640 pAcc8->SetPixelIndex( nYAcc, nImageX, cTmp );
641 pAcc1->SetPixelIndex( nYAcc, nImageX++, cNonTransIndex1 );
645 pAcc8->SetPixelIndex( nYAcc, nImageX++, cTmp );
649 bOverreadBlock =
true;
655void GIFReader::CreateNewBitmaps()
678 if (aAnimationFrame.
mnWait < 2)
679 aAnimationFrame.
mnWait = 10;
681 if( nGCDisposalMethod == 2 )
683 else if( nGCDisposalMethod == 3 )
689 nAnimationMinFileData +=
static_cast<sal_uInt64
>(nImageWidth) * nImageHeight / 2560;
690 aAnimation.Insert(aAnimationFrame);
692 if( aAnimation.Count() == 1 )
694 aAnimation.SetDisplaySizePixel(
Size( nGlobalWidth, nGlobalHeight ) );
695 aAnimation.SetLoopCount( nLoops );
699Graphic GIFReader::GetIntermediateGraphic()
705 if ( bImGraphicReady && !aAnimation.Count() )
709 if ( bGCTransparent )
712 aImGraphic =
BitmapEx( aBmp8, aBmp1 );
715 bStatus = bStatus && pAcc1;
721 bStatus = bStatus && pAcc8;
727bool GIFReader::ProcessGIF()
733 eActAction = ABORT_READING;
736 rIStm.Seek( nLastPos );
745 rIStm.ReadUChar( cByte );
748 eActAction = END_READING;
754 eActAction = EXTENSION_READING;
755 else if( cByte ==
',' )
756 eActAction = LOCAL_HEADER_READING;
757 else if( cByte ==
';' )
758 eActAction = END_READING;
760 eActAction = ABORT_READING;
766 case GLOBAL_HEADER_READING:
768 bRead = ReadGlobalHeader();
771 ClearImageExtensions();
772 eActAction = MARKER_READING;
778 case EXTENSION_READING:
780 bRead = ReadExtension();
782 eActAction = MARKER_READING;
787 case LOCAL_HEADER_READING:
789 bRead = ReadLocalHeader();
792 nYAcc = nImageX = nImageY = 0;
793 eActAction = FIRST_BLOCK_READING;
799 case FIRST_BLOCK_READING:
803 rIStm.ReadUChar( cDataSize );
806 eActAction = ABORT_READING;
807 else if( cDataSize > 12 )
812 pDecomp = std::make_unique<GIFLZWDecompressor>( cDataSize );
813 eActAction = NEXT_BLOCK_READING;
814 bOverreadBlock =
false;
817 eActAction = FIRST_BLOCK_READING;
822 case NEXT_BLOCK_READING:
824 sal_uInt16 nLastX = nImageX;
825 sal_uInt16 nLastY = nImageY;
835 bImGraphicReady =
true;
836 eActAction = NEXT_BLOCK_READING;
837 bOverreadBlock =
false;
845 eActAction = MARKER_READING;
846 ClearImageExtensions();
850 eActAction = NEXT_BLOCK_READING;
851 bOverreadBlock =
true;
857 eActAction = ABORT_READING;
858 ClearImageExtensions();
874 eActAction = END_READING;
886 nLastPos = rIStm.Tell();
891bool GIFReader::ReadIsAnimated()
897 while( ProcessGIF() && ( eActAction != END_READING ) ) {}
900 eReadState = GIFREAD_ERROR;
901 else if( eActAction == END_READING )
902 eReadState = GIFREAD_OK;
905 if ( rIStm.GetError() == ERRCODE_IO_PENDING )
908 eReadState = GIFREAD_NEED_MORE;
911 if (eReadState == GIFREAD_OK)
912 return aAnimation.Count() > 1;
916void GIFReader::GetLogicSize(
Size& rLogicSize)
928 while( ProcessGIF() && ( eActAction != END_READING ) ) {}
931 eReadState = GIFREAD_ERROR;
932 else if( eActAction == END_READING )
933 eReadState = GIFREAD_OK;
936 if ( rIStm.GetError() == ERRCODE_IO_PENDING )
939 eReadState = GIFREAD_NEED_MORE;
942 if( aAnimation.Count() == 1 )
944 rGraphic = aAnimation.Get(0).maBitmapEx;
946 if( nLogWidth100 && nLogHeight100 )
953 rGraphic = aAnimation;
960 GIFReader aReader(rStm);
964 bool bResult = aReader.ReadIsAnimated();
965 aReader.GetLogicSize(rLogicSize);
975 GIFReader* pGIFReader =
dynamic_cast<GIFReader*
>( pContext.get() );
978 pContext = std::make_shared<GIFReader>( rStm );
979 pGIFReader =
static_cast<GIFReader*
>( pContext.get() );
983 rStm.
SetEndian( SvStreamEndian::LITTLE );
987 ReadState eReadState = pGIFReader->ReadGIF(rGraphic);
989 if (eReadState == GIFREAD_ERROR)
993 else if (eReadState == GIFREAD_NEED_MORE)
995 rGraphic = pGIFReader->GetIntermediateGraphic();
#define ANIMATION_TIMEOUT_ON_CLICK
vcl::ScopedBitmapAccess< BitmapWriteAccess, Bitmap, &Bitmap::AcquireWriteAccess > BitmapScopedWriteAccess
sal_Int64 GetSizeBytes() const
static const BitmapPalette & GetGreyPalette(int nEntries)
void SetGreen(sal_uInt8 nGreen)
void SetRed(sal_uInt8 nRed)
void SetBlue(sal_uInt8 nBlue)
void SetPrefMapMode(const MapMode &rPrefMapMode)
void SetReaderContext(const std::shared_ptr< GraphicReader > &pReader)
std::shared_ptr< GraphicReader > & GetReaderContext()
void SetPrefSize(const Size &rPrefSize)
void setWidth(tools::Long nWidth)
void setHeight(tools::Long nHeight)
void SetBuffer(void *pBuf, std::size_t nSize, std::size_t nEOF)
void SetEndian(SvStreamEndian SvStreamEndian)
SvStreamEndian GetEndian() const
SvStream & ReadUInt16(sal_uInt16 &rUInt16)
SvStream & ReadUChar(unsigned char &rChar)
This template handles BitmapAccess the RAII way.
constexpr ::Color COL_WHITE(0xFF, 0xFF, 0xFF)
constexpr ::Color COL_BLACK(0x00, 0x00, 0x00)
VCL_DLLPUBLIC bool ImportGIF(SvStream &rStm, Graphic &rGraphic)
bool IsGIFAnimated(SvStream &rStm, Size &rLogicSize)
#define SAL_WARN(area, stream)
TOOLS_DLLPUBLIC OString read_uInt8s_ToOString(SvStream &rStrm, std::size_t nUnits)