uno grid grid a11y: Return existing cell accessible
[LibreOffice.git] / vcl / headless / svpbmp.cxx
blob23a0d094d00936ee0339026f74b3a5edc1ea42c0
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>
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()
38 Destroy();
41 static std::optional<BitmapBuffer> ImplCreateDIB(
42 const Size& rSize,
43 vcl::PixelFormat ePixelFormat,
44 const BitmapPalette& rPal,
45 bool bClear)
47 if (!rSize.Width() || !rSize.Height())
48 return std::nullopt;
50 std::optional<BitmapBuffer> pDIB(std::in_place);
52 switch (ePixelFormat)
54 case vcl::PixelFormat::N8_BPP:
55 pDIB->meFormat = ScanlineFormat::N8BitPal;
56 break;
57 case vcl::PixelFormat::N24_BPP:
58 pDIB->meFormat = SVP_24BIT_FORMAT;
59 break;
60 case vcl::PixelFormat::N32_BPP:
61 pDIB->meFormat = SVP_CAIRO_FORMAT;
62 break;
63 case vcl::PixelFormat::INVALID:
64 assert(false);
65 pDIB->meFormat = SVP_CAIRO_FORMAT;
66 break;
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);
78 if (bFail)
80 SAL_WARN("vcl.gdi", "checked multiply failed");
81 return std::nullopt;
83 pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
84 if (pDIB->mnScanlineSize < nScanlineBase/8)
86 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
87 return std::nullopt;
89 pDIB->mnBitCount = vcl::pixelFormatBitCount(ePixelFormat);
91 if (nColors)
93 pDIB->maPalette = rPal;
94 pDIB->maPalette.SetEntryCount( nColors );
97 size_t size;
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)
102 return std::nullopt;
107 pDIB->mpBits = new sal_uInt8[size];
108 #ifdef __SANITIZE_ADDRESS__
109 if (!pDIB->mpBits)
110 { // can only happen with ASAN allocator_may_return_null=1
111 pDIB.reset();
113 else
114 #endif
115 if (bClear)
117 std::memset(pDIB->mpBits, 0, size);
120 catch (const std::bad_alloc&)
122 pDIB.reset();
125 return pDIB;
128 void SvpSalBitmap::Create(const std::optional<BitmapBuffer>& pBuf)
130 Destroy();
131 moDIB = pBuf;
134 bool SvpSalBitmap::ImplCreate(const Size& rSize, vcl::PixelFormat ePixelFormat,
135 const BitmapPalette& rPal, bool bClear)
137 Destroy();
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)
149 Destroy();
151 const SvpSalBitmap& rSalBmp = static_cast<const SvpSalBitmap&>(rBmp);
153 if (rSalBmp.moDIB)
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)
161 moDIB.reset();
162 return false;
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&)
173 moDIB.reset();
177 return !rSalBmp.moDIB.has_value() || moDIB.has_value();
180 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
181 SalGraphics* /*pGraphics*/ )
183 return false;
186 bool SvpSalBitmap::Create(const SalBitmap& /*rSalBmp*/,
187 vcl::PixelFormat /*eNewPixelFormat*/)
189 return false;
192 bool SvpSalBitmap::Create( const css::uno::Reference< css::rendering::XBitmapCanvas >& /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
194 return false;
197 void SvpSalBitmap::Destroy()
199 if (moDIB.has_value())
201 delete[] moDIB->mpBits;
202 moDIB.reset();
206 Size SvpSalBitmap::GetSize() const
208 Size aSize;
210 if (moDIB.has_value())
212 aSize.setWidth( moDIB->mnWidth );
213 aSize.setHeight( moDIB->mnHeight );
216 return aSize;
219 sal_uInt16 SvpSalBitmap::GetBitCount() const
221 sal_uInt16 nBitCount;
223 if (moDIB.has_value())
224 nBitCount = moDIB->mnBitCount;
225 else
226 nBitCount = 0;
228 return nBitCount;
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& )
244 return false;
247 bool SvpSalBitmap::ScalingSupported() const
249 return false;
252 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag /*nScaleFlag*/ )
254 return false;
257 bool SvpSalBitmap::Replace( const ::Color& /*rSearchColor*/, const ::Color& /*rReplaceColor*/, sal_uInt8 /*nTol*/ )
259 return false;
262 const basegfx::SystemDependentDataHolder* SvpSalBitmap::accessSystemDependentDataHolder() const
264 return this;
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */