bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / headless / svpbmp.cxx
blob9c12fb91d2c14344414dc8df4fb4f5eda8ca6c30
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <cstring>
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()
43 Destroy();
46 static std::unique_ptr<BitmapBuffer> ImplCreateDIB(
47 const Size& rSize,
48 vcl::PixelFormat ePixelFormat,
49 const BitmapPalette& rPal)
51 if (!rSize.Width() || !rSize.Height())
52 return nullptr;
54 std::unique_ptr<BitmapBuffer> pDIB;
56 try
58 pDIB.reset(new BitmapBuffer);
60 catch (const std::bad_alloc&)
62 return nullptr;
65 switch (ePixelFormat)
67 case vcl::PixelFormat::N8_BPP:
68 pDIB->mnFormat = ScanlineFormat::N8BitPal;
69 break;
70 case vcl::PixelFormat::N24_BPP:
71 pDIB->mnFormat = SVP_24BIT_FORMAT;
72 break;
73 case vcl::PixelFormat::N32_BPP:
74 pDIB->mnFormat = SVP_CAIRO_FORMAT;
75 break;
76 case vcl::PixelFormat::INVALID:
77 assert(false);
78 pDIB->mnFormat = SVP_CAIRO_FORMAT;
79 break;
82 sal_uInt16 nColors = 0;
83 if (ePixelFormat <= vcl::PixelFormat::N8_BPP)
84 nColors = vcl::numberOfColors(ePixelFormat);
86 pDIB->mnFormat |= ScanlineFormat::TopDown;
87 pDIB->mnWidth = rSize.Width();
88 pDIB->mnHeight = rSize.Height();
89 tools::Long nScanlineBase;
90 bool bFail = o3tl::checked_multiply<tools::Long>(pDIB->mnWidth, vcl::pixelFormatBitCount(ePixelFormat), nScanlineBase);
91 if (bFail)
93 SAL_WARN("vcl.gdi", "checked multiply failed");
94 return nullptr;
96 pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
97 if (pDIB->mnScanlineSize < nScanlineBase/8)
99 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
100 return nullptr;
102 pDIB->mnBitCount = vcl::pixelFormatBitCount(ePixelFormat);
104 if (nColors)
106 pDIB->maPalette = rPal;
107 pDIB->maPalette.SetEntryCount( nColors );
110 size_t size;
111 bFail = o3tl::checked_multiply<size_t>(pDIB->mnHeight, pDIB->mnScanlineSize, size);
112 SAL_WARN_IF(bFail, "vcl.gdi", "checked multiply failed");
113 if (bFail || size > SAL_MAX_INT32/2)
115 return nullptr;
120 pDIB->mpBits = new sal_uInt8[size];
121 #ifdef __SANITIZE_ADDRESS__
122 if (!pDIB->mpBits)
123 { // can only happen with ASAN allocator_may_return_null=1
124 pDIB.reset();
126 else
127 #endif
129 std::memset(pDIB->mpBits, 0, size);
132 catch (const std::bad_alloc&)
134 pDIB.reset();
137 return pDIB;
140 void SvpSalBitmap::Create(std::unique_ptr<BitmapBuffer> pBuf)
142 Destroy();
143 mpDIB = std::move(pBuf);
146 bool SvpSalBitmap::Create(const Size& rSize, vcl::PixelFormat ePixelFormat, const BitmapPalette& rPal)
148 Destroy();
149 mpDIB = ImplCreateDIB(rSize, ePixelFormat, rPal);
150 return mpDIB != nullptr;
153 bool SvpSalBitmap::Create(const SalBitmap& rBmp)
155 Destroy();
157 const SvpSalBitmap& rSalBmp = static_cast<const SvpSalBitmap&>(rBmp);
159 if (rSalBmp.mpDIB)
161 // TODO: reference counting...
162 mpDIB.reset(new BitmapBuffer( *rSalBmp.mpDIB ));
164 const size_t size = mpDIB->mnScanlineSize * mpDIB->mnHeight;
165 if (size > SAL_MAX_INT32/2)
167 mpDIB.reset();
168 return false;
171 // TODO: get rid of this when BitmapBuffer gets copy constructor
174 mpDIB->mpBits = new sal_uInt8[size];
175 std::memcpy(mpDIB->mpBits, rSalBmp.mpDIB->mpBits, size);
177 catch (const std::bad_alloc&)
179 mpDIB.reset();
183 return !rSalBmp.mpDIB || (mpDIB != nullptr);
186 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
187 SalGraphics* /*pGraphics*/ )
189 return false;
192 bool SvpSalBitmap::Create(const SalBitmap& /*rSalBmp*/,
193 vcl::PixelFormat /*eNewPixelFormat*/)
195 return false;
198 bool SvpSalBitmap::Create( const css::uno::Reference< css::rendering::XBitmapCanvas >& /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
200 return false;
203 void SvpSalBitmap::Destroy()
205 if (mpDIB)
207 delete[] mpDIB->mpBits;
208 mpDIB.reset();
212 Size SvpSalBitmap::GetSize() const
214 Size aSize;
216 if (mpDIB)
218 aSize.setWidth( mpDIB->mnWidth );
219 aSize.setHeight( mpDIB->mnHeight );
222 return aSize;
225 sal_uInt16 SvpSalBitmap::GetBitCount() const
227 sal_uInt16 nBitCount;
229 if (mpDIB)
230 nBitCount = mpDIB->mnBitCount;
231 else
232 nBitCount = 0;
234 return nBitCount;
237 BitmapBuffer* SvpSalBitmap::AcquireBuffer(BitmapAccessMode)
239 return mpDIB.get();
242 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer*, BitmapAccessMode nMode)
244 if( nMode == BitmapAccessMode::Write )
245 InvalidateChecksum();
248 bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
250 return false;
253 bool SvpSalBitmap::ScalingSupported() const
255 return false;
258 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag /*nScaleFlag*/ )
260 return false;
263 bool SvpSalBitmap::Replace( const ::Color& /*rSearchColor*/, const ::Color& /*rReplaceColor*/, sal_uInt8 /*nTol*/ )
265 return false;
268 const basegfx::SystemDependentDataHolder* SvpSalBitmap::accessSystemDependentDataHolder() const
270 return this;
273 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */