check that jpeg export of 8bit non-grayscale works

This checks what I fixed in 1893024c54ab173ec6262d77a05cad285c17a617
(related to tdf#116888).

Change-Id: I721017fd037d62ba75cbc962fdd9316e02085624
Reviewed-on: https://gerrit.libreoffice.org/69819
Tested-by: Jenkins
Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
diff --git a/vcl/CppunitTest_vcl_jpeg_read_write_test.mk b/vcl/CppunitTest_vcl_jpeg_read_write_test.mk
index e741916..8838587 100644
--- a/vcl/CppunitTest_vcl_jpeg_read_write_test.mk
+++ b/vcl/CppunitTest_vcl_jpeg_read_write_test.mk
@@ -11,6 +11,7 @@ $(eval $(call gb_CppunitTest_CppunitTest,vcl_jpeg_read_write_test))

$(eval $(call gb_CppunitTest_add_exception_objects,vcl_jpeg_read_write_test, \
    vcl/qa/cppunit/jpeg/JpegReaderTest \
    vcl/qa/cppunit/jpeg/JpegWriterTest \
))

$(eval $(call gb_CppunitTest_use_external,vcl_jpeg_read_write_test,boost_headers))
diff --git a/vcl/qa/cppunit/jpeg/JpegWriterTest.cxx b/vcl/qa/cppunit/jpeg/JpegWriterTest.cxx
new file mode 100644
index 0000000..a81d0c4
--- /dev/null
+++ b/vcl/qa/cppunit/jpeg/JpegWriterTest.cxx
@@ -0,0 +1,88 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#include <test/setupvcl.hxx>
#include <unotest/bootstrapfixturebase.hxx>
#include <vcl/graphicfilter.hxx>
#include <vcl/bitmapaccess.hxx>
#include <bitmapwriteaccess.hxx>
#include <tools/stream.hxx>

static OUString const gaDataUrl("/vcl/qa/cppunit/jpeg/data/");

class JpegWriterTest : public test::BootstrapFixtureBase
{
    OUString getFullUrl(const OUString& sFileName)
    {
        return m_directories.getURLFromSrc(gaDataUrl) + sFileName;
    }

    BitmapEx load(const OUString& aURL);
    BitmapEx roundtripJPG(const BitmapEx& bitmap);
    BitmapEx roundtripJPG(const OUString& aURL);

public:
    void testWrite8BitNonGrayscale();

    CPPUNIT_TEST_SUITE(JpegWriterTest);
    CPPUNIT_TEST(testWrite8BitNonGrayscale);
    CPPUNIT_TEST_SUITE_END();
};

BitmapEx JpegWriterTest::load(const OUString& aURL)
{
    GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
    Graphic aGraphic;
    SvFileStream aFileStream(aURL, StreamMode::READ);
    ErrCode bResult = rFilter.ImportGraphic(aGraphic, aURL, aFileStream);
    CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
    return aGraphic.GetBitmapEx();
}

BitmapEx JpegWriterTest::roundtripJPG(const OUString& aURL) { return roundtripJPG(load(aURL)); }

BitmapEx JpegWriterTest::roundtripJPG(const BitmapEx& bitmap)
{
    SvMemoryStream stream;
    GraphicFilter& rFilter = GraphicFilter::GetGraphicFilter();
    sal_uInt16 exportFormatJPG = rFilter.GetExportFormatNumberForShortName(JPG_SHORTNAME);
    Graphic aExportGraphic(bitmap);
    ErrCode bResult = rFilter.ExportGraphic(aExportGraphic, "memory", stream, exportFormatJPG);
    CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
    stream.Seek(0);
    Graphic aImportGraphic;
    sal_uInt16 importFormatJPG = rFilter.GetImportFormatNumberForShortName(JPG_SHORTNAME);
    bResult = rFilter.ImportGraphic(aImportGraphic, "memory", stream, importFormatJPG);
    CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE, bResult);
    return aImportGraphic.GetBitmapEx();
}

void JpegWriterTest::testWrite8BitNonGrayscale()
{
    Bitmap bitmap = roundtripJPG(getFullUrl("8BitNonGrayscale.gif")).GetBitmap();
    Bitmap::ScopedReadAccess access(bitmap);
    const ScanlineFormat format = access->GetScanlineFormat();
    // Check that it's still 8bit grayscale.
    CPPUNIT_ASSERT_EQUAL(ScanlineFormat::N8BitPal, format);
    // The original image has grayscale palette, just with entries in a different order,
    // so do not check for non-grayscale, the roundtrip apparently fixes that. What's important
    // is the content.
    // CPPUNIT_ASSERT(!bitmap.HasGreyPalette());
    // Check that the content is valid.
    CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE), access->GetColor(0, 0));
    CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE), access->GetColor(0, access->Width() - 1));
    CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE), access->GetColor(access->Height() - 1, 0));
    CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK),
                         access->GetColor(access->Height() - 1, access->Width() - 1));
}

CPPUNIT_TEST_SUITE_REGISTRATION(JpegWriterTest);

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/qa/cppunit/jpeg/data/8BitNonGrayscale.gif b/vcl/qa/cppunit/jpeg/data/8BitNonGrayscale.gif
new file mode 100644
index 0000000..2953109
--- /dev/null
+++ b/vcl/qa/cppunit/jpeg/data/8BitNonGrayscale.gif
Binary files differ