vcl/source/filter/GraphicFormatDetector.cxx |   40 +++++++++++++++++++++-
 vcl/source/filter/graphicfilter2.cxx        |   50 ++--------------------------
 2 files changed, 42 insertions(+), 48 deletions(-)

New commits:
commit 86d49f41d46519235b55918ac375bc1d2b99552b
Author:     offtkp <parisop...@gmail.com>
AuthorDate: Sat Aug 13 12:52:26 2022 +0300
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Mon Aug 22 18:05:29 2022 +0200

    Remove code duplication in GraphicDescriptor for PSD
    
    GraphicFormatDetector and GraphicDescriptor have duplicate format
    detection code so now GraphicDescriptor uses GraphicFormatDetector
    functions instead to detect the format for PSD files
    
    Change-Id: Icae532a7143477dada411224293250a26676c684
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138227
    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 5b5ccf33d004..8956c3cd9530 100644
--- a/vcl/source/filter/GraphicFormatDetector.cxx
+++ b/vcl/source/filter/GraphicFormatDetector.cxx
@@ -792,12 +792,48 @@ bool GraphicFormatDetector::checkPCD()
 
 bool GraphicFormatDetector::checkPSD()
 {
+    bool bRet = false;
     if ((mnFirstLong == 0x38425053) && ((mnSecondLong >> 16) == 1))
     {
         maMetadata.mnFormat = GraphicFileFormat::PSD;
-        return true;
+        bRet = true;
+        if (mbExtendedInfo)
+        {
+            sal_uInt16 nChannels = 0;
+            sal_uInt32 nRows = 0;
+            sal_uInt32 nColumns = 0;
+            sal_uInt16 nDepth = 0;
+            sal_uInt16 nMode = 0;
+            mrStream.Seek(mnStreamPosition + 6);
+            mrStream.SeekRel(6); // Pad
+            mrStream.ReadUInt16(nChannels)
+                .ReadUInt32(nRows)
+                .ReadUInt32(nColumns)
+                .ReadUInt16(nDepth)
+                .ReadUInt16(nMode);
+            if ((nDepth == 1) || (nDepth == 8) || (nDepth == 16))
+            {
+                maMetadata.mnBitsPerPixel = (nDepth == 16) ? 8 : nDepth;
+                switch (nChannels)
+                {
+                    case 4:
+                    case 3:
+                        maMetadata.mnBitsPerPixel = 24;
+                        [[fallthrough]];
+                    case 2:
+                    case 1:
+                        maMetadata.maPixSize.setWidth(nColumns);
+                        maMetadata.maPixSize.setHeight(nRows);
+                        break;
+                    default:
+                        bRet = false;
+                }
+            }
+            else
+                bRet = false;
+        }
     }
-    return false;
+    return bRet;
 }
 
 bool GraphicFormatDetector::checkEPS()
diff --git a/vcl/source/filter/graphicfilter2.cxx 
b/vcl/source/filter/graphicfilter2.cxx
index 4327ef5e2f59..1117b2cc9cbb 100644
--- a/vcl/source/filter/graphicfilter2.cxx
+++ b/vcl/source/filter/graphicfilter2.cxx
@@ -683,54 +683,12 @@ bool GraphicDescriptor::ImpDetectTGA( SvStream& rStm, 
bool )
 
 bool GraphicDescriptor::ImpDetectPSD( SvStream& rStm, bool bExtendedInfo )
 {
-    bool bRet = false;
-
-    sal_uInt32  nMagicNumber = 0;
     sal_Int32 nStmPos = rStm.Tell();
-    rStm.SetEndian( SvStreamEndian::BIG );
-    rStm.ReadUInt32( nMagicNumber );
-    if ( nMagicNumber == 0x38425053 )
-    {
-        sal_uInt16 nVersion = 0;
-        rStm.ReadUInt16( nVersion );
-        if ( nVersion == 1 )
-        {
-            bRet = true;
-            if ( bExtendedInfo )
-            {
-                sal_uInt16 nChannels = 0;
-                sal_uInt32 nRows = 0;
-                sal_uInt32 nColumns = 0;
-                sal_uInt16 nDepth = 0;
-                sal_uInt16 nMode = 0;
-                rStm.SeekRel( 6 );  // Pad
-                rStm.ReadUInt16( nChannels ).ReadUInt32( nRows ).ReadUInt32( 
nColumns ).ReadUInt16( nDepth ).ReadUInt16( nMode );
-                if ( ( nDepth == 1 ) || ( nDepth == 8 ) || ( nDepth == 16 ) )
-                {
-                    aMetadata.mnBitsPerPixel = ( nDepth == 16 ) ? 8 : nDepth;
-                    switch ( nChannels )
-                    {
-                        case 4 :
-                        case 3 :
-                            aMetadata.mnBitsPerPixel = 24;
-                            [[fallthrough]];
-                        case 2 :
-                        case 1 :
-                            aMetadata.maPixSize.setWidth( nColumns );
-                            aMetadata.maPixSize.setHeight( nRows );
-                        break;
-                        default:
-                            bRet = false;
-                    }
-                }
-                else
-                    bRet = false;
-            }
-        }
-    }
-
+    vcl::GraphicFormatDetector aDetector( rStm, aPathExt, bExtendedInfo );
+    bool bRet = aDetector.detect();
+    bRet &= aDetector.checkPSD();
     if ( bRet )
-        aMetadata.mnFormat = GraphicFileFormat::PSD;
+        aMetadata = aDetector.getMetadata();
     rStm.Seek( nStmPos );
     return bRet;
 }

Reply via email to