zpng 0.0.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/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +15 -0
- data/Gemfile.lock +32 -0
- data/LICENSE.txt +20 -0
- data/README.md +161 -0
- data/README.md.tpl +65 -0
- data/Rakefile +74 -0
- data/VERSION +1 -0
- data/bin/zpng +7 -0
- data/lib/zpng.rb +41 -0
- data/lib/zpng/block.rb +41 -0
- data/lib/zpng/chunk.rb +133 -0
- data/lib/zpng/cli.rb +90 -0
- data/lib/zpng/color.rb +25 -0
- data/lib/zpng/image.rb +130 -0
- data/lib/zpng/scan_line.rb +222 -0
- data/samples/modify.rb +20 -0
- data/samples/qr_aux_chunks.png +0 -0
- data/samples/qr_bw.png +0 -0
- data/samples/qr_gray_alpha.png +0 -0
- data/samples/qr_grayscale.png +0 -0
- data/samples/qr_plte.png +0 -0
- data/samples/qr_plte_bw.png +0 -0
- data/samples/qr_rgb.png +0 -0
- data/samples/qr_rgba.png +0 -0
- data/spec/ascii_spec.rb +52 -0
- data/spec/modify_spec.rb +63 -0
- data/spec/spec_helper.rb +18 -0
- data/spec/zpng_spec.rb +7 -0
- metadata +148 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
gem 'colorize'
|
6
|
+
gem 'hexdump'
|
7
|
+
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "rspec", "~> 2.8.0"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.6.4"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
colorize (0.5.8)
|
5
|
+
diff-lcs (1.1.3)
|
6
|
+
git (1.2.5)
|
7
|
+
hexdump (0.2.1)
|
8
|
+
jeweler (1.6.4)
|
9
|
+
bundler (~> 1.0)
|
10
|
+
git (>= 1.2.5)
|
11
|
+
rake
|
12
|
+
rake (0.9.2.2)
|
13
|
+
rcov (0.9.11)
|
14
|
+
rspec (2.8.0)
|
15
|
+
rspec-core (~> 2.8.0)
|
16
|
+
rspec-expectations (~> 2.8.0)
|
17
|
+
rspec-mocks (~> 2.8.0)
|
18
|
+
rspec-core (2.8.0)
|
19
|
+
rspec-expectations (2.8.0)
|
20
|
+
diff-lcs (~> 1.1.2)
|
21
|
+
rspec-mocks (2.8.0)
|
22
|
+
|
23
|
+
PLATFORMS
|
24
|
+
ruby
|
25
|
+
|
26
|
+
DEPENDENCIES
|
27
|
+
bundler (~> 1.0.0)
|
28
|
+
colorize
|
29
|
+
hexdump
|
30
|
+
jeweler (~> 1.6.4)
|
31
|
+
rcov
|
32
|
+
rspec (~> 2.8.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Andrey "Zed" Zaikin
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,161 @@
|
|
1
|
+
zpng
|
2
|
+
======
|
3
|
+
|
4
|
+
|
5
|
+
Description
|
6
|
+
-----------
|
7
|
+
A pure ruby PNG file manipulation & validation
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
gem install zpng
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
# zpng -h
|
17
|
+
|
18
|
+
Usage: zpng [options]
|
19
|
+
-v, --verbose Run verbosely (can be used multiple times)
|
20
|
+
-q, --quiet Silent any warnings (can be used multiple times)
|
21
|
+
-I, --info General image info
|
22
|
+
-C, --chunks Show file chunks (default)
|
23
|
+
-A, --ascii Try to display image as ASCII (works best with monochrome images)
|
24
|
+
-S, --scanlines Show scanlines info
|
25
|
+
-P, --palette Show palette
|
26
|
+
|
27
|
+
### Info
|
28
|
+
|
29
|
+
# zpng --info qr_rgb.png
|
30
|
+
|
31
|
+
[.] image size 35x35
|
32
|
+
[.] uncompressed imagedata size = 3710 bytes
|
33
|
+
|
34
|
+
### Chunks
|
35
|
+
|
36
|
+
# zpng --chunks qr_aux_chunks.png
|
37
|
+
|
38
|
+
[.] #<ZPNG::Chunk IHDR size= 13, crc=36a28ef4, width=35, height=35, depth=1, color=0, compression=0, filter=0, interlace=0> CRC OK
|
39
|
+
[.] #<ZPNG::Chunk gAMA size= 4, crc=0bfc6105 > CRC OK
|
40
|
+
[.] #<ZPNG::Chunk sRGB size= 1, crc=aece1ce9 > CRC OK
|
41
|
+
[.] #<ZPNG::Chunk cHRM size= 32, crc=9cba513c > CRC OK
|
42
|
+
[.] #<ZPNG::Chunk pHYs size= 9, crc=46c96b3e > CRC OK
|
43
|
+
[.] #<ZPNG::Chunk IDAT size= 213, crc=5f3f1ff9 > CRC OK
|
44
|
+
[.] #<ZPNG::Chunk tEXt size= 37, crc=8d62fd1a > CRC OK
|
45
|
+
[.] #<ZPNG::Chunk tEXt size= 37, crc=fc3f45a6 > CRC OK
|
46
|
+
[.] #<ZPNG::Chunk IEND size= 0, crc=ae426082 > CRC OK
|
47
|
+
|
48
|
+
### ASCII
|
49
|
+
|
50
|
+
# zpng --ascii qr_rgb.png
|
51
|
+
|
52
|
+
|
53
|
+
####### ## ## # ## #######
|
54
|
+
# # ## # ## # #
|
55
|
+
# ### # # ##### # # ## # ### #
|
56
|
+
# ### # ## # # #### ## # ### #
|
57
|
+
# ### # #### # # #### # ### #
|
58
|
+
# # # # # ##### # #
|
59
|
+
####### # # # # # # # # # #######
|
60
|
+
# # ## ## ##
|
61
|
+
#### # # ## # ## ## ### #
|
62
|
+
# ## ## ## # ## # ### ##
|
63
|
+
# ### ## ## # ## ## # ### #
|
64
|
+
### # ##### # # ## # #
|
65
|
+
## ## # # # # #### ## ##
|
66
|
+
# # ## ## # # ### ## ##
|
67
|
+
# ###### ## # # ##### #######
|
68
|
+
# #### ### ## # ## # ## #
|
69
|
+
## ## ### ## # ## ########
|
70
|
+
# ### ## ## ######### #
|
71
|
+
#### ##### ## ## # # # #
|
72
|
+
## # # # # ##### # # ## #
|
73
|
+
###### ##### # # ## #
|
74
|
+
### # #### # ### # ###
|
75
|
+
### #### #### # # ####### ##
|
76
|
+
# # ## ## # ## # ## ###### #
|
77
|
+
# ####### # ## ## ##### #
|
78
|
+
# ### # ## ## # #
|
79
|
+
####### # # ## ### # #
|
80
|
+
# # ### # # ###### ### #
|
81
|
+
# ### # # #### # ########
|
82
|
+
# ### # ### # #### # # #
|
83
|
+
# ### # ### ##### # ## # #
|
84
|
+
# # #### ## ## ### #
|
85
|
+
####### # # ## # ## # ## #
|
86
|
+
|
87
|
+
### Scanlines
|
88
|
+
|
89
|
+
# zpng --scanlines qr_rgb.png
|
90
|
+
|
91
|
+
[#<ZPNG::ScanLine idx=0, bpp=24, BPP=3, offset=1, filter=1>,
|
92
|
+
#<ZPNG::ScanLine idx=1, bpp=24, BPP=3, offset=107, filter=4>,
|
93
|
+
#<ZPNG::ScanLine idx=2, bpp=24, BPP=3, offset=213, filter=4>,
|
94
|
+
#<ZPNG::ScanLine idx=3, bpp=24, BPP=3, offset=319, filter=4>,
|
95
|
+
#<ZPNG::ScanLine idx=4, bpp=24, BPP=3, offset=425, filter=2>,
|
96
|
+
#<ZPNG::ScanLine idx=5, bpp=24, BPP=3, offset=531, filter=2>,
|
97
|
+
#<ZPNG::ScanLine idx=6, bpp=24, BPP=3, offset=637, filter=4>,
|
98
|
+
#<ZPNG::ScanLine idx=7, bpp=24, BPP=3, offset=743, filter=0>,
|
99
|
+
#<ZPNG::ScanLine idx=8, bpp=24, BPP=3, offset=849, filter=1>,
|
100
|
+
#<ZPNG::ScanLine idx=9, bpp=24, BPP=3, offset=955, filter=0>,
|
101
|
+
#<ZPNG::ScanLine idx=10, bpp=24, BPP=3, offset=1061, filter=0>,
|
102
|
+
#<ZPNG::ScanLine idx=11, bpp=24, BPP=3, offset=1167, filter=0>,
|
103
|
+
#<ZPNG::ScanLine idx=12, bpp=24, BPP=3, offset=1273, filter=1>,
|
104
|
+
#<ZPNG::ScanLine idx=13, bpp=24, BPP=3, offset=1379, filter=2>,
|
105
|
+
#<ZPNG::ScanLine idx=14, bpp=24, BPP=3, offset=1485, filter=4>,
|
106
|
+
#<ZPNG::ScanLine idx=15, bpp=24, BPP=3, offset=1591, filter=0>,
|
107
|
+
#<ZPNG::ScanLine idx=16, bpp=24, BPP=3, offset=1697, filter=4>,
|
108
|
+
#<ZPNG::ScanLine idx=17, bpp=24, BPP=3, offset=1803, filter=0>,
|
109
|
+
#<ZPNG::ScanLine idx=18, bpp=24, BPP=3, offset=1909, filter=4>,
|
110
|
+
#<ZPNG::ScanLine idx=19, bpp=24, BPP=3, offset=2015, filter=4>,
|
111
|
+
#<ZPNG::ScanLine idx=20, bpp=24, BPP=3, offset=2121, filter=0>,
|
112
|
+
#<ZPNG::ScanLine idx=21, bpp=24, BPP=3, offset=2227, filter=1>,
|
113
|
+
#<ZPNG::ScanLine idx=22, bpp=24, BPP=3, offset=2333, filter=2>,
|
114
|
+
#<ZPNG::ScanLine idx=23, bpp=24, BPP=3, offset=2439, filter=0>,
|
115
|
+
#<ZPNG::ScanLine idx=24, bpp=24, BPP=3, offset=2545, filter=2>,
|
116
|
+
#<ZPNG::ScanLine idx=25, bpp=24, BPP=3, offset=2651, filter=1>,
|
117
|
+
#<ZPNG::ScanLine idx=26, bpp=24, BPP=3, offset=2757, filter=1>,
|
118
|
+
#<ZPNG::ScanLine idx=27, bpp=24, BPP=3, offset=2863, filter=4>,
|
119
|
+
#<ZPNG::ScanLine idx=28, bpp=24, BPP=3, offset=2969, filter=4>,
|
120
|
+
#<ZPNG::ScanLine idx=29, bpp=24, BPP=3, offset=3075, filter=4>,
|
121
|
+
#<ZPNG::ScanLine idx=30, bpp=24, BPP=3, offset=3181, filter=4>,
|
122
|
+
#<ZPNG::ScanLine idx=31, bpp=24, BPP=3, offset=3287, filter=2>,
|
123
|
+
#<ZPNG::ScanLine idx=32, bpp=24, BPP=3, offset=3393, filter=4>,
|
124
|
+
#<ZPNG::ScanLine idx=33, bpp=24, BPP=3, offset=3499, filter=4>,
|
125
|
+
#<ZPNG::ScanLine idx=34, bpp=24, BPP=3, offset=3605, filter=1>]
|
126
|
+
|
127
|
+
### Palette
|
128
|
+
|
129
|
+
# zpng --palette qr_plte_bw.png
|
130
|
+
|
131
|
+
#<ZPNG::Chunk PLTE size= 6, crc=55c2d37e >
|
132
|
+
00000000 ff ff ff 00 00 00 |......|
|
133
|
+
|
134
|
+
|
135
|
+
## Image manipulation
|
136
|
+
|
137
|
+
#!/usr/bin/env ruby
|
138
|
+
require 'zpng'
|
139
|
+
include ZPNG
|
140
|
+
|
141
|
+
img = Image.new(File.join(File.dirname(__FILE__),"http.png"))
|
142
|
+
|
143
|
+
puts "[.] original:"
|
144
|
+
puts img.to_s
|
145
|
+
puts
|
146
|
+
|
147
|
+
img.width.times do |x|
|
148
|
+
img[x,0] = (x % 2 == 0) ? Color::WHITE : Color::BLACK
|
149
|
+
end
|
150
|
+
|
151
|
+
puts "[.] modified:"
|
152
|
+
puts img.to_s
|
153
|
+
|
154
|
+
File.open("http-modified.png","wb") do |f|
|
155
|
+
f << img.export
|
156
|
+
end
|
157
|
+
|
158
|
+
|
159
|
+
License
|
160
|
+
-------
|
161
|
+
Released under the MIT License. See the [LICENSE](https://github.com/zed-0xff/zpng/blob/master/LICENSE.txt) file for further details.
|
data/README.md.tpl
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
zpng
|
2
|
+
======
|
3
|
+
|
4
|
+
|
5
|
+
Description
|
6
|
+
-----------
|
7
|
+
A pure ruby PNG file manipulation & validation
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
gem install zpng
|
12
|
+
|
13
|
+
Usage
|
14
|
+
-----
|
15
|
+
|
16
|
+
% zpng -h
|
17
|
+
|
18
|
+
### Info
|
19
|
+
|
20
|
+
% zpng --info qr_rgb.png
|
21
|
+
|
22
|
+
### Chunks
|
23
|
+
|
24
|
+
% zpng --chunks qr_aux_chunks.png
|
25
|
+
|
26
|
+
### ASCII
|
27
|
+
|
28
|
+
% zpng --ascii qr_rgb.png
|
29
|
+
|
30
|
+
### Scanlines
|
31
|
+
|
32
|
+
% zpng --scanlines qr_rgb.png
|
33
|
+
|
34
|
+
### Palette
|
35
|
+
|
36
|
+
% zpng --palette qr_plte_bw.png
|
37
|
+
|
38
|
+
|
39
|
+
## Image manipulation
|
40
|
+
|
41
|
+
#!/usr/bin/env ruby
|
42
|
+
require 'zpng'
|
43
|
+
include ZPNG
|
44
|
+
|
45
|
+
img = Image.new(File.join(File.dirname(__FILE__),"http.png"))
|
46
|
+
|
47
|
+
puts "[.] original:"
|
48
|
+
puts img.to_s
|
49
|
+
puts
|
50
|
+
|
51
|
+
img.width.times do |x|
|
52
|
+
img[x,0] = (x % 2 == 0) ? Color::WHITE : Color::BLACK
|
53
|
+
end
|
54
|
+
|
55
|
+
puts "[.] modified:"
|
56
|
+
puts img.to_s
|
57
|
+
|
58
|
+
File.open("http-modified.png","wb") do |f|
|
59
|
+
f << img.export
|
60
|
+
end
|
61
|
+
|
62
|
+
|
63
|
+
License
|
64
|
+
-------
|
65
|
+
Released under the MIT License. See the [LICENSE](https://github.com/zed-0xff/zpng/blob/master/LICENSE.txt) file for further details.
|
data/Rakefile
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "zpng"
|
18
|
+
gem.homepage = "http://github.com/zed-0xff/zpng"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{pure ruby PNG file manipulation & validation}
|
21
|
+
#gem.description = %Q{TODO: longer description of your gem}
|
22
|
+
gem.email = "zed.0xff@gmail.com"
|
23
|
+
gem.authors = ["Andrey \"Zed\" Zaikin"]
|
24
|
+
gem.executables = %w'zpng'
|
25
|
+
gem.files.include "lib/**/*.rb"
|
26
|
+
# dependencies defined in Gemfile
|
27
|
+
end
|
28
|
+
Jeweler::RubygemsDotOrgTasks.new
|
29
|
+
|
30
|
+
require 'rspec/core'
|
31
|
+
require 'rspec/core/rake_task'
|
32
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
33
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
34
|
+
end
|
35
|
+
|
36
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
37
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
38
|
+
spec.rcov = true
|
39
|
+
end
|
40
|
+
|
41
|
+
task :default => :spec
|
42
|
+
|
43
|
+
#require 'rake/rdoctask'
|
44
|
+
#Rake::RDocTask.new do |rdoc|
|
45
|
+
# version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
#
|
47
|
+
# rdoc.rdoc_dir = 'rdoc'
|
48
|
+
# rdoc.title = "zpng #{version}"
|
49
|
+
# rdoc.rdoc_files.include('README*')
|
50
|
+
# rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
#end
|
52
|
+
|
53
|
+
desc "build readme"
|
54
|
+
task :readme do
|
55
|
+
require 'erb'
|
56
|
+
tpl = File.read('README.md.tpl').gsub(/^%\s+(.+)/) do |x|
|
57
|
+
x.sub! /^%/,''
|
58
|
+
"<%= run(\"#{x}\") %>"
|
59
|
+
end
|
60
|
+
def run cmd
|
61
|
+
cmd.strip!
|
62
|
+
puts "[.] #{cmd} ..."
|
63
|
+
r = " # #{cmd}\n\n"
|
64
|
+
cmd.sub! /^zpng/,"../bin/zpng"
|
65
|
+
lines = `#{cmd}`.sub(/\A\n+/m,'').sub(/\s+\Z/,'').split("\n")
|
66
|
+
lines = lines[0,25] + ['...'] if lines.size > 50
|
67
|
+
r << lines.map{|x| " #{x}"}.join("\n")
|
68
|
+
r << "\n"
|
69
|
+
end
|
70
|
+
Dir.chdir 'samples'
|
71
|
+
result = ERB.new(tpl,nil,'%>').result
|
72
|
+
Dir.chdir '..'
|
73
|
+
File.open('README.md','w'){ |f| f << result }
|
74
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.1
|
data/bin/zpng
ADDED
data/lib/zpng.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'zlib'
|
3
|
+
require 'stringio'
|
4
|
+
require 'colorize'
|
5
|
+
|
6
|
+
require 'zpng/color'
|
7
|
+
require 'zpng/block'
|
8
|
+
require 'zpng/scan_line'
|
9
|
+
require 'zpng/chunk'
|
10
|
+
require 'zpng/image'
|
11
|
+
|
12
|
+
# see alse http://github.com/wvanbergen/chunky_png/
|
13
|
+
|
14
|
+
if $0 == __FILE__
|
15
|
+
if ARGV.size == 0
|
16
|
+
puts "gimme a png filename!"
|
17
|
+
else
|
18
|
+
img = ZPNG::Image.new(ARGV[0])
|
19
|
+
img.dump
|
20
|
+
puts "[.] image size #{img.width}x#{img.height}"
|
21
|
+
puts "[.] uncompressed imagedata size=#{img.imagedata.size}"
|
22
|
+
puts "[.] palette =#{img.palette}"
|
23
|
+
# puts "[.] imagedata: #{img.imagedata[0..30].split('').map{|x| sprintf("%02X",x.ord)}.join(' ')}"
|
24
|
+
|
25
|
+
require 'hexdump'
|
26
|
+
#Hexdump.dump(img.imagedata, :width => 6)
|
27
|
+
require 'pp'
|
28
|
+
pp img.scanlines[0..5]
|
29
|
+
# puts Hexdump.dump(img.imagedata[0,60])
|
30
|
+
# img.scanlines.each do |l|
|
31
|
+
# puts l.to_s
|
32
|
+
# end
|
33
|
+
puts img.to_s
|
34
|
+
img[1,0]= ZPNG::Color.new(0,0,0)
|
35
|
+
puts img.to_s
|
36
|
+
File.open 'export.png','wb' do |f|
|
37
|
+
f << img.export
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
data/lib/zpng/block.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module ZPNG
|
2
|
+
class Block
|
3
|
+
attr_accessor :width, :height, :pixels
|
4
|
+
def initialize image, x, y, w, h
|
5
|
+
@width, @height = w,h
|
6
|
+
@pixels = []
|
7
|
+
h.times do |i|
|
8
|
+
w.times do |j|
|
9
|
+
@pixels << image[x+j,y+i]
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_s
|
15
|
+
a = []
|
16
|
+
height.times do |i|
|
17
|
+
b = []
|
18
|
+
width.times do |j|
|
19
|
+
b << pixels[i*width+j].to_s
|
20
|
+
end
|
21
|
+
a << b.join(" ")
|
22
|
+
end
|
23
|
+
a.join "\n"
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_binary_string c_white = ' ', c_black = 'X'
|
27
|
+
@pixels.each do |p|
|
28
|
+
raise "pixel #{p.inspect} is not white nor black" if !p.white? && !p.black?
|
29
|
+
end
|
30
|
+
a = []
|
31
|
+
height.times do |i|
|
32
|
+
b = []
|
33
|
+
width.times do |j|
|
34
|
+
b << (pixels[i*width+j].black? ? c_black : c_white)
|
35
|
+
end
|
36
|
+
a << b.join(" ")
|
37
|
+
end
|
38
|
+
a.join "\n"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|