Skip to content

Commit

Permalink
Merge pull request #140 from wvanbergen/standardrb
Browse files Browse the repository at this point in the history
Adopt standard Ruby style guide
  • Loading branch information
wvanbergen committed Mar 29, 2019
2 parents cd9de72 + c3694f0 commit 479757c
Show file tree
Hide file tree
Showing 29 changed files with 462 additions and 448 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RSpec::Core::RakeTask.new(:spec) do |task|
task.rspec_opts = ['--color']
end

task :default => [:spec]
task default: [:spec]
12 changes: 6 additions & 6 deletions benchmarks/encoding_benchmark.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
x.report(':fast_rgb') { n.times { image.to_blob(:fast_rgb) } }
x.report(':good_compression') { n.times { image.to_blob(:good_compression) } }
x.report(':best_compression') { n.times { image.to_blob(:best_compression) } }

# Some options
x.report(':rgb') { n.times { image.to_blob(:color_mode => ChunkyPNG::COLOR_TRUECOLOR) } }
x.report(':rgba') { n.times { image.to_blob(:color_mode => ChunkyPNG::COLOR_TRUECOLOR_ALPHA) } }
x.report(':indexed') { n.times { image.to_blob(:color_mode => ChunkyPNG::COLOR_INDEXED) } }
x.report(':interlaced') { n.times { image.to_blob(:interlaced => true) } }
x.report(':rgb') { n.times { image.to_blob(color_mode: ChunkyPNG::COLOR_TRUECOLOR) } }
x.report(':rgba') { n.times { image.to_blob(color_mode: ChunkyPNG::COLOR_TRUECOLOR_ALPHA) } }
x.report(':indexed') { n.times { image.to_blob(color_mode: ChunkyPNG::COLOR_INDEXED) } }
x.report(':interlaced') { n.times { image.to_blob(interlaced: true) } }

# Exports
x.report('to RGBA pixelstream') { n.times { image.to_rgba_stream } }
x.report('to RGB pixelstream') { n.times { image.to_rgb_stream } }
Expand Down
4 changes: 3 additions & 1 deletion chunky_png.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ Gem::Specification.new do |s|
s.license = 'MIT'
s.metadata = {
"source_code_uri" => "https://github.com/wvanbergen/chunky_png",
"wiki_uri" => "https://github.com/wvanbergen/chunky_png/wiki"
"wiki_uri" => "https://github.com/wvanbergen/chunky_png/wiki",
}

s.add_development_dependency('rake')
s.add_development_dependency('standard')
s.add_development_dependency('yard', '~> 0.9')
s.add_development_dependency('rspec', '~> 3')

s.rdoc_options << '--title' << s.name << '--main' << 'README.rdoc' << '--line-numbers' << '--inline-source'
Expand Down
10 changes: 6 additions & 4 deletions lib/chunky_png/canvas.rb
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def include_point?(*point_like)
dimension.include?(ChunkyPNG::Point(*point_like))
end

alias_method :include?, :include_point?
alias include? include_point?

# Checks whether the given x- and y-coordinate are in the range of the
# canvas
Expand Down Expand Up @@ -274,11 +274,13 @@ def palette
# @return [true, false] True if the size and pixel values of the other
# canvas are exactly the same as this canvas's size and pixel values.
def eql?(other)
other.kind_of?(self.class) && other.pixels == self.pixels &&
other.width == self.width && other.height == self.height
other.kind_of?(self.class) &&
other.pixels == pixels &&
other.width == width &&
other.height == height
end

alias :== :eql?
alias == eql?

#################################################################
# EXPORTING
Expand Down
20 changes: 12 additions & 8 deletions lib/chunky_png/canvas/adam7_interlacing.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
module ChunkyPNG
class Canvas

# Methods for decoding and encoding Adam7 interlacing.
#
# Adam7 interlacing extracts 7 pass images out of a single image, that can be encoded to a
# stream separately so the image can be built up progressively. The module is included into
# ChunkyPNG canvas and is used to extract the pass images from the original image, or to
# reconstruct an original image from separate pass images.
module Adam7Interlacing

# Returns an array with the x-shift, x-offset, y-shift and y-offset for the requested pass.
# @param [Integer] pass The pass number, should be in 0..6.
def adam7_multiplier_offset(pass)
[3 - (pass >> 1), (pass & 1 == 0) ? 0 : 8 >> ((pass + 1) >> 1),
pass == 0 ? 3 : 3 - ((pass - 1) >> 1), (pass == 0 || pass & 1 == 1) ? 0 : 8 >> (pass >> 1)]
[
3 - (pass >> 1),
pass & 1 == 0 ? 0 : 8 >> ((pass + 1) >> 1),
pass == 0 ? 3 : 3 - ((pass - 1) >> 1),
pass == 0 || pass & 1 == 1 ? 0 : 8 >> (pass >> 1)
]
end

# Returns the pixel dimensions of the requested pass.
Expand All @@ -25,7 +29,7 @@ def adam7_pass_size(pass, original_width, original_height)
[ (original_width - x_offset + (1 << x_shift) - 1) >> x_shift,
(original_height - y_offset + (1 << y_shift) - 1) >> y_shift]
end

# Returns an array of the dimension of all the pass images.
# @param [Integer] original_width The width of the original image.
# @param [Integer] original_height The height of the original image.
Expand All @@ -49,21 +53,21 @@ def adam7_merge_pass(pass, canvas, subcanvas)
end
end
end

# Extracts a pass from a complete image
# @param [Integer] pass The pass number, should be in 0..6.
# @param [ChunkyPNG::Canvas] canvas The image that is being deconstructed.
# @return [ChunkyPNG::Canvas] The extracted pass image.
def adam7_extract_pass(pass, canvas)
x_shift, x_offset, y_shift, y_offset = adam7_multiplier_offset(pass)
sm_pixels = []

y_offset.step(canvas.height - 1, 1 << y_shift) do |y|
x_offset.step(canvas.width - 1, 1 << x_shift) do |x|
sm_pixels << canvas[x, y]
end
end

new_canvas_args = adam7_pass_size(pass, canvas.width, canvas.height) + [sm_pixels]
ChunkyPNG::Canvas.new(*new_canvas_args)
end
Expand Down
2 changes: 0 additions & 2 deletions lib/chunky_png/canvas/data_url_exporting.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
module ChunkyPNG
class Canvas

# Methods to export a canvas to a PNG data URL.
module DataUrlExporting

# Exports the canvas as a data url (e.g. data:image/png;base64,<data>) that can
# easily be used inline in CSS or HTML.
# @return [String] The canvas formatted as a data URL string.
Expand Down
1 change: 0 additions & 1 deletion lib/chunky_png/canvas/data_url_importing.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module ChunkyPNG
class Canvas

# Methods to import a canvas from a PNG data URL.
module DataUrlImporting

Expand Down
7 changes: 3 additions & 4 deletions lib/chunky_png/canvas/drawing.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
module ChunkyPNG
class Canvas

# Module that adds some primitive drawing methods to {ChunkyPNG::Canvas}.
#
# All of these methods change the current canvas instance and do not create
Expand Down Expand Up @@ -39,8 +38,8 @@ def compose_pixel_unsafe(x, y, color)
def bezier_curve(points, stroke_color = ChunkyPNG::Color::BLACK)
points = ChunkyPNG::Vector(*points)
case points.length
when 0, 1; return self
when 2; return line(points[0].x, points[0].y, points[1].x, points[1].y, stroke_color)
when 0, 1 then return self
when 2 then return line(points[0].x, points[0].y, points[1].x, points[1].y, stroke_color)
end

curve_points = Array.new
Expand Down Expand Up @@ -158,7 +157,7 @@ def line_xiaolin_wu(x0, y0, x1, y1, stroke_color, inclusive = true)
self
end

alias_method :line, :line_xiaolin_wu
alias line line_xiaolin_wu

# Draws a polygon on the canvas using the stroke_color, filled using the
# fill_color if any.
Expand Down
24 changes: 12 additions & 12 deletions lib/chunky_png/canvas/masking.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module ChunkyPNG
class Canvas

# The ChunkyPNG::Canvas::Masking module defines methods to perform masking
# and theming operations on a {ChunkyPNG::Canvas}. The module is included into the Canvas class so all
# these methods are available on every canvas.
Expand All @@ -21,35 +21,35 @@ module Masking
# @param [Integer] old_theme_color The original theme color in this image.
# @param [Integer] new_theme_color The color to replace the old theme color with.
# @param [Integer] bg_color The background color on which the theme colored pixels are placed.
# @param [Integer] tolerance The tolerance level to use when extracting the mask image. Five is
# the default; increase this if the masked image does not extract all the required pixels,
# @param [Integer] tolerance The tolerance level to use when extracting the mask image. Five is
# the default; increase this if the masked image does not extract all the required pixels,
# decrease it if too many pixels get extracted.
# @return [ChunkyPNG::Canvas] Returns itself, but with the theme colored pixels changed.
# @see #change_theme_color!
# @see #change_mask_color!
def change_theme_color!(old_theme_color, new_theme_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)
base, mask = extract_mask(old_theme_color, bg_color, tolerance)
mask.change_mask_color!(new_theme_color)
self.replace!(base.compose!(mask))
replace!(base.compose!(mask))
end

# Creates a base image and a mask image from an original image that has a particular theme color.
# This can be used to easily change a theme color in an image.
#
# It will extract all the pixels that look like the theme color (with a tolerance level) and put
# these in a mask image. All the other pixels will be stored in a base image. Both images will be
# of the exact same size as the original image. The original image will be left untouched.
#
# The color of the mask image can be changed with {#change_mask_color!}. This new mask image can
# then be composed upon the base image to create an image with a new theme color. A call to
# The color of the mask image can be changed with {#change_mask_color!}. This new mask image can
# then be composed upon the base image to create an image with a new theme color. A call to
# {#change_theme_color!} will perform this in one go.
#
# @param [Integer] mask_color The current theme color.
# @param [Integer] bg_color The background color on which the theme colored pixels are applied.
# @param [Integer] tolerance The tolerance level to use when extracting the mask image. Five is
# the default; increase this if the masked image does not extract all the required pixels,
# @param [Integer] tolerance The tolerance level to use when extracting the mask image. Five is
# the default; increase this if the masked image does not extract all the required pixels,
# decrease it if too many pixels get extracted.
# @return [Array<ChunkyPNG::Canvas, ChunkyPNG::Canvas>] An array with the base canvas and the mask
# @return [Array<ChunkyPNG::Canvas, ChunkyPNG::Canvas>] An array with the base canvas and the mask
# canvas as elements.
# @see #change_theme_color!
# @see #change_mask_color!
Expand All @@ -66,10 +66,10 @@ def extract_mask(mask_color, bg_color = ChunkyPNG::Color::WHITE, tolerance = 5)
base_pixels << pixel
end
end

[ self.class.new(width, height, base_pixels), self.class.new(width, height, mask_pixels) ]
end

# Changes the color of a mask image.
#
# This method works on a canvas extracted out of another image using the {#extract_mask} method.
Expand Down
16 changes: 8 additions & 8 deletions lib/chunky_png/canvas/operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ def flip_horizontally!
self
end

alias_method :flip!, :flip_horizontally!
alias_method :flip, :flip_horizontally
alias flip! flip_horizontally!
alias flip flip_horizontally

# Flips the image vertically, leaving the original intact.
#
Expand Down Expand Up @@ -253,8 +253,8 @@ def flip_vertically!
self
end

alias_method :mirror!, :flip_vertically!
alias_method :mirror, :flip_vertically
alias mirror! flip_vertically!
alias mirror flip_vertically

# Returns a new canvas instance that is rotated 90 degrees clockwise.
#
Expand All @@ -278,8 +278,8 @@ def rotate_right!
replace_canvas!(height, width, new_pixels)
end

alias_method :rotate_clockwise, :rotate_right
alias_method :rotate_clockwise!, :rotate_right!
alias rotate_clockwise rotate_right
alias rotate_clockwise! rotate_right!

# Returns an image that is rotated 90 degrees counter-clockwise.
#
Expand All @@ -305,8 +305,8 @@ def rotate_left!
replace_canvas!(height, width, new_pixels)
end

alias_method :rotate_counter_clockwise, :rotate_left
alias_method :rotate_counter_clockwise!, :rotate_left!
alias rotate_counter_clockwise rotate_left
alias rotate_counter_clockwise! rotate_left!

# Rotates the image 180 degrees.
#
Expand Down

0 comments on commit 479757c

Please sign in to comment.