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>
25 #include <headless/svpbmp.hxx>
26 #include <headless/svpgdi.hxx>
27 #include <headless/svpinst.hxx>
29 #include <basegfx/vector/b2ivector.hxx>
30 #include <basegfx/range/b2ibox.hxx>
31 #include <o3tl/safeint.hxx>
32 #include <tools/helpers.hxx>
33 #include <vcl/bitmap.hxx>
35 using namespace basegfx
;
37 SvpSalBitmap::SvpSalBitmap()
41 SvpSalBitmap::~SvpSalBitmap()
46 static std::optional
<BitmapBuffer
> ImplCreateDIB(
48 vcl::PixelFormat ePixelFormat
,
49 const BitmapPalette
& rPal
,
52 if (!rSize
.Width() || !rSize
.Height())
55 std::optional
<BitmapBuffer
> pDIB(std::in_place
);
59 case vcl::PixelFormat::N8_BPP
:
60 pDIB
->mnFormat
= ScanlineFormat::N8BitPal
;
62 case vcl::PixelFormat::N24_BPP
:
63 pDIB
->mnFormat
= SVP_24BIT_FORMAT
;
65 case vcl::PixelFormat::N32_BPP
:
66 pDIB
->mnFormat
= SVP_CAIRO_FORMAT
;
68 case vcl::PixelFormat::INVALID
:
70 pDIB
->mnFormat
= SVP_CAIRO_FORMAT
;
74 sal_uInt16 nColors
= 0;
75 if (ePixelFormat
<= vcl::PixelFormat::N8_BPP
)
76 nColors
= vcl::numberOfColors(ePixelFormat
);
78 pDIB
->mnFormat
|= ScanlineFormat::TopDown
;
79 pDIB
->mnWidth
= rSize
.Width();
80 pDIB
->mnHeight
= rSize
.Height();
81 tools::Long nScanlineBase
;
82 bool bFail
= o3tl::checked_multiply
<tools::Long
>(pDIB
->mnWidth
, vcl::pixelFormatBitCount(ePixelFormat
), nScanlineBase
);
85 SAL_WARN("vcl.gdi", "checked multiply failed");
88 pDIB
->mnScanlineSize
= AlignedWidth4Bytes(nScanlineBase
);
89 if (pDIB
->mnScanlineSize
< nScanlineBase
/8)
91 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
94 pDIB
->mnBitCount
= vcl::pixelFormatBitCount(ePixelFormat
);
98 pDIB
->maPalette
= rPal
;
99 pDIB
->maPalette
.SetEntryCount( nColors
);
103 bFail
= o3tl::checked_multiply
<size_t>(pDIB
->mnHeight
, pDIB
->mnScanlineSize
, size
);
104 SAL_WARN_IF(bFail
, "vcl.gdi", "checked multiply failed");
105 if (bFail
|| size
> SAL_MAX_INT32
/2)
112 pDIB
->mpBits
= new sal_uInt8
[size
];
113 #ifdef __SANITIZE_ADDRESS__
115 { // can only happen with ASAN allocator_may_return_null=1
122 std::memset(pDIB
->mpBits
, 0, size
);
125 catch (const std::bad_alloc
&)
133 void SvpSalBitmap::Create(const std::optional
<BitmapBuffer
>& pBuf
)
139 bool SvpSalBitmap::ImplCreate(const Size
& rSize
, vcl::PixelFormat ePixelFormat
,
140 const BitmapPalette
& rPal
, bool bClear
)
143 moDIB
= ImplCreateDIB(rSize
, ePixelFormat
, rPal
, bClear
);
144 return moDIB
.has_value();
147 bool SvpSalBitmap::Create(const Size
& rSize
, vcl::PixelFormat ePixelFormat
, const BitmapPalette
& rPal
)
149 return ImplCreate(rSize
, ePixelFormat
, rPal
, true);
152 bool SvpSalBitmap::Create(const SalBitmap
& rBmp
)
156 const SvpSalBitmap
& rSalBmp
= static_cast<const SvpSalBitmap
&>(rBmp
);
160 // TODO: reference counting...
161 moDIB
.emplace( *rSalBmp
.moDIB
);
163 const size_t size
= moDIB
->mnScanlineSize
* moDIB
->mnHeight
;
164 if (size
> SAL_MAX_INT32
/2)
170 // TODO: get rid of this when BitmapBuffer gets copy constructor
173 moDIB
->mpBits
= new sal_uInt8
[size
];
174 std::memcpy(moDIB
->mpBits
, rSalBmp
.moDIB
->mpBits
, size
);
176 catch (const std::bad_alloc
&)
182 return !rSalBmp
.moDIB
.has_value() || moDIB
.has_value();
185 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
186 SalGraphics
* /*pGraphics*/ )
191 bool SvpSalBitmap::Create(const SalBitmap
& /*rSalBmp*/,
192 vcl::PixelFormat
/*eNewPixelFormat*/)
197 bool SvpSalBitmap::Create( const css::uno::Reference
< css::rendering::XBitmapCanvas
>& /*xBitmapCanvas*/, Size
& /*rSize*/, bool /*bMask*/ )
202 void SvpSalBitmap::Destroy()
204 if (moDIB
.has_value())
206 delete[] moDIB
->mpBits
;
211 Size
SvpSalBitmap::GetSize() const
215 if (moDIB
.has_value())
217 aSize
.setWidth( moDIB
->mnWidth
);
218 aSize
.setHeight( moDIB
->mnHeight
);
224 sal_uInt16
SvpSalBitmap::GetBitCount() const
226 sal_uInt16 nBitCount
;
228 if (moDIB
.has_value())
229 nBitCount
= moDIB
->mnBitCount
;
236 BitmapBuffer
* SvpSalBitmap::AcquireBuffer(BitmapAccessMode
)
238 return moDIB
? &*moDIB
: nullptr;
241 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer
*, BitmapAccessMode nMode
)
243 if( nMode
== BitmapAccessMode::Write
)
244 InvalidateChecksum();
247 bool SvpSalBitmap::GetSystemData( BitmapSystemData
& )
252 bool SvpSalBitmap::ScalingSupported() const
257 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag
/*nScaleFlag*/ )
262 bool SvpSalBitmap::Replace( const ::Color
& /*rSearchColor*/, const ::Color
& /*rReplaceColor*/, sal_uInt8
/*nTol*/ )
267 const basegfx::SystemDependentDataHolder
* SvpSalBitmap::accessSystemDependentDataHolder() const
272 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */