S
S
StMechanus2013-11-03 16:25:28
gif
StMechanus, 2013-11-03 16:25:28

GIF Frame Counting

How to count the number of frames (images) in a gif animation with only the byte code of the file? I read the specification, found out that the image descriptor starts with the value 0x2C, but how to determine that this is a frame description, and not some pixel or something else?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
F
Fil, 2013-11-04
@Fil

There is no easy way, you need to read the entire file and count the frames. And in order to understand that this is exactly the beginning of the frame, and not something else, you need to read section by section. In the animated GIF, the image descriptor after the Graphic Control Extension (one per frame) is what we need to count. The algorithm is something like this:

  • Reading the Header section
  • Read the Logical Screen Descriptor section. From it we learn the type of color table: global or local. We also find out the size of the table.
  • If global, calculate the length of the Global Color Table section and skip.
  • Then, in the animated gif, there will be an Application Extension block (21 FF) section. Reading.
  • And then we start the cycle of reading sections of the Graphic Control Extension (GCE, 21 F9). Each section corresponds to one frame.
  •    Reading GCE
  •    Then Image Descriptor
  •    After that there will be several sections of LZW codes, before the beginning of which there will be their length. Knowing the lengths, we jump over these sections one by one. We find ourselves at the beginning of the next GCE.

Perhaps it's easier to use a third-party solution :)

S
StMechanus, 2013-11-04
@StMechanus

I said the same, but in response I received "We are not looking for simple ways";)

D
demimurych, 2013-11-04
@demimurych

It is enough to use the identify utility from the Imagemagick package. Parsing its output is not difficult.
www.imagemagick.org/Usage/anim_basics/#identify

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question