tdf#152153 DOCX export: fix lost images at embedded documents

Handling of image counter was incorrect if the
document contains embedded documents, overwriting
images with the other ones.

See also commit cf2dc247ff5f726238856e9b46a4926a30430e14
"DOCX export: image deduplication and clean up" and
 commit 3f6df3835fec71ea61894f9a3bbfe5e4a06a5495
"DOCX export: fix image counters for multiple documents".

Change-Id: I3ce3e370f96fa8b9feca3bc73f06ddca933215d4
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143036
Tested-by: László Németh <nemeth@numbertext.org>
Reviewed-by: László Németh <nemeth@numbertext.org>
(cherry picked from commit 55b1d635350cb76ee3e19e90c938eedd38ac3342)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143256
Reviewed-by: Michael Stahl <michael.stahl@allotropia.de>
Tested-by: Jenkins
diff --git a/include/oox/export/drawingml.hxx b/include/oox/export/drawingml.hxx
index 674457b..9f28a58 100644
--- a/include/oox/export/drawingml.hxx
+++ b/include/oox/export/drawingml.hxx
@@ -142,7 +142,7 @@ class OOX_DLLPUBLIC DrawingML
{

private:
    static int mnImageCounter;
    static std::stack<sal_Int32> mnImageCounter;
    static int mnWdpImageCounter;
    static std::map<OUString, OUString> maWdpCache;
    static sal_Int32 mnDrawingMLCount;
diff --git a/oox/source/export/drawingml.cxx b/oox/source/export/drawingml.cxx
index 5df1859..ea44c42 100644
--- a/oox/source/export/drawingml.cxx
+++ b/oox/source/export/drawingml.cxx
@@ -234,7 +234,7 @@ void WriteGradientPath(const awt::Gradient& rGradient, const FSHelperPtr& pFS, c
}

// not thread safe
int DrawingML::mnImageCounter = 1;
std::stack<sal_Int32> DrawingML::mnImageCounter;
int DrawingML::mnWdpImageCounter = 1;
std::map<OUString, OUString> DrawingML::maWdpCache;
sal_Int32 DrawingML::mnDrawingMLCount = 0;
@@ -267,7 +267,6 @@ sal_Int16 DrawingML::GetScriptType(const OUString& rStr)

void DrawingML::ResetCounters()
{
    mnImageCounter = 1;
    mnWdpImageCounter = 1;
    maWdpCache.clear();
}
@@ -280,11 +279,13 @@ void DrawingML::ResetMlCounters()

void DrawingML::PushExportGraphics()
{
    mnImageCounter.push(1);
    maExportGraphics.emplace();
}

void DrawingML::PopExportGraphics()
{
    mnImageCounter.pop();
    maExportGraphics.pop();
}

@@ -1393,7 +1394,7 @@ OUString DrawingML::WriteImage( const Graphic& rGraphic , bool bRelPathToMedia )
        Reference<XOutputStream> xOutStream = mpFB->openFragmentStream(
            OUStringBuffer()
                .appendAscii(GetComponentDir())
                .append("/media/image" + OUString::number(mnImageCounter))
                .append("/media/image" + OUString::number(mnImageCounter.top()))
                .appendAscii(pExtension)
                .makeStringAndClear(),
            sMediaType);
@@ -1409,7 +1410,7 @@ OUString DrawingML::WriteImage( const Graphic& rGraphic , bool bRelPathToMedia )
        sPath = OUStringBuffer()
                    .appendAscii(sRelationCompPrefix.getStr())
                    .appendAscii(sRelPathToMedia.getStr())
                    .append(static_cast<sal_Int32>(mnImageCounter++))
                    .append(static_cast<sal_Int32>(mnImageCounter.top()++))
                    .appendAscii(pExtension)
                    .makeStringAndClear();

@@ -1488,7 +1489,7 @@ void DrawingML::WriteMediaNonVisualProperties(const css::uno::Reference<css::dra
        Reference<XOutputStream> xOutStream = mpFB->openFragmentStream(OUStringBuffer()
                                                                       .appendAscii(GetComponentDir())
                                                                       .append("/media/media" +
                                                                            OUString::number(mnImageCounter) +
                                                                            OUString::number(mnImageCounter.top()) +
                                                                            aExtension)
                                                                       .makeStringAndClear(),
                                                                       aMimeType);
@@ -1500,7 +1501,7 @@ void DrawingML::WriteMediaNonVisualProperties(const css::uno::Reference<css::dra

        // create the relation
        OUString aPath = OUStringBuffer().appendAscii(GetRelationCompPrefix())
                                         .append("media/media" + OUString::number(mnImageCounter++) + aExtension)
                                         .append("media/media" + OUString::number(mnImageCounter.top()++) + aExtension)
                                         .makeStringAndClear();
        aVideoFileRelId = mpFB->addRelation(mpFS->getOutputStream(), oox::getRelationship(eMediaType), aPath);
        aMediaRelId = mpFB->addRelation(mpFS->getOutputStream(), oox::getRelationship(Relationship::MEDIA), aPath);
diff --git a/sw/qa/extras/ooxmlexport/data/embedded_images.odt b/sw/qa/extras/ooxmlexport/data/embedded_images.odt
new file mode 100644
index 0000000..26166fa
--- /dev/null
+++ b/sw/qa/extras/ooxmlexport/data/embedded_images.odt
Binary files differ
diff --git a/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx b/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
index 6de87f5..2f958b6 100644
--- a/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
+++ b/sw/qa/extras/ooxmlexport/ooxmlexport16.cxx
@@ -1000,6 +1000,26 @@ DECLARE_OOXMLEXPORT_TEST(testTdf143726, "Simple-TOC.odt")
    assertXPath(pXmlStyles, "/w:styles/w:style[@w:styleId='ContentsHeading']/w:name", "val", "TOC Heading");
}

CPPUNIT_TEST_FIXTURE(Test, testTdf152153)
{
    loadAndReload("embedded_images.odt");

    uno::Reference<packages::zip::XZipFileAccess2> xNameAccess
        = packages::zip::ZipFileAccess::createWithURL(comphelper::getComponentContext(m_xSFactory),
                                                      maTempFile.GetURL());
    const uno::Sequence<OUString> aNames(xNameAccess->getElementNames());
    int nImageFiles = 0;
    for (const auto& rElementName : aNames)
        if (rElementName.startsWith("word/media/image"))
            nImageFiles++;

    // Without the accompanying fix in place, this test would have failed with:
    // - Expected: 4
    // - Actual  : 3
    // i.e. the once embedded picture wouldn't have been saved.
    CPPUNIT_ASSERT_EQUAL(4, nImageFiles);
}

CPPUNIT_PLUGIN_IMPLEMENT();

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */