/src/libreoffice/vcl/source/image/Image.cxx
Line | Count | Source |
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/alpha.hxx> |
21 | | #include <vcl/settings.hxx> |
22 | | #include <vcl/outdev.hxx> |
23 | | #include <vcl/graph.hxx> |
24 | | #include <vcl/graphicfilter.hxx> |
25 | | #include <vcl/image.hxx> |
26 | | #include <sal/types.h> |
27 | | #include <image.h> |
28 | | |
29 | | #include <bitmap/BitmapColorizeFilter.hxx> |
30 | | |
31 | | using namespace css; |
32 | | |
33 | | Image::Image() |
34 | 0 | { |
35 | 0 | } |
36 | | |
37 | | Image::Image(const Bitmap& rBitmap) |
38 | 0 | { |
39 | 0 | ImplInit(rBitmap); |
40 | 0 | } |
41 | | |
42 | | Image::Image(uno::Reference<graphic::XGraphic> const & rxGraphic) |
43 | 0 | { |
44 | 0 | if (rxGraphic.is()) |
45 | 0 | { |
46 | 0 | const Graphic aGraphic(rxGraphic); |
47 | |
|
48 | 0 | OUString aPath; |
49 | 0 | if (aGraphic.getOriginURL().startsWith("private:graphicrepository/", &aPath)) |
50 | 0 | mpImplData = std::make_shared<ImplImage>(aPath); |
51 | 0 | else if (aGraphic.GetType() == GraphicType::GdiMetafile) |
52 | 0 | mpImplData = std::make_shared<ImplImage>(aGraphic.GetGDIMetaFile()); |
53 | 0 | else |
54 | 0 | ImplInit(aGraphic.GetBitmap()); |
55 | 0 | } |
56 | 0 | } |
57 | | |
58 | | Image::Image(const OUString & rFileUrl) |
59 | 0 | { |
60 | 0 | OUString sImageName; |
61 | 0 | if (rFileUrl.startsWith("private:graphicrepository/", &sImageName)) |
62 | 0 | mpImplData = std::make_shared<ImplImage>(sImageName); |
63 | 0 | else |
64 | 0 | { |
65 | 0 | Graphic aGraphic; |
66 | 0 | if (ERRCODE_NONE == GraphicFilter::LoadGraphic(rFileUrl, u"" IMP_PNG ""_ustr, aGraphic)) |
67 | 0 | ImplInit(aGraphic.GetBitmap()); |
68 | 0 | } |
69 | 0 | } |
70 | | |
71 | | Image::Image(StockImage, const OUString & rFileUrl) |
72 | 0 | : mpImplData(std::make_shared<ImplImage>(rFileUrl)) |
73 | 0 | { |
74 | 0 | } |
75 | | |
76 | | void Image::ImplInit(const Bitmap& rBitmap) |
77 | 0 | { |
78 | 0 | if (!rBitmap.IsEmpty()) |
79 | 0 | mpImplData = std::make_shared<ImplImage>(rBitmap); |
80 | 0 | } |
81 | | |
82 | | const OUString & Image::GetStock() const |
83 | 0 | { |
84 | 0 | if (mpImplData) |
85 | 0 | return mpImplData->getStock(); |
86 | 0 | return EMPTY_OUSTRING; |
87 | 0 | } |
88 | | |
89 | | Size Image::GetSizePixel() const |
90 | 0 | { |
91 | 0 | if (mpImplData) |
92 | 0 | return mpImplData->getSizePixel(); |
93 | 0 | else |
94 | 0 | return Size(); |
95 | 0 | } |
96 | | |
97 | | Bitmap Image::GetBitmap() const |
98 | 0 | { |
99 | 0 | if (mpImplData) |
100 | 0 | return mpImplData->getBitmap(); |
101 | 0 | else |
102 | 0 | return Bitmap(); |
103 | 0 | } |
104 | | |
105 | | void Image::SetOptional(bool bValue) |
106 | 0 | { |
107 | 0 | if (mpImplData) |
108 | 0 | { |
109 | 0 | mpImplData->SetOptional(bValue); |
110 | 0 | } |
111 | 0 | } |
112 | | |
113 | | bool Image::operator==(const Image& rImage) const |
114 | 0 | { |
115 | 0 | bool bRet = false; |
116 | |
|
117 | 0 | if (rImage.mpImplData == mpImplData) |
118 | 0 | bRet = true; |
119 | 0 | else if (!rImage.mpImplData || !mpImplData) |
120 | 0 | bRet = false; |
121 | 0 | else |
122 | 0 | bRet = rImage.mpImplData->isEqual(*mpImplData); |
123 | |
|
124 | 0 | return bRet; |
125 | 0 | } |
126 | | |
127 | | void Image::Draw(OutputDevice* pOutDev, const Point& rPos, DrawImageFlags nStyle, const Size* pSize) const |
128 | 0 | { |
129 | 0 | if (!mpImplData || (!pOutDev->IsDeviceOutputNecessary() && pOutDev->GetConnectMetaFile() == nullptr)) |
130 | 0 | return; |
131 | | |
132 | 0 | Size aOutSize = pSize ? *pSize : pOutDev->PixelToLogic(mpImplData->getSizePixel()); |
133 | |
|
134 | 0 | Bitmap aRenderBmp = mpImplData->getBitmapForHiDPI(bool(nStyle & DrawImageFlags::Disable), pOutDev->GetGraphics()); |
135 | |
|
136 | 0 | if (!(nStyle & DrawImageFlags::Disable) && |
137 | 0 | (nStyle & (DrawImageFlags::ColorTransform | DrawImageFlags::Highlight | |
138 | 0 | DrawImageFlags::Deactive | DrawImageFlags::SemiTransparent))) |
139 | 0 | { |
140 | 0 | if (nStyle & (DrawImageFlags::Highlight | DrawImageFlags::Deactive)) |
141 | 0 | { |
142 | 0 | const StyleSettings& rSettings = pOutDev->GetSettings().GetStyleSettings(); |
143 | 0 | Color aColor; |
144 | 0 | if (nStyle & DrawImageFlags::Highlight) |
145 | 0 | aColor = rSettings.GetHighlightColor(); |
146 | 0 | else |
147 | 0 | aColor = rSettings.GetDeactiveColor(); |
148 | |
|
149 | 0 | BitmapFilter::Filter(aRenderBmp, BitmapColorizeFilter(aColor)); |
150 | 0 | } |
151 | |
|
152 | 0 | if (nStyle & DrawImageFlags::SemiTransparent) |
153 | 0 | { |
154 | 0 | Bitmap aTempBitmap(aRenderBmp); |
155 | 0 | if (aTempBitmap.HasAlpha()) |
156 | 0 | { |
157 | 0 | Bitmap aAlphaBmp(aTempBitmap.CreateAlphaMask().GetBitmap()); |
158 | 0 | aAlphaBmp.Adjust(50); |
159 | 0 | aTempBitmap = Bitmap(aTempBitmap.CreateColorBitmap(), AlphaMask(aAlphaBmp)); |
160 | 0 | } |
161 | 0 | else |
162 | 0 | { |
163 | 0 | sal_uInt8 cErase = 128; |
164 | 0 | aTempBitmap = Bitmap(aTempBitmap, AlphaMask(aTempBitmap.GetSizePixel(), &cErase)); |
165 | 0 | } |
166 | 0 | aRenderBmp = std::move(aTempBitmap); |
167 | 0 | } |
168 | 0 | } |
169 | |
|
170 | 0 | pOutDev->DrawBitmap(rPos, aOutSize, aRenderBmp); |
171 | 0 | } |
172 | | |
173 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |