Files
odfstudio/include/vcl/BitmapTools.hxx
T
Karthik b7b2913bdc tdf#172730: Add a fast scanline paths for premultiplied 32-bit bitmaps
Since BitmapEx was unified into a single premultiplied 32-bit BGRA
Bitmap, converting such a bitmap runs a per-pixel loop that builds a
BitmapColor and dispatches through the `GetPixelFromData` /
`SetPixelOnData`, (un)premultiplying one channel at a time.

Add a fast scanline path for the common case, a premultiplied 32-bit
truecolor bitmap (ABGR/ARGB/BGRA/RGBA). It resolves each channel's
byte offset once and then moves the scanline bytes directly, using the
premultiply / unpremultiply lookup tables.

Signed-off-by: Karthik <karthik.godha@collabora.com>
Change-Id: Ie8ee47055bdffca9e06d48955fdc0b61b34e08ac
Reviewed-on: https://gerrit.collaboraoffice.com/c/online/+/6752
Reviewed-by: Michael Stahl <michael.stahl@collabora.com>
Tested-by: Jenkins CPCI <releng@collaboraoffice.com>
(cherry picked from commit 54c68922eaa2d2c4e20e10091ad4911e8d32aed2)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/208641
Tested-by: Jenkins
2026-07-30 18:08:49 +02:00

129 lines
4.8 KiB
C++

/* -*- 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 <config_cairo_canvas.h>
#include <config_wasm_strip.h>
#include <tools/long.hxx>
#include <vcl/ImageTree.hxx>
#if ENABLE_CAIRO_CANVAS
#include <vcl/cairo.hxx>
#endif
#include <vcl/Scanline.hxx>
#include <basegfx/range/b2drectangle.hxx>
#include <array>
#include <optional>
class AlphaMask;
class BitmapColor;
class Color;
class Point;
class Size;
class SvStream;
namespace basegfx { class B2DHomMatrix; }
namespace basegfx { class B2DPolyPolygon; }
namespace com::sun::star::geometry { struct IntegerRectangle2D; }
namespace com::sun::star::uno { template <class E> class Sequence; }
namespace vcl::bitmap { class RawBitmap; }
namespace vcl::bitmap {
#if !ENABLE_WASM_STRIP_PREMULTIPLY
typedef std::array<std::array<sal_uInt8, 256>, 256> lookup_table;
lookup_table const & get_premultiply_table();
VCL_DLLPUBLIC lookup_table const & get_unpremultiply_table();
#endif
VCL_DLLPUBLIC sal_uInt8 unpremultiply(sal_uInt8 c, sal_uInt8 a);
VCL_DLLPUBLIC sal_uInt8 premultiply(sal_uInt8 c, sal_uInt8 a);
/** Byte offsets channels within a 4-byte pixel. */
struct ScanlineChannelOffsets
{
sal_uInt8 nBlue;
sal_uInt8 nGreen;
sal_uInt8 nRed;
sal_uInt8 nAlpha;
};
/** For a premultiplied 32-bit truecolor scanline format (ABGR/ARGB/BGRA/RGBA), return the byte offsets */
VCL_DLLPUBLIC std::optional<ScanlineChannelOffsets>
get32BitTcChannelOffsets(ScanlineFormat eFormat);
Bitmap VCL_DLLPUBLIC loadFromName(const OUString& rFileName, const ImageLoadFlags eFlags = ImageLoadFlags::NONE);
void loadFromSvg(SvStream& rStream, const OUString& sPath, Bitmap& rBitmap, double fScaleFactor);
/** Copy block of image data into the bitmap.
Assumes that the Bitmap has been constructed with the desired size.
@param pData
The block of data to copy
@param nStride
The number of bytes in a scanline, must be >= (width * bitcount / 8)
*/
Bitmap VCL_DLLPUBLIC CreateFromData(sal_uInt8 const *pData,
sal_Int32 nWidth, sal_Int32 nHeight, sal_Int32 nStride,
sal_Int8 nBitsPerPixel,
bool bReversColors = false, bool bReverseAlpha = false);
#if defined(_WIN32) && !USE_HEADLESS_CODE
void VCL_DLLPUBLIC fillWithData(sal_uInt8* pData, Bitmap const& rBitmap);
#endif
Bitmap VCL_DLLPUBLIC CreateFromData( RawBitmap && data );
#if ENABLE_CAIRO_CANVAS
VCL_DLLPUBLIC Bitmap CreateFromCairoSurface(Size size, cairo_surface_t* pSurface);
#endif
VCL_DLLPUBLIC ::Bitmap CanvasTransformBitmap( const ::Bitmap& rBitmap,
const ::basegfx::B2DHomMatrix& rTransform,
::basegfx::B2DRectangle const & rDestRect,
::basegfx::B2DHomMatrix const & rLocalTransform );
VCL_DLLPUBLIC void DrawAlphaBitmapAndAlphaGradient(Bitmap & rBitmap, bool bFixedTransparence, float fTransparence, const AlphaMask & rNewMask);
VCL_DLLPUBLIC void DrawAndClipBitmap(const Point& rPos, const Size& rSize, const Bitmap& rBitmap, Bitmap & aBmp, basegfx::B2DPolyPolygon const & rClipPath);
VCL_DLLPUBLIC css::uno::Sequence< sal_Int8 > GetMaskDIB(Bitmap const & aBmp);
/**
* @param data will be filled with alpha data, if xBitmap is alpha/transparent image
* @param bHasAlpha will be set to true if resulting surface has alpha
**/
VCL_DLLPUBLIC void CanvasCairoExtractBitmapData( Bitmap const & rBitmap, unsigned char*& data, bool& bHasAlpha, tools::Long& rnWidth, tools::Long& rnHeight );
VCL_DLLPUBLIC css::uno::Sequence< sal_Int8 > CanvasExtractBitmapData(Bitmap const & rBitmap, const css::geometry::IntegerRectangle2D& rect);
// helper to construct historical 8x8 bitmaps with two colors
Bitmap VCL_DLLPUBLIC createHistorical8x8FromArray(std::array<sal_uInt8,64> const & pArray, Color aColorPix, Color aColorBack);
bool VCL_DLLPUBLIC isHistorical8x8(const Bitmap& rBitmap, Color& o_rBack, Color& o_rFront);
/** Retrieve downsampled and cropped bitmap
Takes destination size in twips units.
@attention This method ignores negative rDstSz values, thus
mirroring must happen outside this method (e.g. in DrawBitmap)
*/
VCL_DLLPUBLIC Bitmap GetDownsampledBitmap(Size const& rDstSizeTwip, Point const& rSrcPt, Size const& rSrcSz,
Bitmap const& rBmp, tools::Long nMaxBmpDPIX, tools::Long nMaxBmpDPIY);
BitmapColor premultiply(const BitmapColor c);
BitmapColor unpremultiply(const BitmapColor c);
} // end vcl::bitmap
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */