#1883·stb

具有许多帧的 GIF 表现非常糟糕

作者: levicki创建于 2026年2月3日更新于 2026年8月9日

if (stbi__gif_test(_Ctx)) { int Layers = 0; stbi_uc* U = nullptr; stbi_uc* TwoBack = nullptr; stbi__gif G; int Stride = 0; std::vector<stbi_uc*> FramePointers; std::vector<int> DelayValues; memset(&G, 0, sizeof(G)); do { U = stbi__gif_load_next(_Ctx, &G, _Comp, _ReqComp, TwoBack); if (U == (stbi_uc*)_Ctx) { U = nullptr; } if (U != nullptr) { *_X = G.w; *_Y = G.h; Stride = G.w * G.h * 4; stbi_uc* FrameData = (stbi_uc*)stbi__malloc(Stride); if (FrameData == nullptr) { for (stbi_uc* P : FramePointers) { STBI_FREE(P); } return stbi__load_gif_main_outofmem(&G, nullptr, _Delays); } memcpy(FrameData, U, Stride); FramePointers.push_back(FrameData); DelayValues.push_back(G.delay); Layers = (int)FramePointers.size(); if (Layers >= 2) { TwoBack = FramePointers[Layers - 2]; } } } while (U != nullptr); STBI_FREE(G.out); STBI_FREE(G.history); STBI_FREE(G.background); if (FramePointers.empty()) { return nullptr; } size_t TotalSize = (size_t)Layers * Stride; stbi_uc* Out = (stbi_uc*)stbi__malloc(TotalSize); if (Out != nullptr) { for (int i = 0; i < Layers; ++i) { memcpy(Out + (i * Stride), FramePointers[i], Stride); STBI_FREE(FramePointers[i]); } if (_Delays != nullptr) { *_Delays = (int*)stbi__malloc(Layers * sizeof(int)); if (*_Delays != nullptr) { memcpy(*_Delays, DelayValues.data(), Layers * sizeof(int)); } } } if (_ReqComp != 0 && _ReqComp != 4) { Out = (stbi_uc*)stbi__malloc(TotalSize); if (Out != nullptr) { for (int i = 0; i < Layers; ++i) { memcpy(Out + (i * Stride), FramePointers[i], Stride); STBI_FREE(FramePointers[i]); } if (_Delays != nullptr) { *_Delays = (int*)stbi__malloc(Layers * sizeof(int)); if (*_Delays != nullptr) { memcpy(*_Delays, DelayValues.data(), Layers * sizeof(int)); } } } }