build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / image / Image.cxx
blobe4dde10e4d5ca3293599b7535cea3c8af3495181
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <osl/file.hxx>
21 #include <tools/debug.hxx>
22 #include <tools/stream.hxx>
23 #include <tools/rc.h>
24 #include <tools/rc.hxx>
25 #include <tools/resmgr.hxx>
26 #include <vcl/settings.hxx>
27 #include <vcl/outdev.hxx>
28 #include <vcl/graph.hxx>
29 #include <vcl/graphicfilter.hxx>
30 #include <vcl/svapp.hxx>
31 #include <vcl/image.hxx>
32 #include <vcl/imagerepository.hxx>
33 #include <vcl/ImageTree.hxx>
34 #include <sal/types.h>
35 #include <image.h>
37 #include "BitmapProcessor.hxx"
39 #if OSL_DEBUG_LEVEL > 0
40 #include <rtl/strbuf.hxx>
41 #endif
43 Image::Image()
47 Image::Image( const ResId& rResId )
49 rResId.SetRT( RSC_IMAGE );
51 ResMgr* pResMgr = rResId.GetResMgr();
52 if( pResMgr && pResMgr->GetResource( rResId ) )
54 pResMgr->Increment( sizeof( RSHEADER_TYPE ) );
56 BitmapEx aBmpEx;
57 RscImageFlags nObjMask = (RscImageFlags)pResMgr->ReadLong();
59 if( nObjMask & RscImageFlags::ImageBitmap )
61 aBmpEx = BitmapEx( ResId( static_cast<RSHEADER_TYPE*>(pResMgr->GetClass()), *pResMgr ) );
62 pResMgr->Increment( ResMgr::GetObjSize( static_cast<RSHEADER_TYPE*>(pResMgr->GetClass()) ) );
65 if( ! aBmpEx.IsEmpty() )
66 ImplInit( aBmpEx );
70 Image::Image(const BitmapEx& rBitmapEx)
72 ImplInit(rBitmapEx);
75 Image::Image(const Bitmap& rBitmap)
77 ImplInit(rBitmap);
80 Image::Image(const Bitmap& rBitmap, const Bitmap& rMaskBitmap)
82 const BitmapEx aBitmapEx(rBitmap, rMaskBitmap);
83 ImplInit(aBitmapEx);
86 Image::Image(const Bitmap& rBitmap, const Color& rColor)
88 const BitmapEx aBitmapEx(rBitmap, rColor);
89 ImplInit(aBitmapEx);
92 Image::Image(const css::uno::Reference< css::graphic::XGraphic >& rxGraphic)
94 const Graphic aGraphic(rxGraphic);
95 ImplInit(aGraphic.GetBitmapEx());
98 Image::Image(const OUString & rFileUrl)
100 OUString aPath;
101 osl::FileBase::getSystemPathFromFileURL(rFileUrl, aPath);
102 Graphic aGraphic;
103 const OUString aFilterName(IMP_PNG);
104 if (GRFILTER_OK == GraphicFilter::LoadGraphic(aPath, aFilterName, aGraphic))
106 ImplInit(aGraphic.GetBitmapEx());
110 void Image::ImplInit(const BitmapEx& rBitmapEx)
112 if (!rBitmapEx.IsEmpty())
114 mpImplData.reset(new ImplImage);
115 mpImplData->mpBitmapEx.reset(new BitmapEx(rBitmapEx));
119 Size Image::GetSizePixel() const
121 Size aRet;
123 if (mpImplData && mpImplData->mpBitmapEx)
125 aRet = mpImplData->mpBitmapEx->GetSizePixel();
128 return aRet;
131 BitmapEx Image::GetBitmapEx() const
133 BitmapEx aRet;
135 if (mpImplData && mpImplData->mpBitmapEx)
137 aRet = BitmapEx(*mpImplData->mpBitmapEx);
140 return aRet;
143 css::uno::Reference< css::graphic::XGraphic > Image::GetXGraphic() const
145 const Graphic aGraphic( GetBitmapEx() );
147 return aGraphic.GetXGraphic();
150 bool Image::operator==(const Image& rImage) const
152 bool bRet = false;
154 if (rImage.mpImplData == mpImplData)
155 bRet = true;
156 else if (!rImage.mpImplData || !mpImplData)
157 bRet = false;
158 else
159 bRet = *rImage.mpImplData->mpBitmapEx == *mpImplData->mpBitmapEx;
161 return bRet;
164 void Image::Draw(OutputDevice* pOutDev, const Point& rPos, DrawImageFlags nStyle, const Size* pSize)
166 if (!mpImplData || !mpImplData->mpBitmapEx ||
167 (!pOutDev->IsDeviceOutputNecessary() && pOutDev->GetConnectMetaFile() == nullptr))
168 return;
170 const Point aSrcPos(0, 0);
171 Size aBitmapSizePixel = mpImplData->mpBitmapEx->GetSizePixel();
173 Size aOutSize = pSize ? *pSize : pOutDev->PixelToLogic(aBitmapSizePixel);
175 if (nStyle & DrawImageFlags::Disable)
177 BitmapChecksum aChecksum = mpImplData->mpBitmapEx->GetChecksum();
178 if (mpImplData->maBitmapChecksum != aChecksum)
180 mpImplData->maBitmapChecksum = aChecksum;
181 mpImplData->maDisabledBitmapEx = BitmapProcessor::createDisabledImage(*mpImplData->mpBitmapEx);
183 pOutDev->DrawBitmapEx(rPos, aOutSize, aSrcPos, aBitmapSizePixel, mpImplData->maDisabledBitmapEx);
185 else
187 if (nStyle & (DrawImageFlags::ColorTransform | DrawImageFlags::Highlight |
188 DrawImageFlags::Deactive | DrawImageFlags::SemiTransparent))
190 BitmapEx aTempBitmapEx(*mpImplData->mpBitmapEx);
192 if (nStyle & (DrawImageFlags::Highlight | DrawImageFlags::Deactive))
194 const StyleSettings& rSettings = pOutDev->GetSettings().GetStyleSettings();
195 Color aColor;
196 if (nStyle & DrawImageFlags::Highlight)
197 aColor = rSettings.GetHighlightColor();
198 else
199 aColor = rSettings.GetDeactiveColor();
201 BitmapProcessor::colorizeImage(aTempBitmapEx, aColor);
204 if (nStyle & DrawImageFlags::SemiTransparent)
206 if (aTempBitmapEx.IsTransparent())
208 Bitmap aAlphaBmp(aTempBitmapEx.GetAlpha().GetBitmap());
209 aAlphaBmp.Adjust(50);
210 aTempBitmapEx = BitmapEx(aTempBitmapEx.GetBitmap(), AlphaMask(aAlphaBmp));
212 else
214 sal_uInt8 cErase = 128;
215 aTempBitmapEx = BitmapEx(aTempBitmapEx.GetBitmap(), AlphaMask(aTempBitmapEx.GetSizePixel(), &cErase));
218 pOutDev->DrawBitmapEx(rPos, aOutSize, aSrcPos, aTempBitmapEx.GetSizePixel(), aTempBitmapEx);
220 else
222 pOutDev->DrawBitmapEx(rPos, aOutSize, aSrcPos, mpImplData->mpBitmapEx->GetSizePixel(), *mpImplData->mpBitmapEx);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */