tdf#130857 qt weld: Implement QtInstanceWidget::strip_mnemonic
[LibreOffice.git] / vcl / qt5 / QtBitmap.cxx
blob7e24dd703a84c68712d86acce4b081e7e6af1467
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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::N8_BPP)
42 assert(256 >= rPal.GetEntryCount());
44 m_pImage.reset(new QImage(toQSize(rSize), getBitFormat(ePixelFormat)));
45 m_pImage->fill(Qt::transparent);
46 m_aPalette = rPal;
48 auto count = rPal.GetEntryCount();
49 if (count && m_pImage)
51 QVector<QRgb> aColorTable(count);
52 for (unsigned i = 0; i < count; ++i)
53 aColorTable[i] = qRgb(rPal[i].GetRed(), rPal[i].GetGreen(), rPal[i].GetBlue());
54 m_pImage->setColorTable(std::move(aColorTable));
56 return true;
59 bool QtBitmap::Create(const SalBitmap& rSalBmp)
61 const QtBitmap* pBitmap = static_cast<const QtBitmap*>(&rSalBmp);
62 m_pImage.reset(new QImage(*pBitmap->m_pImage));
63 m_aPalette = pBitmap->m_aPalette;
64 return true;
67 bool QtBitmap::Create(const SalBitmap& rSalBmp, SalGraphics* pSalGraphics)
69 const QtBitmap* pBitmap = static_cast<const QtBitmap*>(&rSalBmp);
70 QtGraphics* pGraphics = static_cast<QtGraphics*>(pSalGraphics);
71 QImage* pImage = pGraphics->getQImage();
72 m_pImage.reset(new QImage(pBitmap->m_pImage->convertToFormat(pImage->format())));
73 return true;
76 bool QtBitmap::Create(const SalBitmap& rSalBmp, vcl::PixelFormat eNewPixelFormat)
78 if (eNewPixelFormat == vcl::PixelFormat::INVALID)
79 return false;
80 const QtBitmap* pBitmap = static_cast<const QtBitmap*>(&rSalBmp);
81 m_pImage.reset(new QImage(pBitmap->m_pImage->convertToFormat(getBitFormat(eNewPixelFormat))));
82 return true;
85 bool QtBitmap::Create(const css::uno::Reference<css::rendering::XBitmapCanvas>& /*rBitmapCanvas*/,
86 Size& /*rSize*/, bool /*bMask*/)
88 return false;
91 void QtBitmap::Destroy() { m_pImage.reset(); }
93 Size QtBitmap::GetSize() const
95 if (m_pImage)
96 return toSize(m_pImage->size());
97 return Size();
100 sal_uInt16 QtBitmap::GetBitCount() const
102 if (m_pImage)
103 return getFormatBits(m_pImage->format());
104 return 0;
107 BitmapBuffer* QtBitmap::AcquireBuffer(BitmapAccessMode /*nMode*/)
109 static const BitmapPalette aEmptyPalette;
111 if (!m_pImage)
112 return nullptr;
114 BitmapBuffer* pBuffer = new BitmapBuffer;
116 pBuffer->mnWidth = m_pImage->width();
117 pBuffer->mnHeight = m_pImage->height();
118 pBuffer->mnBitCount = getFormatBits(m_pImage->format());
119 pBuffer->mpBits = m_pImage->bits();
120 pBuffer->mnScanlineSize = m_pImage->bytesPerLine();
121 pBuffer->meDirection = ScanlineDirection::TopDown;
123 switch (pBuffer->mnBitCount)
125 case 1:
126 pBuffer->meFormat = ScanlineFormat::N1BitMsbPal;
127 pBuffer->maPalette = m_aPalette;
128 break;
129 case 8:
130 pBuffer->meFormat = ScanlineFormat::N8BitPal;
131 pBuffer->maPalette = m_aPalette;
132 break;
133 case 24:
134 pBuffer->meFormat = ScanlineFormat::N24BitTcRgb;
135 pBuffer->maPalette = aEmptyPalette;
136 break;
137 case 32:
139 #ifdef OSL_BIGENDIAN
140 pBuffer->meFormat = ScanlineFormat::N32BitTcArgb;
141 #else
142 pBuffer->meFormat = ScanlineFormat::N32BitTcBgra;
143 #endif
144 pBuffer->maPalette = aEmptyPalette;
145 break;
147 default:
148 assert(false);
151 return pBuffer;
154 void QtBitmap::ReleaseBuffer(BitmapBuffer* pBuffer, BitmapAccessMode nMode)
156 m_aPalette = pBuffer->maPalette;
157 auto count = m_aPalette.GetEntryCount();
158 if (pBuffer->mnBitCount != 4 && count)
160 QVector<QRgb> aColorTable(count);
161 for (unsigned i = 0; i < count; ++i)
162 aColorTable[i]
163 = qRgb(m_aPalette[i].GetRed(), m_aPalette[i].GetGreen(), m_aPalette[i].GetBlue());
164 m_pImage->setColorTable(std::move(aColorTable));
166 delete pBuffer;
167 if (nMode == BitmapAccessMode::Write)
168 InvalidateChecksum();
171 bool QtBitmap::GetSystemData(BitmapSystemData& /*rData*/) { return false; }
173 bool QtBitmap::ScalingSupported() const { return false; }
175 bool QtBitmap::Scale(const double& /*rScaleX*/, const double& /*rScaleY*/,
176 BmpScaleFlag /*nScaleFlag*/)
178 return false;
181 bool QtBitmap::Replace(const Color& /*rSearchColor*/, const Color& /*rReplaceColor*/,
182 sal_uInt8 /*nTol*/)
184 return false;
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */