How to get frame count of an image?

Blitz3D Forums/Blitz3D Programming/How to get frame count of an image?

Guy Fawkes(Posted 2012) [#1]
Hi all, just as the title describes. How do you get a count of total frames in an animated image? I've SEEN this function created before.. I just can't remember who and where.


Yasha(Posted 2012) [#2]
Are you-

1) sure you've seen this done?

2) sure you've seen it done in pure B3D? (There's always the option of hacking the image structs, but that doesn't really count.)

Anyway, my first reaction is to wonder if you should really need this function. Since there's no way to create or load an animated image without first specifying how many frames it should have as you load it, wouldn't it be infinitely easier to just store that number somewhere the "play" function (or whatever you need to do with the image) can find it?


Guy Fawkes(Posted 2012) [#3]
well, i need a way of counting up how many frames are in an animated image. now, there MAY be one way, but i dont know how to write it. what i could essentially do, is use a for loop, stepped by 32, up to the width & height of the image, and then count how many 32 size squares it returns. but i dont know how to do that...


Guy Fawkes(Posted 2012) [#4]
Something like this:





but i KNOW thats not right... theres GOTTA be a way to return the number of 32 size squares...


Yasha(Posted 2012) [#5]
This line:

Global image = LoadAnimImage("sprites/levelsprites.png", 32, 32, 0, 108)


...says that there are 108 frames. My point above being, you already know this, so why do you want to calculate it again? Just store that number in the same object that's holding the image filename and handle and stuff, for later use.

(Your suggested method in post #3 sounds fine, but it would require you to load the image again as a non-animated one in order to get access to the size of the whole buffer - not very efficient).


Guy Fawkes(Posted 2012) [#6]
Well, i didnt even KNOW there was 108 frames until i kept on doing trial and error with the image. it would give me back an error message stating that there wasnt enough frames in the image, or i had too many frames. i dont want to do that for EVERY single framed image i have... i have thousands of images that are EXACTLY like that :)


Yasha(Posted 2012) [#7]
All I can say is that there's something wrong with your process here. You're supposed to know the number of frames in an animated image in advance - this is something you get with the image, before passing it to the program at all.

To simply work out the number of frames in an image from its dimensions, divide the width and height of the image by the width and height of an individual frame (using integer maths, so that the number rounds down, e.g. 63 / 32 = 1), then multiply those together. Thus a 64x128 image potentially contains (64 / 32 = 2) * (128 / 32 = 4) = 8 32x32 frames.

You should do this in advance, not at runtime, so you don't really need to do it in B3D.

Note however that the artist may not have used all the space; an image may contain more than one animation strip starting from somewhere other than zero; etc. Simply assuming all of the square footage equates to one animation is asking for trouble - again, this is important metadata that the artist should have given you already.


Guy Fawkes(Posted 2012) [#8]
Well, Im using sprites from the web, to learn how to use anim images. that is why i asked :)


Matty(Posted 2012) [#9]
image =loadimage("animatedimage.bmp")
;assuming size is 32x32 of each frame: (you do need to know at least *something* about the animated images in order to extract frames)
frameswide = imagewidth(image) / 32
frameshigh = imageheight(image) / 32
totalframes = frameswide * frameshigh
freeimage image
image = loadanimimage("animatedimage.bmp",32,32,0,totalframes)


Last edited 2012


Guy Fawkes(Posted 2012) [#10]
AH YES! THAT'S IT! THANK YOU, MATTY! =D


Ginger Tea(Posted 2012) [#11]
Well, Im using sprites from the web, to learn how to use anim images. that is why i asked :)


Wherein lies a problem, 100 artists 100 different methods, some are square (above code would work) others rectangular (above code would not work)

Some run the images on seperate lines, others bleed into each other and you have to visually check the file (in preview mspaint paint.net gimp or whatever) to see where the walk ends and the running begins.
The above code would give you a tonne of blank frames as the original image has alot of white space.

(I've seen alot of sonic sprite sheets and they predominantly have white spaces all over the show)

For the bunched up types:
If the artist supplied a text file stating which grid reference was which animation, it takes the burden off of you to work it out from looking at it.


Guy Fawkes(Posted 2012) [#12]
That is where I add to the code, a frame count selection. Where I can select a range of different frames. :)


Also, I only use square sprites.