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

using namespace std;

namespace dchat
{
    void* bitmapCreate(int width, int height)
    {
        return calloc(width * height, 4);
    }
    
    void bitmapDestroy(void *bitmap)
    {
        free(bitmap);
    }
    
    unsigned char* bitmapGetBuffer(void *bitmap)
    {
        return (unsigned char*)bitmap;
    }
    
    void bitmapSetOpaque(void *bitmap, bool opaque)
    {
        
    }
    
    bool bitmapTestOpaque(void *bitmap)
    {
        return false;
    }
    
    void bitmapModified(void *bitmap)
    {
        
    }
    
    const char* gifResultToString(gif_result code)
    {
        switch(code)
        {
            case GIF_INSUFFICIENT_FRAME_DATA:
                return "GIF_INSUFFICIENT_FRAME_DATA";
            case GIF_FRAME_DATA_ERROR:
                return "GIF_FRAME_DATA_ERROR";
            case GIF_INSUFFICIENT_DATA:
                return "GIF_INSUFFICIENT_DATA";
            case GIF_DATA_ERROR:
                return "GIF_DATA_ERROR";
            case GIF_INSUFFICIENT_MEMORY:
                return "GIF_INSUFFICIENT_MEMORY";
            default:
                return "Unknown gif result code";
        }
    }
    
    Gif::Gif(const boost::filesystem::path &filepath) : 
        currentFrame(0),
        timeElapsedCs(0.0)
    {
        try
        {
            fileContent = getFileContent(filepath);
        }
        catch(FileException &e)
        {
            throw GifLoadException(e.what());
        }
        
        try
        {
            init();
        }
        catch(GifLoadException &e)
        {
            delete[] fileContent.data;
            throw e;
        }
    }
    
    Gif::Gif(StringView &&_fileContent) : 
        fileContent(move(_fileContent)),
        currentFrame(0),
        timeElapsedCs(0.0)
    {
        try
        {
            init();
        }
        catch(GifLoadException &e)
        {
            delete[] fileContent.data;
            throw e;
        }
    }
    
    void Gif::init()
    {
        gif_bitmap_callback_vt bitmapCallbacks = 
        {
            bitmapCreate,
            bitmapDestroy,
            bitmapGetBuffer,
            bitmapSetOpaque,
            bitmapTestOpaque,
            bitmapModified
        };
        
        gif_create(&gif, &bitmapCallbacks);
        
        gif_result code;
        do
        {
            code = gif_initialise(&gif, fileContent.size, (unsigned char*)fileContent.data);
            if(code != GIF_OK && code != GIF_WORKING)
            {
                string errMsg = "Failed to initialize gif, reason: ";
                errMsg += gifResultToString(code);
                throw GifLoadException(errMsg);
            }
        }
        while(code != GIF_OK);
        
        if(!texture.create(gif.width, gif.height))
            throw GifLoadException("Failed to create texture for gif");
        
        texture.setSmooth(true);
        sprite.setTexture(texture, true);
    }
    
    Gif::~Gif()
    {
        gif_finalise(&gif);
        delete[] fileContent.data;
    }
    
    sf::Vector2u Gif::getSize() const
    {
        return sprite.getTexture()->getSize();
    }
    
    void Gif::setPosition(const sf::Vector2f &position)
    {
        sprite.setPosition(position);
    }
    
    void Gif::setScale(const sf::Vector2f &scale)
    {
        sprite.setScale(scale);
    }
    
    void Gif::draw(sf::RenderTarget &target, const sf::RenderStates &renderState)
    {
        double timeElapsedMilli = (double)frameTimer.getElapsedTime().asMilliseconds();
        // If gif is not redrawn for a while, then we reset timer (gif is paused). This happens when gif is not visible and then appears visible
        // (because it's visible in window). The reason this is done is to prevent too much time between rendering gif frames, as processing a gif
        // requires to process all frames between two points in time, if elapsed frame time is too high, then we would require to process several
        // frames of gif in one application render frame.
        if(timeElapsedMilli > 1000.0)
            timeElapsedMilli = 0.0;
        double frameDeltaCs = timeElapsedMilli * 0.1; // Centisecond
        frameTimer.restart();
        timeElapsedCs += frameDeltaCs;
        
        unsigned char *image = nullptr;
        u32 startFrame = currentFrame;
        while(true)
        {
            u32 i = currentFrame % gif.frame_count;
            gif_result code = gif_decode_frame(&gif, i);
            if(code != GIF_OK)
            {
                printf("Warning: gif_decode_frame: %s\n", gifResultToString(code));
                break;
            }
            
            gif_frame &frame = gif.frames[i];
            // frame_delay is in centiseconds
            unsigned int frameDelay = frame.frame_delay;
            if(frameDelay == 0)
                frameDelay = 7;
            double fFrameDelay = (double)frameDelay;
            if(timeElapsedCs >= fFrameDelay)
                timeElapsedCs -= fFrameDelay;
            else
                break;
            
            image = (unsigned char*)gif.frame_image;
            ++currentFrame;
        }
        
        if(currentFrame != startFrame)
        {
            texture.update(image);
            // TODO: Check if this is too heavy
            texture.generateMipmap();
            sprite.setTexture(texture, true); 
        }
        target.draw(sprite, renderState);
    }
    
    bool Gif::isDataGif(const StringView &data)
    {
        return data.size >= 6 && (memcmp(data.data, "GIF87a", 6) == 0 || memcmp(data.data, "GIF89a", 6) == 0);
    }
}