Tell if a gif is animated

Hi all.

Is there is a way in xojo to tell if a gif is an animated gif, and not an animated gif?

I don’t want to display it, etc, I just want to know if it IS an animated gif.

Regards

From what I understand, a looping animated gif always has the string NETSCAPE2.0 at offset 784. So:

Dim imgfile As FolderItem ' assume populated
Dim bs As BinaryStream = BinaryStream.Open(imgfile)
bs.Position = 784
If bs.Read(11) = "NETSCAPE2.0" Then
  ' file is a looped animation

Animated gifs that don’t loop may not have this string, though.

@Michael_Cebasek,

Animated gif files have more than one frame (which sounds a bit obvious).

Not sure how use the Xojo “Picture” class to determine the count consistently (someone will), but MBS implements image magick as “GraphicsMagick” so that should work to count frames.

Something like:

 // 1. Point to your GIF image file
Dim f As FolderItem = SpecialFolder.Desktop.Child("myImage.gif")

If f <> Nil And f.Exists Then
  
  // 2. Instantiate the Image Array class to hold potential multi-frames
  Dim imageArray As New GMImageArrayMBS
  
  Try
    // 3. Read the image into the array
    imageArray.readImages(f.NativePath)
    
    // 4. Check the size of the array
    If imageArray.size > 1 Then
      MessageBox("This GIF is ANIMATED! It has " + imageArray.size.ToString + " frames.")
    Else
      MessageBox("This is a static, single-frame GIF.")
    End If
    
  Catch e As GMExceptionMBS
    MessageBox("Error processing the image: " + e.Message)
  End Try

End If

I hope that helps.

have you open the picture and looked at .ImageCount property if this work for a gif file?

Var p As Picture = Picture.Open(f)
Var count as integer = p.ImageCount

If this method returns > 1 then it’s animated.

Function GIFFrameCount(f As FolderItem) As Integer
  If f = Nil Or Not f.Exists Or f.IsFolder Then
    Return -1
  End If
  
  Var bs As BinaryStream = BinaryStream.Open(f, False)
  Var data As MemoryBlock = bs.Read(bs.Length)
  bs.Close
  
  data.LittleEndian = True
  Var size As Integer = data.Size
  
  
  If size < 13 Then // too small to be a gif
    Return -1
  End If
  
  If data.StringValue(0, 3) <> "GIF" Then
    Return -1
  End If
  
  Var pos As Integer = 13 //skip ahead
  
  Var screenPacked As Integer = data.UInt8Value(10)
  If (screenPacked And &h80) <> 0 Then
    Var globalEntries As Integer = 2 ^ ((screenPacked And &h07) + 1)
    pos = pos + globalEntries * 3
  End If
  
  Var frames As Integer = 0
  
  While pos < size
    Var blockType As Integer = data.UInt8Value(pos)
    pos = pos + 1
    
    Select Case blockType
      
    Case &h2C
      ' -- left, top, width, height (2 bytes each) and a packed byte. 
      If pos + 9 > size Then
        Exit While
      End If
      Var imagePacked As Integer = data.UInt8Value(pos + 8)
      pos = pos + 9
      
      If (imagePacked And &h80) <> 0 Then //skip local color table
        Var localEntries As Integer = 2 ^ ((imagePacked And &h07) + 1)
        pos = pos + localEntries * 3
      End If
      
      pos = pos + 1
      If pos > size Then
        Exit While
      End If
      
      frames = frames + 1
      
      While pos < size
        Var imageBlockLen As Integer = data.UInt8Value(pos)
        pos = pos + 1
        If imageBlockLen = 0 Then
          Exit While
        End If
        pos = pos + imageBlockLen
      Wend
      
    Case &h21
      
      pos = pos + 1
      While pos < size
        Var extBlockLen As Integer = data.UInt8Value(pos)
        pos = pos + 1
        If extBlockLen = 0 Then
          Exit While
        End If
        pos = pos + extBlockLen
      Wend
      
    Case &h3B
      
      Exit While //end of the gif
      
    Case &h00
      //ignore 0
      
    Case Else
      
      Exit While //unknown block
      
    End Select
  Wend
  
  Return frames
  
  Exception err As IOException //catch an io error
    Return -1
End Function

FWIW I’ve written an animated gif control for Xojo.

I’m still working on stability (it uses preemptive threads to load the gif) but once I have that nailed down if there’s enough interest I might release it as a xojo library.

While I find Christian’s method elegant and informative, I’m too wondering whether Picture.ImageCount would work, too.
(I don’t have time to test right now, just re-suggesting the idea)

i would expect gif is always an older format for animated images.
and newer is motion jpeg.

all other non animated jpg and lossless png

Unfortunately I’ve never been able to get Picture.ImageCount to return anything other than 0.

@Andrew_Paul_Dickey 's approach works well if you have MBS.

Thanks everyone. I will test some of these options and when I have a result (positive or negative) I will let you know.

Thank you ever so much for your suggestions.

Regards