calc: on editing invalidation of view with different zoom is wrong
[LibreOffice.git] / vcl / qt5 / QtBitmap.cxx
blob666cb673db65496d0c27257c9e495c6bdb3a4290
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 <QtBitmap.hxx>
21 #include <QtTools.hxx>
22 #include <QtGraphics.hxx>
24 #include <QtGui/QImage>
25 #include <QtCore/QVector>
26 #include <QtGui/QColor>
28 #include <o3tl/safeint.hxx>
29 #include <sal/log.hxx>
30 #include <tools/helpers.hxx>
32 QtBitmap::QtBitmap() {}
34 QtBitmap::QtBitmap(const QImage& rImage) { m_pImage.reset(new QImage(rImage)); }
36 bool QtBitmap::Create(const Size& rSize, vcl::PixelFormat ePixelFormat, const BitmapPalette& rPal)
38 if (ePixelFormat == vcl::PixelFormat::INVALID)
39 return false;
41 if (ePixelFormat == vcl::PixelFormat::N1_BPP)
42 assert(2 >= rPal.GetEntryCount());
43 if (ePixelFormat == vcl::PixelFormat::N8_BPP)
44 assert(256 >= rPal.GetEntryCount());
46 m_pImage.reset(new QImage(toQSize(rSize), getBitFormat(ePixelFormat)));
47 m_pImage->fill(Qt::transparent);
48 m_aPalette = rPal;
50 auto count = rPal.GetEntryCount();
51 if (count && m_pImage)
53 QVector<QRgb> aColorTable(count);
54 for (unsigned i = 0; i < count; ++i)
55 aColorTable[i] = qRgb(rPal[i].GetRed(), rPal[i].GetGreen(), rPal[i].GetBlue());
56 m_pImage->setColorTable(std::move(aColorTable));
58 return true;
61 bool QtBitmap::Create(const SalBitmap& rSalBmp)
63 const QtBitmap* pBitmap = static_cast<const QtBitmap*>(&rSalBmp);
64 m_pImage.reset(new QImage(*pBitmap->m_pImage));
65 m_aPalette = pBitmap->m_aPalette;
66 return true;
69 bool QtBitmap::Create(const SalBitmap& rSalBmp, SalGraphics* pSalGraphics)
71 const QtBitmap* pBitmap = static_cast<const QtBitmap*>(&rSalBmp);
72 QtGraphics* pGraphics = static_cast<QtGraphics*>(pSalGraphics);
73 QImage* pImage = pGraphics->getQImage();
74 m_pImage.reset(new QImage(pBitmap->m_pImage->convertToFormat(pImage->format())));
75 return true;
78 bool QtBitmap::Create(const SalBitmap& rSalBmp, vcl::PixelFormat eNewPixelFormat)
80 if (eNewPixelFormat == vcl::PixelFormat::INVALID)
81 return false;
82 const QtBitmap* pBitmap = static_cast<const QtBitmap*>(&rSalBmp);
83 m_pImage.reset(new QImage(pBitmap->m_pImage->convertToFormat(getBitFormat(eNewPixelFormat))));
84 return true;
87 bool QtBitmap::Create(const css::uno::Reference<css::rendering::XBitmapCanvas>& /*rBitmapCanvas*/,
88 Size& /*rSize*/, bool /*bMask*/)
90 return false;
93 void QtBitmap::Destroy() { m_pImage.reset(); }
95 Size QtBitmap::GetSize() const
97 if (m_pImage)
98 return toSize(m_pImage->size());
99 return Size();
102 sal_uInt16 QtBitmap::GetBitCount() const
104 if (m_pImage)
105 return getFormatBits(m_pImage->format());
106 return 0;
109 BitmapBuffer* QtBitmap::AcquireBuffer(BitmapAccessMode /*nMode*/)
111 static const BitmapPalette aEmptyPalette;
113 if (!m_pImage)
114 return nullptr;
116 BitmapBuffer* pBuffer = new BitmapBuffer;
118 pBuffer->mnWidth = m_pImage->width();
119 pBuffer->mnHeight = m_pImage->height();
120 pBuffer->mnBitCount = getFormatBits(m_pImage->format());
121 pBuffer->mpBits = m_pImage->bits();
122 pBuffer->mnScanlineSize = m_pImage->bytesPerLine();
124 switch (pBuffer->mnBitCount)
126 case 1:
127 pBuffer->mnFormat = ScanlineFormat::N1BitMsbPal | ScanlineFormat::TopDown;
128 pBuffer->maPalette = m_aPalette;
129 break;
130 case 8:
131 pBuffer->mnFormat = ScanlineFormat::N8BitPal | ScanlineFormat::TopDown;
132 pBuffer->maPalette = m_aPalette;
133 break;
134 case 24:
135 pBuffer->mnFormat = ScanlineFormat::N24BitTcRgb | ScanlineFormat::TopDown;
136 pBuffer->maPalette = aEmptyPalette;
137 break;
138 case 32:
140 #ifdef OSL_BIGENDIAN
141 pBuffer->mnFormat = ScanlineFormat::N32BitTcArgb | ScanlineFormat::TopDown;
142 #else
143 pBuffer->mnFormat = ScanlineFormat::N32BitTcBgra | ScanlineFormat::TopDown;
144 #endif
145 pBuffer->maPalette = aEmptyPalette;
146 break;
148 default:
149 assert(false);
152 return pBuffer;
155 void QtBitmap::ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode)
157 m_aPalette = pBuffer->maPalette;
158 auto count = m_aPalette.GetEntryCount();
159 if (pBuffer->mnBitCount != 4 && count)
161 QVector<QRgb> aColorTable(count);
162 for (unsigned i = 0; i < count; ++i)
163 aColorTable[i]
164 = qRgb(m_aPalette[i].GetRed(), m_aPalette[i].GetGreen(), m_aPalette[i].GetBlue());
165 m_pImage->setColorTable(std::move(aColorTable));
167 delete pBuffer;
168 if (nMode == BitmapAccessMode::Write)
169 InvalidateChecksum();
172 bool QtBitmap::GetSystemData(BitmapSystemData& /*rData*/) { return false; }
174 bool QtBitmap::ScalingSupported() const { return false; }
176 bool QtBitmap::Scale(const double& /*rScaleX*/, const double& /*rScaleY*/,
177 BmpScaleFlag /*nScaleFlag*/)
179 return false;
182 bool QtBitmap::Replace(const Color& /*rSearchColor*/, const Color& /*rReplaceColor*/,
183 sal_uInt8 /*nTol*/)
185 return false;
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */