vcl/source/filter/GraphicFormatDetector.cxx |   71 ++++++++++++++++++++++---
 vcl/source/filter/graphicfilter2.cxx        |   77 +---------------------------
 2 files changed, 67 insertions(+), 81 deletions(-)

New commits:
commit a809d89307ecfdc00fd6dcda96c53986189f35ef
Author:     offtkp <parisop...@gmail.com>
AuthorDate: Fri Sep 2 22:40:09 2022 +0300
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Thu Sep 15 08:46:52 2022 +0200

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

diff --git a/vcl/source/filter/GraphicFormatDetector.cxx 
b/vcl/source/filter/GraphicFormatDetector.cxx
index 5ae7d5fb67d2..bfb3da3ac861 100644
--- a/vcl/source/filter/GraphicFormatDetector.cxx
+++ b/vcl/source/filter/GraphicFormatDetector.cxx
@@ -617,18 +617,71 @@ bool GraphicFormatDetector::checkEMF()
 
 bool GraphicFormatDetector::checkPCX()
 {
-    if (maFirstBytes[0] != 0x0a)
-        return false;
-
-    sal_uInt8 nVersion = maFirstBytes[1];
-    sal_uInt8 nEncoding = maFirstBytes[2];
-    if ((nVersion == 0 || nVersion == 2 || nVersion == 3 || nVersion == 5) && 
nEncoding <= 1)
+    // ! Because 0x0a can be interpreted as LF too ...
+    // we can't be sure that this special sign represent a PCX file only.
+    // Every Ascii file is possible here :-(
+    // We must detect the whole header.
+    bool bRet = false;
+    sal_uInt8 cByte = 0;
+    auto nStmPos = mrStream.Tell();
+    SeekGuard aGuard(mrStream, nStmPos);
+    mrStream.SetEndian(SvStreamEndian::LITTLE);
+    mrStream.ReadUChar(cByte);
+    if (cByte == 0x0a)
     {
         maMetadata.mnFormat = GraphicFileFormat::PCX;
-        return true;
-    }
+        mrStream.SeekRel(1);
+        // compression
+        mrStream.ReadUChar(cByte);
+        bRet = (cByte == 0 || cByte == 1);
+        if (bRet)
+        {
+            sal_uInt16 nTemp16;
+            sal_uInt16 nXmin;
+            sal_uInt16 nXmax;
+            sal_uInt16 nYmin;
+            sal_uInt16 nYmax;
+            sal_uInt16 nDPIx;
+            sal_uInt16 nDPIy;
+
+            // Bits/Pixel
+            mrStream.ReadUChar(cByte);
+            maMetadata.mnBitsPerPixel = cByte;
+
+            // image dimensions
+            mrStream.ReadUInt16(nTemp16);
+            nXmin = nTemp16;
+            mrStream.ReadUInt16(nTemp16);
+            nYmin = nTemp16;
+            mrStream.ReadUInt16(nTemp16);
+            nXmax = nTemp16;
+            mrStream.ReadUInt16(nTemp16);
+            nYmax = nTemp16;
 
-    return false;
+            maMetadata.maPixSize.setWidth(nXmax - nXmin + 1);
+            maMetadata.maPixSize.setHeight(nYmax - nYmin + 1);
+
+            // resolution
+            mrStream.ReadUInt16(nTemp16);
+            nDPIx = nTemp16;
+            mrStream.ReadUInt16(nTemp16);
+            nDPIy = nTemp16;
+
+            // set logical size
+            MapMode aMap(MapUnit::MapInch, Point(), Fraction(1, nDPIx), 
Fraction(1, nDPIy));
+            maMetadata.maLogSize = 
OutputDevice::LogicToLogic(maMetadata.maPixSize, aMap,
+                                                              
MapMode(MapUnit::Map100thMM));
+
+            // number of color planes
+            cByte = 5; // Illegal value in case of EOF.
+            mrStream.SeekRel(49);
+            mrStream.ReadUChar(cByte);
+            maMetadata.mnPlanes = cByte;
+
+            bRet = (maMetadata.mnPlanes <= 4);
+        }
+    }
+    return bRet;
 }
 
 bool GraphicFormatDetector::checkTIF()
diff --git a/vcl/source/filter/graphicfilter2.cxx 
b/vcl/source/filter/graphicfilter2.cxx
index 9cda65957aa2..1146acb9d6c6 100644
--- a/vcl/source/filter/graphicfilter2.cxx
+++ b/vcl/source/filter/graphicfilter2.cxx
@@ -342,78 +342,11 @@ bool GraphicDescriptor::ImpDetectPCD( SvStream& rStm, 
bool )
 
 bool GraphicDescriptor::ImpDetectPCX( SvStream& rStm )
 {
-    // ! Because 0x0a can be interpreted as LF too ...
-    // we can't be sure that this special sign represent a PCX file only.
-    // Every Ascii file is possible here :-(
-    // We must detect the whole header.
-
-    bool    bRet = false;
-    sal_uInt8   cByte = 0;
-
-    sal_Int32 nStmPos = rStm.Tell();
-    rStm.SetEndian( SvStreamEndian::LITTLE );
-    rStm.ReadUChar( cByte );
-
-    if ( cByte == 0x0a )
-    {
-        aMetadata.mnFormat = GraphicFileFormat::PCX;
-
-        rStm.SeekRel( 1 );
-
-        // compression
-        rStm.ReadUChar( cByte );
-
-        bRet = (cByte==0 || cByte ==1);
-        if (bRet)
-        {
-            sal_uInt16  nTemp16;
-            sal_uInt16  nXmin;
-            sal_uInt16  nXmax;
-            sal_uInt16  nYmin;
-            sal_uInt16  nYmax;
-            sal_uInt16  nDPIx;
-            sal_uInt16  nDPIy;
-
-            // Bits/Pixel
-            rStm.ReadUChar( cByte );
-            aMetadata.mnBitsPerPixel = cByte;
-
-            // image dimensions
-            rStm.ReadUInt16( nTemp16 );
-            nXmin = nTemp16;
-            rStm.ReadUInt16( nTemp16 );
-            nYmin = nTemp16;
-            rStm.ReadUInt16( nTemp16 );
-            nXmax = nTemp16;
-            rStm.ReadUInt16( nTemp16 );
-            nYmax = nTemp16;
-
-            aMetadata.maPixSize.setWidth( nXmax - nXmin + 1 );
-            aMetadata.maPixSize.setHeight( nYmax - nYmin + 1 );
-
-            // resolution
-            rStm.ReadUInt16( nTemp16 );
-            nDPIx = nTemp16;
-            rStm.ReadUInt16( nTemp16 );
-            nDPIy = nTemp16;
-
-            // set logical size
-            MapMode aMap( MapUnit::MapInch, Point(),
-                          Fraction( 1, nDPIx ), Fraction( 1, nDPIy ) );
-            aMetadata.maLogSize = OutputDevice::LogicToLogic( 
aMetadata.maPixSize, aMap,
-                                                   MapMode( 
MapUnit::Map100thMM ) );
-
-            // number of color planes
-            cByte = 5; // Illegal value in case of EOF.
-            rStm.SeekRel( 49 );
-            rStm.ReadUChar( cByte );
-            aMetadata.mnPlanes = cByte;
-
-            bRet = (aMetadata.mnPlanes<=4);
-        }
-    }
-
-    rStm.Seek( nStmPos );
+    vcl::GraphicFormatDetector aDetector( rStm, aPathExt, true 
/*bExtendedInfo*/ );
+    bool bRet = aDetector.detect();
+    bRet &= aDetector.checkPCX();
+    if ( bRet )
+        aMetadata = aDetector.getMetadata();
     return bRet;
 }
 

Reply via email to