gifenc 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.yardopts +1 -0
- data/README.md +69 -0
- data/docs/Specification.md +2477 -0
- data/lib/color_table.rb +306 -0
- data/lib/errors.rb +24 -0
- data/lib/extensions.rb +275 -0
- data/lib/geometry.rb +118 -0
- data/lib/gif.rb +228 -0
- data/lib/gifenc.rb +60 -0
- data/lib/image.rb +512 -0
- data/lib/util.rb +46 -0
- metadata +79 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 8f9e18acc20b6acafb26735998f88134ede1cef587114377b996c6ec1c3e6432
|
|
4
|
+
data.tar.gz: 7a18a82e735f39c023bf9b7cf4918ad13fa2e0ea59e6c5594f9ddf969f7e8624
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: b11f2d061f5443a35d4134f077a2ae529b5084bde5ae42f449fb11c7ac0b8c9c7b7c359110b978494e2f96ac5b0f8bc9cae8e0156d1fb5598e22f2c06d92f2f1
|
|
7
|
+
data.tar.gz: f9c1e03801eebffbf4e78c48a90c49e040e1b87f3bc3f8d34c8ec8d069008456276e5c3333d5af53edfe4006518ec2f65ff92bdcf4730856556615682c41e411
|
data/.yardopts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lib/**/*.rb -m markdown --no-private - docs/**/*
|
data/README.md
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
# gifenc
|
|
2
|
+
|
|
3
|
+
A pure Ruby library with no external dependencies that allows to encode, decode and edit GIFs. Its main goals are to:
|
|
4
|
+
|
|
5
|
+
* Support the full GIF [specification](https://www.w3.org/Graphics/GIF/spec-gif89a.txt) for both encoding and decoding.
|
|
6
|
+
* Have a decent suite of editing functionalities, so that the need for external tools is avoided as much as possible.
|
|
7
|
+
* Have a succint and comfortable syntax to use.
|
|
8
|
+
|
|
9
|
+
Currently, the specs are almost fully supported for encoding. Decoding is not yet available, and the editing methods are very limited.
|
|
10
|
+
|
|
11
|
+
## A first example
|
|
12
|
+
|
|
13
|
+
Consider the following GIF and the variation next to it. They already showcase most of the main elements of the format, including global and local color tables, transparency, animation, and different disposal methods. It also makes use of some basic drawing methods:
|
|
14
|
+
|
|
15
|
+
<p align="center">
|
|
16
|
+
<img src="https://github.com/edelkas/gifenc/raw/master/res/first_a.gif">
|
|
17
|
+
<img src="https://github.com/edelkas/gifenc/raw/master/res/first_b.gif">
|
|
18
|
+
</p>
|
|
19
|
+
|
|
20
|
+
The code to generate the first version with `Gifenc` is the following:
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
require 'gifenc'
|
|
24
|
+
|
|
25
|
+
# Build a couple color tables
|
|
26
|
+
reds = Gifenc::ColorTable.new(64.times.map{ |c| 4 * c << 16 | 0x40 } + [0])
|
|
27
|
+
greens = Gifenc::ColorTable.new(4.times.map{ |c| 64 * c << 8 | 0x40 })
|
|
28
|
+
|
|
29
|
+
# Paint a first frame that will act as a background, using a local color table
|
|
30
|
+
gif = Gifenc::Gif.new(128, 128, gct: reds, loops: -1)
|
|
31
|
+
gif.images << Gifenc::Image.new(128, 128, lct: greens, color: 0, delay: 2, trans_color: 0)
|
|
32
|
+
(1 ... 4).each do |z|
|
|
33
|
+
gif.images.last.rect(16 * z, 16 * z, 128 - 32 * z, 128 - 32 * z, z, z)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Add animation frames drawing a gradient, using the global color table
|
|
37
|
+
(0 ... 8).each do |y|
|
|
38
|
+
(0 ... 8).each do |x|
|
|
39
|
+
gif.images << Gifenc::Image.new(
|
|
40
|
+
14, 14, 16 * x + 1, 16 * y + 1, color: 8 * y + x, delay: 5, trans_color: 64
|
|
41
|
+
).rect(4, 4, 6, 6, 64, 64)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Export the GIF to a file
|
|
46
|
+
gif.save('test.gif')
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
Let's see a step-by-step overview, refer to the following sections for an in-depth explanation of the actual details for each of the topics involved.
|
|
50
|
+
1. The first thing we do is create two **Color Tables**, one with red shades, and another with green shades. Since GIF is an indexed image format, it can only use colors from predefined palettes of at most 256 colors. `Gifenc` comes equipped with several default ones, but you can build your own, and operate with them.
|
|
51
|
+
2. We create the GIF object. We select the red color table to be the **GCT** (_Global Color Table_), which is used for all frames that do not contain an explicit **LCT** (_Local Color Table_). We also set the GIF to loop indefinitely.
|
|
52
|
+
3. We create the first frame, this will act as the background. We use the green color table as LCT for this frame. We set a few attributes, such as the default color of the canvas, the length of the frame, and the color used for transparency. We draw a sequence of centered green squares, they will help to see the transparency of the next frames.
|
|
53
|
+
4. Now, we create a sequence of frames, each of them being a small square located at an offset of the canvas. Since they have no LCT, they will use the GCT, and will thus be red. We draw an even smaller square in their center with the transparent color, the background will then show through these holes in the GIF.
|
|
54
|
+
5. Finally, we export the GIF to a file, and voilà, we're done!
|
|
55
|
+
|
|
56
|
+
Producing the second variation is surprisingly simple. It suffices to add the option `disposal: Gifenc::DISPOSAL_PREV` to the frames (except for the background). More on this later.
|
|
57
|
+
|
|
58
|
+
See the [Examples](examples) folder for more code samples; the resulting GIFs are stored [here](res).
|
|
59
|
+
|
|
60
|
+
## Resources
|
|
61
|
+
|
|
62
|
+
The following are a few of the excellent resources one can find on the net to get a deep understanding of the GIF file format.
|
|
63
|
+
- [What's in a GIF](http://www.matthewflickinger.com/lab/whatsinagif/): Comprehensive description of the GIF file format, LZW compression, and other related aspects. Illustrated with diagrams and examples. Includes online tools to explore GIF contents.
|
|
64
|
+
- [Manipulating GIF Color Tables](https://www.codeproject.com/Articles/1042433/Manipulating-GIF-Color-Tables): Detailed breakdown of the format, including a very nice diagram. Develops an open source GIF manipulation tool in C#.
|
|
65
|
+
- [Inside the GIF file format](https://web.archive.org/web/20230315204422/https://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art011): Step by step explanation of a GIF decoder in C (has a similar article on [LZW compression](https://web.archive.org/web/20230315204422/http://commandlinefanatic.com/cgi-bin/showarticle.cgi?article=art010)).
|
|
66
|
+
- [Gifology](http://www.theimage.com/animation/toc/toc.html): Practical explanation of all the properties and elements of the GIF specification, with technical examples.
|
|
67
|
+
- [Web design in a nutshell](https://docstore.mik.ua/orelly/web2/wdesign/ch19_01.htm): Chapters 19 and 23 present an exposition of the GIF file format and how several elements of its specification work.
|
|
68
|
+
- [Wikipedia GIF article](https://en.wikipedia.org/wiki/GIF): History of the format, break down of its structure, and other related interesting topics.
|
|
69
|
+
- [GIF specification](https://www.w3.org/Graphics/GIF/spec-gif89a.txt): And of course, the original GIF specification, which details every field of every block.
|