File tree Expand file tree Collapse file tree 4 files changed +635
-1
lines changed Expand file tree Collapse file tree 4 files changed +635
-1
lines changed Original file line number Diff line number Diff line change 1
1
MIT License
2
2
3
- Copyright (c) 2017
3
+ Copyright (c) 2017 Egor Skriptunoff
4
4
5
5
Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
of this software and associated documentation files (the "Software"), to deal
Original file line number Diff line number Diff line change 1
1
# pure_lua_GIF
2
+
2
3
GIF decoder in pure Lua
4
+
5
+ ### Installation
6
+
7
+ Just copy "gif.lua" to Lua modules' folder.
8
+
9
+ ### Decoding
10
+
11
+ ``` lua
12
+ local filename = " SOME_PICTURE.gif"
13
+
14
+ -- Open GIF file as read-only
15
+ local gif = require ' gif' (filename )
16
+
17
+ -- Extract some information about this GIF
18
+ local w , h = gif .get_width_height ()
19
+ print (' Picture dimensions: ' .. w .. ' x ' .. h )
20
+ print (' Number of animation frames: ' .. gif .get_file_parameters ().number_of_images )
21
+ print (' Comment inside this GIF: ' .. (gif .get_file_parameters ().comment or ' NO_COMMENT' ))
22
+
23
+ -- Get the color of pixel with coordinates (2,0)
24
+ local matrix = gif .read_matrix () -- first animation frame of this GIF as 2D-matrix of colors
25
+ local row = 1 -- top row of the picture
26
+ local col = 3 -- third pixel from the left
27
+ local color = matrix [row ][col ]
28
+ if color == - 1 then
29
+ print (" This pixel is transparent" )
30
+ else
31
+ print ((" The color of this pixel in hexadecimal RRGGBB format is %06X" ):format (color ))
32
+ end
33
+
34
+ -- Close GIF file
35
+ gif .close ()
36
+ ```
Original file line number Diff line number Diff line change
1
+ -- Example of non-animated gif:
2
+ local filename = " mini-graphics-ducks-425811.gif"
3
+ -- This .GIF was downloaded from:
4
+ -- http://www.picgifs.com/mini-graphics/mini-graphics/ducks/mini-graphics-ducks-425811.gif
5
+
6
+ --[[
7
+ -- Example of animated gif:
8
+ local filename = "mini-graphics-cats-864491.gif"
9
+ -- This .GIF was downloaded from:
10
+ -- http://www.picgifs.com/mini-graphics/mini-graphics/cats/mini-graphics-cats-864491.gif
11
+ --]]
12
+
13
+ local gif = require ' gif' (filename )
14
+ local w , h = gif .get_width_height ()
15
+ print (' Picture dimensions: ' .. w .. ' x ' .. h )
16
+ print (' Total Frames: ' .. gif .get_file_parameters ().number_of_images )
17
+ print (' Comment: ' .. (gif .get_file_parameters ().comment or ' NO_COMMENT' ))
18
+ print (' Looped: ' .. (gif .get_file_parameters ().looped and ' YES' or ' NO' ))
19
+ repeat
20
+ local image_no = gif .get_image_parameters ().image_no
21
+ print (" Frame #" .. image_no )
22
+ local m = gif .read_matrix ()
23
+ -- Print the matrix for this frame
24
+ for row = 1 , # m do
25
+ local r = {}
26
+ for col = 1 , # m [1 ] do
27
+ local color = m [row ][col ]
28
+ if color == - 1 then -- Transparent color
29
+ color = " ------"
30
+ else -- Non-transparent color in 0xRRGGBB format
31
+ color = (" %06X" ):format (color )
32
+ end
33
+ table.insert (r , color )
34
+ end
35
+ print (table.concat (r , " " ))
36
+ end
37
+ until not gif .next_image () -- try to switch to next animation frame
38
+ gif .close ()
You can’t perform that action at this time.
0 commit comments