tdf#113050 sdext.pdfimport: Flip bitmap

We need to flip the bitmap between the wrapper and LO, but there's no
easy way to do this in a Poly image fill in LO, so do it as a simple
bitmap flip in the wrapper.

Change-Id: Ifd84d37926c21edf30654d3884be975849a6dca3
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/163563
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
diff --git a/sdext/source/pdfimport/xpdfwrapper/pdfioutdev_gpl.cxx b/sdext/source/pdfimport/xpdfwrapper/pdfioutdev_gpl.cxx
index 57eb300..ec2632b 100644
--- a/sdext/source/pdfimport/xpdfwrapper/pdfioutdev_gpl.cxx
+++ b/sdext/source/pdfimport/xpdfwrapper/pdfioutdev_gpl.cxx
@@ -432,8 +432,6 @@ static void writeImage_( OutputBuffer&     o_rOutputBuf,
}

// forwarders


static void writeImageLF( OutputBuffer&     o_rOutputBuf,
                          Stream*           str,
                          int               width,
@@ -445,6 +443,45 @@ static void writeMaskLF( OutputBuffer&     o_rOutputBuf,
                         int               height,
                         bool              bInvert ) { writeMask_(o_rOutputBuf,str,width,height,bInvert); }

// Vertically flip the bitmap
static void flipSplashBitmap(SplashBitmap *pBitmap)
{
    if (pBitmap->getRowSize() <= 0)
        return;

    auto nBitmapHeight = static_cast<size_t>(pBitmap->getHeight());
    auto nRowSize = static_cast<size_t>(pBitmap->getRowSize());
    auto nAlphaRowSize = static_cast<size_t>(pBitmap->getAlphaRowSize());

    auto aTmpRow = new unsigned char[nRowSize];
    auto aTmpAlphaRow = new unsigned char[nAlphaRowSize];

    auto pBitmapData = pBitmap->getDataPtr();
    auto pAlphaData = pBitmap->getAlphaPtr();

    // Set up pairs of pointers working from each end of the bitmap
    auto pCurRowA = pBitmapData;
    auto pCurAlphaA = pAlphaData;
    auto pCurRowB = pBitmapData+nRowSize*(nBitmapHeight-1);
    auto pCurAlphaB = pAlphaData+nAlphaRowSize*(nBitmapHeight-1);

    for (size_t nCur = 0;
         nCur < nBitmapHeight/2;
         nCur++, pCurRowA+=nRowSize, pCurRowB-=nRowSize,
         pCurAlphaA+=nAlphaRowSize, pCurAlphaB-=nAlphaRowSize)
    {
        memcpy(aTmpRow, pCurRowA, nRowSize);
        memcpy(pCurRowA, pCurRowB, nRowSize);
        memcpy(pCurRowB, aTmpRow, nRowSize);

        memcpy(aTmpAlphaRow, pCurAlphaA, nAlphaRowSize);
        memcpy(pCurAlphaA, pCurAlphaB, nAlphaRowSize);
        memcpy(pCurAlphaB, aTmpAlphaRow, nAlphaRowSize);
    }
    delete[] aTmpRow;
    delete[] aTmpAlphaRow;
}

int PDFOutDev::parseFont( long long nNewId, GfxFont* gfxFont, const GfxState* state ) const
{
    FontAttributes aNewFont;
@@ -1236,6 +1273,9 @@ poppler_bool PDFOutDev::tilingPatternFill(GfxState *state, Gfx *, Catalog *,
    delete pSplashGfx;
    delete pSplashOut;

    // Add a vertical flip, we can't do this in LO for an image filled poly
    flipSplashBitmap(pSplashBitmap);

    auto nBitmapWidth = static_cast<size_t>(pSplashBitmap->getWidth());
    auto nBitmapHeight = static_cast<size_t>(pSplashBitmap->getHeight());