Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use file animation timings #37

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Source/RuntimeImageLoader/Private/Helpers/GIFLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ class IGIFLoader
virtual const int32 GetHeight() const = 0;
virtual const int32 GetTotalFrames() const = 0;
virtual const FColor* GetNextFrame(int32 FrameIndex) = 0;
virtual const float GetNextFrameDelay(int32 FrameIndex) = 0;
};

class FGIFLoaderFactory
{
public:
static TUniquePtr<IGIFLoader> CreateLoader(const FString& GifURI, const TArray<uint8>& GifData);
};
};
9 changes: 8 additions & 1 deletion Source/RuntimeImageLoader/Private/Helpers/NSGIFLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ bool FNSGIFLoader::DecodeInternal(nsgif_t* gif, bool first)
return false;
}

Timestamps.Add(delay_cs * 10.f / 1000.f);

if (frame_new < frame_prev) {
// Must be an animation that loops. We only care about
// decoding each frame once in this utility.
Expand Down Expand Up @@ -197,4 +199,9 @@ const FColor* FNSGIFLoader::GetNextFrame(int32 FrameIndex)
return &FColor::Black; // return a default FColor value, like FColor::Black
}
}
#endif //WITH_LIBNSGIF

const float FNSGIFLoader::GetNextFrameDelay(int32 FrameIndex)
{
return Timestamps[FrameIndex];
}
#endif //WITH_LIBNSGIF
2 changes: 2 additions & 0 deletions Source/RuntimeImageLoader/Private/Helpers/NSGIFLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class FNSGIFLoader : public IGIFLoader

public: /** Get Next Frame Texture Data*/
const FColor* GetNextFrame(int32 FrameIndex) override;
const float GetNextFrameDelay(int32 FrameIndex) override;
bool DecodeGIF(TArray<uint8>&& GifBytes) override;

protected: /** Bitmap Callbacks Methods */
Expand All @@ -44,6 +45,7 @@ class FNSGIFLoader : public IGIFLoader
nsgif_t* Gif;
const nsgif_info_t* Info;
TArray<FColor> TextureData;
TArray<float> Timestamps;

int32 Width = -1;
int32 Height = -1;
Expand Down
8 changes: 7 additions & 1 deletion Source/RuntimeImageLoader/Private/Helpers/WEBPGIFLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ const FColor* FWEBPGIFLoader::GetNextFrame(int32 FrameIndex)
}
}

const float FWEBPGIFLoader::GetNextFrameDelay(int32 FrameIndex)
{
return Timestamps[FrameIndex];
}


bool FWEBPGIFLoader::DecodeGIF(TArray<uint8>&& GifBytes)
{
Expand Down Expand Up @@ -121,6 +126,7 @@ bool FWEBPGIFLoader::DecodeGIF(TArray<uint8>&& GifBytes)
SetError("Failed to decode .webp frame. Please check input data is valid!");
return false;
}
Timestamps.Add(timestamp / 1000.f);

FPlatformMemory::Memcpy((uint8_t*)TextureData.GetData() + FrameInd * FrameBytes, DecodedData, FrameBytes);
++FrameInd;
Expand All @@ -134,7 +140,7 @@ bool FWEBPGIFLoader::DecodeGIF(TArray<uint8>&& GifBytes)

return true;
}

Timestamps.Add(100.f);
TotalFrameCount = 1;

TextureData.SetNumZeroed(GetWidth() * GetHeight());
Expand Down
4 changes: 3 additions & 1 deletion Source/RuntimeImageLoader/Private/Helpers/WEBPGIFLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class FWEBPGIFLoader : public IGIFLoader

public: /** Get Next Frame Texture Data*/
const FColor* GetNextFrame(int32 FrameIndex) override;
bool DecodeGIF(TArray<uint8>&& GifBytes) override;
const float GetNextFrameDelay(int32 FrameIndex);
bool DecodeGIF(TArray<uint8>&& GifBytes) override;

static bool HasValidWebpHeader(const TArray<uint8>& GifBytes);

Expand All @@ -38,6 +39,7 @@ class FWEBPGIFLoader : public IGIFLoader

private:
TArray<FColor> TextureData;
TArray<float> Timestamps;

int32 Width = -1;
int32 Height = -1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void UAnimatedTexture2D::Tick(float DeltaTime)
if (!Decoder) return;

FrameTime += DeltaTime * PlayRate;
if (FrameTime < DefaultFrameDelay)
if (FrameTime < Decoder->GetNextFrameDelay(CurrentFrame))
return;

FrameTime = 0;
Expand Down