diff options
author | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2020-08-29 11:44:21 +0200 |
---|---|---|
committer | Tomaž Vajngerl <tomaz.vajngerl@collabora.co.uk> | 2024-02-06 11:14:54 +0900 |
commit | 7598c5d3fc296530ceeb8ffb35654da1b479616f (patch) | |
tree | 7dbe4a4c3f846bacc082ba504fd02f18a2a90423 | |
parent | vcl: add Bitmap 24+8 to 32 bit conversion tool (diff) | |
download | core-7598c5d3fc296530ceeb8ffb35654da1b479616f.tar.gz core-7598c5d3fc296530ceeb8ffb35654da1b479616f.zip |
Combo image down-scaler and HalfScaler
Change-Id: I2c422f983e378cff47c5534f0f2774ffb41e2a25
-rw-r--r-- | include/vcl/bitmap.hxx | 3 | ||||
-rw-r--r-- | vcl/Library_vcl.mk | 2 | ||||
-rw-r--r-- | vcl/inc/BitmapComboScaleFilter.hxx | 52 | ||||
-rw-r--r-- | vcl/inc/bitmap/ScanlineHalfScaler.hxx | 87 | ||||
-rw-r--r-- | vcl/source/bitmap/BitmapComboScaleFilter.cxx | 370 | ||||
-rw-r--r-- | vcl/source/bitmap/ScanlineHalfScaler.cxx | 1356 | ||||
-rw-r--r-- | vcl/source/bitmap/bitmap.cxx | 5 |
7 files changed, 1874 insertions, 1 deletions
diff --git a/include/vcl/bitmap.hxx b/include/vcl/bitmap.hxx index 8ce5d9cf74fa..69ee04c90214 100644 --- a/include/vcl/bitmap.hxx +++ b/include/vcl/bitmap.hxx @@ -65,7 +65,8 @@ enum class BmpScaleFlag Interpolate, // fast, integer bilinear Lanczos, BiCubic, - BiLinear + BiLinear, + Combo }; #define BMP_COL_TRANS Color( 252, 3, 251 ) diff --git a/vcl/Library_vcl.mk b/vcl/Library_vcl.mk index d4c72bfe9c0d..2ae224c3f533 100644 --- a/vcl/Library_vcl.mk +++ b/vcl/Library_vcl.mk @@ -365,6 +365,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/bitmap/BitmapSeparableUnsharpenFilter \ vcl/source/bitmap/BitmapFastScaleFilter \ vcl/source/bitmap/BitmapScaleSuperFilter \ + vcl/source/bitmap/BitmapComboScaleFilter \ vcl/source/bitmap/BitmapScaleConvolutionFilter \ vcl/source/bitmap/BitmapSymmetryCheck \ vcl/source/bitmap/BitmapColorQuantizationFilter \ @@ -372,6 +373,7 @@ $(eval $(call gb_Library_add_exception_objects,vcl,\ vcl/source/bitmap/BitmapTools \ vcl/source/bitmap/Octree \ vcl/source/bitmap/salbmp \ + vcl/source/bitmap/ScanlineHalfScaler \ vcl/source/image/Image \ vcl/source/image/ImageTree \ vcl/source/image/ImageRepository \ diff --git a/vcl/inc/BitmapComboScaleFilter.hxx b/vcl/inc/BitmapComboScaleFilter.hxx new file mode 100644 index 000000000000..b70254b96c59 --- /dev/null +++ b/vcl/inc/BitmapComboScaleFilter.hxx @@ -0,0 +1,52 @@ +/* -*- 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_BITMAPCOMBOSCALEFILTER_HXX +#define INCLUDED_VCL_BITMAPCOMBOSCALEFILTER_HXX + +#include <vcl/BitmapFilter.hxx> + +#include <comphelper/threadpool.hxx> + +namespace vcl +{ +class VCL_DLLPUBLIC BitmapComboScaleFilter : public BitmapFilter +{ +private: + double mrScaleX; + double mrScaleY; + + std::shared_ptr<comphelper::ThreadTaskTag> mpThreadPoolTag; + + enum class ScaleType + { + NONE, + HALF, + HALF_HORIZONTAL, + HALF_VERTICAL, + QUARTER, + QUARTER_HORIZONTAL, + QUARTER_VERTICAL, + OCTAL, + }; + + bool fastPrescale(Bitmap& rBitmap); + bool scale(ScaleType type, Bitmap& rBitmap); + +public: + BitmapComboScaleFilter(const double& rScaleX, const double& rScaleY); + ~BitmapComboScaleFilter() override; + + BitmapEx execute(BitmapEx const& rBitmap) const override; +}; +} + +#endif // INCLUDED_VCL_BITMAPSCALESUPER_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/inc/bitmap/ScanlineHalfScaler.hxx b/vcl/inc/bitmap/ScanlineHalfScaler.hxx new file mode 100644 index 000000000000..1566324183c6 --- /dev/null +++ b/vcl/inc/bitmap/ScanlineHalfScaler.hxx @@ -0,0 +1,87 @@ +/* -*- 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/. + * + */ + +#pragma once + +#include <vcl/BitmapWriteAccess.hxx> + +#if defined(_MSC_VER) +#define VCL_FORCEINLINE __forceinline +#elif defined(__GNUC__) +#define VCL_FORCEINLINE __attribute__((always_inline)) inline +#else +#define VCL_FORCEINLINE inline +#endif + +namespace vcl +{ +struct VCL_DLLPUBLIC ScaleContext +{ + BitmapReadAccess const* const mpSource; + BitmapWriteAccess* mpTarget; + long mnSourceW; + long mnSourceH; + long mnTargetW; + long mnTargetH; + bool mbHMirr; + bool mbVMirr; + + VCL_FORCEINLINE Scanline getSourceScanline(long y) { return mpSource->GetScanline(y); } + + VCL_FORCEINLINE Scanline getTargetScanline(long y) { return mpTarget->GetScanline(y); } + + VCL_FORCEINLINE BitmapColor getSourcePixel(Scanline const& pScanline, long x) + { + return mpSource->GetPixelFromData(pScanline, x); + } + + VCL_FORCEINLINE void setTargetPixel(Scanline const& pScanline, long x, BitmapColor& rColor) + { + return mpTarget->SetPixelOnData(pScanline, x, rColor); + } + + ScaleContext(BitmapReadAccess* pSource, BitmapWriteAccess* pTarget, long nSourceW, + long nTargetW, long nSourceH, long nTargetH, bool bHMirr, bool bVMirr) + : mpSource(pSource) + , mpTarget(pTarget) + , mnSourceW(nSourceW) + , mnSourceH(nSourceH) + , mnTargetW(nTargetW) + , mnTargetH(nTargetH) + , mbHMirr(bHMirr) + , mbVMirr(bVMirr) + { + } +}; + +void scaleHalfGeneralHorizontal(ScaleContext& rContext, long nStartY, long nEndY); +void scaleHalfGeneralVertical(ScaleContext& rContext, long nStartY, long nEndY); +void scaleHalfGeneral(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarterGeneral(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarterGeneralHorizontal(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarterGeneralVertical(ScaleContext& rContext, long nStartY, long nEndY); +void scaleOctalGeneral(ScaleContext& rContext, long nStartY, long nEndY); + +void scaleOctal32(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarter32_1(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarter32_2(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarter32_SSE(ScaleContext& rContext, long nStartY, long nEndY); +void scaleHalf32_SSE2(ScaleContext& rContext, long nStartY, long nEndY); +void scaleHalf32_2(ScaleContext& rContext, long nStartY, long nEndY); +void scaleHalf32_1(ScaleContext& rContext, long nStartY, long nEndY); + +void scaleHalf24(ScaleContext& rContext, long nStartY, long nEndY); +void scaleHalf32(ScaleContext& rContext, long nStartY, long nEndY); + +void scaleOctal24(ScaleContext& rContext, long nStartY, long nEndY); +void scaleQuarter24(ScaleContext& rContext, long nStartY, long nEndY); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/bitmap/BitmapComboScaleFilter.cxx b/vcl/source/bitmap/BitmapComboScaleFilter.cxx new file mode 100644 index 000000000000..b88678badd1e --- /dev/null +++ b/vcl/source/bitmap/BitmapComboScaleFilter.cxx @@ -0,0 +1,370 @@ +/* -*- 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/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +#include <BitmapComboScaleFilter.hxx> + +#include <vcl/BitmapWriteAccess.hxx> +#include <bitmap/BitmapScaleSuperFilter.hxx> + +#include <tools/helpers.hxx> +#include <algorithm> +#include <memory> +#include <sal/log.hxx> +#include <tools/ScopedNanoTimer.hxx> +#include <tools/cpuid.hxx> + +#include <bitmap/ScanlineHalfScaler.hxx> + +namespace vcl +{ +namespace +{ +constexpr long constScaleThreadStrip = 4; + +constexpr bool ALLOW_HORIZONTAL_VERTICAL = false; + +typedef void (*ScaleRangeFn)(ScaleContext& rContext, long nStartY, long nEndY); + +class ScaleTask : public comphelper::ThreadTask +{ + ScaleRangeFn const mpScaleRangeFunction; + ScaleContext& mrContext; + const long mnStartY; + const long mnEndY; + +public: + explicit ScaleTask(const std::shared_ptr<comphelper::ThreadTaskTag>& pTag, + ScaleRangeFn pScaleRangeFunction, ScaleContext& rContext, long nStartY, + long nEndY) + : comphelper::ThreadTask(pTag) + , mpScaleRangeFunction(pScaleRangeFunction) + , mrContext(rContext) + , mnStartY(nStartY) + , mnEndY(nEndY) + { + } + + virtual void doWork() override { mpScaleRangeFunction(mrContext, mnStartY, mnEndY); } +}; + +} // anonymous namespace + +BitmapComboScaleFilter::BitmapComboScaleFilter(const double& rScaleX, const double& rScaleY) + : mrScaleX(rScaleX) + , mrScaleY(rScaleY) + , mpThreadPoolTag(comphelper::ThreadPool::createThreadTaskTag()) +{ +} + +BitmapComboScaleFilter::~BitmapComboScaleFilter() {} + +bool BitmapComboScaleFilter::scale(ScaleType eType, Bitmap& rBitmap) +{ + if (eType == ScaleType::NONE) + return false; + + bool bResult = false; + + const Size aSizePix(rBitmap.GetSizePixel()); + + bool bMirrorHorizontal = mrScaleX < 0; + bool bMirrorVertical = mrScaleY < 0; + + double nScaleX = 0.0; + double nScaleY = 0.0; + + switch (eType) + { + case ScaleType::OCTAL: + nScaleX = 0.125; + nScaleY = 0.125; + break; + case ScaleType::QUARTER: + nScaleX = 0.25; + nScaleY = 0.25; + break; + case ScaleType::QUARTER_HORIZONTAL: + nScaleX = 0.25; + nScaleY = 1.0; + break; + case ScaleType::QUARTER_VERTICAL: + nScaleX = 1.0; + nScaleY = 0.25; + break; + case ScaleType::HALF: + nScaleX = 0.5; + nScaleY = 0.5; + break; + case ScaleType::HALF_HORIZONTAL: + nScaleX = 0.5; + nScaleY = 1.0; + break; + case ScaleType::HALF_VERTICAL: + nScaleX = 1.0; + nScaleY = 0.5; + break; + case ScaleType::NONE: + break; + } + + const Size aDestinationSize(std::lround(aSizePix.Width() * nScaleX), + std::lround(aSizePix.Height() * nScaleY)); + + if (aDestinationSize.Width() == aSizePix.Width() + && aDestinationSize.Height() == aSizePix.Height()) + { + return true; + } + + if (aDestinationSize.Width() <= 1L || aDestinationSize.Height() <= 1L) + { + return false; + } + + auto eSourcePixelFormat = rBitmap.getPixelFormat(); + auto ePixelFormat = eSourcePixelFormat; + if (sal_uInt16(eSourcePixelFormat) < 24) + ePixelFormat = vcl::PixelFormat::N24_BPP; + + Bitmap aOutBmp(aDestinationSize, ePixelFormat); + Size aOutSize = aOutBmp.GetSizePixel(); + + const long nStartY = 0; + const long nEndY = aDestinationSize.Height() - 1; + + if (!aOutSize.Width() || !aOutSize.Height()) + { + SAL_WARN("vcl.gdi", "bmp creation failed"); + return false; + } + + { + BitmapScopedReadAccess pReadAccess(rBitmap); + BitmapScopedWriteAccess pWriteAccess(aOutBmp); + + if (pReadAccess && pWriteAccess) + { + ScaleRangeFn pScaleRangeFn; + ScaleContext aContext(pReadAccess.get(), pWriteAccess.get(), pReadAccess->Width(), + pWriteAccess->Width(), pReadAccess->Height(), + pWriteAccess->Height(), bMirrorVertical, bMirrorHorizontal); + + switch (eType) + { + case ScaleType::OCTAL: + { + switch (pReadAccess->GetScanlineFormat()) + { + case ScanlineFormat::N24BitTcBgr: + case ScanlineFormat::N24BitTcRgb: + pScaleRangeFn = scaleOctal24; + break; + case ScanlineFormat::N32BitTcRgba: + case ScanlineFormat::N32BitTcBgra: + case ScanlineFormat::N32BitTcArgb: + case ScanlineFormat::N32BitTcAbgr: + pScaleRangeFn = scaleOctal32; + break; + default: + pScaleRangeFn = scaleOctalGeneral; + break; + } + + break; + } + case ScaleType::QUARTER: + switch (pReadAccess->GetScanlineFormat()) + { + case ScanlineFormat::N24BitTcBgr: + case ScanlineFormat::N24BitTcRgb: + pScaleRangeFn = scaleQuarter24; + break; + case ScanlineFormat::N32BitTcRgba: + case ScanlineFormat::N32BitTcBgra: + case ScanlineFormat::N32BitTcArgb: + case ScanlineFormat::N32BitTcAbgr: + pScaleRangeFn = scaleQuarter32_SSE; + break; + default: + pScaleRangeFn = scaleQuarterGeneral; + break; + } + + break; + case ScaleType::QUARTER_HORIZONTAL: + pScaleRangeFn = scaleQuarterGeneralHorizontal; + break; + case ScaleType::QUARTER_VERTICAL: + pScaleRangeFn = scaleQuarterGeneralVertical; + break; + case ScaleType::HALF: + switch (pReadAccess->GetScanlineFormat()) + { + case ScanlineFormat::N24BitTcBgr: + case ScanlineFormat::N24BitTcRgb: + pScaleRangeFn = scaleHalf24; + break; + case ScanlineFormat::N32BitTcRgba: + case ScanlineFormat::N32BitTcBgra: + case ScanlineFormat::N32BitTcArgb: + case ScanlineFormat::N32BitTcAbgr: + pScaleRangeFn = scaleHalf32_SSE2; + break; + default: + pScaleRangeFn = scaleHalfGeneral; + break; + } + + break; + case ScaleType::HALF_HORIZONTAL: + pScaleRangeFn = scaleHalfGeneralHorizontal; + break; + case ScaleType::HALF_VERTICAL: + pScaleRangeFn = scaleHalfGeneralVertical; + break; + default: + return false; + } + + bool bUseThreads = true; + + static bool bDisableThreadedScaling = getenv("VCL_NO_THREAD_SCALE"); + if (bDisableThreadedScaling) + { + SAL_INFO("vcl.gdi", "Scale in main thread"); + bUseThreads = false; + } + + if (bUseThreads) + { + try + { + comphelper::ThreadPool& rShared + = comphelper::ThreadPool::getSharedOptimalPool(); + // partition and queue work + long nStripYStart = nStartY; + long nStripYEnd = nStripYStart + constScaleThreadStrip - 1; + + while (nStripYEnd < nEndY) + { + std::unique_ptr<ScaleTask> pTask(new ScaleTask( + mpThreadPoolTag, pScaleRangeFn, aContext, nStripYStart, nStripYEnd)); + rShared.pushTask(std::move(pTask)); + nStripYStart += constScaleThreadStrip; + nStripYEnd += constScaleThreadStrip; + } + if (nStripYStart <= nEndY) + { + std::unique_ptr<ScaleTask> pTask(new ScaleTask( + mpThreadPoolTag, pScaleRangeFn, aContext, nStripYStart, nEndY)); + rShared.pushTask(std::move(pTask)); + } + rShared.waitUntilDone(mpThreadPoolTag); + SAL_INFO("vcl.gdi", "All threaded scaling tasks complete"); + } + catch (...) + { + SAL_WARN("vcl.gdi", "threaded bitmap scaling failed"); + bUseThreads = false; + } + } + + if (!bUseThreads) + pScaleRangeFn(aContext, nStartY, nEndY); + + bResult = true; + } + } + + if (bResult) + { + rBitmap = aOutBmp; + } + return bResult; +} + +bool BitmapComboScaleFilter::fastPrescale(Bitmap& rBitmap) +{ + const Size aSize(rBitmap.GetSizePixel()); + + const long nDestinationWidth = std::lround(aSize.Width() * std::fabs(mrScaleX)); + const long nDestinationHeight = std::lround(aSize.Height() * std::fabs(mrScaleY)); + + bool bResult = false; + ScaleType eType = ScaleType::NONE; + if (mrScaleX <= 0.125 && mrScaleY <= 0.125) + { + eType = ScaleType::OCTAL; + } + else if (mrScaleX <= 0.25 && mrScaleY <= 0.25) + { + eType = ScaleType::QUARTER; + } + else if (mrScaleX <= 0.25 && ALLOW_HORIZONTAL_VERTICAL) + { + eType = ScaleType::QUARTER_HORIZONTAL; + } + else if (mrScaleY <= 0.25 && ALLOW_HORIZONTAL_VERTICAL) + { + eType = ScaleType::QUARTER_VERTICAL; + } + else if (mrScaleX <= 0.5 && mrScaleY <= 0.5) + { + eType = ScaleType::HALF; + } + else if (mrScaleX <= 0.5 && ALLOW_HORIZONTAL_VERTICAL) + { + eType = ScaleType::HALF_HORIZONTAL; + } + else if (mrScaleY <= 0.5 && ALLOW_HORIZONTAL_VERTICAL) + { + eType = ScaleType::HALF_VERTICAL; + } + + bResult = scale(eType, rBitmap); + + if (bResult) + { + const Size aNewSize(rBitmap.GetSizePixel()); + mrScaleX = nDestinationWidth / double(aNewSize.Width()); + mrScaleY = nDestinationHeight / double(aNewSize.Height()); + printf("Combo: %ld %ld -> %ld %ld\n", aSize.Width(), aSize.Height(), aNewSize.Width(), + aNewSize.Height()); + } + + return bResult; +} + +BitmapEx BitmapComboScaleFilter::execute(BitmapEx const& rBitmapEx) const +{ + Bitmap aBitmap(rBitmapEx.GetBitmap()); + + while (const_cast<BitmapComboScaleFilter*>(this)->fastPrescale(aBitmap)) + ; + + BitmapEx aBitmapEx(aBitmap); + BitmapScaleSuperFilter aScaleSuper(mrScaleX, mrScaleY); + BitmapEx aResult = aScaleSuper.execute(aBitmapEx); + + return aResult; +} + +} // end namespace vcl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/bitmap/ScanlineHalfScaler.cxx b/vcl/source/bitmap/ScanlineHalfScaler.cxx new file mode 100644 index 000000000000..5f6b323cf056 --- /dev/null +++ b/vcl/source/bitmap/ScanlineHalfScaler.cxx @@ -0,0 +1,1356 @@ +/* -*- 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 <bitmap/ScanlineHalfScaler.hxx> + +#include <tools/helpers.hxx> +#include <algorithm> +#include <memory> +#include <tools/cpuid.hxx> +#include <tools/simdsupport.hxx> + +#if defined(LO_SSE2_AVAILABLE) +#include <emmintrin.h> +#endif + +namespace vcl +{ +namespace +{ +inline sal_uInt32 Avg2x2(sal_uInt32 a, sal_uInt32 b, sal_uInt32 c, sal_uInt32 d) +{ + // Prepare half-adder work + sal_uInt32 sum = a ^ b ^ c; + sal_uInt32 carry = (a & b) | (a & c) | (b & c); + + // Before shifting, mask lower order bits of each byte to avoid underflow. + sal_uInt32 mask = 0xfefefefe; + + // Add d to sum and divide by 2. + sum = (((sum ^ d) & mask) >> 1) + (sum & d); + + // Sum is now shifted into place relative to carry, add them together. + return (((sum ^ carry) & mask) >> 1) + (sum & carry); +} +} + +void scaleHalfGeneralHorizontal(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + Scanline pSourceScanline = rContext.getSourceScanline(nY); + Scanline pTargetScanline = rContext.getTargetScanline(nY); + + long nTargetX = 0; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nSourceX = nX * 2; + + BitmapColor aColor0 = rContext.getSourcePixel(pSourceScanline, nSourceX); + BitmapColor aColor1 = rContext.getSourcePixel(pSourceScanline, nSourceX + 1); + + BitmapColor aColorResult((aColor0.GetRed() + aColor1.GetRed()) / 2, + (aColor0.GetGreen() + aColor1.GetGreen()) / 2, + (aColor0.GetBlue() + aColor1.GetBlue()) / 2); + + rContext.setTargetPixel(pTargetScanline, nTargetX++, aColorResult); + } + } +} + +void scaleHalfGeneralVertical(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * 2; + + Scanline pSourceScanline0 = rContext.mpSource->GetScanline(nSourceY); + Scanline pSourceScanline1 = rContext.mpSource->GetScanline(nSourceY + 1); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + long nSourceX = nStartX; + long nTargetX = 0; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + BitmapColor aColor0 = rContext.mpSource->GetPixelFromData(pSourceScanline0, nSourceX); + BitmapColor aColor1 = rContext.mpSource->GetPixelFromData(pSourceScanline1, nSourceX); + + BitmapColor aColorResult((aColor0.GetRed() + aColor1.GetRed()) / 2, + (aColor0.GetGreen() + aColor1.GetGreen()) / 2, + (aColor0.GetBlue() + aColor1.GetBlue()) / 2); + + rContext.mpTarget->SetPixelOnData(pTargetScanline, nTargetX++, aColorResult); + } + } +} + +void scaleHalfGeneral(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + long nSourceY = nStartY * 2; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + + Scanline pScanDest = rContext.mpTarget->GetScanline(nY); + + long nTargetX = 0; + + BitmapColor aColor; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nRed = 0; + long nGreen = 0; + long nBlue = 0; + long nAlpha = 0; + + long nSourceX = nX * 2; + + aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX + 1); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX + 1); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + BitmapColor aColorResult(ColorAlpha, nRed / 4, nGreen / 4, nBlue / 4, nAlpha / 4); + + rContext.mpTarget->SetPixelOnData(pScanDest, nTargetX++, aColorResult); + } + } +} + +void scaleQuarterGeneral(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + long nSourceY = nStartY * 4; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource2 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource3 = rContext.mpSource->GetScanline(nSourceY++); + + Scanline pScanDest = rContext.mpTarget->GetScanline(nY); + + long nTargetX = 0; + + BitmapColor aColor; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nRed = 0; + long nGreen = 0; + long nBlue = 0; + long nAlpha = 0; + + long nSourceX = nX * 4; + + aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX + 1); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX + 2); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX + 3); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX + 1); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX + 2); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX + 3); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource2, nSourceX); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource2, nSourceX + 1); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource2, nSourceX + 2); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource2, nSourceX + 3); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource3, nSourceX); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource3, nSourceX + 1); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource3, nSourceX + 2); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + aColor = rContext.mpSource->GetPixelFromData(pSource3, nSourceX + 3); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + + BitmapColor aColorResult(ColorAlpha, nRed / 16, nGreen / 16, nBlue / 16, nAlpha / 16); + + rContext.mpTarget->SetPixelOnData(pScanDest, nTargetX++, aColorResult); + } + } +} + +void scaleQuarterGeneralHorizontal(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + Scanline pSourceScanline = rContext.mpSource->GetScanline(nY); + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + long nTargetX = 0; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nSourceX = nX * 4; + + BitmapColor aColor0 = rContext.mpSource->GetPixelFromData(pSourceScanline, nSourceX); + BitmapColor aColor1 + = rContext.mpSource->GetPixelFromData(pSourceScanline, nSourceX + 1); + BitmapColor aColor2 + = rContext.mpSource->GetPixelFromData(pSourceScanline, nSourceX + 2); + BitmapColor aColor3 + = rContext.mpSource->GetPixelFromData(pSourceScanline, nSourceX + 3); + + BitmapColor aColorResult( + (aColor0.GetRed() + aColor1.GetRed() + aColor2.GetRed() + aColor3.GetRed()) / 4, + (aColor0.GetGreen() + aColor1.GetGreen() + aColor2.GetGreen() + aColor3.GetGreen()) + / 4, + (aColor0.GetBlue() + aColor1.GetBlue() + aColor2.GetBlue() + aColor3.GetBlue()) + / 4); + + rContext.mpTarget->SetPixelOnData(pTargetScanline, nTargetX++, aColorResult); + } + } +} + +void scaleQuarterGeneralVertical(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * 4; + + Scanline pSourceScanline0 = rContext.mpSource->GetScanline(nSourceY); + Scanline pSourceScanline1 = rContext.mpSource->GetScanline(nSourceY + 1); + Scanline pSourceScanline2 = rContext.mpSource->GetScanline(nSourceY + 2); + Scanline pSourceScanline3 = rContext.mpSource->GetScanline(nSourceY + 3); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + long nSourceX = nStartX; + long nTargetX = 0; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + BitmapColor aColor0 = rContext.mpSource->GetPixelFromData(pSourceScanline0, nSourceX); + BitmapColor aColor1 = rContext.mpSource->GetPixelFromData(pSourceScanline1, nSourceX); + BitmapColor aColor2 = rContext.mpSource->GetPixelFromData(pSourceScanline2, nSourceX); + BitmapColor aColor3 = rContext.mpSource->GetPixelFromData(pSourceScanline3, nSourceX); + + BitmapColor aColorResult( + (aColor0.GetRed() + aColor1.GetRed() + aColor2.GetRed() + aColor3.GetRed()) / 4, + (aColor0.GetGreen() + aColor1.GetGreen() + aColor2.GetGreen() + aColor3.GetGreen()) + / 4, + (aColor0.GetBlue() + aColor1.GetBlue() + aColor2.GetBlue() + aColor3.GetBlue()) + / 4); + + rContext.mpTarget->SetPixelOnData(pTargetScanline, nTargetX++, aColorResult); + } + } +} + +void scaleOctalGeneral(ScaleContext& rContext, long nStartY, long nEndY) +{ + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * 8; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource2 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource3 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource4 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource5 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource6 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource7 = rContext.mpSource->GetScanline(nSourceY); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + long nTargetX = 0; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nRed = 0; + long nGreen = 0; + long nBlue = 0; + long nAlpha = 0; + + long nSourceX = nX * 8; + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource0, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource1, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource2, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource3, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource4, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource5, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource6, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + for (long i = 0; i < 8; i++) + { + BitmapColor aColor = rContext.mpSource->GetPixelFromData(pSource7, nSourceX + i); + nRed += aColor.GetRed(); + nGreen += aColor.GetGreen(); + nBlue += aColor.GetBlue(); + nAlpha += aColor.GetAlpha(); + } + + BitmapColor aColorResult(nRed / 64, nGreen / 64, nBlue / 64); + + rContext.mpTarget->SetPixelOnData(pTargetScanline, nTargetX++, aColorResult); + } + } +} + +void scaleOctal32(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constColorComponents = 4; + constexpr int constNumberSamples = 4; + constexpr int constNumberSamplesSquared = (constNumberSamples * constNumberSamples); + + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * constNumberSamples; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource2 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource3 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource4 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource5 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource6 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource7 = rContext.mpSource->GetScanline(nSourceY); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + Scanline pColorPtr; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nComponent1 = 0; + long nComponent2 = 0; + long nComponent3 = 0; + long nComponent4 = 0; + + long nSourceX = nX * constNumberSamples; + + pColorPtr = pSource0 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource1 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource2 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource3 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource4 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource5 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource6 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource7 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + *pTargetScanline = nComponent1 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent2 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent3 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent4 / constNumberSamplesSquared; + pTargetScanline++; + } + } +} + +void scaleQuarter32_1(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constColorComponents = 4; + constexpr int constNumberSamples = 4; + constexpr int constNumberSamplesSquared = (constNumberSamples * constNumberSamples); + + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * constNumberSamples; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource2 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource3 = rContext.mpSource->GetScanline(nSourceY); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + Scanline pColorPtr; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nComponent1 = 0; + long nComponent2 = 0; + long nComponent3 = 0; + long nComponent4 = 0; + + long nSourceX = nX * constNumberSamples; + + pColorPtr = pSource0 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource1 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource2 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource3 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + *pTargetScanline = nComponent1 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent2 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent3 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent4 / constNumberSamplesSquared; + pTargetScanline++; + } + } +} + +void scaleQuarter32_2(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constNumberSamples = 4; + + const long nSourceW = rContext.mnSourceW; + + sal_uInt32 nColor00; + sal_uInt32 nColor01; + sal_uInt32 nColor10; + sal_uInt32 nColor11; + + sal_uInt32 nA1; + sal_uInt32 nA2; + sal_uInt32 nA3; + sal_uInt32 nA4; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY1 = nY * constNumberSamples; + long nSourceY2 = nSourceY1 + 1; + long nSourceY3 = nSourceY2 + 1; + long nSourceY4 = nSourceY3 + 1; + + sal_uInt32* pSource0 + = reinterpret_cast<sal_uInt32*>(rContext.mpSource->GetScanline(nSourceY1)); + sal_uInt32* pSource1 + = reinterpret_cast<sal_uInt32*>(rContext.mpSource->GetScanline(nSourceY2)); + sal_uInt32* pSource2 + = reinterpret_cast<sal_uInt32*>(rContext.mpSource->GetScanline(nSourceY3)); + sal_uInt32* pSource3 + = reinterpret_cast<sal_uInt32*>(rContext.mpSource->GetScanline(nSourceY4)); + + sal_uInt32* pTarget = reinterpret_cast<sal_uInt32*>(rContext.mpTarget->GetScanline(nY)); + + for (long nSourceX = 0; nSourceX < nSourceW; nSourceX += constNumberSamples) + { + nColor00 = *pSource0; + pSource0++; + nColor01 = *pSource0; + pSource0++; + + nColor10 = *pSource1; + pSource1++; + nColor11 = *pSource1; + pSource1++; + + nA1 = Avg2x2(nColor00, nColor01, nColor10, nColor11); + + nColor00 = *pSource0; + pSource0++; + nColor01 = *pSource0; + pSource0++; + + nColor10 = *pSource1; + pSource1++; + nColor11 = *pSource1; + pSource1++; + + nA2 = Avg2x2(nColor00, nColor01, nColor10, nColor11); + + nColor00 = *pSource2; + pSource2++; + nColor01 = *pSource2; + pSource2++; + + nColor10 = *pSource3; + pSource3++; + nColor11 = *pSource3; + pSource3++; + + nA3 = Avg2x2(nColor00, nColor01, nColor10, nColor11); + + nColor00 = *pSource2; + pSource2++; + nColor01 = *pSource2; + pSource2++; + + nColor10 = *pSource3; + pSource3++; + nColor11 = *pSource3; + pSource3++; + + nA4 = Avg2x2(nColor00, nColor01, nColor10, nColor11); + + *pTarget = Avg2x2(nA1, nA2, nA3, nA4); + pTarget++; + } + } +} + +VCL_FORCEINLINE __m128i _mm_not_si128(__m128i arg) +{ + __m128i minusone = _mm_set1_epi32(0xffffffff); + return _mm_xor_si128(arg, minusone); +} + +VCL_FORCEINLINE __m128i avg_sse2_8x2(__m128i* a, __m128i* b, __m128i* c, __m128i* d) +{ +#define shuffle_si128(arga, argb, imm) \ + _mm_castps_si128(_mm_shuffle_ps(_mm_castsi128_ps((arga)), _mm_castsi128_ps((argb)), (imm))); + + __m128i t = shuffle_si128(*a, *b, _MM_SHUFFLE(2, 0, 2, 0)); + *b = shuffle_si128(*a, *b, _MM_SHUFFLE(3, 1, 3, 1)); + *a = t; + t = shuffle_si128(*c, *d, _MM_SHUFFLE(2, 0, 2, 0)); + *d = shuffle_si128(*c, *d, _MM_SHUFFLE(3, 1, 3, 1)); + *c = t; + +#undef shuffle_si128 + + __m128i sum = _mm_xor_si128(*a, _mm_xor_si128(*b, *c)); + + __m128i carry = _mm_or_si128(_mm_and_si128(*a, *b), + _mm_or_si128(_mm_and_si128(*a, *c), _mm_and_si128(*b, *c))); + + sum = _mm_avg_epu8(_mm_not_si128(sum), _mm_not_si128(*d)); + + return _mm_not_si128(_mm_avg_epu8(sum, _mm_not_si128(carry))); +} + +void scaleQuarter32_SSE(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constNumberSamples = 4; + + const long nSourceW = rContext.mnSourceW; + + __m128i nColor00; + __m128i nColor01; + __m128i nColor02; + __m128i nColor03; + + __m128i nColor10; + __m128i nColor11; + __m128i nColor12; + __m128i nColor13; + + __m128i nA1; + __m128i nA2; + __m128i nA3; + __m128i nA4; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY1 = nY * constNumberSamples; + long nSourceY2 = nSourceY1 + 1; + long nSourceY3 = nSourceY2 + 1; + long nSourceY4 = nSourceY3 + 1; + + __m128i* pSource0 = reinterpret_cast<__m128i*>(rContext.mpSource->GetScanline(nSourceY1)); + __m128i* pSource1 = reinterpret_cast<__m128i*>(rContext.mpSource->GetScanline(nSourceY2)); + __m128i* pSource2 = reinterpret_cast<__m128i*>(rContext.mpSource->GetScanline(nSourceY3)); + __m128i* pSource3 = reinterpret_cast<__m128i*>(rContext.mpSource->GetScanline(nSourceY4)); + + __m128i* pTarget = reinterpret_cast<__m128i*>(rContext.mpTarget->GetScanline(nY)); + + for (long nSourceX = 0; nSourceX < nSourceW; nSourceX += constNumberSamples * 4) + { + nColor00 = _mm_loadu_si128(pSource0); + pSource0++; + nColor01 = _mm_loadu_si128(pSource0); + pSource0++; + nColor02 = _mm_loadu_si128(pSource0); + pSource0++; + nColor03 = _mm_loadu_si128(pSource0); + pSource0++; + + nColor10 = _mm_loadu_si128(pSource1); + pSource1++; + nColor11 = _mm_loadu_si128(pSource1); + pSource1++; + nColor12 = _mm_loadu_si128(pSource1); + pSource1++; + nColor13 = _mm_loadu_si128(pSource1); + pSource1++; + + nA1 = avg_sse2_8x2(&nColor00, &nColor01, &nColor10, &nColor11); + nA2 = avg_sse2_8x2(&nColor02, &nColor03, &nColor12, &nColor13); + + nColor00 = _mm_loadu_si128(pSource2); + pSource2++; + nColor01 = _mm_loadu_si128(pSource2); + pSource2++; + nColor02 = _mm_loadu_si128(pSource2); + pSource2++; + nColor03 = _mm_loadu_si128(pSource2); + pSource2++; + + nColor10 = _mm_loadu_si128(pSource3); + pSource3++; + nColor11 = _mm_loadu_si128(pSource3); + pSource3++; + nColor12 = _mm_loadu_si128(pSource3); + pSource3++; + nColor13 = _mm_loadu_si128(pSource3); + pSource3++; + + nA3 = avg_sse2_8x2(&nColor00, &nColor01, &nColor10, &nColor11); + nA4 = avg_sse2_8x2(&nColor02, &nColor03, &nColor12, &nColor13); + + _mm_storeu_si128(pTarget, avg_sse2_8x2(&nA1, &nA2, &nA3, &nA4)); + pTarget++; + } + } +} + +void scaleHalf32_SSE2(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constNumberSamples = 2; + + const long nSourceW = rContext.mnSourceW; + + __m128i nColor00; + __m128i nColor01; + __m128i nColor10; + __m128i nColor11; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY1 = nY * constNumberSamples; + long nSourceY2 = nSourceY1 + 1; + + __m128i* pSource0 = reinterpret_cast<__m128i*>(rContext.mpSource->GetScanline(nSourceY1)); + __m128i* pSource1 = reinterpret_cast<__m128i*>(rContext.mpSource->GetScanline(nSourceY2)); + + __m128i* pTarget = reinterpret_cast<__m128i*>(rContext.mpTarget->GetScanline(nY)); + + for (long nSourceX = 0; nSourceX < nSourceW; nSourceX += constNumberSamples * 4) + { + nColor00 = _mm_loadu_si128(pSource0); + pSource0++; + nColor01 = _mm_loadu_si128(pSource0); + pSource0++; + nColor10 = _mm_loadu_si128(pSource1); + pSource1++; + nColor11 = _mm_loadu_si128(pSource1); + pSource1++; + + _mm_storeu_si128(pTarget, avg_sse2_8x2(&nColor00, &nColor01, &nColor10, &nColor11)); + pTarget++; + } + } +} + +void scaleHalf32_2(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constNumberSamples = 2; + + const long nSourceW = rContext.mnSourceW; + + sal_uInt32 nColor00; + sal_uInt32 nColor01; + sal_uInt32 nColor10; + sal_uInt32 nColor11; + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY1 = nY * constNumberSamples; + long nSourceY2 = nSourceY1 + 1; + + sal_uInt32* pSource0 + = reinterpret_cast<sal_uInt32*>(rContext.mpSource->GetScanline(nSourceY1)); + sal_uInt32* pSource1 + = reinterpret_cast<sal_uInt32*>(rContext.mpSource->GetScanline(nSourceY2)); + sal_uInt32* pTarget = reinterpret_cast<sal_uInt32*>(rContext.mpTarget->GetScanline(nY)); + + for (long nSourceX = 0; nSourceX < nSourceW; nSourceX += constNumberSamples) + { + nColor00 = *pSource0; + pSource0++; + nColor01 = *pSource0; + pSource0++; + nColor10 = *pSource1; + pSource1++; + nColor11 = *pSource1; + pSource1++; + + *pTarget = Avg2x2(nColor00, nColor01, nColor10, nColor11); + pTarget++; + } + } +} + +void scaleHalf32_1(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constColorComponents = 4; + constexpr int constNumberSamples = 2; + constexpr int constNumberSamplesSquared = (constNumberSamples * constNumberSamples); + + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + long nComponent1 = 0; + long nComponent2 = 0; + long nComponent3 = 0; + long nComponent4 = 0; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * constNumberSamples; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + Scanline pColorPtr; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + nComponent1 = 0; + nComponent2 = 0; + nComponent3 = 0; + nComponent4 = 0; + + long nSourceX = nX * constNumberSamples; + + pColorPtr = pSource0 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource1 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + nComponent4 += *pColorPtr; + pColorPtr++; + } + + *pTargetScanline = nComponent1 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent2 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent3 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent4 / constNumberSamplesSquared; + pTargetScanline++; + } + } +} + +template <int constColorComponents> +void scaleHalf32or24(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constNumberSamples = 2; + constexpr int constNumberSamplesSquared = constNumberSamples * constNumberSamples; + + const long nSourceW = rContext.mnSourceW; + + std::array<long, constColorComponents> aComponents; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY1 = nY * constNumberSamples; + long nSourceY2 = nSourceY1 + 1; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY1); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY2); + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + long nSourceX; + for (nSourceX = 0; nSourceX < nSourceW; nSourceX += constNumberSamples) + { + aComponents.fill(0); + long i; + long c; + + for (i = 0; i < constNumberSamples; i++) + { + for (c = 0; c < constColorComponents; c++) + { + aComponents[c] += *pSource0; + pSource0++; + } + } + + for (i = 0; i < constNumberSamples; i++) + { + for (c = 0; c < constColorComponents; c++) + { + aComponents[c] += *pSource1; + pSource1++; + } + } + + for (c = 0; c < constColorComponents; c++) + { + *pTargetScanline = aComponents[c] / constNumberSamplesSquared; + pTargetScanline++; + } + } + } +} + +void scaleHalf32(ScaleContext& rContext, long nStartY, long nEndY) +{ + scaleHalf32or24<4>(rContext, nStartY, nEndY); +} + +void scaleHalf24(ScaleContext& rContext, long nStartY, long nEndY) +{ + scaleHalf32or24<3>(rContext, nStartY, nEndY); +} + +void scaleOctal24(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constColorComponents = 3; + constexpr int constNumberSamples = 8; + constexpr int constNumberSamplesSquared = (constNumberSamples * constNumberSamples); + + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * constNumberSamples; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource2 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource3 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource4 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource5 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource6 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource7 = rContext.mpSource->GetScanline(nSourceY); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + Scanline pColorPtr; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nComponent1 = 0; + long nComponent2 = 0; + long nComponent3 = 0; + + long nSourceX = nX * constNumberSamples; + + pColorPtr = pSource0 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource1 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource2 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource3 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource4 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource5 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource6 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource7 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + *pTargetScanline = nComponent1 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent2 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent3 / constNumberSamplesSquared; + pTargetScanline++; + } + } +} + +void scaleQuarter24(ScaleContext& rContext, long nStartY, long nEndY) +{ + constexpr int constColorComponents = 3; + constexpr int constNumberSamples = 4; + constexpr int constNumberSamplesSquared = (constNumberSamples * constNumberSamples); + + const long nStartX = 0; + const long nEndX = rContext.mnTargetW - 1; + + for (long nY = nStartY; nY <= nEndY; nY++) + { + long nSourceY = nY * constNumberSamples; + + Scanline pSource0 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource1 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource2 = rContext.mpSource->GetScanline(nSourceY++); + Scanline pSource3 = rContext.mpSource->GetScanline(nSourceY++); + + Scanline pTargetScanline = rContext.mpTarget->GetScanline(nY); + + Scanline pColorPtr; + + for (long nX = nStartX; nX <= nEndX; nX++) + { + long nComponent1 = 0; + long nComponent2 = 0; + long nComponent3 = 0; + + long nSourceX = nX * constNumberSamples; + + pColorPtr = pSource0 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource1 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource2 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + pColorPtr = pSource3 + constColorComponents * nSourceX; + for (long i = 0; i < constNumberSamples; i++) + { + nComponent1 += *pColorPtr; + pColorPtr++; + nComponent2 += *pColorPtr; + pColorPtr++; + nComponent3 += *pColorPtr; + pColorPtr++; + } + + *pTargetScanline = nComponent1 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent2 / constNumberSamplesSquared; + pTargetScanline++; + *pTargetScanline = nComponent3 / constNumberSamplesSquared; + pTargetScanline++; + } + } +} + +} // namespace vcl + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/vcl/source/bitmap/bitmap.cxx b/vcl/source/bitmap/bitmap.cxx index 53b30d0b3114..4a48ff9fc5c9 100644 --- a/vcl/source/bitmap/bitmap.cxx +++ b/vcl/source/bitmap/bitmap.cxx @@ -36,6 +36,7 @@ #endif #include <vcl/BitmapMonochromeFilter.hxx> +#include <BitmapComboScaleFilter.hxx> #include <bitmap/BitmapScaleSuperFilter.hxx> #include <bitmap/BitmapScaleConvolutionFilter.hxx> #include <bitmap/BitmapFastScaleFilter.hxx> @@ -1283,6 +1284,10 @@ bool Bitmap::Scale( const double& rScaleX, const double& rScaleY, BmpScaleFlag n case BmpScaleFlag::BiLinear: bRetval = BitmapFilter::Filter(aBmpEx, vcl::BitmapScaleBilinearFilter(rScaleX, rScaleY)); break; + + case BmpScaleFlag::Combo: + bRetval = BitmapFilter::Filter(aBmpEx, vcl::BitmapComboScaleFilter(rScaleX, rScaleY)); + break; } if (bRetval) |