summaryrefslogtreecommitdiff
path: root/wx+/image_tools.cpp
blob: 8f94d1bc0ca6f7d34320901799029558da348357 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under    *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0          *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************

#include "image_tools.h"
#include <zen/string_tools.h>
#include <zen/zstring.h>
#include <wx/app.h>

using namespace zen;


namespace
{
void writeToImage(wxImage& output, const wxImage& top, const wxPoint& pos)
{
    const int topWidth  = top.GetWidth ();
    const int topHeight = top.GetHeight();
    const int outWidth  = output.GetWidth();

    assert(0 <= pos.x && pos.x + topWidth  <= outWidth          ); //draw area must be a
    assert(0 <= pos.y && pos.y + topHeight <= output.GetHeight()); //subset of output image!
    assert(top.HasAlpha() && output.HasAlpha());

    //https://en.wikipedia.org/wiki/Alpha_compositing
    const unsigned char* topRgb   = top.GetData();
    const unsigned char* topAlpha = top.GetAlpha();

    for (int y = 0; y < topHeight; ++y)
    {
        unsigned char* outRgb   = output.GetData () + 3 * (pos.x + (pos.y + y) * outWidth);
        unsigned char* outAlpha = output.GetAlpha() +      pos.x + (pos.y + y) * outWidth;

        for (int x = 0; x < topWidth; ++x)
        {
            const int w1 = *topAlpha; //alpha-composition interpreted as weighted average
            const int w2 = *outAlpha * (255 - w1) / 255;
            const int wSum = w1 + w2;

            auto calcColor = [w1, w2, wSum](unsigned char colTop, unsigned char colBot)
            {
                return static_cast<unsigned char>(wSum == 0 ? 0 : (colTop * w1 + colBot * w2) / wSum);
            };
            outRgb[0] = calcColor(topRgb[0], outRgb[0]);
            outRgb[1] = calcColor(topRgb[1], outRgb[1]);
            outRgb[2] = calcColor(topRgb[2], outRgb[2]);

            *outAlpha = static_cast<unsigned char>(wSum);

            topRgb += 3;
            outRgb += 3;
            ++topAlpha;
            ++outAlpha;
        }
    }
}
}


wxImage zen::stackImages(const wxImage& img1, const wxImage& img2, ImageStackLayout dir, ImageStackAlignment align, int gap)
{
    assert(gap >= 0);
    gap = std::max(0, gap);

    const int img1Width  = img1.GetWidth ();
    const int img1Height = img1.GetHeight();
    const int img2Width  = img2.GetWidth ();
    const int img2Height = img2.GetHeight();

    int width  = std::max(img1Width,  img2Width);
    int height = std::max(img1Height, img2Height);

    if (dir == ImageStackLayout::horizontal)
        width = img1Width + gap + img2Width;
    else
        height = img1Height + gap + img2Height;

    wxImage output(width, height);
    output.SetAlpha();
    ::memset(output.GetAlpha(), wxIMAGE_ALPHA_TRANSPARENT, width * height);

    auto calcPos = [&](int imageExtent, int totalExtent)
    {
        switch (align)
        {
            case ImageStackAlignment::center:
                return static_cast<int>(std::floor((totalExtent - imageExtent) / 2.0)); //consistency: round down negative values, too!
            case ImageStackAlignment::left:
                return 0;
            case ImageStackAlignment::right:
                return totalExtent - imageExtent;
        }
        assert(false);
        return 0;
    };

    switch (dir)
    {
        case ImageStackLayout::horizontal:
            writeToImage(output, img1, wxPoint(0,               calcPos(img1Height, height)));
            writeToImage(output, img2, wxPoint(img1Width + gap, calcPos(img2Height, height)));
            break;

        case ImageStackLayout::vertical:
            writeToImage(output, img1, wxPoint(calcPos(img1Width, width), 0));
            writeToImage(output, img2, wxPoint(calcPos(img2Width, width), img1Height + gap));
            break;
    }
    return output;
}


namespace
{
std::vector<std::pair<wxString, wxSize>> getTextExtentInfo(const wxString& text, const wxFont& font)
{
    wxMemoryDC dc; //the context used for bitmaps
    dc.SetFont(font); //the font parameter of GetMultiLineTextExtent() is not evalated on OS X, wxWidgets 2.9.5, so apply it to the DC directly!

    std::vector<std::pair<wxString, wxSize>> lineInfo; //text + extent
    for (const wxString& line : split(text, L"\n", SplitType::ALLOW_EMPTY))
        lineInfo.emplace_back(line, line.empty() ? wxSize() : dc.GetTextExtent(line));

    return lineInfo;
}
}

wxImage zen::createImageFromText(const wxString& text, const wxFont& font, const wxColor& col, ImageStackAlignment textAlign)
{
    //assert(!contains(text, L"&")); //accelerator keys not supported here
    wxString textFmt = replaceCpy(text, L"&", L"", false);

    //for some reason wxDC::DrawText messes up "weak" bidi characters even when wxLayout_RightToLeft is set! (--> arrows in hebrew/arabic)
    //=> use mark characters instead:
    if (wxTheApp->GetLayoutDirection() == wxLayout_RightToLeft)
        textFmt = RTL_MARK + textFmt + RTL_MARK;

    const std::vector<std::pair<wxString, wxSize>> lineInfo = getTextExtentInfo(textFmt, font);

    int maxWidth   = 0;
    int lineHeight = 0;
    for (const auto& [lineText, lineSize] : lineInfo)
    {
        maxWidth   = std::max(maxWidth,   lineSize.GetWidth());
        lineHeight = std::max(lineHeight, lineSize.GetHeight()); //wxWidgets comment "GetTextExtent will return 0 for empty string"
    }
    if (maxWidth == 0 || lineHeight == 0)
        return wxImage();

    wxBitmap newBitmap(maxWidth, lineHeight * lineInfo.size()); //seems we don't need to pass 24-bit depth here even for high-contrast color schemes
    {
        wxMemoryDC dc(newBitmap);
        dc.SetBackground(*wxWHITE_BRUSH);
        dc.Clear();

        dc.SetTextForeground(*wxBLACK); //for use in calcAlphaForBlackWhiteImage
        dc.SetTextBackground(*wxWHITE); //
        dc.SetFont(font);

        int posY = 0;
        for (const auto& [lineText, lineSize] : lineInfo)
        {
            if (!lineText.empty())
                switch (textAlign)
                {
                    case ImageStackAlignment::left:
                        dc.DrawText(lineText, wxPoint(0, posY));
                        break;
                    case ImageStackAlignment::right:
                        dc.DrawText(lineText, wxPoint(maxWidth - lineSize.GetWidth(), posY));
                        break;
                    case ImageStackAlignment::center:
                        dc.DrawText(lineText, wxPoint((maxWidth - lineSize.GetWidth()) / 2, posY));
                        break;
                }

            posY += lineHeight;
        }
    }

    //wxDC::DrawLabel() doesn't respect alpha channel => calculate alpha values manually:
    wxImage output(newBitmap.ConvertToImage());
    output.SetAlpha();

    unsigned char* rgb   = output.GetData();
    unsigned char* alpha = output.GetAlpha();
    const int pixelCount = output.GetWidth() * output.GetHeight();

    for (int i = 0; i < pixelCount; ++i)
    {
        //black(0,0,0) becomes wxIMAGE_ALPHA_OPAQUE(255), while white(255,255,255) becomes wxIMAGE_ALPHA_TRANSPARENT(0)
        *alpha++ = static_cast<unsigned char>((255 - rgb[0] + 255 - rgb[1] + 255 - rgb[2]) / 3); //mixed-mode arithmetics!

        rgb[0] = col.Red  (); //
        rgb[1] = col.Green(); //apply actual text color
        rgb[2] = col.Blue (); //

        rgb += 3;
    }
    return output;
}


wxImage zen::layOver(const wxImage& back, const wxImage& front, int alignment)
{
    if (!front.IsOk()) return back;
    assert(front.HasAlpha() && back.HasAlpha());

    const int width  = std::max(back.GetWidth(),  front.GetWidth());
    const int height = std::max(back.GetHeight(), front.GetHeight());

    const int offsetX = [&]
    {
        if (alignment & wxALIGN_RIGHT)
            return back.GetWidth() - front.GetWidth();
        if (alignment & wxALIGN_CENTER_HORIZONTAL)
            return (back.GetWidth() - front.GetWidth()) / 2;

        static_assert(wxALIGN_LEFT == 0);
        return 0;
    }();

    const int offsetY = [&]
    {
        if (alignment & wxALIGN_BOTTOM)
            return back.GetHeight() - front.GetHeight();
        if (alignment & wxALIGN_CENTER_VERTICAL)
            return (back.GetHeight() - front.GetHeight()) / 2;

        static_assert(wxALIGN_TOP == 0);
        return 0;
    }();

    //can't use wxMemoryDC and wxDC::DrawBitmap(): no alpha channel support on wxGTK!
    wxImage output(width, height);
    output.SetAlpha();
    ::memset(output.GetAlpha(), wxIMAGE_ALPHA_TRANSPARENT, width * height);

    const wxPoint posBack(std::max(-offsetX, 0), std::max(-offsetY, 0));
    writeToImage(output, back, posBack);
    writeToImage(output, front, posBack + wxPoint(offsetX, offsetY));
    return output;
}


void zen::convertToVanillaImage(wxImage& img)
{
    if (!img.HasAlpha())
    {
        const int width  = img.GetWidth ();
        const int height = img.GetHeight();
        if (width <= 0 || height <= 0) return;

        unsigned char mask_r = 0;
        unsigned char mask_g = 0;
        unsigned char mask_b = 0;
        const bool haveMask = img.HasMask() && img.GetOrFindMaskColour(&mask_r, &mask_g, &mask_b);
        //check for mask before calling wxImage::GetOrFindMaskColour() to skip needlessly searching for new mask color

        img.SetAlpha();
        ::memset(img.GetAlpha(), wxIMAGE_ALPHA_OPAQUE, width * height);

        //wxWidgets, as always, tries to be more clever than it really is and fucks up wxStaticBitmap if wxBitmap is fully opaque:
        img.GetAlpha()[width * height - 1] = 254;

        if (haveMask)
        {
            img.SetMask(false);
            unsigned char*       alphaPtr = img.GetAlpha();
            const unsigned char* dataPtr  = img.GetData();

            const int pixelCount = width * height;
            for (int i = 0; i < pixelCount; ++ i)
            {
                const unsigned char r = *dataPtr++;
                const unsigned char g = *dataPtr++;
                const unsigned char b = *dataPtr++;

                if (r == mask_r &&
                    g == mask_g &&
                    b == mask_b)
                    alphaPtr[i] = wxIMAGE_ALPHA_TRANSPARENT;
            }
        }
    }
    else
    {
        assert(!img.HasMask());
    }
}
bgstack15