Native image viewer supporting animated GIFs

I was recently playing with my image manipulation library, adding the ability to create/modify animated GIFs (works perfectly on Haiku, by the way). And then I realized I need a way to actually view the GIFs created. When working on other operating systems I just use viewnior because it is very lightweight. Now, it’s not like we lack ways to view animated GIFS on Haiku: qView seems to be the most lightweight, but there is also nomacs and others.
For now, I’m just using qView, but I wonder if there is a native image viewer able to show animated GIFs (and respecting frame times). Being able to extract image metadata would be a bonus but not required (we already have exiftool for that).

ShowImage doesn’t seem to support animated GIFs (it just shows the first frame), and Album is not designed for that. The only native way I found is to directly open the GIF on WebPositive. Am I missing something here?

Can MediaPlayer play them? It seems like the obvious place to me.

I think VLC also supports them

No, it cannot. It says it is not a media file, which is correct. Technically, an animated GIF is just a collection of images (“pages”), with additional metadata concerning frame rate etc. Running MediaPlayer from the command line also says “MIME Type = image/gif” which is correct as well. It is an image, not a media file. Most media players in other operating systems don’t support animated GIFs either.

Unsurprisingly, mpv does play animated GIFs (mpv basically accepts whatever file you drop in it, because it uses the swiss-knife called ffmpeg). But mpv is not a native application.
I don’t know if VLC is able to play GIFs (I didn’t try, not willing to install VLC with all its dependencies). But even if it can, it is not a native application either.

ShowImage doesn’t work with GIF? Sounds like ShowImage would be ideal for this use case.

It doesn’t. It only shows the first “page”, as it is called in GIF terminology (meaning image, frame). So do all image viewers not supporting animated GIFs (they only read the first page and that’s it, the rest of the GIF file is ignored). But yes, the ideal application for playing animated GIFS would be the default native image viewer, aka, ShowImage.

I don’t think an image viewer should ever play animations. I don’t see any distinction between an animated gif , an animated png and a video file with no audio track. Especially if you have timing information.

1 Like

ShowImage at a minimum should show each frame of the GIF file similar to how Apple’s Preview.app does.

Well, there is a reason most image viewers do play animated GIFs. Even ristretto, XFCE’s default image viewer (a simple one), does support animated GIFs (albeit it’s slow while loading the frames). On the other hand, the only media player I am aware of that does that is mpv (but then again, I wouldn’t call mpv just a media player, it’s more like a swiss knife tool).
MIME Type clearly says a GIF is an image. It is essentially a bunch of “palettized” (indexed) png images.

Anyway, I guess there isn’t a native way to view animated GIFs, other than directly loading the file in WebPositive. So I’ll stick with qView for now. It does the job pretty nicely and it’s pretty lightweight.

2 Likes

IMO, to the user it’s irrelevant which application opens their animated GIF as long as it’s opened on a simple double-click. Personally, aniGIFs behaving pretty much as a video clip, I wouldn’t be surprised if they are played by MediaPlayer.

As ShowImage uses data translators that return a bitmap for any supported image format, adding the needed support for playing an aniGIF probably isn’t trivial. Add to that that sooner or later people want to pause and step forward and backward etc., this starts to sound like much work…

OTOH, MediaPlayer uses ffmpeg that already supports aniGIFs. It has a nice scrobble slider, pause and frame-stepping.
Good news: MediaPlayer already plays aniGIFs. Start the FileType add-on with an aniGIF and change the type to e.g. video/mpeg. Presto, double-click opens in MediaPlayer.

Now, how to have MediaPlayer play back aniGIFs but leave normal GIFs to ShowImage? They both have the same MIME type image/gif it appears. Maybe we can “sniff” out aniGIFs and assign it a custom MIME type that opens with MediaPlayer. That may confuse other OSs, but OTOH those rely much on the file extension .gif, so they may be fine?

1 Like

Doesn’t seem to work for animated PNGs, FWIW. But there, image/apng is actually available to use. There doesn’t seem to be an image/agif. (Source)

It’s perfectly fine to invent MIME types as long as you use the x- notation. I think this should be the way to go, although our current sniffer might not be able to properly detect animated GIFs; AFAIK, the header may come late in the file, and with a full color palette won’t come before the first 780 bytes.

Agreed. As long it works, I don’t care which application is used to open the file. Although I humbly insist, an animated GIF is not a video file. I understand why people see it as such, but it has way more restrictions and its internal structure is completely different than any video file format. Essentially, an animated GIF is as much a “media” file as a normal image is.
FreeImage, an excellent and quite popular library, uses the term “multibitmap” for the structure of an animated GIF (not to be confused with the BMP file format, it has nothing to do with that). Such a name describes what it is much better, I think. I have seen it called a “multipage image” as well, which is an even more precise description.

Thank you for the lead. This indeed works… but not quite. MediaPlayer is able to play the animated GIF that way (and it even respects the frame times) but it crashes at the end:

Changing the video filetype to something else may give somewhat different results. Dependng on the video filetype, it may crash right away without playing anything, or it may play once without crashing, then crash right away if you try again.
Forcing MediaPlayer to treat animated GIFs as some kind of video files seems to be an interesting hack, so to speak. But it can’t replace an application designed to play animated GIFs properly.

Edit: I tried with smaller GIFs (e.g., I used 6 or 16 frames only). And MediaPlayer works without crashing. But, depending on how detailed the image are, just slightly more frames causes crashing - either in the end of playing, or right away in the beginning. Clearly you can’t do as much as you could using qView, nomacs, etc.

2 Likes

If it can be fixed to work without crashing, it should be enough as out-of-the-box support. If anyone cares enough for additional features to provide a nice 3rd party app, all the better.

Crashes might be fixable if reported. I suggest creating a ticket with working/non-working example files.

I think if ShowImage could at least offer previewing the frames of a GIF would be a nice new capability for that app.

1 Like

Luckily, ShowImage already has the infrastructure for that, as it can already show “pages” of multi-page formats like TIFF.

Maybe doing play-back support in ShowImage isn’t far off after all. Get the fps from the GIF translator and start a timer to go to the next page accordingly…

5 Likes

Maybe an extension to the sniffer, “in case of GIF search again at point X to see it this is a animated file” could make things easier ?

I’ve linked this thread to the existing Trac ticket #9835.

Flipping through the pages of an aniGIF in ShowImage should be possible after extending the GIF translator to recognize and serve pages. There are some peculiarities of GIFs that can complicate things:

  • A page doesn’t have to describe the whole image, but only provide the data of an area of it. By keeping the image data of the previous page, you can e.g. only provide the new data of the upper left corner and that’ll get painted over the old image.
  • You don’t necessarily have a uniform frames/s (or better pages/s) over the whole aniGIF. Every page can include info how long to display it before the next.

While the second point only applies if actual animations are to be supported and feels easily solvable, the first also impacts simple manual page flipping. This makes it all quite a bit more complicated, I suppose, esp. considering going to previous pages…

The MediaPlayer route, leaving all those headaches to the ffmpeg backend, does have its charms. :slight_smile:
(I didn’t check how ffmpeg deals with the above two points though…)

1 Like

Those two points heavily sound like video properties to me, and not “just an archive”.

Anyway, since MediaPlayer apparently already supports it it should be possible to let it accept agif files…

I was aware of those two complications. I just wanted a native way to play simple animated gifs, but I guess it’s time to address the gif complications as well.

For the purpose of testing, I created a procedural test gif image using my own image manipulation library. The testing gif contains 80 pages, but each page is palettized (indexed), and all of them share a common palette of just two colors. So, despite the rather large amount of pages, the resulting gif file is small. The animation shows a “swirling” effect growing exponentially from the center. This will help identifying differences in tests about gif complications.
The original “simple” image has a fixed 100 ms frame time and it is supposed to loop forever. WebPositve does display the test image properly, and it respects looping as well:
test
Now, if you manually change filetype to video/mpeg, MediaPlayer will play the animation with the correct frame time, but looping is not respected (it just plays once and that’s it). Fast forward / rewind controls won’t work, and if you drag the time bar to the beginning then press Play it will replay the animation, but with occasional crashes (those can’t be trivially reproduced, they just seem to happen randomly).

To test complication #1 in @humdinger’s post, you can easily optimize the image so that each page only provides new data and the rest is borrowed from the previous page. All you need to do is to use gifsicle, a great little tool (which is of course available in HaikuDepot):

gifsicle -O3 test.gif -o test_optimized.gif

This will create an optimized version of the test image. I am not posting that one here because there is no visible difference in the resulting image. However, the optimized image file is, as expected, smaller in size. You can verify each page only contains new data by loading the optimized gif to Gimp, or similar applications that are able to show each frame individually.
If you drop that optimized gif in WebPositive you won’t see any difference, it will render the image perfectly. Surprisingly enough, manually changing the filetype to video/mpeg will open the file with MediaPlayer, and the animation is rendered correctly even with the optimized version of the gif. However, all the issues mentioned before for the normal gif remain.

Next, to test varying frame time (complication #2), I modified the program that creates the image so that pages 21 and 61 (the frames with the maximum swirling effect) have a frame time equal of 1000 ms instead of 100 ms. This “pauses” the animation for one second at that point. Once again WebPositive will render the image properly, and it is aware of the varying frame time:
test_varying_frametime
However the trick to open the image with MediaPlayer does have the same issues as before, plus it completely ignores varying frame time. Animation is played as if all frames had the same frame time, which is not the case.

1 Like