1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
)
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
);
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
));
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
;
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())));
78 bool QtBitmap::Create(const SalBitmap
& rSalBmp
, vcl::PixelFormat eNewPixelFormat
)
80 if (eNewPixelFormat
== vcl::PixelFormat::INVALID
)
82 const QtBitmap
* pBitmap
= static_cast<const QtBitmap
*>(&rSalBmp
);
83 m_pImage
.reset(new QImage(pBitmap
->m_pImage
->convertToFormat(getBitFormat(eNewPixelFormat
))));
87 bool QtBitmap::Create(const css::uno::Reference
<css::rendering::XBitmapCanvas
>& /*rBitmapCanvas*/,
88 Size
& /*rSize*/, bool /*bMask*/)
93 void QtBitmap::Destroy() { m_pImage
.reset(); }
95 Size
QtBitmap::GetSize() const
98 return toSize(m_pImage
->size());
102 sal_uInt16
QtBitmap::GetBitCount() const
105 return getFormatBits(m_pImage
->format());
109 BitmapBuffer
* QtBitmap::AcquireBuffer(BitmapAccessMode
/*nMode*/)
111 static const BitmapPalette aEmptyPalette
;
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
)
127 pBuffer
->mnFormat
= ScanlineFormat::N1BitMsbPal
| ScanlineFormat::TopDown
;
128 pBuffer
->maPalette
= m_aPalette
;
131 pBuffer
->mnFormat
= ScanlineFormat::N8BitPal
| ScanlineFormat::TopDown
;
132 pBuffer
->maPalette
= m_aPalette
;
135 pBuffer
->mnFormat
= ScanlineFormat::N24BitTcRgb
| ScanlineFormat::TopDown
;
136 pBuffer
->maPalette
= aEmptyPalette
;
141 pBuffer
->mnFormat
= ScanlineFormat::N32BitTcArgb
| ScanlineFormat::TopDown
;
143 pBuffer
->mnFormat
= ScanlineFormat::N32BitTcBgra
| ScanlineFormat::TopDown
;
145 pBuffer
->maPalette
= aEmptyPalette
;
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
)
164 = qRgb(m_aPalette
[i
].GetRed(), m_aPalette
[i
].GetGreen(), m_aPalette
[i
].GetBlue());
165 m_pImage
->setColorTable(std::move(aColorTable
));
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*/)
182 bool QtBitmap::Replace(const Color
& /*rSearchColor*/, const Color
& /*rReplaceColor*/,
188 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */