1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 #include <unotest/bootstrapfixturebase.hxx>
12 #include <vcl/graphicfilter.hxx>
13 #include <vcl/bitmapaccess.hxx>
14 #include <tools/stream.hxx>
16 constexpr OUStringLiteral
gaDataUrl(u
"/vcl/qa/cppunit/jpeg/data/");
18 class JpegWriterTest
: public test::BootstrapFixtureBase
20 OUString
getFullUrl(const OUString
& sFileName
)
22 return m_directories
.getURLFromSrc(gaDataUrl
) + sFileName
;
25 BitmapEx
load(const OUString
& aURL
);
26 BitmapEx
roundtripJPG(const BitmapEx
& bitmap
);
27 BitmapEx
roundtripJPG(const OUString
& aURL
);
30 void testWrite8BitGrayscale();
31 void testWrite8BitNonGrayscale();
33 CPPUNIT_TEST_SUITE(JpegWriterTest
);
34 CPPUNIT_TEST(testWrite8BitGrayscale
);
35 CPPUNIT_TEST(testWrite8BitNonGrayscale
);
36 CPPUNIT_TEST_SUITE_END();
39 BitmapEx
JpegWriterTest::load(const OUString
& aURL
)
41 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
43 SvFileStream
aFileStream(aURL
, StreamMode::READ
);
44 ErrCode bResult
= rFilter
.ImportGraphic(aGraphic
, aURL
, aFileStream
);
45 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, bResult
);
46 return aGraphic
.GetBitmapEx();
49 BitmapEx
JpegWriterTest::roundtripJPG(const OUString
& aURL
) { return roundtripJPG(load(aURL
)); }
51 BitmapEx
JpegWriterTest::roundtripJPG(const BitmapEx
& bitmap
)
53 SvMemoryStream stream
;
54 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
55 sal_uInt16 exportFormatJPG
= rFilter
.GetExportFormatNumberForShortName(JPG_SHORTNAME
);
56 Graphic
aExportGraphic(bitmap
);
57 ErrCode bResult
= rFilter
.ExportGraphic(aExportGraphic
, "memory", stream
, exportFormatJPG
);
58 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, bResult
);
60 Graphic aImportGraphic
;
61 sal_uInt16 importFormatJPG
= rFilter
.GetImportFormatNumberForShortName(JPG_SHORTNAME
);
62 bResult
= rFilter
.ImportGraphic(aImportGraphic
, "memory", stream
, importFormatJPG
);
63 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, bResult
);
64 return aImportGraphic
.GetBitmapEx();
67 void JpegWriterTest::testWrite8BitGrayscale()
69 Bitmap bitmap
= roundtripJPG(getFullUrl("8BitGrayscale.jpg")).GetBitmap();
70 Bitmap::ScopedReadAccess
access(bitmap
);
71 const ScanlineFormat format
= access
->GetScanlineFormat();
72 // Check that it's still 8bit grayscale.
73 CPPUNIT_ASSERT_EQUAL(ScanlineFormat::N8BitPal
, format
);
74 CPPUNIT_ASSERT(bitmap
.HasGreyPalette8Bit());
75 // Check that the content is valid.
76 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, 0));
77 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, access
->Width() - 1));
78 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(access
->Height() - 1, 0));
79 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK
),
80 access
->GetColor(access
->Height() - 1, access
->Width() - 1));
83 void JpegWriterTest::testWrite8BitNonGrayscale()
85 Bitmap bitmap
= roundtripJPG(getFullUrl("8BitNonGrayscale.gif")).GetBitmap();
86 Bitmap::ScopedReadAccess
access(bitmap
);
87 const ScanlineFormat format
= access
->GetScanlineFormat();
88 // Check that it's still 8bit grayscale.
89 CPPUNIT_ASSERT_EQUAL(ScanlineFormat::N8BitPal
, format
);
90 // The original image has grayscale palette, just with entries in a different order.
91 // Do not check for grayscale 8bit, the roundtrip apparently fixes that. What's important
93 CPPUNIT_ASSERT(bitmap
.HasGreyPaletteAny());
94 // CPPUNIT_ASSERT(bitmap.HasGreyPalette8Bit());
95 // Check that the content is valid.
96 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, 0));
97 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, access
->Width() - 1));
98 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(access
->Height() - 1, 0));
99 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK
),
100 access
->GetColor(access
->Height() - 1, access
->Width() - 1));
103 CPPUNIT_TEST_SUITE_REGISTRATION(JpegWriterTest
);
105 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */