vcl/inc/graphic/GraphicFormatDetector.hxx   |    4 +
 vcl/source/filter/GraphicFormatDetector.cxx |   63 ++++++++++++++++----------
 vcl/source/filter/graphicfilter2.cxx        |   67 +++++++---------------------
 3 files changed, 61 insertions(+), 73 deletions(-)

New commits:
commit 868fb06a2ef8f915350c77f0d63c03844fc33f62
Author:     offtkp <parisop...@gmail.com>
AuthorDate: Thu Aug 25 22:33:04 2022 +0300
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Wed Sep 14 13:25:26 2022 +0200

    Remove code duplication in GraphicDescriptor for PBM/PGM/PPM
    
    GraphicFormatDetector and GraphicDescriptor have duplicate format
    detection code so now GraphicDescriptor uses GraphicFormatDetector
    functions instead to detect the format for PBM/PGM/PPM files
    
    Change-Id: Ib239bb0aafcfa691d63025d5965f199bf11c7649
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138847
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>
    Tested-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/vcl/inc/graphic/GraphicFormatDetector.hxx 
b/vcl/inc/graphic/GraphicFormatDetector.hxx
index 103519b6870c..64d1e74de80a 100644
--- a/vcl/inc/graphic/GraphicFormatDetector.hxx
+++ b/vcl/inc/graphic/GraphicFormatDetector.hxx
@@ -170,7 +170,9 @@ public:
     bool checkEPS();
     bool checkDXF();
     bool checkPCT();
-    bool checkPBMorPGMorPPM();
+    bool checkPBM();
+    bool checkPGM();
+    bool checkPPM();
     bool checkRAS();
     bool checkXPM();
     bool checkXBM();
diff --git a/vcl/source/filter/GraphicFormatDetector.cxx 
b/vcl/source/filter/GraphicFormatDetector.cxx
index 30573bf9b885..219915957101 100644
--- a/vcl/source/filter/GraphicFormatDetector.cxx
+++ b/vcl/source/filter/GraphicFormatDetector.cxx
@@ -199,7 +199,7 @@ bool peekGraphicFormat(SvStream& rStream, OUString& 
rFormatExtension, bool bTest
         || rFormatExtension.startsWith("PPM"))
     {
         bSomethingTested = true;
-        if (aDetector.checkPBMorPGMorPPM())
+        if (aDetector.checkPBM() || aDetector.checkPGM() || 
aDetector.checkPPM())
         {
             rFormatExtension = 
getImportFormatShortName(aDetector.getMetadata().mnFormat);
             return true;
@@ -1085,29 +1085,46 @@ bool GraphicFormatDetector::checkPCT()
     return false;
 }
 
-bool GraphicFormatDetector::checkPBMorPGMorPPM()
+bool GraphicFormatDetector::checkPBM()
 {
-    if (maFirstBytes[0] == 'P')
-    {
-        switch (maFirstBytes[1])
-        {
-            case '1':
-            case '4':
-                maMetadata.mnFormat = GraphicFileFormat::PBM;
-                return true;
-
-            case '2':
-            case '5':
-                maMetadata.mnFormat = GraphicFileFormat::PGM;
-                return true;
-
-            case '3':
-            case '6':
-                maMetadata.mnFormat = GraphicFileFormat::PPM;
-                return true;
-        }
-    }
-    return false;
+    bool bRet = false;
+    sal_Int32 nStmPos = mrStream.Tell();
+    sal_uInt8 nFirst = 0, nSecond = 0, nThird = 0;
+    mrStream.ReadUChar(nFirst).ReadUChar(nSecond).ReadUChar(nThird);
+    if (nFirst == 'P' && ((nSecond == '1') || (nSecond == '4')) && 
isspace(nThird))
+        bRet = true;
+    mrStream.Seek(nStmPos);
+    if (bRet)
+        maMetadata.mnFormat = GraphicFileFormat::PBM;
+    return bRet;
+}
+
+bool GraphicFormatDetector::checkPGM()
+{
+    bool bRet = false;
+    sal_uInt8 nFirst = 0, nSecond = 0, nThird = 0;
+    sal_Int32 nStmPos = mrStream.Tell();
+    mrStream.ReadUChar(nFirst).ReadUChar(nSecond).ReadUChar(nThird);
+    if (nFirst == 'P' && ((nSecond == '2') || (nSecond == '5')) && 
isspace(nThird))
+        bRet = true;
+    mrStream.Seek(nStmPos);
+    if (bRet)
+        maMetadata.mnFormat = GraphicFileFormat::PGM;
+    return bRet;
+}
+
+bool GraphicFormatDetector::checkPPM()
+{
+    bool bRet = false;
+    sal_uInt8 nFirst = 0, nSecond = 0, nThird = 0;
+    sal_Int32 nStmPos = mrStream.Tell();
+    mrStream.ReadUChar(nFirst).ReadUChar(nSecond).ReadUChar(nThird);
+    if (nFirst == 'P' && ((nSecond == '3') || (nSecond == '6')) && 
isspace(nThird))
+        bRet = true;
+    mrStream.Seek(nStmPos);
+    if (bRet)
+        maMetadata.mnFormat = GraphicFileFormat::PPM;
+    return bRet;
 }
 
 bool GraphicFormatDetector::checkRAS()
diff --git a/vcl/source/filter/graphicfilter2.cxx 
b/vcl/source/filter/graphicfilter2.cxx
index 6d051458ccbd..42dd8816563f 100644
--- a/vcl/source/filter/graphicfilter2.cxx
+++ b/vcl/source/filter/graphicfilter2.cxx
@@ -473,68 +473,37 @@ bool GraphicDescriptor::ImpDetectXPM( SvStream& rStm, 
bool )
 
 bool GraphicDescriptor::ImpDetectPBM( SvStream& rStm, bool )
 {
-    bool bRet = false;
-
-    // check file extension first, as this trumps the 2 ID bytes
-    if ( aPathExt.startsWith( "pbm" ) )
-        bRet = true;
-    else
-    {
-        sal_Int32 nStmPos = rStm.Tell();
-        sal_uInt8   nFirst = 0, nSecond = 0;
-        rStm.ReadUChar( nFirst ).ReadUChar( nSecond );
-        if ( nFirst == 'P' && ( ( nSecond == '1' ) || ( nSecond == '4' ) ) )
-            bRet = true;
-        rStm.Seek( nStmPos );
-    }
-
+    sal_Int32 nStmPos = rStm.Tell();
+    vcl::GraphicFormatDetector aDetector( rStm, aPathExt, false /* 
bExtendedInfo */ );
+    bool bRet = aDetector.detect();
+    bRet &= aDetector.checkPBM();
     if ( bRet )
-        aMetadata.mnFormat = GraphicFileFormat::PBM;
-
+        aMetadata = aDetector.getMetadata();
+    rStm.Seek( nStmPos );
     return bRet;
 }
 
 bool GraphicDescriptor::ImpDetectPGM( SvStream& rStm, bool )
 {
-    bool bRet = false;
-
-    if ( aPathExt.startsWith( "pgm" ) )
-        bRet = true;
-    else
-    {
-        sal_uInt8 nFirst = 0, nSecond = 0;
-        sal_Int32 nStmPos = rStm.Tell();
-        rStm.ReadUChar( nFirst ).ReadUChar( nSecond );
-        if ( nFirst == 'P' && ( ( nSecond == '2' ) || ( nSecond == '5' ) ) )
-            bRet = true;
-        rStm.Seek( nStmPos );
-    }
-
+    sal_Int32 nStmPos = rStm.Tell();
+    vcl::GraphicFormatDetector aDetector( rStm, aPathExt, false /* 
bExtendedInfo */ );
+    bool bRet = aDetector.detect();
+    bRet &= aDetector.checkPGM();
     if ( bRet )
-        aMetadata.mnFormat = GraphicFileFormat::PGM;
-
+        aMetadata = aDetector.getMetadata();
+    rStm.Seek( nStmPos );
     return bRet;
 }
 
 bool GraphicDescriptor::ImpDetectPPM( SvStream& rStm, bool )
 {
-    bool bRet = false;
-
-    if ( aPathExt.startsWith( "ppm" ) )
-        bRet = true;
-    else
-    {
-        sal_uInt8   nFirst = 0, nSecond = 0;
-        sal_Int32 nStmPos = rStm.Tell();
-        rStm.ReadUChar( nFirst ).ReadUChar( nSecond );
-        if ( nFirst == 'P' && ( ( nSecond == '3' ) || ( nSecond == '6' ) ) )
-            bRet = true;
-        rStm.Seek( nStmPos );
-    }
-
+    sal_Int32 nStmPos = rStm.Tell();
+    vcl::GraphicFormatDetector aDetector( rStm, aPathExt, false /* 
bExtendedInfo */ );
+    bool bRet = aDetector.detect();
+    bRet &= aDetector.checkPPM();
     if ( bRet )
-        aMetadata.mnFormat = GraphicFileFormat::PPM;
-
+        aMetadata = aDetector.getMetadata();
+    rStm.Seek( nStmPos );
     return bRet;
 }
 

Reply via email to