tdf#122471 xlsx import: pivottable error OUString, not uInt8
[LibreOffice.git] / vcl / headless / svpbmp.cxx
blob167a5165a893387c169b082a3b87a45e755c9b95
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::N1_BPP:
68 pDIB->mnFormat = ScanlineFormat::N1BitLsbPal;
69 break;
70 case vcl::PixelFormat::N8_BPP:
71 pDIB->mnFormat = ScanlineFormat::N8BitPal;
72 break;
73 case vcl::PixelFormat::N24_BPP:
74 pDIB->mnFormat = SVP_24BIT_FORMAT;
75 break;
76 case vcl::PixelFormat::N32_BPP:
77 pDIB->mnFormat = SVP_CAIRO_FORMAT;
78 break;
79 case vcl::PixelFormat::INVALID:
80 assert(false);
81 pDIB->mnFormat = SVP_CAIRO_FORMAT;
82 break;
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);
94 if (bFail)
96 SAL_WARN("vcl.gdi", "checked multiply failed");
97 return nullptr;
99 pDIB->mnScanlineSize = AlignedWidth4Bytes(nScanlineBase);
100 if (pDIB->mnScanlineSize < nScanlineBase/8)
102 SAL_WARN("vcl.gdi", "scanline calculation wraparound");
103 return nullptr;
105 pDIB->mnBitCount = vcl::pixelFormatBitCount(ePixelFormat);
107 if (nColors)
109 pDIB->maPalette = rPal;
110 pDIB->maPalette.SetEntryCount( nColors );
113 size_t size;
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)
118 return nullptr;
123 pDIB->mpBits = new sal_uInt8[size];
124 #ifdef __SANITIZE_ADDRESS__
125 if (!pDIB->mpBits)
126 { // can only happen with ASAN allocator_may_return_null=1
127 pDIB.reset();
129 else
130 #endif
132 std::memset(pDIB->mpBits, 0, size);
135 catch (const std::bad_alloc&)
137 pDIB.reset();
140 return pDIB;
143 void SvpSalBitmap::Create(std::unique_ptr<BitmapBuffer> pBuf)
145 Destroy();
146 mpDIB = std::move(pBuf);
149 bool SvpSalBitmap::Create(const Size& rSize, vcl::PixelFormat ePixelFormat, const BitmapPalette& rPal)
151 Destroy();
152 mpDIB = ImplCreateDIB(rSize, ePixelFormat, rPal);
153 return mpDIB != nullptr;
156 bool SvpSalBitmap::Create(const SalBitmap& rBmp)
158 Destroy();
160 const SvpSalBitmap& rSalBmp = static_cast<const SvpSalBitmap&>(rBmp);
162 if (rSalBmp.mpDIB)
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)
170 mpDIB.reset();
171 return false;
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&)
182 mpDIB.reset();
186 return !rSalBmp.mpDIB || (mpDIB != nullptr);
189 bool SvpSalBitmap::Create( const SalBitmap& /*rSalBmp*/,
190 SalGraphics* /*pGraphics*/ )
192 return false;
195 bool SvpSalBitmap::Create(const SalBitmap& /*rSalBmp*/,
196 vcl::PixelFormat /*eNewPixelFormat*/)
198 return false;
201 bool SvpSalBitmap::Create( const css::uno::Reference< css::rendering::XBitmapCanvas >& /*xBitmapCanvas*/, Size& /*rSize*/, bool /*bMask*/ )
203 return false;
206 void SvpSalBitmap::Destroy()
208 if (mpDIB)
210 delete[] mpDIB->mpBits;
211 mpDIB.reset();
215 Size SvpSalBitmap::GetSize() const
217 Size aSize;
219 if (mpDIB)
221 aSize.setWidth( mpDIB->mnWidth );
222 aSize.setHeight( mpDIB->mnHeight );
225 return aSize;
228 sal_uInt16 SvpSalBitmap::GetBitCount() const
230 sal_uInt16 nBitCount;
232 if (mpDIB)
233 nBitCount = mpDIB->mnBitCount;
234 else
235 nBitCount = 0;
237 return nBitCount;
240 BitmapBuffer* SvpSalBitmap::AcquireBuffer(BitmapAccessMode)
242 return mpDIB.get();
245 void SvpSalBitmap::ReleaseBuffer(BitmapBuffer*, BitmapAccessMode nMode)
247 if( nMode == BitmapAccessMode::Write )
248 InvalidateChecksum();
251 bool SvpSalBitmap::GetSystemData( BitmapSystemData& )
253 return false;
256 bool SvpSalBitmap::ScalingSupported() const
258 return false;
261 bool SvpSalBitmap::Scale( const double& /*rScaleX*/, const double& /*rScaleY*/, BmpScaleFlag /*nScaleFlag*/ )
263 return false;
266 bool SvpSalBitmap::Replace( const ::Color& /*rSearchColor*/, const ::Color& /*rReplaceColor*/, sal_uInt8 /*nTol*/ )
268 return false;
271 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */