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 <osl/file.hxx>
21 #include <tools/debug.hxx>
22 #include <tools/stream.hxx>
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>
37 #include "BitmapProcessor.hxx"
39 #if OSL_DEBUG_LEVEL > 0
40 #include <rtl/strbuf.hxx>
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
) );
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() )
70 Image::Image(const BitmapEx
& rBitmapEx
)
75 Image::Image(const Bitmap
& rBitmap
)
80 Image::Image(const Bitmap
& rBitmap
, const Bitmap
& rMaskBitmap
)
82 const BitmapEx
aBitmapEx(rBitmap
, rMaskBitmap
);
86 Image::Image(const Bitmap
& rBitmap
, const Color
& rColor
)
88 const BitmapEx
aBitmapEx(rBitmap
, rColor
);
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
)
101 osl::FileBase::getSystemPathFromFileURL(rFileUrl
, aPath
);
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
123 if (mpImplData
&& mpImplData
->mpBitmapEx
)
125 aRet
= mpImplData
->mpBitmapEx
->GetSizePixel();
131 BitmapEx
Image::GetBitmapEx() const
135 if (mpImplData
&& mpImplData
->mpBitmapEx
)
137 aRet
= BitmapEx(*mpImplData
->mpBitmapEx
);
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
154 if (rImage
.mpImplData
== mpImplData
)
156 else if (!rImage
.mpImplData
|| !mpImplData
)
159 bRet
= *rImage
.mpImplData
->mpBitmapEx
== *mpImplData
->mpBitmapEx
;
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))
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
);
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();
196 if (nStyle
& DrawImageFlags::Highlight
)
197 aColor
= rSettings
.GetHighlightColor();
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
));
214 sal_uInt8 cErase
= 128;
215 aTempBitmapEx
= BitmapEx(aTempBitmapEx
.GetBitmap(), AlphaMask(aTempBitmapEx
.GetSizePixel(), &cErase
));
218 pOutDev
->DrawBitmapEx(rPos
, aOutSize
, aSrcPos
, aTempBitmapEx
.GetSizePixel(), aTempBitmapEx
);
222 pOutDev
->DrawBitmapEx(rPos
, aOutSize
, aSrcPos
, mpImplData
->mpBitmapEx
->GetSizePixel(), *mpImplData
->mpBitmapEx
);
227 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */