1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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::N8_BPP
)
42 assert(256 >= rPal
.GetEntryCount());
44 m_pImage
.reset(new QImage(toQSize(rSize
), getBitFormat(ePixelFormat
)));
45 m_pImage
->fill(Qt::transparent
);
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
));
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
;
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())));
76 bool QtBitmap::Create(const SalBitmap
& rSalBmp
, vcl::PixelFormat eNewPixelFormat
)
78 if (eNewPixelFormat
== vcl::PixelFormat::INVALID
)
80 const QtBitmap
* pBitmap
= static_cast<const QtBitmap
*>(&rSalBmp
);
81 m_pImage
.reset(new QImage(pBitmap
->m_pImage
->convertToFormat(getBitFormat(eNewPixelFormat
))));
85 bool QtBitmap::Create(const css::uno::Reference
<css::rendering::XBitmapCanvas
>& /*rBitmapCanvas*/,
86 Size
& /*rSize*/, bool /*bMask*/)
91 void QtBitmap::Destroy() { m_pImage
.reset(); }
93 Size
QtBitmap::GetSize() const
96 return toSize(m_pImage
->size());
100 sal_uInt16
QtBitmap::GetBitCount() const
103 return getFormatBits(m_pImage
->format());
107 BitmapBuffer
* QtBitmap::AcquireBuffer(BitmapAccessMode
/*nMode*/)
109 static const BitmapPalette aEmptyPalette
;
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
)
126 pBuffer
->meFormat
= ScanlineFormat::N1BitMsbPal
;
127 pBuffer
->maPalette
= m_aPalette
;
130 pBuffer
->meFormat
= ScanlineFormat::N8BitPal
;
131 pBuffer
->maPalette
= m_aPalette
;
134 pBuffer
->meFormat
= ScanlineFormat::N24BitTcRgb
;
135 pBuffer
->maPalette
= aEmptyPalette
;
140 pBuffer
->meFormat
= ScanlineFormat::N32BitTcArgb
;
142 pBuffer
->meFormat
= ScanlineFormat::N32BitTcBgra
;
144 pBuffer
->maPalette
= aEmptyPalette
;
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
)
163 = qRgb(m_aPalette
[i
].GetRed(), m_aPalette
[i
].GetGreen(), m_aPalette
[i
].GetBlue());
164 m_pImage
->setColorTable(std::move(aColorTable
));
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*/)
181 bool QtBitmap::Replace(const Color
& /*rSearchColor*/, const Color
& /*rReplaceColor*/,
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */