|
| 1 | +#include "FreeImage.h" |
| 2 | + |
| 3 | +#include <cstdio> |
| 4 | +#include <cstdlib> |
| 5 | +#include <vector> |
| 6 | + |
| 7 | +static void append_u16(std::vector<BYTE> &gif, unsigned value) { |
| 8 | + gif.push_back(static_cast<BYTE>(value)); |
| 9 | + gif.push_back(static_cast<BYTE>(value >> 8)); |
| 10 | +} |
| 11 | + |
| 12 | +static void append_graphic_control(std::vector<BYTE> &gif, BYTE disposal) { |
| 13 | + const BYTE block[] = {0x21, 0xF9, 0x04, disposal, 0, 0, 0, 0}; |
| 14 | + gif.insert(gif.end(), block, block + sizeof(block)); |
| 15 | +} |
| 16 | + |
| 17 | +static void append_frame(std::vector<BYTE> &gif, unsigned left, unsigned top, |
| 18 | + unsigned width, unsigned height, const std::vector<BYTE> &compressed) { |
| 19 | + gif.push_back(0x2C); |
| 20 | + append_u16(gif, left); |
| 21 | + append_u16(gif, top); |
| 22 | + append_u16(gif, width); |
| 23 | + append_u16(gif, height); |
| 24 | + gif.push_back(0); // no local color table |
| 25 | + gif.push_back(2); // LZW minimum code size |
| 26 | + gif.push_back(static_cast<BYTE>(compressed.size())); |
| 27 | + gif.insert(gif.end(), compressed.begin(), compressed.end()); |
| 28 | + gif.push_back(0); // end of image data |
| 29 | +} |
| 30 | + |
| 31 | +static std::vector<BYTE> make_gif() { |
| 32 | + std::vector<BYTE> gif = { |
| 33 | + 'G', 'I', 'F', '8', '9', 'a', |
| 34 | + 10, 0, 10, 0, 0x80, 0, 0, // 10x10 logical screen, two-entry global palette |
| 35 | + 0, 0, 0, 255, 255, 255 |
| 36 | + }; |
| 37 | + |
| 38 | + // A 5x1 frame begins at x=8, beyond the 10-pixel logical screen. |
| 39 | + append_graphic_control(gif, 0x08); // disposal to background |
| 40 | + append_frame(gif, 8, 0, 5, 1, |
| 41 | + std::vector<BYTE>{0x0C, 0xC3, 0x30, 0x4C, 0x01}); |
| 42 | + |
| 43 | + // Page 1 clears frame 0's partially visible rectangle and then places a |
| 44 | + // one-pixel frame entirely outside the logical screen. |
| 45 | + append_graphic_control(gif, 0x08); |
| 46 | + append_frame(gif, 60000, 1, 1, 1, std::vector<BYTE>{0x4C, 0x01}); |
| 47 | + |
| 48 | + // Page 2 clears a prior frame whose left edge is far beyond the canvas. |
| 49 | + append_graphic_control(gif, 0); |
| 50 | + append_frame(gif, 0, 2, 1, 1, std::vector<BYTE>{0x4C, 0x01}); |
| 51 | + gif.push_back(0x3B); |
| 52 | + return gif; |
| 53 | +} |
| 54 | + |
| 55 | +static void require(bool condition, const char *message) { |
| 56 | + if(!condition) { |
| 57 | + std::fprintf(stderr, "GIF playback bounds test failed: %s\n", message); |
| 58 | + std::exit(1); |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +int main() { |
| 63 | + FreeImage_Initialise(); |
| 64 | + std::vector<BYTE> bytes = make_gif(); |
| 65 | + FIMEMORY *memory = FreeImage_OpenMemory(bytes.data(), static_cast<DWORD>(bytes.size())); |
| 66 | + require(memory != NULL, "could not open in-memory GIF"); |
| 67 | + |
| 68 | + FIMULTIBITMAP *animation = FreeImage_LoadMultiBitmapFromMemory(FIF_GIF, memory, GIF_PLAYBACK); |
| 69 | + require(animation != NULL, "could not load animated GIF"); |
| 70 | + require(FreeImage_GetPageCount(animation) == 3, "unexpected frame count"); |
| 71 | + |
| 72 | + for(int page = 0; page < 3; ++page) { |
| 73 | + FIBITMAP *frame = FreeImage_LockPage(animation, page); |
| 74 | + require(frame != NULL, "playback frame failed to load"); |
| 75 | + require(FreeImage_GetWidth(frame) == 10 && FreeImage_GetHeight(frame) == 10, |
| 76 | + "playback canvas has the wrong size"); |
| 77 | + require(FreeImage_GetBPP(frame) == 32, "playback canvas is not 32-bit"); |
| 78 | + FreeImage_UnlockPage(animation, frame, FALSE); |
| 79 | + } |
| 80 | + |
| 81 | + FreeImage_CloseMultiBitmap(animation, 0); |
| 82 | + FreeImage_CloseMemory(memory); |
| 83 | + FreeImage_DeInitialise(); |
| 84 | + return 0; |
| 85 | +} |
0 commit comments