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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
21 #include <sal/log.hxx>
24 #include <vcl/cairo.hxx>
25 #include <headless/svpbmp.hxx>
26 #include <o3tl/safeint.hxx>
27 #include <tools/helpers.hxx>
28 #include <vcl/bitmap.hxx>
30 using namespace basegfx
;
32 SvpSalBitmap::SvpSalBitmap()
36 SvpSalBitmap::~SvpSalBitmap()
41 static std::optional
<BitmapBuffer
> ImplCreateDIB(
43 vcl::PixelFormat ePixelFormat
,
44 const BitmapPalette
& rPal
,
47 if (!rSize
.Width() || !rSize
.Height())
50 std::optional
<BitmapBuffer
> pDIB(std::in_place
);
54 case vcl::PixelFormat::N8_BPP
:
55 pDIB
->meFormat
= ScanlineFormat::N8BitPal
;
57 case vcl::PixelFormat::N24_BPP
:
58 pDIB
->meFormat
= SVP_24BIT_FORMAT
;
60 case vcl::PixelFormat::N32_BPP
:
61 pDIB
->meFormat
= SVP_CAIRO_FORMAT
;
63 case vcl::PixelFormat::INVALID
:
65 pDIB
->meFormat
= SVP_CAIRO_FORMAT
;
69 sal_uInt16 nColors
= 0;
70 if (ePixelFormat
<= vcl::PixelFormat::N8_BPP
)
71 nColors
= vcl::numberOfColors(ePixelFormat
);
73 pDIB
->meDirection
= ScanlineDirection::TopDown
;
74 pDIB
->mnWidth
= rSize
.Width();
75 pDIB
->mnHeight
= rSize
.Height();
76 tools::Long nScanlineBase
;
77 bool bFail
= o3tl::checked_multiply
<tools::Long
>(pDIB
->mnWidth
, vcl::pixelFormatBitCount(ePixelFormat
), nScanlineBase
);
80 SAL_WARN("vcl.gdi", "checked multiply failed");
83 pDIB
->mnScanlineSize
= AlignedWidth4Bytes(nScanlineBase
);
84 if (pDIB
->mnScanlineSize
< nScanlineBase
/8)
86 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
89 pDIB
->mnBitCount
= vcl::pixelFormatBitCount(ePixelFormat
);
93 pDIB
->maPalette
= rPal
;
94 pDIB
->maPalette
.SetEntryCount( nColors
);
98 bFail
= o3tl::checked_multiply
<size_t>(pDIB
->mnHeight
, pDIB
->mnScanlineSize
, size
);
99 SAL_WARN_IF(bFail
, "vcl.gdi", "checked multiply failed");
100 if (bFail
|| size
> SAL_MAX_INT32
/2)
107 pDIB
->mpBits
= new sal_uInt8
[size
];
108 #ifdef __SANITIZE_ADDRESS__
110 { // can only happen with ASAN allocator_may_return_null=1
117 std::memset(pDIB
->mpBits
, 0, size
);
120 catch (const std::bad_alloc
&)
128 void SvpSalBitmap::Create(const std::optional
<BitmapBuffer
>& pBuf
)
134 bool SvpSalBitmap::ImplCreate(const Size
& rSize
, vcl::PixelFormat ePixelFormat
,
135 const BitmapPalette
& rPal
, bool bClear
)
138 moDIB
= ImplCreateDIB(rSize
, ePixelFormat
, rPal
, bClear
);
139 return moDIB
.has_value();
142 bool SvpSalBitmap::Create(const Size
& rSize
, vcl::PixelFormat ePixelFormat
, const BitmapPalette
& rPal
)
144 return ImplCreate(rSize
, ePixelFormat
, rPal
, true);
147 bool SvpSalBitmap::Create(const SalBitmap
& rBmp
)
151 const SvpSalBitmap
& rSalBmp
= static_cast<const SvpSalBitmap
&>(rBmp
);
155 // TODO: reference counting...
156 moDIB
.emplace( *rSalBmp
.moDIB
);
158 const size_t size
= moDIB
->mnScanlineSize
* moDIB
->mnHeight
;
159 if (size
> SAL_MAX_INT32
/2)
165 // TODO: get rid of this when BitmapBuffer gets copy constructor
168 moDIB
->mpBits
= new sal_uInt8
[size
];
169 std::memcpy(moDIB
->mpBits
, rSalBmp
.moDIB
->mpBits
, size
);
171 catch (const std::bad_alloc
&)
177 return !rSalBmp
.moDIB
.has_value() || moDIB
.has_value();
180 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
181 SalGraphics
* /*pGraphics*/ )
186 bool SvpSalBitmap::Create(const SalBitmap
& /*rSalBmp*/,
187 vcl::PixelFormat
/*eNewPixelFormat*/)
192 bool SvpSalBitmap::Create( const css::uno::Reference
< css::rendering::XBitmapCanvas
>& /*xBitmapCanvas*/, Size
& /*rSize*/, bool /*bMask*/ )
197 void SvpSalBitmap::Destroy()
199 if (moDIB
.has_value())
201 delete[] moDIB
->mpBits
;
206 Size
SvpSalBitmap::GetSize() const
210 if (moDIB
.has_value())
212 aSize
.setWidth( moDIB
->mnWidth
);
213 aSize
.setHeight( moDIB
->mnHeight
);
219 sal_uInt16
SvpSalBitmap::GetBitCount() const
221 sal_uInt16 nBitCount
;
223 if (moDIB
.has_value())
224 nBitCount
= moDIB
->mnBitCount
;
231 BitmapBuffer
* SvpSalBitmap::AcquireBuffer(BitmapAccessMode
)
233 return moDIB
? &*moDIB
: nullptr;
236 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer
*, BitmapAccessMode nMode
)
238 if( nMode
== BitmapAccessMode::Write
)
239 InvalidateChecksum();
242 bool SvpSalBitmap::GetSystemData( BitmapSystemData
& )
247 bool SvpSalBitmap::ScalingSupported() const
252 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag
/*nScaleFlag*/ )
257 bool SvpSalBitmap::Replace( const ::Color
& /*rSearchColor*/, const ::Color
& /*rReplaceColor*/, sal_uInt8
/*nTol*/ )
262 const basegfx::SystemDependentDataHolder
* SvpSalBitmap::accessSystemDependentDataHolder() const
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */