Skip to content

Commit 96f4099

Browse files
authored
Merge pull request #183 from danoli3/fix/gif-playback-frame-bounds
Clip GIF playback frames to logical screen
2 parents 2237343 + 0da5ddb commit 96f4099

4 files changed

Lines changed: 178 additions & 6 deletions

File tree

‎Source/FreeImage/PluginGIF.cpp‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,9 @@ GifPlaybackReadPageInfo(FreeImageIO *io, fi_handle handle, GIFinfo *info, int pa
718718
static void
719719
GifPlaybackCompositeFrame(FIBITMAP *canvas, FIBITMAP *pagedib, const PageInfo &pinfo, int logicalheight, int *delay_time) {
720720
RGBQUAD *pal = FreeImage_GetPalette(pagedib);
721+
const int logicalwidth = static_cast<int>(FreeImage_GetWidth(canvas));
722+
const int visible_width = pinfo.left < logicalwidth ? logicalwidth - pinfo.left : 0;
723+
const int frame_width = pinfo.width < visible_width ? pinfo.width : visible_width;
721724
bool have_transparent = false;
722725
int transparent_color = 0;
723726
if( FreeImage_IsTransparent(pagedib) ) {
@@ -732,13 +735,17 @@ GifPlaybackCompositeFrame(FIBITMAP *canvas, FIBITMAP *pagedib, const PageInfo &p
732735
}
733736
}
734737
for( int y = 0; y < pinfo.height; y++ ) {
735-
const int scanidx = logicalheight - (y + pinfo.top) - 1;
736-
if ( scanidx < 0 ) {
738+
const int canvas_y = y + pinfo.top;
739+
if ( canvas_y >= logicalheight ) {
737740
break; // If data is corrupt, don't calculate in invalid scanline
738741
}
742+
if( frame_width == 0 ) {
743+
continue;
744+
}
745+
const int scanidx = logicalheight - canvas_y - 1;
739746
RGBQUAD *scanline = (RGBQUAD *)FreeImage_GetScanLine(canvas, scanidx) + pinfo.left;
740747
BYTE *pageline = FreeImage_GetScanLine(pagedib, pinfo.height - y - 1);
741-
for( int x = 0; x < pinfo.width; x++ ) {
748+
for( int x = 0; x < frame_width; x++ ) {
742749
if( !have_transparent || *pageline != transparent_color ) {
743750
*scanline = pal[*pageline];
744751
scanline->rgbReserved = 255;
@@ -758,13 +765,20 @@ GifPlaybackCompositeFrame(FIBITMAP *canvas, FIBITMAP *pagedib, const PageInfo &p
758765
// fills a frame's rect on the playback canvas with a flat color (GIF_DISPOSAL_BACKGROUND)
759766
static void
760767
GifPlaybackFillRect(FIBITMAP *canvas, const PageInfo &pinfo, int logicalheight, const RGBQUAD &color) {
768+
const int logicalwidth = static_cast<int>(FreeImage_GetWidth(canvas));
769+
const int visible_width = pinfo.left < logicalwidth ? logicalwidth - pinfo.left : 0;
770+
const int frame_width = pinfo.width < visible_width ? pinfo.width : visible_width;
761771
for( int y = 0; y < pinfo.height; y++ ) {
762-
const int scanidx = logicalheight - (y + pinfo.top) - 1;
763-
if ( scanidx < 0 ) {
772+
const int canvas_y = y + pinfo.top;
773+
if ( canvas_y >= logicalheight ) {
764774
break;
765775
}
776+
if( frame_width == 0 ) {
777+
continue;
778+
}
779+
const int scanidx = logicalheight - canvas_y - 1;
766780
RGBQUAD *scanline = (RGBQUAD *)FreeImage_GetScanLine(canvas, scanidx) + pinfo.left;
767-
for( int x = 0; x < pinfo.width; x++ ) {
781+
for( int x = 0; x < frame_width; x++ ) {
768782
*scanline++ = color;
769783
}
770784
}

‎TestAPI/CMakeLists.txt‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,28 @@ if (BUILD_TESTS)
5454
endif()
5555
add_test(NAME IffPbmRleTest COMMAND IffPbmRleTest)
5656
set_tests_properties(IffPbmRleTest PROPERTIES TIMEOUT 5)
57+
58+
add_executable(GifPlaybackBoundsTest testGifPlaybackBounds.cpp)
59+
target_link_libraries(GifPlaybackBoundsTest PRIVATE FreeImage)
60+
if(WIN32 AND NOT FREEIMAGE_STATIC)
61+
add_custom_command(TARGET GifPlaybackBoundsTest POST_BUILD
62+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
63+
$<TARGET_FILE:FreeImage> $<TARGET_FILE_DIR:GifPlaybackBoundsTest>
64+
VERBATIM)
65+
endif()
66+
add_test(NAME GifPlaybackBoundsTest COMMAND GifPlaybackBoundsTest)
67+
set_tests_properties(GifPlaybackBoundsTest PROPERTIES TIMEOUT 10)
68+
69+
add_executable(RasWordLoopTest testRasWordLoop.cpp)
70+
target_link_libraries(RasWordLoopTest PRIVATE FreeImage)
71+
if(WIN32 AND NOT FREEIMAGE_STATIC)
72+
add_custom_command(TARGET RasWordLoopTest POST_BUILD
73+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
74+
$<TARGET_FILE:FreeImage> $<TARGET_FILE_DIR:RasWordLoopTest>
75+
VERBATIM)
76+
endif()
77+
add_test(NAME RasWordLoopTest COMMAND RasWordLoopTest)
78+
set_tests_properties(RasWordLoopTest PROPERTIES TIMEOUT 10)
5779
endif ()
5880

5981
# These tests call vendored codec APIs directly to create inputs that exercise

‎TestAPI/testGifPlaybackBounds.cpp‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
}

‎TestAPI/testRasWordLoop.cpp‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include "FreeImage.h"
2+
3+
#include <cstdio>
4+
#include <cstdlib>
5+
#include <vector>
6+
7+
static void append_be32(std::vector<BYTE> &file, unsigned value) {
8+
file.push_back(static_cast<BYTE>(value >> 24));
9+
file.push_back(static_cast<BYTE>(value >> 16));
10+
file.push_back(static_cast<BYTE>(value >> 8));
11+
file.push_back(static_cast<BYTE>(value));
12+
}
13+
14+
int main() {
15+
const unsigned width = 70000;
16+
const unsigned height = 1;
17+
const unsigned data_size = width * 3;
18+
std::vector<BYTE> file;
19+
file.reserve(32 + data_size);
20+
append_be32(file, 0x59A66A95); // Sun raster magic
21+
append_be32(file, width);
22+
append_be32(file, height);
23+
append_be32(file, 24); // depth
24+
append_be32(file, data_size);
25+
append_be32(file, 1); // RT_STANDARD
26+
append_be32(file, 0); // RMT_NONE
27+
append_be32(file, 0); // no color map
28+
file.insert(file.end(), data_size, 0);
29+
30+
FreeImage_Initialise();
31+
FIMEMORY *memory = FreeImage_OpenMemory(file.data(), static_cast<DWORD>(file.size()));
32+
if(memory == NULL) {
33+
std::fprintf(stderr, "RAS word-loop test failed: could not open memory stream\n");
34+
FreeImage_DeInitialise();
35+
return 1;
36+
}
37+
38+
FIBITMAP *image = FreeImage_LoadFromMemory(FIF_RAS, memory, 0);
39+
const bool valid = image != NULL && FreeImage_GetWidth(image) == width &&
40+
FreeImage_GetHeight(image) == height && FreeImage_GetBPP(image) == 24;
41+
if(image != NULL) {
42+
FreeImage_Unload(image);
43+
}
44+
FreeImage_CloseMemory(memory);
45+
FreeImage_DeInitialise();
46+
if(!valid) {
47+
std::fprintf(stderr, "RAS word-loop test failed: large valid raster did not decode\n");
48+
return 1;
49+
}
50+
return 0;
51+
}

0 commit comments

Comments
 (0)