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()
39 basegfx::SystemDependentDataHolder(), // MM02
44 SvpSalBitmap::~SvpSalBitmap()
49 static std::unique_ptr
<BitmapBuffer
> ImplCreateDIB(
52 const BitmapPalette
& rPal
)
61 && "Unsupported BitCount!");
63 if (!rSize
.Width() || !rSize
.Height())
66 std::unique_ptr
<BitmapBuffer
> pDIB
;
70 pDIB
.reset(new BitmapBuffer
);
72 catch (const std::bad_alloc
&)
77 const sal_uInt16 nColors
= ( nBitCount
<= 8 ) ? ( 1 << nBitCount
) : 0;
82 pDIB
->mnFormat
= ScanlineFormat::N1BitLsbPal
;
85 pDIB
->mnFormat
= ScanlineFormat::N4BitMsnPal
;
88 pDIB
->mnFormat
= ScanlineFormat::N8BitPal
;
91 pDIB
->mnFormat
= SVP_24BIT_FORMAT
;
97 pDIB
->mnFormat
= SVP_CAIRO_FORMAT
;
101 pDIB
->mnFormat
|= ScanlineFormat::TopDown
;
102 pDIB
->mnWidth
= rSize
.Width();
103 pDIB
->mnHeight
= rSize
.Height();
104 tools::Long nScanlineBase
;
105 bool bFail
= o3tl::checked_multiply
<tools::Long
>(pDIB
->mnWidth
, nBitCount
, nScanlineBase
);
108 SAL_WARN("vcl.gdi", "checked multiply failed");
111 pDIB
->mnScanlineSize
= AlignedWidth4Bytes(nScanlineBase
);
112 if (pDIB
->mnScanlineSize
< nScanlineBase
/8)
114 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
117 pDIB
->mnBitCount
= nBitCount
;
121 pDIB
->maPalette
= rPal
;
122 pDIB
->maPalette
.SetEntryCount( nColors
);
126 bFail
= o3tl::checked_multiply
<size_t>(pDIB
->mnHeight
, pDIB
->mnScanlineSize
, size
);
127 SAL_WARN_IF(bFail
, "vcl.gdi", "checked multiply failed");
128 if (bFail
|| size
> SAL_MAX_INT32
/2)
135 pDIB
->mpBits
= new sal_uInt8
[size
];
136 #ifdef __SANITIZE_ADDRESS__
138 { // can only happen with ASAN allocator_may_return_null=1
144 std::memset(pDIB
->mpBits
, 0, size
);
147 catch (const std::bad_alloc
&)
155 void SvpSalBitmap::Create(std::unique_ptr
<BitmapBuffer
> pBuf
)
158 mpDIB
= std::move(pBuf
);
161 bool SvpSalBitmap::Create(const Size
& rSize
, sal_uInt16 nBitCount
, const BitmapPalette
& rPal
)
164 mpDIB
= ImplCreateDIB( rSize
, nBitCount
, rPal
);
165 return mpDIB
!= nullptr;
168 bool SvpSalBitmap::Create(const SalBitmap
& rBmp
)
172 const SvpSalBitmap
& rSalBmp
= static_cast<const SvpSalBitmap
&>(rBmp
);
176 // TODO: reference counting...
177 mpDIB
.reset(new BitmapBuffer( *rSalBmp
.mpDIB
));
179 const size_t size
= mpDIB
->mnScanlineSize
* mpDIB
->mnHeight
;
180 if (size
> SAL_MAX_INT32
/2)
186 // TODO: get rid of this when BitmapBuffer gets copy constructor
189 mpDIB
->mpBits
= new sal_uInt8
[size
];
190 std::memcpy(mpDIB
->mpBits
, rSalBmp
.mpDIB
->mpBits
, size
);
192 catch (const std::bad_alloc
&)
198 return !rSalBmp
.mpDIB
|| (mpDIB
!= nullptr);
201 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
202 SalGraphics
* /*pGraphics*/ )
207 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
208 sal_uInt16
/*nNewBitCount*/ )
213 bool SvpSalBitmap::Create( const css::uno::Reference
< css::rendering::XBitmapCanvas
>& /*xBitmapCanvas*/, Size
& /*rSize*/, bool /*bMask*/ )
218 void SvpSalBitmap::Destroy()
222 delete[] mpDIB
->mpBits
;
227 Size
SvpSalBitmap::GetSize() const
233 aSize
.setWidth( mpDIB
->mnWidth
);
234 aSize
.setHeight( mpDIB
->mnHeight
);
240 sal_uInt16
SvpSalBitmap::GetBitCount() const
242 sal_uInt16 nBitCount
;
245 nBitCount
= mpDIB
->mnBitCount
;
252 BitmapBuffer
* SvpSalBitmap::AcquireBuffer(BitmapAccessMode
)
257 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer
*, BitmapAccessMode nMode
)
259 if( nMode
== BitmapAccessMode::Write
)
260 InvalidateChecksum();
263 bool SvpSalBitmap::GetSystemData( BitmapSystemData
& )
268 bool SvpSalBitmap::ScalingSupported() const
273 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag
/*nScaleFlag*/ )
278 bool SvpSalBitmap::Replace( const ::Color
& /*rSearchColor*/, const ::Color
& /*rReplaceColor*/, sal_uInt8
/*nTol*/ )
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */