Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure codebase conforms to standard.rb style guide #141

Merged
merged 26 commits into from Apr 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
9f7d1ef
Formatting
wvanbergen Apr 15, 2019
ae05365
standard.rb: Spacing around operators.
wvanbergen Apr 15, 2019
a2110ec
standard.rb: Fix spacing issues.
wvanbergen Apr 15, 2019
769b311
standard.rb: Use `is_a?` instead of `kind_of?`
wvanbergen Apr 15, 2019
91f5c0e
stadard.rb: Fix missing spacing around commas.
wvanbergen Apr 15, 2019
8efdba4
standard.rb: Add trailing commas to array literals, reformat where ne…
wvanbergen Apr 15, 2019
4d1af58
standrad.rb: Use { … } block syntax.
wvanbergen Apr 15, 2019
e2788e3
standard.rb: Fix spacing inside array literals.
wvanbergen Apr 15, 2019
b1876ce
standard.rb: Remove unnecessary empty lines.
wvanbergen Apr 15, 2019
0d6e8b5
standard.rb: remove unnecessary require statement.
wvanbergen Apr 15, 2019
108f801
standard.rb: Manually disabled some Rubocop rules for some lines.
wvanbergen Apr 15, 2019
b9c34fa
standard.rb: Fix spacing issues
wvanbergen Apr 15, 2019
7cb14ba
standard.rb: Various style fixes
wvanbergen Apr 15, 2019
b431cb3
standard.rb: Fix some parameter alignment issues.
wvanbergen Apr 15, 2019
de83806
standard.rb: Fix some small style issue sin drawing.rb
wvanbergen Apr 15, 2019
219369a
standard.rb: Remove unnecessary parentheses.
wvanbergen Apr 15, 2019
1e34404
Fix some specs that were not properly converted to should syntax.
wvanbergen Apr 15, 2019
a2a9faf
standard.rb: Allow some exceptions on Rubocop spacing rules for bette…
wvanbergen Apr 15, 2019
1e5cf92
standard.rb: Use `delete` rather then unneeded `gsub` to delete newli…
wvanbergen Apr 15, 2019
c90fcc6
standard.rb: Don’t use nested ternary operators.
wvanbergen Apr 15, 2019
07c48e4
Fix yardoc description of ChunkyPNG::OutOfBounds exception class.
wvanbergen Apr 15, 2019
0a60707
standard.rb: Fix unnecessary else clauses.
wvanbergen Apr 15, 2019
9e28ec7
standard.rb: Various style fixes.
wvanbergen Apr 15, 2019
c6ac130
standard.rb: Avoid ambiguous `-` operator.
wvanbergen Apr 15, 2019
ef47fb4
standard.rb: Consistently use double quotes for strings.
wvanbergen Apr 15, 2019
7511a51
Add standard.rb configuration file.
wvanbergen Apr 15, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions .standard.yml
@@ -0,0 +1,14 @@
# ChunkyPNG uses and enforces standard.rb as code style (see https://github.com/testdouble/standard).
# For backwards compatilibity and idiosyncratic preferences of the main author,
# there are some minor differences listed in here.

ignore:
- lib/chunky_png/**/*.rb:
# We allow `for` loops in the codebase, especially in hot paths,
# because they perform better than `each` blocks.
- "Style/For"

- spec/chunky_png/**/*.rb:
# In RSpec, having to follow this rule will cause expectations to
# be less readable, specifically blocks for the `change` matcher.
- "Lint/AmbiguousBlockAssociation"
6 changes: 3 additions & 3 deletions Gemfile
@@ -1,10 +1,10 @@
source 'https://rubygems.org'
source "https://rubygems.org"
gemspec

platforms :jruby do
gem 'jruby-openssl'
gem "jruby-openssl"
end

platform :rbx do
gem 'rubysl'
gem "rubysl"
end
4 changes: 2 additions & 2 deletions Rakefile
@@ -1,11 +1,11 @@
require "bundler/gem_tasks"
require "rspec/core/rake_task"

Dir['tasks/*.rake'].each { |file| load(file) }
Dir["tasks/*.rake"].each { |file| load(file) }

RSpec::Core::RakeTask.new(:spec) do |task|
task.pattern = "./spec/**/*_spec.rb"
task.rspec_opts = ['--color']
task.rspec_opts = ["--color"]
end

task default: [:spec]
34 changes: 17 additions & 17 deletions benchmarks/decoding_benchmark.rb
@@ -1,36 +1,36 @@
require 'rubygems'
require 'bundler/setup'
require "rubygems"
require "bundler/setup"

require 'benchmark'
require 'chunky_png'
require "benchmark"
require "chunky_png"

def image_file(name)
File.join(File.dirname(__FILE__), '..', 'spec', 'resources', name)
File.join(File.dirname(__FILE__), "..", "spec", "resources", name)
end

def image_data(name)
data = nil
File.open(image_file(name), 'rb') { |f| data = f.read }
File.open(image_file(name), "rb") { |f| data = f.read }
data
end

no_filtering_stream = image_data('pixelstream_fast_rgba.png')
up_filtering_stream = image_data('pixelstream_reference.png')
paeth_filtering_stream = image_data('pixelstream_best_compression.png')
rgba_pixelstream = image_data('pixelstream.rgba')
rgb_pixelstream = image_data('pixelstream.rgb')
no_filtering_stream = image_data("pixelstream_fast_rgba.png")
up_filtering_stream = image_data("pixelstream_reference.png")
paeth_filtering_stream = image_data("pixelstream_best_compression.png")
rgba_pixelstream = image_data("pixelstream.rgba")
rgb_pixelstream = image_data("pixelstream.rgb")

n = (ENV['N'] || '5').to_i
n = (ENV["N"] || "5").to_i

puts "---------------------------------------------"
puts "ChunkyPNG (#{ChunkyPNG::VERSION}) decoding benchmark (n=#{n})"
puts "---------------------------------------------"
puts

Benchmark.bmbm do |x|
x.report('PNG - no filtering') { n.times { ChunkyPNG::Image.from_blob(no_filtering_stream) } }
x.report('PNG - UP filtering') { n.times { ChunkyPNG::Image.from_blob(up_filtering_stream) } }
x.report('PNG - PAETH filtering') { n.times { ChunkyPNG::Image.from_blob(paeth_filtering_stream) } }
x.report('From RGBA pixelstream') { n.times { ChunkyPNG::Image.from_rgba_stream(240, 180, rgba_pixelstream) } }
x.report('From RGB pixelstream') { n.times { ChunkyPNG::Image.from_rgb_stream(240, 180, rgb_pixelstream) } }
x.report("PNG - no filtering") { n.times { ChunkyPNG::Image.from_blob(no_filtering_stream) } }
x.report("PNG - UP filtering") { n.times { ChunkyPNG::Image.from_blob(up_filtering_stream) } }
x.report("PNG - PAETH filtering") { n.times { ChunkyPNG::Image.from_blob(paeth_filtering_stream) } }
x.report("From RGBA pixelstream") { n.times { ChunkyPNG::Image.from_rgba_stream(240, 180, rgba_pixelstream) } }
x.report("From RGB pixelstream") { n.times { ChunkyPNG::Image.from_rgb_stream(240, 180, rgb_pixelstream) } }
end
37 changes: 20 additions & 17 deletions benchmarks/encoding_benchmark.rb
@@ -1,40 +1,43 @@
require 'rubygems'
require 'bundler/setup'
require "rubygems"
require "bundler/setup"

require 'benchmark'
require 'chunky_png'
require "benchmark"
require "chunky_png"

image = ChunkyPNG::Image.new(240, 180, ChunkyPNG::Color::TRANSPARENT)

# set some random pixels

# rubocop:disable Layout/ExtraSpacing, Layout/SpaceInsideParens
image[10, 20] = ChunkyPNG::Color.rgba(255, 0, 0, 255)
image[50, 87] = ChunkyPNG::Color.rgba( 0, 255, 0, 255)
image[33, 99] = ChunkyPNG::Color.rgba( 0, 0, 255, 255)
# rubocop:enable Layout/ExtraSpacing, Layout/SpaceInsideParens

n = (ENV['N'] || '5').to_i
n = (ENV["N"] || "5").to_i

puts "---------------------------------------------"
puts "ChunkyPNG (#{ChunkyPNG::VERSION}) encoding benchmark (n=#{n})"
puts "---------------------------------------------"
puts

Benchmark.bmbm do |x|
x.report('Autodetect (indexed)') { n.times { image.to_blob } }
x.report("Autodetect (indexed)") { n.times { image.to_blob } }

# Presets
x.report(':no_compression') { n.times { image.to_blob(:no_compression) } }
x.report(':fast_rgba') { n.times { image.to_blob(:fast_rgba) } }
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) } }
x.report(":no_compression") { n.times { image.to_blob(:no_compression) } }
x.report(":fast_rgba") { n.times { image.to_blob(:fast_rgba) } }
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 } }
x.report("to RGBA pixelstream") { n.times { image.to_rgba_stream } }
x.report("to RGB pixelstream") { n.times { image.to_rgb_stream } }
end
12 changes: 6 additions & 6 deletions benchmarks/filesize_benchmark.rb
@@ -1,10 +1,10 @@
require 'rubygems'
require 'bundler/setup'
require "rubygems"
require "bundler/setup"

require 'benchmark'
require 'chunky_png'
require "benchmark"
require "chunky_png"

files = ['pixelstream_reference.png', 'operations.png', 'clock.png']
files = ["pixelstream_reference.png", "operations.png", "clock.png"]

def encode_png(image, constraints = {})
filesize = nil
Expand All @@ -13,7 +13,7 @@ def encode_png(image, constraints = {})
end

files.each do |file|
filename = File.join(File.dirname(__FILE__), '..', 'spec', 'resources', file)
filename = File.join(File.dirname(__FILE__), "..", "spec", "resources", file)
image = ChunkyPNG::Canvas.from_file(filename)

puts "#{file}: #{image.width}x#{image.height} - #{image.palette.size} colors"
Expand Down
32 changes: 15 additions & 17 deletions chunky_png.gemspec
@@ -1,11 +1,9 @@
# -*- encoding: utf-8 -*-

lib = File.expand_path('../lib', __FILE__)
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'chunky_png/version'
require "chunky_png/version"

Gem::Specification.new do |s|
s.name = 'chunky_png'
s.name = "chunky_png"

# Do not change the version and date fields by hand. This will be done
# automatically by the gem release script.
Expand All @@ -31,22 +29,22 @@ Gem::Specification.new do |s|
provides a massive speed boost to encoding and decoding.
EOT

s.authors = ['Willem van Bergen']
s.email = ['willem@railsdoctors.com']
s.homepage = 'https://github.com/wvanbergen/chunky_png/wiki'
s.license = 'MIT'
s.authors = ["Willem van Bergen"]
s.email = ["willem@railsdoctors.com"]
s.homepage = "https://github.com/wvanbergen/chunky_png/wiki"
s.license = "MIT"
s.metadata = {
"source_code_uri" => "https://github.com/wvanbergen/chunky_png",
"wiki_uri" => "https://github.com/wvanbergen/chunky_png/wiki",
"source_code_uri" => "https://github.com/wvanbergen/chunky_png",
"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.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'
s.extra_rdoc_files = ['README.md', 'BENCHMARKING.rdoc', 'CONTRIBUTING.rdoc', 'CHANGELOG.rdoc']
s.rdoc_options << "--title" << s.name << "--main" << "README.rdoc" << "--line-numbers" << "--inline-source"
s.extra_rdoc_files = ["README.md", "BENCHMARKING.rdoc", "CONTRIBUTING.rdoc", "CHANGELOG.rdoc"]

s.files = `git ls-files`.split($/)
s.test_files = s.files.grep(%r{^(test|spec|features)/})
Expand Down
30 changes: 14 additions & 16 deletions lib/chunky_png.rb
@@ -1,8 +1,7 @@
# Basic requirements from standard library
require 'set'
require 'zlib'
require 'stringio'
require 'enumerator'
require "set"
require "zlib"
require "stringio"

# ChunkyPNG - the pure ruby library to access PNG files.
#
Expand All @@ -22,7 +21,6 @@
#
# @author Willem van Bergen
module ChunkyPNG

###################################################
# PNG international standard defined constants
###################################################
Expand Down Expand Up @@ -131,7 +129,7 @@ class InvalidUTF8 < ChunkyPNG::Exception
class ExpectationFailed < ChunkyPNG::Exception
end

# Exception that is raised if an expectation fails.
# Exception that when provided coordinates are out of bounds for the canvas
class OutOfBounds < ChunkyPNG::ExpectationFailed
end

Expand All @@ -152,21 +150,21 @@ class UnitsUnknown < ChunkyPNG::Exception
EXTRA_BYTE = "\0".force_encoding(Encoding::BINARY).freeze
end

require 'chunky_png/version'
require "chunky_png/version"

# PNG file structure
require 'chunky_png/datastream'
require 'chunky_png/chunk'
require "chunky_png/datastream"
require "chunky_png/chunk"

# Colors
require 'chunky_png/palette'
require 'chunky_png/color'
require "chunky_png/palette"
require "chunky_png/color"

# Geometry
require 'chunky_png/point'
require 'chunky_png/vector'
require 'chunky_png/dimension'
require "chunky_png/point"
require "chunky_png/vector"
require "chunky_png/dimension"

# Canvas / Image classes
require 'chunky_png/canvas'
require 'chunky_png/image'
require "chunky_png/canvas"
require "chunky_png/image"
41 changes: 20 additions & 21 deletions lib/chunky_png/canvas.rb
@@ -1,14 +1,14 @@
require 'chunky_png/canvas/png_encoding'
require 'chunky_png/canvas/png_decoding'
require 'chunky_png/canvas/adam7_interlacing'
require 'chunky_png/canvas/stream_exporting'
require 'chunky_png/canvas/stream_importing'
require 'chunky_png/canvas/data_url_exporting'
require 'chunky_png/canvas/data_url_importing'
require 'chunky_png/canvas/operations'
require 'chunky_png/canvas/drawing'
require 'chunky_png/canvas/resampling'
require 'chunky_png/canvas/masking'
require "chunky_png/canvas/png_encoding"
require "chunky_png/canvas/png_decoding"
require "chunky_png/canvas/adam7_interlacing"
require "chunky_png/canvas/stream_exporting"
require "chunky_png/canvas/stream_importing"
require "chunky_png/canvas/data_url_exporting"
require "chunky_png/canvas/data_url_importing"
require "chunky_png/canvas/operations"
require "chunky_png/canvas/drawing"
require "chunky_png/canvas/resampling"
require "chunky_png/canvas/masking"

module ChunkyPNG
# The ChunkyPNG::Canvas class represents a raster image as a matrix of
Expand Down Expand Up @@ -56,7 +56,6 @@ class Canvas
# This array always should have +width * height+ elements.
attr_reader :pixels


#################################################################
# CONSTRUCTORS
#################################################################
Expand All @@ -78,9 +77,10 @@ class Canvas
def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT)
@width, @height = width, height

if initial.kind_of?(Array)
unless initial.length == width * height
raise ArgumentError, "The initial array should have #{width}x#{height} = #{width*height} elements!"
if initial.is_a?(Array)
pixel_count = width * height
unless initial.length == pixel_count
raise ArgumentError, "The initial array should have #{width}x#{height} = #{pixel_count} elements!"
end
@pixels = initial
else
Expand All @@ -104,7 +104,6 @@ def self.from_canvas(canvas)
new(canvas.width, canvas.height, canvas.pixels.dup)
end


#################################################################
# PROPERTIES
#################################################################
Expand Down Expand Up @@ -274,7 +273,7 @@ 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.is_a?(self.class) &&
other.pixels == pixels &&
other.width == width &&
other.height == height
Expand All @@ -298,7 +297,7 @@ def to_image
def inspect
inspected = "<#{self.class.name} #{width}x#{height} ["
for y in 0...height
inspected << "\n\t[" << row(y).map { |p| ChunkyPNG::Color.to_hex(p) }.join(' ') << ']'
inspected << "\n\t[" << row(y).map { |p| ChunkyPNG::Color.to_hex(p) }.join(" ") << "]"
end
inspected << "\n]>"
end
Expand Down Expand Up @@ -360,13 +359,13 @@ def assert_width!(vector_length)

# Throws an exception if the matrix width and height does not match this canvas' dimensions.
def assert_size!(matrix_width, matrix_height)
if width != matrix_width
if width != matrix_width
raise ChunkyPNG::ExpectationFailed,
'The width of the matrix does not match the canvas width!'
"The width of the matrix does not match the canvas width!"
end
if height != matrix_height
raise ChunkyPNG::ExpectationFailed,
'The height of the matrix does not match the canvas height!'
"The height of the matrix does not match the canvas height!"
end
true
end
Expand Down