Skip to content

Commit 76595af

Browse files
First version, only decoder
1 parent 1af1565 commit 76595af

File tree

4 files changed

+635
-1
lines changed

4 files changed

+635
-1
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017
3+
Copyright (c) 2017 Egor Skriptunoff
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,36 @@
11
# pure_lua_GIF
2+
23
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+
```

example.lua

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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()

0 commit comments

Comments
 (0)