8000 feat: updated imageCodec by diegogarciar · Pull Request #10 · saytoonz/flutter_gif · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: updated imageCodec #10

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
8000
from

Conversation

diegogarciar
Copy link

I updated the image codec to support flutter3.16, I also had to revert this change because it doesn't support frames with duration below 1 second

#3

@fernan542
Copy link

You should maintained the loop and instead added the validation for milliseconds.

@diegogarciar
Copy link
Author
diegogarciar commented May 6, 2024

Hello @fernan542, Just revisiting this, do you know exactly what the loop does?
I agree that I reverted it on my motivation to keep using the version that I had on my pubspec as I wanted to avoid a regression testing on all gif animations with the introduction of that loop.
Why should we repeat the frames for each second?

and is this your suggestion?

if(duration>0){
    for (int sec = 1; sec <= duration; sec++) {
       infos.add(ImageInfo(image: frameInfo.image));
    }
}else{
    infos.add(ImageInfo(image: frameInfo.image));
 }

@fernan542
Copy link

Hey @diegogarciar.

Why should we repeat the frames for each second?

I encountered a problem where the codec treats frames with similar design as only 1 second duration, even if they have 3 seconds durations of frame. So to fix that, I need to add another loop for each codec to determine the actual seconds that the current codec or frame has in order to correctly dissect the GIF's frame per second.

The goal of this package is to render each frame per second 824E that's why I've added a loop in order to correctly render each frame per second, not per codec. Now based on your use-case, you want to support milliseconds, you might want to change the duration to get the milliseconds instead of seconds.

Formula from GPT:
To calculate the time interval between frames for a given frames per second (fps), you can use the formula:

Time Interval (in milliseconds)=1/FPS×1000

Given your requirement of 15 to 30 frames per second (fps), you would have:

For 15 fps:
Time Interval=1/15*1000=66.67 milliseconds

For 30 fps:
Time Interval=1/30*1000=33.33 milliseconds

So, you would need to increment by approximately 66.67 milliseconds for 15 fps and 33.33 milliseconds for 30 fps.

Now to apply that in your code (P.S: I didn't test this code, but you get the point)

const _fps15 = 66.67;
const _fps30 = 33.33;
  final buffer = await ImmutableBuffer.fromUint8List(bytes);
  ui.Codec codec = await PaintingBinding.instance.instantiateImageCodecWithSize(buffer);
  infos = [];

   // Log how many frames are decoded by codec.
   print('frames: ${codec.frameCount}');
   
    for (int i = 0; i < codec.frameCount; i++) {
      final frameInfo = await codec.getNextFrame();
      // Use milliseconds.
      final duration = frameInfo.duration.inMilliseconds;
  
     // Means the GIF duration is shorter than a single fps.
    // Either throw an error or early return.
     if (duration <= _fps15 ) {
           infos.add(ImageInfo(image: frameInfo.image));
           // Exit the loop immediately since there's no other frame left.
           break;
     }
    
     // Loop through each rate. Each rate is incremented by the computation previously.
      for (int rate = 1; rate <= duration; rate+=_fps15 ) {
        infos.add(ImageInfo(image: frameInfo.image));
      }
  }

  GifImage.cache.caches.putIfAbsent(key, () => infos);
  return infos;

If this does not work, I think you may want to read the (FrameInfo docs)[https://api.flutter.dev/flutter/dart-ui/FrameInfo-class.html] because AFAIK the rate is limited to second.

One thing that I'm wrong about is deleting the cache of GIF. I didn't notice that I'd pushed my version for cacheing; I intended to remove it because it causes a problem with my dynamic image cacheing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0