summaryrefslogtreecommitdiff
path: root/vcl
diff options
context:
space:
mode:
authorofftkp <parisoplop@gmail.com>2022-08-23 15:43:43 +0300
committerTomaž Vajngerl <quikee@gmail.com>2022-09-05 09:56:17 +0200
commit2d27ce875546f19054a8ba39a87fb07a5337216d (patch)
tree9bdfbbed9d16ddb81e488187197644a36b313e99 /vcl
parent28c8cddd4218905bca05778dcdbae5911132a096 (diff)
Remove code duplication in GraphicDescriptor for TIF
GraphicFormatDetector and GraphicDescriptor have duplicate format detection code so now GraphicDescriptor uses GraphicFormatDetector functions instead to detect the format for TIF files Change-Id: Ia55b1606df5171dc41d94d19262a83910bd024a9 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138730 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl')
-rw-r--r--vcl/source/filter/GraphicFormatDetector.cxx129
-rw-r--r--vcl/source/filter/graphicfilter2.cxx128
2 files changed, 130 insertions, 127 deletions
diff --git a/vcl/source/filter/GraphicFormatDetector.cxx b/vcl/source/filter/GraphicFormatDetector.cxx
index 8956c3cd9530..9c9529f261a9 100644
--- a/vcl/source/filter/GraphicFormatDetector.cxx
+++ b/vcl/source/filter/GraphicFormatDetector.cxx
@@ -31,6 +31,7 @@
constexpr sal_uInt32 SVG_CHECK_SIZE = 2048;
constexpr sal_uInt32 WMF_EMF_CHECK_SIZE = 44;
+constexpr sal_uInt32 DATA_SIZE = 640;
namespace vcl
{
@@ -614,12 +615,132 @@ bool GraphicFormatDetector::checkPCX()
bool GraphicFormatDetector::checkTIF()
{
- if (mnFirstLong == 0x49492a00 || mnFirstLong == 0x4d4d002a)
+ mrStream.Seek(mnStreamPosition);
+ bool bRet = false;
+ sal_uInt8 cByte1 = 0;
+ sal_uInt8 cByte2 = 1;
+
+ sal_Int32 nStmPos = mrStream.Tell();
+ mrStream.ReadUChar(cByte1);
+ mrStream.ReadUChar(cByte2);
+ if (cByte1 == cByte2)
{
- maMetadata.mnFormat = GraphicFileFormat::TIF;
- return true;
+ bool bDetectOk = false;
+
+ if (cByte1 == 0x49)
+ {
+ mrStream.SetEndian(SvStreamEndian::LITTLE);
+ bDetectOk = true;
+ }
+ else if (cByte1 == 0x4d)
+ {
+ mrStream.SetEndian(SvStreamEndian::BIG);
+ bDetectOk = true;
+ }
+
+ if (bDetectOk)
+ {
+ sal_uInt16 nTemp16 = 0;
+
+ mrStream.ReadUInt16(nTemp16);
+ if (nTemp16 == 0x2a)
+ {
+ maMetadata.mnFormat = GraphicFileFormat::TIF;
+ bRet = true;
+
+ if (mbExtendedInfo)
+ {
+ sal_uLong nCount;
+ sal_uLong nMax = DATA_SIZE - 48;
+ sal_uInt32 nTemp32 = 0;
+
+ // Offset of the first IFD
+ mrStream.ReadUInt32(nTemp32);
+ nCount = nTemp32 + 2;
+ mrStream.SeekRel(nCount - 0x08);
+
+ if (nCount < nMax)
+ {
+ bool bOk = false;
+
+ // read tags till we find Tag256 ( Width )
+ // do not read more bytes than DATA_SIZE
+ mrStream.ReadUInt16(nTemp16);
+ while (nTemp16 != 256)
+ {
+ bOk = nCount < nMax;
+ if (!bOk)
+ {
+ break;
+ }
+ mrStream.SeekRel(10);
+ mrStream.ReadUInt16(nTemp16);
+ nCount += 12;
+ }
+
+ if (bOk)
+ {
+ // width
+ mrStream.ReadUInt16(nTemp16);
+ mrStream.SeekRel(4);
+ if (nTemp16 == 3)
+ {
+ mrStream.ReadUInt16(nTemp16);
+ maMetadata.maPixSize.setWidth(nTemp16);
+ mrStream.SeekRel(2);
+ }
+ else
+ {
+ mrStream.ReadUInt32(nTemp32);
+ maMetadata.maPixSize.setWidth(nTemp32);
+ }
+
+ // height
+ mrStream.SeekRel(2);
+ mrStream.ReadUInt16(nTemp16);
+ mrStream.SeekRel(4);
+ if (nTemp16 == 3)
+ {
+ mrStream.ReadUInt16(nTemp16);
+ maMetadata.maPixSize.setHeight(nTemp16);
+ mrStream.SeekRel(2);
+ }
+ else
+ {
+ mrStream.ReadUInt32(nTemp32);
+ maMetadata.maPixSize.setHeight(nTemp32);
+ }
+
+ // Bits/Pixel
+ mrStream.ReadUInt16(nTemp16);
+ if (nTemp16 == 258)
+ {
+ mrStream.SeekRel(6);
+ mrStream.ReadUInt16(nTemp16);
+ maMetadata.mnBitsPerPixel = nTemp16;
+ mrStream.SeekRel(2);
+ }
+ else
+ mrStream.SeekRel(-2);
+
+ // compression
+ mrStream.ReadUInt16(nTemp16);
+ if (nTemp16 == 259)
+ {
+ mrStream.SeekRel(6);
+ mrStream.ReadUInt16(nTemp16); // compression
+ mrStream.SeekRel(2);
+ }
+ else
+ mrStream.SeekRel(-2);
+ }
+ }
+ }
+ }
+ }
}
- return false;
+ mrStream.Seek(nStmPos);
+ return bRet;
}
bool GraphicFormatDetector::checkGIF()
diff --git a/vcl/source/filter/graphicfilter2.cxx b/vcl/source/filter/graphicfilter2.cxx
index 1117b2cc9cbb..eb4f80d8a526 100644
--- a/vcl/source/filter/graphicfilter2.cxx
+++ b/vcl/source/filter/graphicfilter2.cxx
@@ -29,7 +29,6 @@
#include <graphic/GraphicFormatDetector.hxx>
#include "graphicfilter_internal.hxx"
-#define DATA_SIZE 640
namespace
{
enum class MetafileType : sal_uInt16
@@ -439,129 +438,12 @@ bool GraphicDescriptor::ImpDetectPNG( SvStream& rStm, bool bExtendedInfo )
bool GraphicDescriptor::ImpDetectTIF( SvStream& rStm, bool bExtendedInfo )
{
- bool bRet = false;
- sal_uInt8 cByte1 = 0;
- sal_uInt8 cByte2 = 1;
-
sal_Int32 nStmPos = rStm.Tell();
- rStm.ReadUChar( cByte1 );
- rStm.ReadUChar( cByte2 );
- if ( cByte1 == cByte2 )
- {
- bool bDetectOk = false;
-
- if ( cByte1 == 0x49 )
- {
- rStm.SetEndian( SvStreamEndian::LITTLE );
- bDetectOk = true;
- }
- else if ( cByte1 == 0x4d )
- {
- rStm.SetEndian( SvStreamEndian::BIG );
- bDetectOk = true;
- }
-
- if ( bDetectOk )
- {
- sal_uInt16 nTemp16 = 0;
-
- rStm.ReadUInt16( nTemp16 );
- if ( nTemp16 == 0x2a )
- {
- aMetadata.mnFormat = GraphicFileFormat::TIF;
- bRet = true;
-
- if ( bExtendedInfo )
- {
- sal_uLong nCount;
- sal_uLong nMax = DATA_SIZE - 48;
- sal_uInt32 nTemp32 = 0;
-
- // Offset of the first IFD
- rStm.ReadUInt32( nTemp32 );
- nCount = nTemp32 + 2;
- rStm.SeekRel( nCount - 0x08 );
-
- if ( nCount < nMax )
- {
- bool bOk = false;
-
- // read tags till we find Tag256 ( Width )
- // do not read more bytes than DATA_SIZE
- rStm.ReadUInt16( nTemp16 );
- while ( nTemp16 != 256 )
- {
- bOk = nCount < nMax;
- if ( !bOk )
- {
- break;
- }
- rStm.SeekRel( 10 );
- rStm.ReadUInt16( nTemp16 );
- nCount += 12;
- }
-
- if ( bOk )
- {
- // width
- rStm.ReadUInt16( nTemp16 );
- rStm.SeekRel( 4 );
- if ( nTemp16 == 3 )
- {
- rStm.ReadUInt16( nTemp16 );
- aMetadata.maPixSize.setWidth( nTemp16 );
- rStm.SeekRel( 2 );
- }
- else
- {
- rStm.ReadUInt32( nTemp32 );
- aMetadata.maPixSize.setWidth( nTemp32 );
- }
-
- // height
- rStm.SeekRel( 2 );
- rStm.ReadUInt16( nTemp16 );
- rStm.SeekRel( 4 );
- if ( nTemp16 == 3 )
- {
- rStm.ReadUInt16( nTemp16 );
- aMetadata.maPixSize.setHeight( nTemp16 );
- rStm.SeekRel( 2 );
- }
- else
- {
- rStm.ReadUInt32( nTemp32 );
- aMetadata.maPixSize.setHeight( nTemp32 );
- }
-
- // Bits/Pixel
- rStm.ReadUInt16( nTemp16 );
- if ( nTemp16 == 258 )
- {
- rStm.SeekRel( 6 );
- rStm.ReadUInt16( nTemp16 );
- aMetadata.mnBitsPerPixel = nTemp16;
- rStm.SeekRel( 2 );
- }
- else
- rStm.SeekRel( -2 );
-
- // compression
- rStm.ReadUInt16( nTemp16 );
- if ( nTemp16 == 259 )
- {
- rStm.SeekRel( 6 );
- rStm.ReadUInt16( nTemp16 ); // compression
- rStm.SeekRel( 2 );
- }
- else
- rStm.SeekRel( -2 );
- }
- }
- }
- }
- }
- }
+ vcl::GraphicFormatDetector aDetector( rStm, aPathExt, bExtendedInfo );
+ bool bRet = aDetector.detect();
+ bRet &= aDetector.checkTIF();
+ if ( bRet )
+ aMetadata = aDetector.getMetadata();
rStm.Seek( nStmPos );
return bRet;
}