summaryrefslogtreecommitdiff
path: root/vcl
diff options
context:
space:
mode:
authorTomaž Vajngerl <tomaz.vajngerl@collabora.co.uk>2019-05-08 22:50:19 +0900
committerTomaž Vajngerl <quikee@gmail.com>2019-05-09 07:08:16 +0200
commit09ca8648a47f02c77c26ac6257a51bff62a7bf72 (patch)
tree809021e1df0305e9bc98dd053a0816166af6e5c4 /vcl
parent865371a35d7f9d9fac08e9e75ac3c9c173144746 (diff)
vcl: Add ScanlineTransformer and make tests
This is intended to replace the ImplPixelFormat in macOS and OpenGL backends, which are duplicated. It'll also come in handy for other situations in the future. Also ImplPixelFormat4 (4-bit Palette) in the macOS backend doesn't work and never has worked correctly (bug at writing). This fact was discovered while testing the ScanlineTranformer, which was based on the ImplPixelFormat* code. Change-Id: I96a172bd61c2cc116ab3e8b9f47f23f945243e96 Reviewed-on: https://gerrit.libreoffice.org/71992 Tested-by: Jenkins Reviewed-by: Tomaž Vajngerl <quikee@gmail.com>
Diffstat (limited to 'vcl')
-rw-r--r--vcl/CppunitTest_vcl_bitmap_test.mk1
-rw-r--r--vcl/inc/bitmap/ScanlineTools.hxx265
-rw-r--r--vcl/qa/cppunit/ScanlineToolsTest.cxx268
3 files changed, 534 insertions, 0 deletions
diff --git a/vcl/CppunitTest_vcl_bitmap_test.mk b/vcl/CppunitTest_vcl_bitmap_test.mk
index 3bb293cfc66d..714b390cba93 100644
--- a/vcl/CppunitTest_vcl_bitmap_test.mk
+++ b/vcl/CppunitTest_vcl_bitmap_test.mk
@@ -13,6 +13,7 @@ $(eval $(call gb_CppunitTest_add_exception_objects,vcl_bitmap_test, \
vcl/qa/cppunit/BitmapTest \
vcl/qa/cppunit/BitmapExTest \
vcl/qa/cppunit/bitmapcolor \
+ vcl/qa/cppunit/ScanlineToolsTest \
))
$(eval $(call gb_CppunitTest_use_externals,vcl_bitmap_test,\
diff --git a/vcl/inc/bitmap/ScanlineTools.hxx b/vcl/inc/bitmap/ScanlineTools.hxx
new file mode 100644
index 000000000000..e87bded7e2d6
--- /dev/null
+++ b/vcl/inc/bitmap/ScanlineTools.hxx
@@ -0,0 +1,265 @@
+/* -*- 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/.
+ *
+ */
+
+#ifndef INCLUDED_VCL_INC_BITMAP_SCANLINETOOLS_HXX
+#define INCLUDED_VCL_INC_BITMAP_SCANLINETOOLS_HXX
+
+#include <tools/color.hxx>
+#include <vcl/BitmapPalette.hxx>
+
+namespace vcl::bitmap
+{
+class ScanlineTransformer
+{
+public:
+ virtual void startLine(sal_uInt8* pLine) = 0;
+ virtual void skipPixel(sal_uInt32 nPixel) = 0;
+ virtual Color readPixel() = 0;
+ virtual void writePixel(Color nColor) = 0;
+
+ virtual ~ScanlineTransformer() = default;
+};
+
+class ScanlineTransformer_ARGB : public ScanlineTransformer
+{
+private:
+ sal_uInt8* pData;
+
+public:
+ virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
+
+ virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel << 2; }
+
+ virtual Color readPixel() override
+ {
+ const Color aColor = Color(pData[4], pData[1], pData[2], pData[3]);
+ pData += 4;
+ return aColor;
+ }
+
+ virtual void writePixel(Color nColor) override
+ {
+ *pData++ = nColor.GetTransparency();
+ *pData++ = nColor.GetRed();
+ *pData++ = nColor.GetGreen();
+ *pData++ = nColor.GetBlue();
+ }
+};
+
+class ScanlineTransformer_BGR : public ScanlineTransformer
+{
+private:
+ sal_uInt8* pData;
+
+public:
+ virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
+
+ virtual void skipPixel(sal_uInt32 nPixel) override { pData += (nPixel << 1) + nPixel; }
+
+ virtual Color readPixel() override
+ {
+ const Color aColor = Color(pData[2], pData[1], pData[0]);
+ pData += 3;
+ return aColor;
+ }
+
+ virtual void writePixel(Color nColor) override
+ {
+ *pData++ = nColor.GetBlue();
+ *pData++ = nColor.GetGreen();
+ *pData++ = nColor.GetRed();
+ }
+};
+
+class ScanlineTransformer_RGB565 : public ScanlineTransformer
+{
+private:
+ sal_uInt16* pData;
+
+public:
+ virtual void startLine(sal_uInt8* pLine) override
+ {
+ pData = reinterpret_cast<sal_uInt16*>(pLine);
+ }
+
+ virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel; }
+
+ virtual Color readPixel() override
+ {
+ const Color nColor
+ = Color((*pData & 0xf800) >> 8, (*pData & 0x07e0) >> 3, (*pData & 0x001f) << 3);
+ pData++;
+ return nColor;
+ }
+
+ virtual void writePixel(Color nColor) override
+ {
+ *pData++ = ((nColor.GetRed() & 0xf8) << 8) | ((nColor.GetGreen() & 0xfc) << 3)
+ | ((nColor.GetBlue() & 0xf8) >> 3);
+ }
+};
+
+class ScanlineTransformer_8BitPalette : public ScanlineTransformer
+{
+private:
+ sal_uInt8* pData;
+ const BitmapPalette& mrPalette;
+
+public:
+ explicit ScanlineTransformer_8BitPalette(const BitmapPalette& rPalette)
+ : pData(nullptr)
+ , mrPalette(rPalette)
+ {
+ }
+
+ virtual void startLine(sal_uInt8* pLine) override { pData = pLine; }
+
+ virtual void skipPixel(sal_uInt32 nPixel) override { pData += nPixel; }
+
+ virtual Color readPixel() override
+ {
+ const sal_uInt8 nIndex(*pData++);
+ if (nIndex < mrPalette.GetEntryCount())
+ return mrPalette[nIndex].GetColor();
+ else
+ return COL_BLACK;
+ }
+
+ virtual void writePixel(Color nColor) override
+ {
+ *pData++ = static_cast<sal_uInt8>(mrPalette.GetBestIndex(nColor));
+ }
+};
+
+class ScanlineTransformer_4BitPalette : public ScanlineTransformer
+{
+private:
+ sal_uInt8* pData;
+ const BitmapPalette& mrPalette;
+ sal_uInt32 mnX;
+ sal_uInt32 mnShift;
+
+public:
+ explicit ScanlineTransformer_4BitPalette(const BitmapPalette& rPalette)
+ : pData(nullptr)
+ , mrPalette(rPalette)
+ , mnX(0)
+ , mnShift(0)
+ {
+ }
+
+ virtual void skipPixel(sal_uInt32 nPixel) override
+ {
+ mnX += nPixel;
+ if (nPixel & 1) // is nPixel an odd number
+ mnShift ^= 4;
+ }
+
+ virtual void startLine(sal_uInt8* pLine) override
+ {
+ pData = pLine;
+ mnX = 0;
+ mnShift = 4;
+ }
+
+ virtual Color readPixel() override
+ {
+ const sal_uInt32 nDataIndex = mnX / 2;
+ const sal_uInt8 nIndex((pData[nDataIndex] >> mnShift) & 0x0f);
+ mnX++;
+ mnShift ^= 4;
+
+ if (nIndex < mrPalette.GetEntryCount())
+ return mrPalette[nIndex].GetColor();
+ else
+ return COL_BLACK;
+ }
+
+ virtual void writePixel(Color nColor) override
+ {
+ const sal_uInt32 nDataIndex = mnX / 2;
+ const sal_uInt8 nColorIndex = mrPalette.GetBestIndex(nColor);
+ pData[nDataIndex] |= (nColorIndex & 0x0f) << mnShift;
+ mnX++;
+ mnShift ^= 4;
+ }
+};
+
+class ScanlineTransformer_1BitPalette : public ScanlineTransformer
+{
+private:
+ sal_uInt8* pData;
+ const BitmapPalette& mrPalette;
+ sal_uInt32 mnX;
+
+public:
+ explicit ScanlineTransformer_1BitPalette(const BitmapPalette& rPalette)
+ : pData(nullptr)
+ , mrPalette(rPalette)
+ , mnX(0)
+ {
+ }
+
+ virtual void skipPixel(sal_uInt32 nPixel) override { mnX += nPixel; }
+
+ virtual void startLine(sal_uInt8* pLine) override
+ {
+ pData = pLine;
+ mnX = 0;
+ }
+
+ virtual Color readPixel() override
+ {
+ const sal_uInt8 nIndex((pData[mnX >> 3] >> (7 - (mnX & 7))) & 1);
+ mnX++;
+
+ if (nIndex < mrPalette.GetEntryCount())
+ return mrPalette[nIndex].GetColor();
+ else
+ return COL_BLACK;
+ }
+
+ virtual void writePixel(Color nColor) override
+ {
+ if (mrPalette.GetBestIndex(nColor) & 1)
+ pData[mnX >> 3] |= 1 << (7 - (mnX & 7));
+ else
+ pData[mnX >> 3] &= ~(1 << (7 - (mnX & 7)));
+ mnX++;
+ }
+};
+
+std::unique_ptr<ScanlineTransformer> getScanlineTransformer(sal_uInt16 nBits,
+ const BitmapPalette& rPalette)
+{
+ switch (nBits)
+ {
+ case 1:
+ return std::make_unique<ScanlineTransformer_1BitPalette>(rPalette);
+ case 4:
+ return std::make_unique<ScanlineTransformer_4BitPalette>(rPalette);
+ case 8:
+ return std::make_unique<ScanlineTransformer_8BitPalette>(rPalette);
+ case 16:
+ return std::make_unique<ScanlineTransformer_RGB565>();
+ case 24:
+ return std::make_unique<ScanlineTransformer_BGR>();
+ case 32:
+ return std::make_unique<ScanlineTransformer_ARGB>();
+ default:
+ break;
+ }
+ return nullptr;
+}
+}
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/vcl/qa/cppunit/ScanlineToolsTest.cxx b/vcl/qa/cppunit/ScanlineToolsTest.cxx
new file mode 100644
index 000000000000..b0a979b21bd0
--- /dev/null
+++ b/vcl/qa/cppunit/ScanlineToolsTest.cxx
@@ -0,0 +1,268 @@
+/* -*- 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 <cppunit/TestAssert.h>
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+#include <cppunit/plugin/TestPlugIn.h>
+
+#include <bitmap/ScanlineTools.hxx>
+
+namespace
+{
+class ScanlineToolsTest : public CppUnit::TestFixture
+{
+ void ScanlineTransformer_32_ARGB();
+ void ScanlineTransformer_24_BGR();
+ void ScanlineTransformer_16_RGB565();
+ void ScanlineTransformer_8bit_Palette();
+ void ScanlineTransformer_4bit_Palette();
+ void ScanlineTransformer_1bit_Palette();
+
+ CPPUNIT_TEST_SUITE(ScanlineToolsTest);
+ CPPUNIT_TEST(ScanlineTransformer_32_ARGB);
+ CPPUNIT_TEST(ScanlineTransformer_24_BGR);
+ CPPUNIT_TEST(ScanlineTransformer_16_RGB565);
+ CPPUNIT_TEST(ScanlineTransformer_8bit_Palette);
+ CPPUNIT_TEST(ScanlineTransformer_4bit_Palette);
+ CPPUNIT_TEST(ScanlineTransformer_1bit_Palette);
+ CPPUNIT_TEST_SUITE_END();
+};
+
+void ScanlineToolsTest::ScanlineTransformer_32_ARGB()
+{
+ BitmapPalette aPalette;
+ std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer
+ = vcl::bitmap::getScanlineTransformer(32, aPalette);
+
+ std::vector<sal_uInt8> aScanLine(5 * 4, 0); // 5 * 4 BytesPerPixel
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ std::vector<Color> aColors{
+ Color(0, 10, 250, 120), Color(50, 30, 230, 110), Color(100, 50, 210, 100),
+ Color(150, 70, 190, 90), Color(200, 90, 170, 80),
+ };
+
+ for (Color const& aColor : aColors)
+ {
+ pScanlineTransformer->writePixel(aColor);
+ }
+
+ std::vector<sal_uInt8> aExpectedBytes{ 0, 10, 250, 120, 50, 30, 230, 110, 100, 50,
+ 210, 100, 150, 70, 190, 90, 200, 90, 170, 80 };
+
+ for (size_t i = 0; i < aScanLine.size(); ++i)
+ {
+ CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i]));
+ }
+}
+
+void ScanlineToolsTest::ScanlineTransformer_24_BGR()
+{
+ BitmapPalette aPalette;
+ std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer
+ = vcl::bitmap::getScanlineTransformer(24, aPalette);
+
+ std::vector<sal_uInt8> aScanLine(5 * 3, 0); // 5 * 3 BytesPerPixel
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ std::vector<Color> aColors{
+ Color(0, 10, 250, 120), Color(50, 30, 230, 110), Color(100, 50, 210, 100),
+ Color(150, 70, 190, 90), Color(200, 90, 170, 80),
+ };
+
+ for (Color const& aColor : aColors)
+ {
+ pScanlineTransformer->writePixel(aColor);
+ }
+
+ std::vector<sal_uInt8> aExpectedBytes{ 120, 250, 10, 110, 230, 30, 100, 210,
+ 50, 90, 190, 70, 80, 170, 90 };
+
+ for (size_t i = 0; i < aScanLine.size(); ++i)
+ {
+ CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i]));
+ }
+}
+
+void ScanlineToolsTest::ScanlineTransformer_16_RGB565()
+
+{
+ BitmapPalette aPalette;
+ std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer
+ = vcl::bitmap::getScanlineTransformer(16, aPalette);
+
+ std::vector<sal_uInt8> aScanLine(5 * 2, 0); // 5 * 2 BytesPerPixel
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ std::vector<Color> aColors{
+ Color(0, 10, 250, 120), Color(50, 30, 230, 110), Color(100, 50, 210, 100),
+ Color(150, 70, 190, 90), Color(200, 90, 170, 80),
+ };
+
+ for (Color const& aColor : aColors)
+ {
+ pScanlineTransformer->writePixel(aColor);
+ }
+
+ std::vector<sal_uInt8> aExpectedBytes{ 207, 15, 45, 31, 140, 54, 235, 69, 74, 93 };
+
+ for (size_t i = 0; i < aScanLine.size(); ++i)
+ {
+ CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i]));
+ }
+
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ std::vector<Color> aExpectedColors{
+ Color(8, 248, 120), Color(24, 228, 104), Color(48, 208, 96),
+ Color(64, 188, 88), Color(88, 168, 80),
+ };
+
+ for (size_t i = 0; i < aExpectedColors.size(); ++i)
+ {
+ Color aColor = pScanlineTransformer->readPixel();
+ CPPUNIT_ASSERT_EQUAL(aExpectedColors[i], aColor);
+ }
+}
+
+void ScanlineToolsTest::ScanlineTransformer_8bit_Palette()
+{
+ std::vector<Color> aColors{
+ Color(0, 10, 250, 120), Color(50, 30, 230, 110), Color(100, 50, 210, 100),
+ Color(150, 70, 190, 90), Color(200, 90, 170, 80),
+ };
+
+ BitmapPalette aPalette(256);
+ for (size_t i = 0; i < aColors.size(); ++i)
+ aPalette[i] = aColors[i];
+
+ std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer
+ = vcl::bitmap::getScanlineTransformer(8, aPalette);
+
+ std::vector<sal_uInt8> aScanLine(5, 0); // 5 * 1 BytesPerPixel
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ for (Color const& aColor : aColors)
+ {
+ pScanlineTransformer->writePixel(aColor);
+ }
+
+ std::vector<sal_uInt8> aExpectedBytes{ 0, 1, 2, 3, 4 };
+
+ for (size_t i = 0; i < aScanLine.size(); ++i)
+ {
+ CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i]));
+ }
+
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ for (size_t i = 0; i < aColors.size(); ++i)
+ {
+ Color aColor = pScanlineTransformer->readPixel();
+ CPPUNIT_ASSERT_EQUAL(aColors[i], aColor);
+ }
+}
+
+void ScanlineToolsTest::ScanlineTransformer_4bit_Palette()
+{
+ std::vector<Color> aColors{
+ Color(10, 250, 120), Color(30, 230, 110), Color(50, 210, 100),
+ Color(70, 190, 90), Color(90, 170, 80), Color(110, 150, 70),
+ };
+
+ BitmapPalette aPalette(16);
+ for (size_t i = 0; i < aColors.size(); ++i)
+ {
+ aPalette[i] = aColors[i];
+ }
+
+ std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer
+ = vcl::bitmap::getScanlineTransformer(4, aPalette);
+
+ std::vector<sal_uInt8> aScanLine(3, 0); // 6 * 0.5 BytesPerPixel
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ for (Color const& aColor : aColors)
+ {
+ pScanlineTransformer->writePixel(aColor);
+ }
+
+ std::vector<sal_uInt8> aExpectedBytes{ 0x01, 0x23, 0x45 };
+
+ for (size_t i = 0; i < aScanLine.size(); ++i)
+ {
+ CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i]));
+ }
+
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ for (size_t i = 0; i < aColors.size(); ++i)
+ {
+ Color aColor = pScanlineTransformer->readPixel();
+ CPPUNIT_ASSERT_EQUAL(aColors[i], aColor);
+ }
+}
+
+void ScanlineToolsTest::ScanlineTransformer_1bit_Palette()
+{
+ std::vector<Color> aColors{
+ Color(10, 250, 120), Color(30, 230, 110), Color(50, 210, 100), Color(70, 190, 90),
+ Color(90, 170, 80), Color(110, 150, 70), Color(130, 130, 60), Color(150, 110, 50),
+ Color(170, 90, 40), Color(190, 70, 30), Color(210, 50, 20), Color(230, 30, 10),
+ Color(250, 10, 0),
+ };
+
+ BitmapPalette aPalette(2);
+ aPalette[0] = Color(10, 250, 120);
+ aPalette[1] = Color(110, 150, 70);
+
+ std::unique_ptr<vcl::bitmap::ScanlineTransformer> pScanlineTransformer
+ = vcl::bitmap::getScanlineTransformer(1, aPalette);
+
+ std::vector<sal_uInt8> aScanLine(2, 0); // 13 * 1/8 BytesPerPixel
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ for (Color const& aColor : aColors)
+ {
+ pScanlineTransformer->writePixel(aColor);
+ }
+
+ std::vector<sal_uInt8> aExpectedBytes{
+ // We expect 3x index 0 and 10x index 1 => 000 111111111
+ 0x1f, // 0001 1111
+ 0xf8 // 1111 1000
+ };
+
+ for (size_t i = 0; i < aScanLine.size(); ++i)
+ {
+ CPPUNIT_ASSERT_EQUAL(int(aExpectedBytes[i]), int(aScanLine[i]));
+ }
+
+ pScanlineTransformer->startLine(aScanLine.data());
+
+ std::vector<Color> aColorsExpected{
+ Color(10, 250, 120), Color(10, 250, 120), Color(10, 250, 120), Color(110, 150, 70),
+ Color(110, 150, 70), Color(110, 150, 70), Color(110, 150, 70), Color(110, 150, 70),
+ Color(110, 150, 70), Color(110, 150, 70), Color(110, 150, 70), Color(110, 150, 70),
+ Color(110, 150, 70),
+ };
+
+ for (size_t i = 0; i < aColors.size(); ++i)
+ {
+ Color aColor = pScanlineTransformer->readPixel();
+ CPPUNIT_ASSERT_EQUAL(aColorsExpected[i], aColor);
+ }
+}
+
+} // namespace
+
+CPPUNIT_TEST_SUITE_REGISTRATION(ScanlineToolsTest);
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab: */