LibreOffice Module vcl (master)  1
Image.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <vcl/settings.hxx>
21 #include <vcl/outdev.hxx>
22 #include <vcl/graph.hxx>
23 #include <vcl/graphicfilter.hxx>
24 #include <vcl/image.hxx>
25 #include <sal/types.h>
26 #include <image.h>
27 
28 #include <BitmapColorizeFilter.hxx>
29 
30 using namespace css;
31 
33 {
34 }
35 
36 Image::Image(const BitmapEx& rBitmapEx)
37 {
38  ImplInit(rBitmapEx);
39 }
40 
41 Image::Image(uno::Reference<graphic::XGraphic> const & rxGraphic)
42 {
43  if (rxGraphic.is())
44  {
45  const Graphic aGraphic(rxGraphic);
46 
47  OUString aPath;
48  if (aGraphic.getOriginURL().startsWith("private:graphicrepository/", &aPath))
49  mpImplData = std::make_shared<ImplImage>(aPath, Size());
50  else
51  ImplInit(aGraphic.GetBitmapEx());
52  }
53 }
54 
55 Image::Image(const OUString & rFileUrl)
56 {
57  OUString sImageName;
58  if (rFileUrl.startsWith("private:graphicrepository/", &sImageName))
59  mpImplData = std::make_shared<ImplImage>(sImageName, Size());
60  else
61  {
62  Graphic aGraphic;
63  if (ERRCODE_NONE == GraphicFilter::LoadGraphic(rFileUrl, IMP_PNG, aGraphic))
64  ImplInit(aGraphic.GetBitmapEx());
65  }
66 }
67 
68 Image::Image(StockImage, const OUString & rFileUrl, Size aSpecificSize)
69  : mpImplData(std::make_shared<ImplImage>(rFileUrl, aSpecificSize))
70 {
71 }
72 
73 void Image::ImplInit(const BitmapEx& rBitmapEx)
74 {
75  if (!rBitmapEx.IsEmpty())
76  mpImplData = std::make_shared<ImplImage>(rBitmapEx);
77 }
78 
79 OUString Image::GetStock() const
80 {
81  if (mpImplData)
82  return mpImplData->getStock();
83  return OUString();
84 }
85 
87 {
88  if (mpImplData)
89  return mpImplData->getSizePixel();
90  else
91  return Size();
92 }
93 
95 {
96  if (mpImplData)
97  return mpImplData->getBitmapEx();
98  else
99  return BitmapEx();
100 }
101 
102 bool Image::operator==(const Image& rImage) const
103 {
104  bool bRet = false;
105 
106  if (rImage.mpImplData == mpImplData)
107  bRet = true;
108  else if (!rImage.mpImplData || !mpImplData)
109  bRet = false;
110  else
111  bRet = rImage.mpImplData->isEqual(*mpImplData);
112 
113  return bRet;
114 }
115 
116 void Image::Draw(OutputDevice* pOutDev, const Point& rPos, DrawImageFlags nStyle, const Size* pSize)
117 {
118  if (!mpImplData || (!pOutDev->IsDeviceOutputNecessary() && pOutDev->GetConnectMetaFile() == nullptr))
119  return;
120 
121  Size aOutSize = pSize ? *pSize : pOutDev->PixelToLogic(mpImplData->getSizePixel());
122 
123  BitmapEx aRenderBmp = mpImplData->getBitmapExForHiDPI(bool(nStyle & DrawImageFlags::Disable));
124 
125  if (!(nStyle & DrawImageFlags::Disable) &&
128  {
129  BitmapEx aTempBitmapEx(aRenderBmp);
130 
132  {
133  const StyleSettings& rSettings = pOutDev->GetSettings().GetStyleSettings();
134  Color aColor;
135  if (nStyle & DrawImageFlags::Highlight)
136  aColor = rSettings.GetHighlightColor();
137  else
138  aColor = rSettings.GetDeactiveColor();
139 
140  BitmapFilter::Filter(aTempBitmapEx, BitmapColorizeFilter(aColor));
141  }
142 
143  if (nStyle & DrawImageFlags::SemiTransparent)
144  {
145  if (aTempBitmapEx.IsTransparent())
146  {
147  Bitmap aAlphaBmp(aTempBitmapEx.GetAlpha().GetBitmap());
148  aAlphaBmp.Adjust(50);
149  aTempBitmapEx = BitmapEx(aTempBitmapEx.GetBitmap(), AlphaMask(aAlphaBmp));
150  }
151  else
152  {
153  sal_uInt8 cErase = 128;
154  aTempBitmapEx = BitmapEx(aTempBitmapEx.GetBitmap(), AlphaMask(aTempBitmapEx.GetSizePixel(), &cErase));
155  }
156  }
157  aRenderBmp = aTempBitmapEx;
158  }
159 
160  pOutDev->DrawBitmapEx(rPos, aOutSize, aRenderBmp);
161 }
162 
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Size GetSizePixel() const
Definition: Image.cxx:86
bool Adjust(short nLuminancePercent, short nContrastPercent=0, short nChannelRPercent=0, short nChannelGPercent=0, short nChannelBPercent=0, double fGamma=1.0, bool bInvert=false, bool msoBrightness=false)
Change various global color characteristics.
Definition: bitmap3.cxx:1044
bool operator==(const Image &rImage) const
Definition: Image.cxx:102
const Color & GetDeactiveColor() const
void DrawBitmapEx(const Point &rDestPt, const BitmapEx &rBitmapEx)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Bitmap const & GetBitmap() const
Definition: alpha.cxx:70
const StyleSettings & GetStyleSettings() const
GDIMetaFile * GetConnectMetaFile() const
Definition: outdev.hxx:417
const Color & GetHighlightColor() const
void Draw(OutputDevice *pOutDev, const Point &rPos, DrawImageFlags nStyle, const Size *pSize=nullptr)
Definition: Image.cxx:116
static bool Filter(BitmapEx &rBmpEx, BitmapFilter const &rFilter)
Bitmap GetBitmap(const Color *pTransReplaceColor=nullptr) const
Definition: bitmapex.cxx:236
static ErrCode LoadGraphic(const OUString &rPath, const OUString &rFilter, Graphic &rGraphic, GraphicFilter *pFilter=nullptr, sal_uInt16 *pDeterminedFormat=nullptr)
OUString GetStock() const
Definition: Image.cxx:79
bool IsEmpty() const
Definition: bitmapex.cxx:203
Some things multiple-inherit from VclAbstractDialog and OutputDevice, so we need to use virtual inher...
Definition: outdev.hxx:304
const AllSettings & GetSettings() const
Definition: outdev.hxx:420
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Definition: graph.cxx:349
bool IsTransparent() const
Definition: bitmapex.cxx:221
Point PixelToLogic(const Point &rDevicePt) const
Definition: map.cxx:1185
StockImage
Definition: image.hxx:38
AlphaMask GetAlpha() const
Definition: bitmapex.cxx:268
#define ERRCODE_NONE
Definition: errcode.hxx:198
unsigned char sal_uInt8
#define IMP_PNG
Definition: image.hxx:40
Image()
Definition: Image.cxx:32
BitmapEx GetBitmapEx() const
Definition: Image.cxx:94
std::shared_ptr< ImplImage > mpImplData
Definition: image.hxx:69
SAL_DLLPRIVATE void ImplInit(BitmapEx const &rBmpEx)
Definition: Image.cxx:73
const Size & GetSizePixel() const
Definition: bitmapex.hxx:83
bool IsDeviceOutputNecessary() const
Definition: outdev.hxx:589
DrawImageFlags
Definition: outdev.hxx:172