Resolves: fdo#52226 swap in graphics on .docx and .rtf export

Change-Id: Ie818b382c0b17760c720ff2f2c73a3697989f97e
(cherry picked from commit 9dd5caac62083f7162d83319284df68ee83e3777)
Reviewed-on: https://gerrit.libreoffice.org/10281
Reviewed-by: Michael Stahl <mstahl@redhat.com>
Tested-by: Michael Stahl <mstahl@redhat.com>
diff --git a/sw/source/filter/ww8/docxattributeoutput.cxx b/sw/source/filter/ww8/docxattributeoutput.cxx
index 98ed78b..a77b8fa 100644
--- a/sw/source/filter/ww8/docxattributeoutput.cxx
+++ b/sw/source/filter/ww8/docxattributeoutput.cxx
@@ -3637,7 +3637,7 @@ void DocxAttributeOutput::FlyFrameGraphic( const SwGrfNode* pGrfNode, const Size
        // inline, we also have to write the image itself
        const Graphic* pGraphic = 0;
        if (pGrfNode)
            pGraphic = &const_cast< Graphic& >( pGrfNode->GetGrf() );
            pGraphic = &pGrfNode->GetGrf();
        else
            pGraphic = pOLENode->GetGraphic();

@@ -3648,8 +3648,24 @@ void DocxAttributeOutput::FlyFrameGraphic( const SwGrfNode* pGrfNode, const Size
        {
            // Not in cache, then need to write it.
            m_rDrawingML.SetFS( m_pSerializer ); // to be sure that we write to the right stream

            bool bSwapped = pGraphic->IsSwapOut();
            if (bSwapped)
            {
                if (pGrfNode)
                {
                    // always swapin via the Node
                    const_cast<SwGrfNode*>(pGrfNode)->SwapIn();
                }
                else
                    const_cast<Graphic*>(pGraphic)->SwapIn();
            }

            OUString aImageId = m_rDrawingML.WriteImage( *pGraphic );

            if (bSwapped)
                const_cast<Graphic*>(pGraphic)->SwapOut();

            aRelId = OUStringToOString( aImageId, RTL_TEXTENCODING_UTF8 );
            m_aRelIdCache[pGraphic] = aRelId;
        }
diff --git a/sw/source/filter/ww8/rtfattributeoutput.cxx b/sw/source/filter/ww8/rtfattributeoutput.cxx
index b9ab17b..9f14309 100644
--- a/sw/source/filter/ww8/rtfattributeoutput.cxx
+++ b/sw/source/filter/ww8/rtfattributeoutput.cxx
@@ -3586,17 +3586,24 @@ void RtfAttributeOutput::FlyFrameGraphic(const SwFlyFrmFmt* pFlyFrmFmt, const Sw
    const sal_uInt8* pGraphicAry = 0;
    sal_uInt32 nSize = 0;

    Graphic aGraphic(pGrfNode->GetGrf());
    const Graphic& rGraphic(pGrfNode->GetGrf());

    // If there is no graphic there is not much point in parsing it
    if (aGraphic.GetType()==GRAPHIC_NONE)
    if (rGraphic.GetType()==GRAPHIC_NONE)
        return;

    bool bSwapped = rGraphic.IsSwapOut();
    if (bSwapped)
    {
        // always swapin via the Node
        const_cast<SwGrfNode*>(pGrfNode)->SwapIn();
    }

    GfxLink aGraphicLink;
    const sal_Char* pBLIPType = 0;
    if (aGraphic.IsLink())
    if (rGraphic.IsLink())
    {
        aGraphicLink = aGraphic.GetLink();
        aGraphicLink = rGraphic.GetLink();
        nSize = aGraphicLink.GetDataSize();
        pGraphicAry = aGraphicLink.GetData();
        switch (aGraphicLink.GetType())
@@ -3629,10 +3636,10 @@ void RtfAttributeOutput::FlyFrameGraphic(const SwFlyFrmFmt* pFlyFrmFmt, const Sw
        }
    }

    GraphicType eGraphicType = aGraphic.GetType();
    GraphicType eGraphicType = rGraphic.GetType();
    if (!pGraphicAry)
    {
        if (ERRCODE_NONE == GraphicConverter::Export(aStream, aGraphic,
        if (ERRCODE_NONE == GraphicConverter::Export(aStream, rGraphic,
                (eGraphicType == GRAPHIC_BITMAP) ? CVT_PNG : CVT_WMF))
        {
            pBLIPType = (eGraphicType == GRAPHIC_BITMAP) ?
@@ -3643,7 +3650,7 @@ void RtfAttributeOutput::FlyFrameGraphic(const SwFlyFrmFmt* pFlyFrmFmt, const Sw
        }
    }

    Size aMapped(eGraphicType == GRAPHIC_BITMAP ? aGraphic.GetSizePixel() : aGraphic.GetPrefSize());
    Size aMapped(eGraphicType == GRAPHIC_BITMAP ? rGraphic.GetSizePixel() : rGraphic.GetPrefSize());

    const SwCropGrf& rCr = (const SwCropGrf&)pGrfNode->GetAttr(RES_GRFATR_CROPGRF);

@@ -3672,7 +3679,7 @@ void RtfAttributeOutput::FlyFrameGraphic(const SwFlyFrmFmt* pFlyFrmFmt, const Sw
    else
    {
        aStream.Seek(0);
        GraphicConverter::Export(aStream, aGraphic, CVT_WMF);
        GraphicConverter::Export(aStream, rGraphic, CVT_WMF);
        pBLIPType = OOO_STRING_SVTOOLS_RTF_WMETAFILE;
        aStream.Seek(STREAM_SEEK_TO_END);
        nSize = aStream.Tell();
@@ -3686,7 +3693,7 @@ void RtfAttributeOutput::FlyFrameGraphic(const SwFlyFrmFmt* pFlyFrmFmt, const Sw
        m_rExport.Strm().WriteCharPtr("}" "{" OOO_STRING_SVTOOLS_RTF_NONSHPPICT);

        aStream.Seek(0);
        GraphicConverter::Export(aStream, aGraphic, CVT_WMF);
        GraphicConverter::Export(aStream, rGraphic, CVT_WMF);
        pBLIPType = OOO_STRING_SVTOOLS_RTF_WMETAFILE;
        aStream.Seek(STREAM_SEEK_TO_END);
        nSize = aStream.Tell();
@@ -3697,6 +3704,9 @@ void RtfAttributeOutput::FlyFrameGraphic(const SwFlyFrmFmt* pFlyFrmFmt, const Sw
        m_rExport.Strm().WriteChar('}');
    }

    if (bSwapped)
        const_cast<Graphic&>(rGraphic).SwapOut();

    m_rExport.Strm().WriteCharPtr(SAL_NEWLINE_STRING);
}

diff --git a/sw/source/filter/ww8/wrtww8gr.cxx b/sw/source/filter/ww8/wrtww8gr.cxx
index 28a2652..4e30e17 100644
--- a/sw/source/filter/ww8/wrtww8gr.cxx
+++ b/sw/source/filter/ww8/wrtww8gr.cxx
@@ -726,7 +726,7 @@ void SwWW8WrGrf::WriteGrfFromGrfNode(SvStream& rStrm, const SwGrfNode &rGrfNd,
        {
            Graphic& rGrf = const_cast<Graphic&>(rGrfNd.GetGrf());
            bool bSwapped = rGrf.IsSwapOut();
            // immer ueber den Node einswappen!
            // always swapin via the Node
            const_cast<SwGrfNode&>(rGrfNd).SwapIn();

            GDIMetaFile aMeta;