chunky_png 1.3.0 → 1.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,54 +1,54 @@
1
1
  module ChunkyPNG
2
2
  class Canvas
3
-
4
- # The ChunkyPNG::Canvas::Operations module defines methods to perform operations
5
- # on a {ChunkyPNG::Canvas}. The module is included into the Canvas class so all
6
- # these methods are available on every canvas.
3
+ # The ChunkyPNG::Canvas::Operations module defines methods to perform
4
+ # operations on a {ChunkyPNG::Canvas}. The module is included into the
5
+ # Canvas class so all these methods are available on every canvas.
7
6
  #
8
- # Note that some of these operations modify the canvas, while some operations return
9
- # a new canvas and leave the original intact.
7
+ # Note that some of these operations modify the canvas, while some
8
+ # operations return a new canvas and leave the original intact.
10
9
  #
11
10
  # @see ChunkyPNG::Canvas
12
11
  module Operations
13
-
14
- # Converts the canvas to grascale.
12
+ # Converts the canvas to grayscale.
15
13
  #
16
- # This method will modify the canvas. The obtain a new canvas and leave the
17
- # current instance intact, use {#grayscale} instead.
14
+ # This method will modify the canvas. The obtain a new canvas and leave
15
+ # the current instance intact, use {#grayscale} instead.
18
16
  #
19
17
  # @return [ChunkyPNG::Canvas] Returns itself, converted to grayscale.
20
18
  # @see {#grayscale}
21
19
  # @see {ChunkyPNG::Color#to_grayscale}
22
20
  def grayscale!
23
21
  pixels.map! { |pixel| ChunkyPNG::Color.to_grayscale(pixel) }
24
- return self
22
+ self
25
23
  end
26
24
 
27
- # Converts the canvas to grascale, returning a new canvas.
25
+ # Converts the canvas to grayscale, returning a new canvas.
28
26
  #
29
27
  # This method will not modify the canvas. To modift the current canvas,
30
28
  # use {#grayscale!} instead.
31
29
  #
32
- # @return [ChunkyPNG::Canvas] A copy of the canvas, converted to grasycale.
30
+ # @return [ChunkyPNG::Canvas] A copy of the canvas, converted to
31
+ # grayscale.
33
32
  # @see {#grayscale!}
34
33
  # @see {ChunkyPNG::Color#to_grayscale}
35
34
  def grayscale
36
35
  dup.grayscale!
37
36
  end
38
-
39
- # Composes another image onto this image using alpha blending. This will modify
40
- # the current canvas.
37
+
38
+ # Composes another image onto this image using alpha blending. This will
39
+ # modify the current canvas.
41
40
  #
42
- # If you simply want to replace pixels or when the other image does not have
43
- # transparency, it is faster to use {#replace!}.
41
+ # If you simply want to replace pixels or when the other image does not
42
+ # have transparency, it is faster to use {#replace!}.
44
43
  #
45
- # @param [ChunkyPNG::Canvas] other The foreground canvas to compose on the
46
- # current canvas, using alpha compositing.
44
+ # @param [ChunkyPNG::Canvas] other The foreground canvas to compose on
45
+ # the current canvas, using alpha compositing.
47
46
  # @param [Integer] offset_x The x-offset to apply the new foreground on.
48
47
  # @param [Integer] offset_y The y-offset to apply the new foreground on.
49
- # @return [ChunkyPNG::Canvas] Returns itself, but with the other canvas composed onto it.
50
- # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on this one,
51
- # given the offset and size of the other canvas.
48
+ # @return [ChunkyPNG::Canvas] Returns itself, but with the other canvas
49
+ # composed onto it.
50
+ # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on
51
+ # this one, given the offset and size of the other canvas.
52
52
  # @see #replace!
53
53
  # @see #compose
54
54
  def compose!(other, offset_x = 0, offset_y = 0)
@@ -56,43 +56,51 @@ module ChunkyPNG
56
56
 
57
57
  for y in 0...other.height do
58
58
  for x in 0...other.width do
59
- set_pixel(x + offset_x, y + offset_y, ChunkyPNG::Color.compose(other.get_pixel(x, y), get_pixel(x + offset_x, y + offset_y)))
59
+ set_pixel(x + offset_x,
60
+ y + offset_y,
61
+ ChunkyPNG::Color.compose(other.get_pixel(x, y),
62
+ get_pixel(x + offset_x,
63
+ y + offset_y)))
60
64
  end
61
65
  end
62
66
  self
63
67
  end
64
-
65
- # Composes another image onto this image using alpha blending. This will return
66
- # a new canvas and leave the original intact.
68
+
69
+ # Composes another image onto this image using alpha blending. This will
70
+ # return a new canvas and leave the original intact.
67
71
  #
68
- # If you simply want to replace pixels or when the other image does not have
69
- # transparency, it is faster to use {#replace}.
72
+ # If you simply want to replace pixels or when the other image does not
73
+ # have transparency, it is faster to use {#replace}.
70
74
  #
71
75
  # @param (see #compose!)
72
- # @return [ChunkyPNG::Canvas] Returns the new canvas, composed of the other 2.
73
- # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on this one,
74
- # given the offset and size of the other canvas.
75
- #
76
- # @note API changed since 1.0 - This method now no longer is in place, but returns
77
- # a new canvas and leaves the original intact. Use {#compose!} if you want to
78
- # compose on the canvas in place.
76
+ # @return [ChunkyPNG::Canvas] Returns the new canvas, composed of the
77
+ # other 2.
78
+ # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on
79
+ # this one, given the offset and size of the other canvas.
80
+ #
81
+ # @note API changed since 1.0 - This method now no longer is in place,
82
+ # but returns a new canvas and leaves the original intact. Use
83
+ # {#compose!} if you want to compose on the canvas in place.
79
84
  # @see #replace
80
85
  def compose(other, offset_x = 0, offset_y = 0)
81
86
  dup.compose!(other, offset_x, offset_y)
82
87
  end
83
-
84
- # Replaces pixels on this image by pixels from another pixels, on a given offset.
85
- # This method will modify the current canvas.
88
+
89
+ # Replaces pixels on this image by pixels from another pixels, on a given
90
+ # offset. This method will modify the current canvas.
86
91
  #
87
- # This will completely replace the pixels of the background image. If you want to blend
88
- # them with semi-transparent pixels from the foreground image, see {#compose!}.
92
+ # This will completely replace the pixels of the background image. If you
93
+ # want to blend them with semi-transparent pixels from the foreground
94
+ # image, see {#compose!}.
89
95
  #
90
- # @param [ChunkyPNG::Canvas] other The foreground canvas to get the pixels from.
96
+ # @param [ChunkyPNG::Canvas] other The foreground canvas to get the
97
+ # pixels from.
91
98
  # @param [Integer] offset_x The x-offset to apply the new foreground on.
92
99
  # @param [Integer] offset_y The y-offset to apply the new foreground on.
93
- # @return [ChunkyPNG::Canvas] Returns itself, but with the other canvas placed onto it.
94
- # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on this one,
95
- # given the offset and size of the other canvas.
100
+ # @return [ChunkyPNG::Canvas] Returns itself, but with the other canvas
101
+ # placed onto it.
102
+ # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on
103
+ # this one, given the offset and size of the other canvas.
96
104
  # @see #compose!
97
105
  # @see #replace
98
106
  def replace!(other, offset_x = 0, offset_y = 0)
@@ -106,80 +114,93 @@ module ChunkyPNG
106
114
  self
107
115
  end
108
116
 
109
- # Replaces pixels on this image by pixels from another pixels, on a given offset.
110
- # This method will modify the current canvas.
117
+ # Replaces pixels on this image by pixels from another pixels, on a given
118
+ # offset. This method will modify the current canvas.
111
119
  #
112
- # This will completely replace the pixels of the background image. If you want to blend
113
- # them with semi-transparent pixels from the foreground image, see {#compose!}.
120
+ # This will completely replace the pixels of the background image. If you
121
+ # want to blend them with semi-transparent pixels from the foreground
122
+ # image, see {#compose!}.
114
123
  #
115
124
  # @param (see #replace!)
116
125
  # @return [ChunkyPNG::Canvas] Returns a new, combined canvas.
117
- # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on this one,
118
- # given the offset and size of the other canvas.
126
+ # @raise [ChunkyPNG::OutOfBounds] when the other canvas doesn't fit on
127
+ # this one, given the offset and size of the other canvas.
119
128
  #
120
- # @note API changed since 1.0 - This method now no longer is in place, but returns
121
- # a new canvas and leaves the original intact. Use {#replace!} if you want to
122
- # replace pixels on the canvas in place.
123
- # @see #compose
129
+ # @note API changed since 1.0 - This method now no longer is in place,
130
+ # but returns a new canvas and leaves the original intact. Use
131
+ # {#replace!} if you want to replace pixels on the canvas in place.
132
+ # @see #compose
124
133
  def replace(other, offset_x = 0, offset_y = 0)
125
134
  dup.replace!(other, offset_x, offset_y)
126
135
  end
127
136
 
128
- # Crops an image, given the coordinates and size of the image that needs to be cut out.
129
- # This will leave the original image intact and return a new, cropped image with pixels
130
- # copied from the original image.
131
- #
132
- # @param [Integer] x The x-coordinate of the top left corner of the image to be cropped.
133
- # @param [Integer] y The y-coordinate of the top left corner of the image to be cropped.
137
+ # Crops an image, given the coordinates and size of the image that needs
138
+ # to be cut out. This will leave the original image intact and return a
139
+ # new, cropped image with pixels copied from the original image.
140
+ #
141
+ # @param [Integer] x The x-coordinate of the top left corner of the image
142
+ # to be cropped.
143
+ # @param [Integer] y The y-coordinate of the top left corner of the image
144
+ # to be cropped.
134
145
  # @param [Integer] crop_width The width of the image to be cropped.
135
146
  # @param [Integer] crop_height The height of the image to be cropped.
136
147
  # @return [ChunkyPNG::Canvas] Returns the newly created cropped image.
137
- # @raise [ChunkyPNG::OutOfBounds] when the crop dimensions plus the given coordinates
138
- # are bigger then the original image.
148
+ # @raise [ChunkyPNG::OutOfBounds] when the crop dimensions plus the given
149
+ # coordinates are bigger then the original image.
139
150
  def crop(x, y, crop_width, crop_height)
140
151
  dup.crop!(x, y, crop_width, crop_height)
141
152
  end
142
-
143
- # Crops an image, given the coordinates and size of the image that needs to be cut out.
153
+
154
+ # Crops an image, given the coordinates and size of the image that needs
155
+ # to be cut out.
144
156
  #
145
- # This will change the size and content of the current canvas. Use {#crop} if you want to
146
- # have a new canvas returned instead, leaving the current canvas intact.
157
+ # This will change the size and content of the current canvas. Use
158
+ # {#crop} if you want to have a new canvas returned instead, leaving the
159
+ # current canvas intact.
147
160
  #
148
- # @param [Integer] x The x-coordinate of the top left corner of the image to be cropped.
149
- # @param [Integer] y The y-coordinate of the top left corner of the image to be cropped.
161
+ # @param [Integer] x The x-coordinate of the top left corner of the image
162
+ # to be cropped.
163
+ # @param [Integer] y The y-coordinate of the top left corner of the image
164
+ # to be cropped.
150
165
  # @param [Integer] crop_width The width of the image to be cropped.
151
166
  # @param [Integer] crop_height The height of the image to be cropped.
152
- # @return [ChunkyPNG::Canvas] Returns itself, but cropped.
153
- # @raise [ChunkyPNG::OutOfBounds] when the crop dimensions plus the given coordinates
154
- # are bigger then the original image.
167
+ # @return [ChunkyPNG::Canvas] Returns itself, but cropped.
168
+ # @raise [ChunkyPNG::OutOfBounds] when the crop dimensions plus the given
169
+ # coordinates are bigger then the original image.
155
170
  def crop!(x, y, crop_width, crop_height)
156
- raise ChunkyPNG::OutOfBounds, "Original image width is too small!" if crop_width + x > width
157
- raise ChunkyPNG::OutOfBounds, "Original image height is too small!" if crop_height + y > height
158
-
171
+ if crop_width + x > width
172
+ raise ChunkyPNG::OutOfBounds, 'Original image width is too small!'
173
+ end
174
+ if crop_height + y > height
175
+ raise ChunkyPNG::OutOfBounds, 'Original image height is too small!'
176
+ end
177
+
159
178
  new_pixels = []
160
179
  for cy in 0...crop_height do
161
180
  new_pixels += pixels.slice((cy + y) * width + x, crop_width)
162
181
  end
163
182
  replace_canvas!(crop_width, crop_height, new_pixels)
164
183
  end
165
-
184
+
166
185
  # Flips the image horizontally, leaving the original intact.
167
186
  #
168
- # This will flip the image on its horizontal axis, e.g. pixels on the top will now
169
- # be pixels on the bottom. Chaining this method twice will return the original canvas.
170
- # This method will leave the original object intact and return a new canvas.
187
+ # This will flip the image on its horizontal axis, e.g. pixels on the top
188
+ # will now be pixels on the bottom. Chaining this method twice will
189
+ # return the original canvas. This method will leave the original object
190
+ # intact and return a new canvas.
171
191
  #
172
192
  # @return [ChunkyPNG::Canvas] The flipped image
173
193
  # @see #flip_horizontally!
174
194
  def flip_horizontally
175
195
  dup.flip_horizontally!
176
196
  end
177
-
197
+
178
198
  # Flips the image horizontally in place.
179
199
  #
180
- # This will flip the image on its horizontal axis, e.g. pixels on the top will now
181
- # be pixels on the bottom. Chaining this method twice will return the original canvas.
182
- # This method will leave the original object intact and return a new canvas.
200
+ # This will flip the image on its horizontal axis, e.g. pixels on the top
201
+ # will now be pixels on the bottom. Chaining this method twice will
202
+ # return the original canvas. This method will leave the original object
203
+ # intact and return a new canvas.
183
204
  #
184
205
  # @return [ChunkyPNG::Canvas] Itself, but flipped
185
206
  # @see #flip_horizontally
@@ -190,17 +211,18 @@ module ChunkyPNG
190
211
  replace_row!(other_y, row(y))
191
212
  replace_row!(y, other_row)
192
213
  end
193
- return self
214
+ self
194
215
  end
195
-
216
+
196
217
  alias_method :flip!, :flip_horizontally!
197
218
  alias_method :flip, :flip_horizontally
198
-
219
+
199
220
  # Flips the image vertically, leaving the original intact.
200
221
  #
201
- # This will flip the image on its vertical axis, e.g. pixels on the left will now
202
- # be pixels on the right. Chaining this method twice will return the original canvas.
203
- # This method will leave the original object intact and return a new canvas.
222
+ # This will flip the image on its vertical axis, e.g. pixels on the left
223
+ # will now be pixels on the right. Chaining this method twice will return
224
+ # the original canvas. This method will leave the original object intact
225
+ # and return a new canvas.
204
226
  #
205
227
  # @return [ChunkyPNG::Canvas] The flipped image
206
228
  # @see #flip_vertically!
@@ -210,9 +232,10 @@ module ChunkyPNG
210
232
 
211
233
  # Flips the image vertically in place.
212
234
  #
213
- # This will flip the image on its vertical axis, e.g. pixels on the left will now
214
- # be pixels on the right. Chaining this method twice will return the original canvas.
215
- # This method will leave the original object intact and return a new canvas.
235
+ # This will flip the image on its vertical axis, e.g. pixels on the left
236
+ # will now be pixels on the right. Chaining this method twice will return
237
+ # the original canvas. This method will leave the original object intact
238
+ # and return a new canvas.
216
239
  #
217
240
  # @return [ChunkyPNG::Canvas] Itself, but flipped
218
241
  # @see #flip_vertically
@@ -220,48 +243,49 @@ module ChunkyPNG
220
243
  for y in 0...height do
221
244
  replace_row!(y, row(y).reverse)
222
245
  end
223
- return self
246
+ self
224
247
  end
225
-
248
+
226
249
  alias_method :mirror!, :flip_vertically!
227
250
  alias_method :mirror, :flip_vertically
228
251
 
229
252
  # Returns a new canvas instance that is rotated 90 degrees clockwise.
230
253
  #
231
- # This method will return a new canvas and leaves the original intact.
232
- # See {#rotate_right!} for the in place version.
254
+ # This method will return a new canvas and leaves the original intact.
233
255
  #
234
256
  # @return [ChunkyPNG::Canvas] A clockwise-rotated copy.
257
+ # @see #rotate_right! for the in place version.
235
258
  def rotate_right
236
259
  dup.rotate_right!
237
260
  end
238
261
 
239
262
  # Rotates the image 90 degrees clockwise in place.
240
263
  #
241
- # This method will change the current canvas. See {#rotate_right} for
242
- # a version that leaves th current canvas intact
264
+ # This method will change the current canvas.
243
265
  #
244
266
  # @return [ChunkyPNG::Canvas] Itself, but rotated clockwise.
267
+ # @see #rotate_right for a version that leaves the current canvas intact
245
268
  def rotate_right!
246
269
  rotated = self.class.new(height, width)
247
270
  new_pixels = []
248
271
  0.upto(width - 1) { |i| new_pixels += column(i).reverse }
249
272
  replace_canvas!(height, width, new_pixels)
250
273
  end
251
-
274
+
252
275
  alias_method :rotate_clockwise, :rotate_right
253
276
  alias_method :rotate_clockwise!, :rotate_right!
254
-
277
+
255
278
  # Returns an image that is rotated 90 degrees counter-clockwise.
256
279
  #
257
- # This method will leave the original object intact and return a new canvas.
258
- # See {#rotate_left!} for the in place version.
280
+ # This method will leave the original object intact and return a new
281
+ # canvas.
259
282
  #
260
283
  # @return [ChunkyPNG::Canvas] A rotated copy of itself.
284
+ # @see #rotate_left! for the in-place version.
261
285
  def rotate_left
262
286
  dup.rotate_left!
263
287
  end
264
-
288
+
265
289
  # Rotates the image 90 degrees counter-clockwise in place.
266
290
  #
267
291
  # This method will change the original canvas. See {#rotate_left} for a
@@ -274,29 +298,32 @@ module ChunkyPNG
274
298
  (width - 1).downto(0) { |i| new_pixels += column(i) }
275
299
  replace_canvas!(height, width, new_pixels)
276
300
  end
277
-
301
+
278
302
  alias_method :rotate_counter_clockwise, :rotate_left
279
303
  alias_method :rotate_counter_clockwise!, :rotate_left!
280
-
304
+
281
305
  # Rotates the image 180 degrees.
282
- # This method will leave the original object intact and return a new canvas.
306
+ #
307
+ # This method will leave the original object intact and return a new
308
+ # canvas.
283
309
  #
284
310
  # @return [ChunkyPNG::Canvas] The rotated image.
285
311
  # @see #rotate_180!
286
312
  def rotate_180
287
313
  dup.rotate_180!
288
314
  end
289
-
315
+
290
316
  # Rotates the image 180 degrees in place.
291
317
  #
292
318
  # @return [ChunkyPNG::Canvas] Itself, but rotated 180 degrees.
293
319
  # @see #rotate_180
294
320
  def rotate_180!
295
321
  pixels.reverse!
296
- return self
322
+ self
297
323
  end
298
-
299
- # Trims the border around the image, presumed to be the color of the first pixel.
324
+
325
+ # Trims the border around the image, presumed to be the color of the
326
+ # first pixel.
300
327
  #
301
328
  # @param [Integer] border The color to attempt to trim.
302
329
  # @return [ChunkyPNG::Canvas] The trimmed image.
@@ -308,17 +335,18 @@ module ChunkyPNG
308
335
  # Trims the border around the image in place.
309
336
  #
310
337
  # @param [Integer] border The color to attempt to trim.
311
- # @return [ChunkyPNG::Canvas] Returns itself, but with the border trimmed.
338
+ # @return [ChunkyPNG::Canvas] Returns itself, but with the border
339
+ # trimmed.
312
340
  # @see #trim
313
341
  def trim!(border = pixels.first)
314
342
  x1 = [*0...width].index { |c| column(c).uniq != [border] }
315
343
  x2 = [*0...width].rindex { |c| column(c).uniq != [border] }
316
344
  y1 = [*0...height].index { |r| row(r).uniq != [border] }
317
345
  y2 = [*0...height].rindex { |r| row(r).uniq != [border] }
318
-
346
+
319
347
  crop! x1, y1, x2 - x1 + 1, y2 - y1 + 1
320
348
  end
321
-
349
+
322
350
  # Draws a border around the image.
323
351
  #
324
352
  # @param [Integer] size The size of the border.
@@ -344,16 +372,23 @@ module ChunkyPNG
344
372
  end
345
373
 
346
374
  protected
347
-
348
- # Checks whether another image has the correct dimension to be used for an operation
349
- # on the current image, given an offset coordinate to work with.
375
+
376
+ # Checks whether another image has the correct dimension to be used for
377
+ # an operation on the current image, given an offset coordinate to work
378
+ # with.
350
379
  # @param [ChunkyPNG::Canvas] other The other canvas
351
- # @param [Integer] offset_x The x offset on which the other image will be applied.
352
- # @param [Integer] offset_y The y offset on which the other image will be applied.
380
+ # @param [Integer] offset_x The x offset on which the other image will be
381
+ # applied.
382
+ # @param [Integer] offset_y The y offset on which the other image will be
383
+ # applied.
353
384
  # @raise [ChunkyPNG::OutOfBounds] when the other image doesn't fit.
354
385
  def check_size_constraints!(other, offset_x, offset_y)
355
- raise ChunkyPNG::OutOfBounds, "Background image width is too small!" if width < other.width + offset_x
356
- raise ChunkyPNG::OutOfBounds, "Background image height is too small!" if height < other.height + offset_y
386
+ if width < other.width + offset_x
387
+ raise ChunkyPNG::OutOfBounds, 'Background image width is too small!'
388
+ end
389
+ if height < other.height + offset_y
390
+ raise ChunkyPNG::OutOfBounds, 'Background image height is too small!'
391
+ end
357
392
  end
358
393
  end
359
394
  end