GIF Frame and delay preview test code

Share your advanced PureBasic knowledge/code with the community.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

GIF Frame and delay preview test code

Post by walbus »

This code is a part from BucketFill advanced
http://www.purebasic.fr/english/viewtop ... 12&t=66927

Code: Select all

; GIF Frame and delay preview test code
; This code generate a complete sheet from a GIF and also a separately Info file
; You must see a grey background with a grid on the transparence GIF parts and each frame exactely, then it is OK
; Author W. Albus - http://www.nachtoptik.de - http://www.quick-aes-256.de

UsePNGImageDecoder()
UseJPEGImageDecoder()
UseGIFImageDecoder()
UsePNGImageEncoder()

EnableExplicit

Define gif_ID, sheet_ID, i, ii, iii, iiii, file
Define gif_width, gif_height, sheet_width, sheet_height, frames, frames_x, frames_y
Define path$

Define background_color=$808080 ; Set here The Background color for the transparence color

path$=OpenFileRequester("Select a GIF picture", "", "", 0)
If path$="" : End : EndIf

gif_ID=LoadImage(#PB_Any, path$) ; Sprite mode

gif_width=ImageWidth(gif_ID)
gif_height=ImageHeight(gif_ID)
frames=ImageFrameCount(gif_ID)
frames_x=Sqr(frames)
frames_y=frames/frames_x+1
sheet_width=frames_x*gif_width
sheet_height=frames_y*gif_height

For i=0 To frames-1
  iiii=iii : ii+gif_width
  If ii=>sheet_width
    ii=0 : iii+gif_height
  EndIf   
Next i

sheet_ID=CreateImage(#PB_Any, sheet_width, iiii+gif_height, 24, background_color)

StartDrawing(ImageOutput(sheet_ID))
For i=1 To sheet_width-1 Step 3
  LineXY(i, 0, i, sheet_height, $DCDCDC)
Next i
For i=1 To sheet_height-1 Step 3
  LineXY(0, i, sheet_width, i, $DCDCDC)
Next i
StopDrawing()

file=OpenFile(#PB_Any, GetPathPart(path$)+GetFilePart(path$, #PB_FileSystem_NoExtension)+"_sheet_info.txt")

WriteStringN(file, "         Frames #"+RSet(Str(frames), 8, "0 "), #PB_Ascii)
WriteStringN(file, "    Frame width #"+RSet(Str(gif_width), 8, "0 "), #PB_Ascii)
WriteStringN(file, "   Frame height #"+RSet(Str(gif_height), 8, "0 "), #PB_Ascii)

ii=0 : iii=0
For i=0 To frames-1
  SetImageFrame(gif_ID, i)
  WriteStringN(file, "  Frame / delay :"+RSet(Str(i), 8, "0 ")+" *"+RSet(Str(GetImageFrameDelay(gif_ID)), 8, "0"), #PB_Ascii)
  StartDrawing(ImageOutput(sheet_ID))
  DrawingMode(#PB_2DDrawing_AlphaBlend)
  DrawImage(ImageID(gif_ID), ii, iii)
  StopDrawing()
  ii+gif_width
  If ii=>sheet_width
    ii=0 : iii+gif_height
  EndIf   
Next i

CloseFile(file)

SaveImage(sheet_ID, GetPathPart(path$)+GetFilePart(path$, #PB_FileSystem_NoExtension)+".png", #PB_ImagePlugin_PNG, 10, 24)
Last edited by walbus on Tue Feb 28, 2017 7:59 pm, edited 3 times in total.
walbus
Addict
Addict
Posts: 929
Joined: Sat Mar 02, 2013 9:17 am

Re: GIF Frame and delay preview test code

Post by walbus »

I see, users have problems with the GIF structures and the delay
So, i have the code a little enhanced
Now you become also a delay table and a little more info
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5494
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: GIF Frame and delay preview test code

Post by Kwai chang caine »

Cool i have one image with all the frames inside
Thanks for sharing 8)
ImageThe happiness is a road...
Not a destination
Post Reply