gifenc 0.1.0 → 0.2.1

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.
data/lib/gif.rb CHANGED
@@ -93,6 +93,16 @@ module Gifenc
93
93
  # @return [Array<Extension>] Extension list.
94
94
  attr_accessor :extensions
95
95
 
96
+ # Automatically destroy each image right after processing it during encoding.
97
+ # This is intended to save as much memory as possible whenever that's sensitive.
98
+ # Normally, since the pixel data for each image gets encoded and appended to
99
+ # the final output, the data is briefly duplicated in memory. This avoids it.
100
+ # @note This will make the images unusable after the first encoding! So if
101
+ # you plan on reusing them multiple times, do not enable this setting, or
102
+ # only do so before the very last encoding.
103
+ # @return [Boolean] Auto-destroy mode.
104
+ attr_accessor :destroy
105
+
96
106
  # Creates a new GIF object.
97
107
  # @param width [Integer] Width of the logical screen (canvas) in pixels (see {#width} and {#resize}).
98
108
  # @param height [Integer] Height of the logical screen (canvas) in pixels (see {#height} and {#resize}).
@@ -104,6 +114,7 @@ module Gifenc
104
114
  # @param loops [Integer] Amount of times to loop the GIF (see {#loops}).
105
115
  # @param bg [Integer] Background color (see {#bg}).
106
116
  # @param ar [Integer] Pixel aspect ratio (see {#ar}).
117
+ # @param destroy [Boolean] Auto-destroy each image right after encoding (see {#destroy}).
107
118
  def initialize(
108
119
  width,
109
120
  height,
@@ -115,7 +126,8 @@ module Gifenc
115
126
  disposal: nil,
116
127
  loops: DEFAULT_LOOPS,
117
128
  bg: DEFAULT_BACKGROUND,
118
- ar: DEFAULT_ASPECT_RATIO
129
+ ar: DEFAULT_ASPECT_RATIO,
130
+ destroy: false
119
131
  )
120
132
  # GIF attributes
121
133
  @width = width
@@ -135,6 +147,9 @@ module Gifenc
135
147
  @images = []
136
148
  @extensions = []
137
149
 
150
+ # Other
151
+ @destroy = destroy
152
+
138
153
  # If we want the GIF to loop, then add the Netscape Extension
139
154
  self.loops = loops
140
155
  end
@@ -159,7 +174,13 @@ module Gifenc
159
174
  @extensions.each{ |e| e.encode(stream) }
160
175
 
161
176
  # Encode frames containing image data (and local extensions)
162
- @images.each{ |f| f.encode(stream) }
177
+ @images.size.times.each{ |i|
178
+ @images[i].encode(stream)
179
+ if @destroy
180
+ @images[i].destroy
181
+ @images[i] = nil
182
+ end
183
+ }
163
184
 
164
185
  # Trailer
165
186
  stream << TRAILER
@@ -208,6 +229,30 @@ module Gifenc
208
229
  self
209
230
  end
210
231
 
232
+ # Duplicates all frames in reverse order. Generates a boomerang effect,
233
+ # particularly neat when looped.
234
+ # @param first [Boolean] Whether to duplicate the very first frame. Probably
235
+ # not desired for looping GIFs, because then the first frame would appear
236
+ # twice in a row (at the start, and at the end). Useful for non-looping
237
+ # boomerangs.
238
+ # @return [Gif] The GIF object.
239
+ def boomerang(first: false)
240
+ (@images.size - 2).downto(first ? 0 : 1).each{ |i|
241
+ @images << @images[i].dup
242
+ }
243
+ self
244
+ end
245
+
246
+ # Shortcut to modify the delay of the GIF's last frame, intended to
247
+ # showcase the final result before looping it.
248
+ # @param delay [Integer] The time, in 1/100ths of a second, to show the last
249
+ # frame.
250
+ # @return [Gif] The GIF object.
251
+ def exhibit(delay = DEFAULT_EXHIBIT_TIME)
252
+ @images.last.delay = delay
253
+ self
254
+ end
255
+
211
256
  # Encode and write the GIF to a string.
212
257
  # @return [String] The string containing the encoded GIF file.
213
258
  def write
data/lib/gifenc.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'pry-byebug'
2
+
1
3
  # Gifenc is a pure Ruby library to encode, decode and edit GIF files. It aims
2
4
  # to eventually support the complete {file:docs/Specification.md specification}.
3
5
  # The Gifenc module serves as a namespace to encapsulate the functionality
@@ -49,8 +51,13 @@ module Gifenc
49
51
  # Default pixel aspect ratio (`0` = square). Note that this setting is ignored
50
52
  # by most decoders.
51
53
  DEFAULT_ASPECT_RATIO = 0
54
+
55
+ # Default time to exhibit GIF's last frame when selected (in 1/100ths of sec)
56
+ DEFAULT_EXHIBIT_TIME = 100
52
57
  end
53
58
 
59
+ require 'lzwrb'
60
+
54
61
  require_relative 'errors.rb'
55
62
  require_relative 'util.rb'
56
63
  require_relative 'geometry.rb'