Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-48038

Introduce a QAnimatedImage class

    XMLWordPrintable

Details

    Description

      Introduce a QAnimatedImage class, this could be used then as a container for movie images in a similar way as QImage is used currently.

      The following is quoted with permission from the original requester:

      "The problem with QMovie is that it is a QObject - you can't easily copy it if you want multiple instances of the same animation. This isn't a problem for my situation, as we only one copy of the animation showing at a time, but it is still a concern for the QQuickImageProvider API. And it is probably why QQuickImageProvider doesn't support animated images already.

      I think there ought to be something like QAnimatedImage. This class acts as a container for the frames, just like QImage does for normal/static images.

      And then, a QMovie works with a QAnimatedImage. The QMovie tracks what frame it is on, and how long remains before switching to the next frame, etc (e.g. QMovie's currentFrameNumber, currentImage, currentPixmap, jumpToFrame, nextFrameDelay, setBackgroundColor). The QAnimatedImage takes care of reading/caching the relevant data (the frame image, and how long this frame should be displayed) from the file (e.g. frameCount, frameRect, isValid, loopCount, setFileName, etc). This allows multiple QMovies to access the QAnimatedImage without stepping on eachother's toes.

      The one thing that I would think you need to watch out for is using QMovie in one thread to view the animation, and a different thread trying to have the animated image read in more data from the file. A useful use-case.

      I would imagine that the QAnimatedImage should be able to be "incomplete", partially read the file, and/or not caching the data. Sort of a "read the data on demand" type functionality, which is what QMovie does. This is so that you don't have to read/load the whole file in one go.

      The QAnimatedImage would be implicitly shared, just like QImage. I imagine that it would use a QImageReader in the background to do the actual reading of the file. After it is done reading the file, it can close the native file.
      (Fixing a problem when reading MNG files - both libmng and QMovie keep a copy of the image, consuming twice as much memory as needed.)

      class QAnimatedImageFrame {
         QImage frameImage;
         int duration;
      };
      
      class QAnimatedImage {
         QAnimatedImage(QString filename);
         QString filename() const;
         bool isValid() const;
         int frameCount() const;
         QRect frameSize() const;
         QAnimatedImageFrame frame(int index) const;
         bool isCompletelyRead() const;
         bool hasFrameLoaded(int index);
         bool loadFrame(int frame);
         bool loadAllFrames();
         // load error?
         QString errorString();
         // allow user to manually create their own animated image...
         void setFrame(int index, QFrameImageFrame frame);
      };
      

      Attachments

        Issue Links

          Activity

            People

              vgt Eirik Aavitsland
              andysh Andy Shaw
              Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated: