summaryrefslogtreecommitdiffstats
path: root/vcl/source/image/Image.cxx
blob: 0cb7c5d44949af67dd3aa18a0f8b5ab62ee7a2ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#include <vcl/settings.hxx>
#include <vcl/outdev.hxx>
#include <vcl/graph.hxx>
#include <vcl/graphicfilter.hxx>
#include <vcl/image.hxx>
#include <sal/types.h>
#include <image.h>

#include <BitmapColorizeFilter.hxx>

using namespace css;

Image::Image()
{
}

Image::Image(const BitmapEx& rBitmapEx)
{
    ImplInit(rBitmapEx);
}

Image::Image(uno::Reference<graphic::XGraphic> const & rxGraphic)
{
    if (rxGraphic.is())
    {
        const Graphic aGraphic(rxGraphic);

        OUString aPath;
        if (aGraphic.getOriginURL().startsWith("private:graphicrepository/", &aPath))
            mpImplData = std::make_shared<ImplImage>(aPath, Size());
        else
            ImplInit(aGraphic.GetBitmapEx());
    }
}

Image::Image(const OUString & rFileUrl)
{
    OUString sImageName;
    if (rFileUrl.startsWith("private:graphicrepository/", &sImageName))
        mpImplData = std::make_shared<ImplImage>(sImageName, Size());
    else
    {
        Graphic aGraphic;
        if (ERRCODE_NONE == GraphicFilter::LoadGraphic(rFileUrl, IMP_PNG, aGraphic))
            ImplInit(aGraphic.GetBitmapEx());
    }
}

Image::Image(StockImage, const OUString & rFileUrl, Size aSpecificSize)
    : mpImplData(std::make_shared<ImplImage>(rFileUrl, aSpecificSize))
{
}

void Image::ImplInit(const BitmapEx& rBitmapEx)
{
    if (!rBitmapEx.IsEmpty())
        mpImplData = std::make_shared<ImplImage>(rBitmapEx);
}

OUString Image::GetStock() const
{
    if (mpImplData)
        return mpImplData->getStock();
    return OUString();
}

Size Image::GetSizePixel() const
{
    if (mpImplData)
        return mpImplData->getSizePixel();
    else
        return Size();
}

BitmapEx Image::GetBitmapEx() const
{
    if (mpImplData)
        return mpImplData->getBitmapEx();
    else
        return BitmapEx();
}

bool Image::operator==(const Image& rImage) const
{
    bool bRet = false;

    if (rImage.mpImplData == mpImplData)
        bRet = true;
    else if (!rImage.mpImplData || !mpImplData)
        bRet = false;
    else
        bRet = rImage.mpImplData->isEqual(*mpImplData);

    return bRet;
}

void Image::Draw(OutputDevice* pOutDev, const Point& rPos, DrawImageFlags nStyle, const Size* pSize)
{
    if (!mpImplData || (!pOutDev->IsDeviceOutputNecessary() && pOutDev->GetConnectMetaFile() == nullptr))
        return;

    Size aOutSize = pSize ? *pSize : pOutDev->PixelToLogic(mpImplData->getSizePixel());

    BitmapEx aRenderBmp = mpImplData->getBitmapExForHiDPI(bool(nStyle & DrawImageFlags::Disable));

    if (!(nStyle & DrawImageFlags::Disable) &&
        (nStyle & (DrawImageFlags::ColorTransform | DrawImageFlags::Highlight |
                   DrawImageFlags::Deactive | DrawImageFlags::SemiTransparent)))
    {
        BitmapEx aTempBitmapEx(aRenderBmp);

        if (nStyle & (DrawImageFlags::Highlight | DrawImageFlags::Deactive))
        {
            const StyleSettings& rSettings = pOutDev->GetSettings().GetStyleSettings();
            Color aColor;
            if (nStyle & DrawImageFlags::Highlight)
                aColor = rSettings.GetHighlightColor();
            else
                aColor = rSettings.GetDeactiveColor();

            BitmapFilter::Filter(aTempBitmapEx, BitmapColorizeFilter(aColor));
        }

        if (nStyle & DrawImageFlags::SemiTransparent)
        {
            if (aTempBitmapEx.IsTransparent())
            {
                Bitmap aAlphaBmp(aTempBitmapEx.GetAlpha().GetBitmap());
                aAlphaBmp.Adjust(50);
                aTempBitmapEx = BitmapEx(aTempBitmapEx.GetBitmap(), AlphaMask(aAlphaBmp));
            }
            else
            {
                sal_uInt8 cErase = 128;
                aTempBitmapEx = BitmapEx(aTempBitmapEx.GetBitmap(), AlphaMask(aTempBitmapEx.GetSizePixel(), &cErase));
            }
        }
        aRenderBmp = aTempBitmapEx;
    }

    pOutDev->DrawBitmapEx(rPos, aOutSize, aRenderBmp);
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */