diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2021-02-16 10:35:11 +0900 |
---|---|---|
committer | Tomaž Vajngerl <quikee@gmail.com> | 2021-02-18 07:10:08 +0100 |
commit | 68db2610c2911d6f7f2c9f2ed85fa359743c6947 (patch) | |
tree | 640cee16df2dc013ad1aa72215328210245b376f | |
parent | c3e3bed2db816bf2c929f9c3bde76b2af57a4d76 (diff) |
vcl: move def. of peekGraphicFormat into GraphicFormatDetector
Change-Id: I3b89009324f21b54ccf00f16eb47f9967a6b4e1f
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/111091
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
-rw-r--r-- | vcl/inc/graphic/GraphicFormatDetector.hxx | 13 | ||||
-rw-r--r-- | vcl/qa/cppunit/GraphicFormatDetectorTest.cxx | 32 | ||||
-rw-r--r-- | vcl/source/filter/GraphicFormatDetector.cxx | 249 | ||||
-rw-r--r-- | vcl/source/filter/graphicfilter.cxx | 268 |
4 files changed, 278 insertions, 284 deletions
diff --git a/vcl/inc/graphic/GraphicFormatDetector.hxx b/vcl/inc/graphic/GraphicFormatDetector.hxx index b38561f790c3..f2f0393caeee 100644 --- a/vcl/inc/graphic/GraphicFormatDetector.hxx +++ b/vcl/inc/graphic/GraphicFormatDetector.hxx @@ -23,10 +23,19 @@ #include <tools/stream.hxx> #include <vector> -VCL_DLLPUBLIC bool peekGraphicFormat(SvStream& rStream, OUString& rFormatExtension, bool bTest); - namespace vcl { +/*** + * This function is has two modes: + * - determine the file format when bTest = false + * returns true, success + * out rFormatExtension - on success: file format string + * - verify file format when bTest = true + * returns false, if file type can't be verified + * true, if the format is verified or the format is not known + */ +VCL_DLLPUBLIC bool peekGraphicFormat(SvStream& rStream, OUString& rFormatExtension, bool bTest); + class VCL_DLLPUBLIC GraphicFormatDetector { public: diff --git a/vcl/qa/cppunit/GraphicFormatDetectorTest.cxx b/vcl/qa/cppunit/GraphicFormatDetectorTest.cxx index 1fb2fe0cb4ee..264a0e8cd48d 100644 --- a/vcl/qa/cppunit/GraphicFormatDetectorTest.cxx +++ b/vcl/qa/cppunit/GraphicFormatDetectorTest.cxx @@ -83,7 +83,7 @@ void GraphicFormatDetectorTest::testDetectMET() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("MET"), rFormatExtension); } @@ -98,7 +98,7 @@ void GraphicFormatDetectorTest::testDetectBMP() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("BMP"), rFormatExtension); } @@ -113,7 +113,7 @@ void GraphicFormatDetectorTest::testDetectWMF() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("WMF"), rFormatExtension); } @@ -128,7 +128,7 @@ void GraphicFormatDetectorTest::testDetectPCX() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("PCX"), rFormatExtension); } @@ -143,7 +143,7 @@ void GraphicFormatDetectorTest::testDetectJPG() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("JPG"), rFormatExtension); } @@ -158,7 +158,7 @@ void GraphicFormatDetectorTest::testDetectPNG() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("PNG"), rFormatExtension); } @@ -173,7 +173,7 @@ void GraphicFormatDetectorTest::testDetectGIF() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("GIF"), rFormatExtension); } @@ -188,7 +188,7 @@ void GraphicFormatDetectorTest::testDetectPSD() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("PSD"), rFormatExtension); } @@ -203,7 +203,7 @@ void GraphicFormatDetectorTest::testDetectTGA() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension("TGA"); // detection is based on extension only - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("TGA"), rFormatExtension); } @@ -218,7 +218,7 @@ void GraphicFormatDetectorTest::testDetectTIF() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("TIF"), rFormatExtension); } @@ -233,7 +233,7 @@ void GraphicFormatDetectorTest::testDetectXBM() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("XBM"), rFormatExtension); } @@ -248,7 +248,7 @@ void GraphicFormatDetectorTest::testDetectXPM() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("XPM"), rFormatExtension); } @@ -263,7 +263,7 @@ void GraphicFormatDetectorTest::testDetectSVG() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("SVG"), rFormatExtension); } @@ -278,7 +278,7 @@ void GraphicFormatDetectorTest::testDetectSVGZ() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("SVG"), rFormatExtension); } @@ -293,7 +293,7 @@ void GraphicFormatDetectorTest::testDetectPDF() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("PDF"), rFormatExtension); } @@ -308,7 +308,7 @@ void GraphicFormatDetectorTest::testDetectEPS() aFileStream.Seek(aDetector.mnStreamPosition); OUString rFormatExtension; - CPPUNIT_ASSERT(peekGraphicFormat(aFileStream, rFormatExtension, false)); + CPPUNIT_ASSERT(vcl::peekGraphicFormat(aFileStream, rFormatExtension, false)); CPPUNIT_ASSERT_EQUAL(OUString("EPS"), rFormatExtension); } diff --git a/vcl/source/filter/GraphicFormatDetector.cxx b/vcl/source/filter/GraphicFormatDetector.cxx index 4a2117b6f5b9..69aa225eb108 100644 --- a/vcl/source/filter/GraphicFormatDetector.cxx +++ b/vcl/source/filter/GraphicFormatDetector.cxx @@ -28,6 +28,255 @@ namespace vcl { +bool peekGraphicFormat(SvStream& rStream, OUString& rFormatExtension, bool bTest) +{ + vcl::GraphicFormatDetector aDetector(rStream, rFormatExtension); + if (!aDetector.detect()) + return false; + + // The following variable is used when bTest == true. It remains false + // if the format (rFormatExtension) has not yet been set. + bool bSomethingTested = false; + + // Now the different formats are checked. The order *does* matter. e.g. a MET file + // could also go through the BMP test, however, a BMP file can hardly go through the MET test. + // So MET should be tested prior to BMP. However, theoretically a BMP file could conceivably + // go through the MET test. These problems are of course not only in MET and BMP. + // Therefore, in the case of a format check (bTest == true) we only test *exactly* this + // format. Everything else could have fatal consequences, for example if the user says it is + // a BMP file (and it is a BMP) file, and the file would go through the MET test ... + + if (!bTest || rFormatExtension.startsWith("MET")) + { + bSomethingTested = true; + if (aDetector.checkMET()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("BMP")) + { + bSomethingTested = true; + if (aDetector.checkBMP()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("WMF") || rFormatExtension.startsWith("EMF")) + { + bSomethingTested = true; + if (aDetector.checkWMForEMF()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PCX")) + { + bSomethingTested = true; + if (aDetector.checkPCX()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("TIF")) + { + bSomethingTested = true; + if (aDetector.checkTIF()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("GIF")) + { + bSomethingTested = true; + if (aDetector.checkGIF()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PNG")) + { + bSomethingTested = true; + if (aDetector.checkPNG()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("JPG")) + { + bSomethingTested = true; + if (aDetector.checkJPG()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("SVM")) + { + bSomethingTested = true; + if (aDetector.checkSVM()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PCD")) + { + bSomethingTested = true; + if (aDetector.checkPCD()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PSD")) + { + bSomethingTested = true; + if (aDetector.checkPSD()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("EPS")) + { + bSomethingTested = true; + if (aDetector.checkEPS()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("DXF")) + { + if (aDetector.checkDXF()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PCT")) + { + bSomethingTested = true; + if (aDetector.checkPCT()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PBM") || rFormatExtension.startsWith("PGM") + || rFormatExtension.startsWith("PPM")) + { + bSomethingTested = true; + if (aDetector.checkPBMorPGMorPPM()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("RAS")) + { + bSomethingTested = true; + if (aDetector.checkRAS()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest) + { + bSomethingTested = true; + if (aDetector.checkXPM()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + else if (rFormatExtension.startsWith("XPM")) + { + return true; + } + + if (!bTest) + { + if (aDetector.checkXBM()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + else if (rFormatExtension.startsWith("XBM")) + { + return true; + } + + if (!bTest) + { + if (aDetector.checkSVG()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + else if (rFormatExtension.startsWith("SVG")) + { + return true; + } + + if (!bTest || rFormatExtension.startsWith("TGA")) + { + bSomethingTested = true; + if (aDetector.checkTGA()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("MOV")) + { + if (aDetector.checkMOV()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + if (!bTest || rFormatExtension.startsWith("PDF")) + { + if (aDetector.checkPDF()) + { + rFormatExtension = aDetector.msDetectedFormat; + return true; + } + } + + return bTest && !bSomethingTested; +} + namespace { bool isPCT(SvStream& rStream, sal_uLong nStreamPos, sal_uLong nStreamLen) diff --git a/vcl/source/filter/graphicfilter.cxx b/vcl/source/filter/graphicfilter.cxx index 08e0454db1a2..a6ef4d2488b7 100644 --- a/vcl/source/filter/graphicfilter.cxx +++ b/vcl/source/filter/graphicfilter.cxx @@ -181,277 +181,13 @@ bool isPCT(SvStream& rStream, sal_uLong nStreamPos, sal_uLong nStreamLen) return false; } -/*** - * This function is has two modes: - * - determine the file format when bTest = false - * returns true, success - * out rFormatExtension - on success: file format string - * - verify file format when bTest = true - * returns false, if file type can't be verified - * true, if the format is PROBABLY verified - * or WHEN THE FORMAT IS NOT KNOWN! - */ -bool peekGraphicFormat( SvStream& rStream, OUString& rFormatExtension, bool bTest ) -{ - vcl::GraphicFormatDetector aDetector(rStream, rFormatExtension); - if (!aDetector.detect()) - return false; - - // The following variable is used when bTest == true. It remains false - // if the format (rFormatExtension) has not yet been set. - bool bSomethingTested = false; - - // Now the different formats are checked. The order *does* matter. e.g. a MET file - // could also go through the BMP test, however, a BMP file can hardly go through the MET test. - // So MET should be tested prior to BMP. However, theoretically a BMP file could conceivably - // go through the MET test. These problems are of course not only in MET and BMP. - // Therefore, in the case of a format check (bTest == true) we only test *exactly* this - // format. Everything else could have fatal consequences, for example if the user says it is - // a BMP file (and it is a BMP) file, and the file would go through the MET test ... - - if (!bTest || rFormatExtension.startsWith("MET")) - { - bSomethingTested = true; - if (aDetector.checkMET()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("BMP")) - { - bSomethingTested = true; - if (aDetector.checkBMP()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || - rFormatExtension.startsWith("WMF") || - rFormatExtension.startsWith("EMF")) - { - bSomethingTested = true; - if (aDetector.checkWMForEMF()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("PCX")) - { - bSomethingTested = true; - if (aDetector.checkPCX()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("TIF")) - { - bSomethingTested = true; - if (aDetector.checkTIF()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("GIF")) - { - bSomethingTested = true; - if (aDetector.checkGIF()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("PNG")) - { - bSomethingTested = true; - if (aDetector.checkPNG()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("JPG")) - { - bSomethingTested = true; - if (aDetector.checkJPG()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("SVM")) - { - bSomethingTested = true; - if (aDetector.checkSVM()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("PCD")) - { - bSomethingTested = true; - if (aDetector.checkPCD()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("PSD")) - { - bSomethingTested = true; - if (aDetector.checkPSD()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("EPS")) - { - bSomethingTested = true; - if (aDetector.checkEPS()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("DXF")) - { - if (aDetector.checkDXF()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("PCT")) - { - bSomethingTested = true; - if (aDetector.checkPCT()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || - rFormatExtension.startsWith("PBM") || - rFormatExtension.startsWith("PGM") || - rFormatExtension.startsWith("PPM")) - { - bSomethingTested = true; - if (aDetector.checkPBMorPGMorPPM()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("RAS")) - { - bSomethingTested = true; - if (aDetector.checkRAS()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest) - { - bSomethingTested = true; - if (aDetector.checkXPM()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - - } - else if (rFormatExtension.startsWith("XPM")) - { - return true; - } - - if (!bTest) - { - if (aDetector.checkXBM()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - else if (rFormatExtension.startsWith("XBM")) - { - return true; - } - - if (!bTest) - { - if (aDetector.checkSVG()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - else if (rFormatExtension.startsWith("SVG")) - { - return true; - } - - if (!bTest || rFormatExtension.startsWith("TGA")) - { - bSomethingTested = true; - if (aDetector.checkTGA()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("MOV")) - { - if (aDetector.checkMOV()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - if (!bTest || rFormatExtension.startsWith("PDF")) - { - if (aDetector.checkPDF()) - { - rFormatExtension = aDetector.msDetectedFormat; - return true; - } - } - - return bTest && !bSomethingTested; -} - ErrCode GraphicFilter::ImpTestOrFindFormat( const OUString& rPath, SvStream& rStream, sal_uInt16& rFormat ) { // determine or check the filter/format by reading into it if( rFormat == GRFILTER_FORMAT_DONTKNOW ) { OUString aFormatExt; - if( peekGraphicFormat( rStream, aFormatExt, false ) ) + if (vcl::peekGraphicFormat(rStream, aFormatExt, false)) { rFormat = pConfig->GetImportFormatNumberForExtension( aFormatExt ); if( rFormat != GRFILTER_FORMAT_DONTKNOW ) @@ -471,7 +207,7 @@ ErrCode GraphicFilter::ImpTestOrFindFormat( const OUString& rPath, SvStream& rSt { OUString aTmpStr( pConfig->GetImportFormatExtension( rFormat ) ); aTmpStr = aTmpStr.toAsciiUpperCase(); - if( !peekGraphicFormat( rStream, aTmpStr, true ) ) + if (!vcl::peekGraphicFormat(rStream, aTmpStr, true)) return ERRCODE_GRFILTER_FORMATERROR; if ( pConfig->GetImportFormatExtension( rFormat ).equalsIgnoreAsciiCase( "pcd" ) ) { |