aboutsummaryrefslogtreecommitdiff
path: root/src/GtkGif.cpp
blob: dc5cc53f0f7fc06f1b9cfe4107f878e2499f2f4b (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
#include "../include/GtkGif.hpp"

namespace dchat
{
    GtkGif::GtkGif(StringView fileContent) : 
        Gif(fileContent)
    {

    }

    bool GtkGif::createTexture(int width, int height)
    {
        surface = Cairo::ImageSurface::create(Cairo::Format::FORMAT_ARGB32, width, height);
        return true;
    }

    void GtkGif::updateTexture(void *textureData)
    {
        unsigned char *pixels = surface->get_data();
        surface->flush();
        unsigned char *p = (unsigned char*)textureData;
        // TODO: Optimize this
        for(int i = 0; i < surface->get_stride() * surface->get_height(); i += 4)
        {
            pixels[i + 0] = p[i + 2];
            pixels[i + 1] = p[i + 1];
            pixels[i + 2] = p[i + 0];
            pixels[i + 3] = p[i + 3];
        }
        //memcpy(pixels, textureData, surface->get_stride() * surface->get_height());
        surface->mark_dirty();
    }

    void GtkGif::draw(const Cairo::RefPtr<Cairo::Context> &cairo, int width, int height, bool circularMask)
    {
        update();

        if(circularMask)
        {
            int minSize = std::min(width/2, height/2);
            cairo->arc(width/2, height/2, minSize, 0.0, 2.0 * M_PI);
            cairo->clip();
        }

        double scaleX = (double)width / (double)surface->get_width();
        double scaleY = (double)height / (double)surface->get_height();
        cairo->scale(scaleX, scaleY);
        cairo->set_source(surface, 0.0, 0.0);
        cairo->mask(surface, 0.0, 0.0);
    }
}