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::unique_ptr
<BitmapBuffer
> ImplCreateDIB(
48 vcl::PixelFormat ePixelFormat
,
49 const BitmapPalette
& rPal
)
51 if (!rSize
.Width() || !rSize
.Height())
54 std::unique_ptr
<BitmapBuffer
> pDIB
;
58 pDIB
.reset(new BitmapBuffer
);
60 catch (const std::bad_alloc
&)
67 case vcl::PixelFormat::N1_BPP
:
68 pDIB
->mnFormat
= ScanlineFormat::N1BitLsbPal
;
70 case vcl::PixelFormat::N8_BPP
:
71 pDIB
->mnFormat
= ScanlineFormat::N8BitPal
;
73 case vcl::PixelFormat::N24_BPP
:
74 pDIB
->mnFormat
= SVP_24BIT_FORMAT
;
76 case vcl::PixelFormat::N32_BPP
:
77 pDIB
->mnFormat
= SVP_CAIRO_FORMAT
;
79 case vcl::PixelFormat::INVALID
:
81 pDIB
->mnFormat
= SVP_CAIRO_FORMAT
;
85 sal_uInt16 nColors
= 0;
86 if (ePixelFormat
<= vcl::PixelFormat::N8_BPP
)
87 nColors
= vcl::numberOfColors(ePixelFormat
);
89 pDIB
->mnFormat
|= ScanlineFormat::TopDown
;
90 pDIB
->mnWidth
= rSize
.Width();
91 pDIB
->mnHeight
= rSize
.Height();
92 tools::Long nScanlineBase
;
93 bool bFail
= o3tl::checked_multiply
<tools::Long
>(pDIB
->mnWidth
, vcl::pixelFormatBitCount(ePixelFormat
), nScanlineBase
);
96 SAL_WARN("vcl.gdi", "checked multiply failed");
99 pDIB
->mnScanlineSize
= AlignedWidth4Bytes(nScanlineBase
);
100 if (pDIB
->mnScanlineSize
< nScanlineBase
/8)
102 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
105 pDIB
->mnBitCount
= vcl::pixelFormatBitCount(ePixelFormat
);
109 pDIB
->maPalette
= rPal
;
110 pDIB
->maPalette
.SetEntryCount( nColors
);
114 bFail
= o3tl::checked_multiply
<size_t>(pDIB
->mnHeight
, pDIB
->mnScanlineSize
, size
);
115 SAL_WARN_IF(bFail
, "vcl.gdi", "checked multiply failed");
116 if (bFail
|| size
> SAL_MAX_INT32
/2)
123 pDIB
->mpBits
= new sal_uInt8
[size
];
124 #ifdef __SANITIZE_ADDRESS__
126 { // can only happen with ASAN allocator_may_return_null=1
132 std::memset(pDIB
->mpBits
, 0, size
);
135 catch (const std::bad_alloc
&)
143 void SvpSalBitmap::Create(std::unique_ptr
<BitmapBuffer
> pBuf
)
146 mpDIB
= std::move(pBuf
);
149 bool SvpSalBitmap::Create(const Size
& rSize
, vcl::PixelFormat ePixelFormat
, const BitmapPalette
& rPal
)
152 mpDIB
= ImplCreateDIB(rSize
, ePixelFormat
, rPal
);
153 return mpDIB
!= nullptr;
156 bool SvpSalBitmap::Create(const SalBitmap
& rBmp
)
160 const SvpSalBitmap
& rSalBmp
= static_cast<const SvpSalBitmap
&>(rBmp
);
164 // TODO: reference counting...
165 mpDIB
.reset(new BitmapBuffer( *rSalBmp
.mpDIB
));
167 const size_t size
= mpDIB
->mnScanlineSize
* mpDIB
->mnHeight
;
168 if (size
> SAL_MAX_INT32
/2)
174 // TODO: get rid of this when BitmapBuffer gets copy constructor
177 mpDIB
->mpBits
= new sal_uInt8
[size
];
178 std::memcpy(mpDIB
->mpBits
, rSalBmp
.mpDIB
->mpBits
, size
);
180 catch (const std::bad_alloc
&)
186 return !rSalBmp
.mpDIB
|| (mpDIB
!= nullptr);
189 bool SvpSalBitmap::Create( const SalBitmap
& /*rSalBmp*/,
190 SalGraphics
* /*pGraphics*/ )
195 bool SvpSalBitmap::Create(const SalBitmap
& /*rSalBmp*/,
196 vcl::PixelFormat
/*eNewPixelFormat*/)
201 bool SvpSalBitmap::Create( const css::uno::Reference
< css::rendering::XBitmapCanvas
>& /*xBitmapCanvas*/, Size
& /*rSize*/, bool /*bMask*/ )
206 void SvpSalBitmap::Destroy()
210 delete[] mpDIB
->mpBits
;
215 Size
SvpSalBitmap::GetSize() const
221 aSize
.setWidth( mpDIB
->mnWidth
);
222 aSize
.setHeight( mpDIB
->mnHeight
);
228 sal_uInt16
SvpSalBitmap::GetBitCount() const
230 sal_uInt16 nBitCount
;
233 nBitCount
= mpDIB
->mnBitCount
;
240 BitmapBuffer
* SvpSalBitmap::AcquireBuffer(BitmapAccessMode
)
245 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer
*, BitmapAccessMode nMode
)
247 if( nMode
== BitmapAccessMode::Write
)
248 InvalidateChecksum();
251 bool SvpSalBitmap::GetSystemData( BitmapSystemData
& )
256 bool SvpSalBitmap::ScalingSupported() const
261 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag
/*nScaleFlag*/ )
266 bool SvpSalBitmap::Replace( const ::Color
& /*rSearchColor*/, const ::Color
& /*rReplaceColor*/, sal_uInt8
/*nTol*/ )
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */