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 <sal/config.h>
13 #include <string_view>
15 #include <unotest/bootstrapfixturebase.hxx>
16 #include <vcl/graphicfilter.hxx>
17 #include <vcl/BitmapReadAccess.hxx>
18 #include <tools/stream.hxx>
19 #include <graphic/GraphicFormatDetector.hxx>
21 constexpr OUStringLiteral
gaDataUrl(u
"/vcl/qa/cppunit/jpeg/data/");
23 class JpegWriterTest
: public test::BootstrapFixtureBase
25 OUString
getFullUrl(std::u16string_view sFileName
)
27 return m_directories
.getURLFromSrc(gaDataUrl
) + sFileName
;
30 BitmapEx
load(const OUString
& aURL
);
31 BitmapEx
roundtripJPG(const BitmapEx
& bitmap
);
32 BitmapEx
roundtripJPG(const OUString
& aURL
);
35 void testWrite8BitGrayscale();
36 void testWrite8BitNonGrayscale();
38 CPPUNIT_TEST_SUITE(JpegWriterTest
);
39 CPPUNIT_TEST(testWrite8BitGrayscale
);
40 CPPUNIT_TEST(testWrite8BitNonGrayscale
);
41 CPPUNIT_TEST_SUITE_END();
44 BitmapEx
JpegWriterTest::load(const OUString
& aURL
)
46 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
48 SvFileStream
aFileStream(aURL
, StreamMode::READ
);
49 ErrCode bResult
= rFilter
.ImportGraphic(aGraphic
, aURL
, aFileStream
);
50 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, bResult
);
51 return aGraphic
.GetBitmapEx();
54 BitmapEx
JpegWriterTest::roundtripJPG(const OUString
& aURL
) { return roundtripJPG(load(aURL
)); }
56 BitmapEx
JpegWriterTest::roundtripJPG(const BitmapEx
& bitmap
)
59 SvMemoryStream aStream
;
60 GraphicFilter
& rFilter
= GraphicFilter::GetGraphicFilter();
61 sal_uInt16 exportFormatJPG
= rFilter
.GetExportFormatNumberForShortName(JPG_SHORTNAME
);
62 Graphic
aExportGraphic(bitmap
);
63 ErrCode bResult
= rFilter
.ExportGraphic(aExportGraphic
, u
"memory", aStream
, exportFormatJPG
);
64 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, bResult
);
65 //Detect the magic bytes - we need to be sure the file is actually a JPEG
67 vcl::GraphicFormatDetector
aDetector(aStream
, "");
68 CPPUNIT_ASSERT(aDetector
.detect());
69 CPPUNIT_ASSERT(aDetector
.checkJPG());
72 Graphic aImportGraphic
;
73 sal_uInt16 importFormatJPG
= rFilter
.GetImportFormatNumberForShortName(JPG_SHORTNAME
);
74 bResult
= rFilter
.ImportGraphic(aImportGraphic
, u
"memory", aStream
, importFormatJPG
);
75 CPPUNIT_ASSERT_EQUAL(ERRCODE_NONE
, bResult
);
76 return aImportGraphic
.GetBitmapEx();
79 void JpegWriterTest::testWrite8BitGrayscale()
81 Bitmap bitmap
= roundtripJPG(getFullUrl(u
"8BitGrayscale.jpg")).GetBitmap();
82 Bitmap::ScopedReadAccess
access(bitmap
);
83 const ScanlineFormat format
= access
->GetScanlineFormat();
84 // Check that it's still 8bit grayscale.
85 CPPUNIT_ASSERT_EQUAL(ScanlineFormat::N8BitPal
, format
);
86 CPPUNIT_ASSERT(bitmap
.HasGreyPalette8Bit());
87 // Check that the content is valid.
88 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, 0));
89 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, access
->Width() - 1));
90 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(access
->Height() - 1, 0));
91 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK
),
92 access
->GetColor(access
->Height() - 1, access
->Width() - 1));
95 void JpegWriterTest::testWrite8BitNonGrayscale()
97 Bitmap bitmap
= roundtripJPG(getFullUrl(u
"8BitNonGrayscale.gif")).GetBitmap();
98 Bitmap::ScopedReadAccess
access(bitmap
);
99 const ScanlineFormat format
= access
->GetScanlineFormat();
100 // Check that it's still 8bit grayscale.
101 CPPUNIT_ASSERT_EQUAL(ScanlineFormat::N8BitPal
, format
);
102 // The original image has grayscale palette, just with entries in a different order.
103 // Do not check for grayscale 8bit, the roundtrip apparently fixes that. What's important
105 CPPUNIT_ASSERT(bitmap
.HasGreyPaletteAny());
106 // CPPUNIT_ASSERT(bitmap.HasGreyPalette8Bit());
107 // Check that the content is valid.
108 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, 0));
109 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(0, access
->Width() - 1));
110 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_WHITE
), access
->GetColor(access
->Height() - 1, 0));
111 CPPUNIT_ASSERT_EQUAL(BitmapColor(COL_BLACK
),
112 access
->GetColor(access
->Height() - 1, access
->Width() - 1));
115 CPPUNIT_TEST_SUITE_REGISTRATION(JpegWriterTest
);
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */