zpng 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Binary file
@@ -0,0 +1,78 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ include ZPNG
4
+
5
+ describe Image do
6
+ def _new_img bpp, color
7
+ Image.new(:width => 8, :height => 8, :bpp => bpp, :color => color)
8
+ end
9
+
10
+ [1,2,4,8].each do |bpp|
11
+ [true, false].each do |color|
12
+ describe "new( :bpp => #{bpp}, :color => #{color} )" do
13
+ subject(:img){ _new_img(bpp,color) }
14
+ it("should export"){ img.export.should start_with(Image::PNG_HDR) }
15
+ it("should to_s") { img.to_s.strip.split("\n").size.should == 8 }
16
+
17
+ subject{ img.hdr }
18
+ its(:depth) { should == bpp }
19
+ its(:color) { should == (color ? COLOR_INDEXED : COLOR_GRAYSCALE) }
20
+ end
21
+ end
22
+ end
23
+
24
+ describe "new( :bpp => 16, :color => false )" do
25
+ subject(:img){ _new_img(16,false) }
26
+ it("should export"){ img.export.should start_with(Image::PNG_HDR) }
27
+ it("should to_s") { img.to_s.strip.split("\n").size.should == 8 }
28
+
29
+ subject{ img.hdr }
30
+ its(:depth) { should == 8 } # 8 bits per color + 8 per alpha = 16 bpp
31
+ its(:color) { should == COLOR_GRAY_ALPHA }
32
+ end
33
+
34
+ describe "new( :bpp => 16, :color => true )" do
35
+ subject{ lambda{ _new_img(16,true) } }
36
+ it { should raise_exception }
37
+ end
38
+
39
+ describe "new( :bpp => 24, :color => false )" do
40
+ subject(:img){ _new_img(24,false) }
41
+ it("should export"){ img.export.should start_with(Image::PNG_HDR) }
42
+ it("should to_s") { img.to_s.strip.split("\n").size.should == 8 }
43
+
44
+ subject{ img.hdr }
45
+ its(:depth) { should == 8 } # each channel depth = 8
46
+ its(:color) { should == COLOR_RGB }
47
+ end
48
+
49
+ describe "new( :bpp => 24, :color => true )" do
50
+ subject(:img){ _new_img(24,true) }
51
+ it("should export"){ img.export.should start_with(Image::PNG_HDR) }
52
+ it("should to_s") { img.to_s.strip.split("\n").size.should == 8 }
53
+
54
+ subject{ img.hdr }
55
+ its(:depth) { should == 8 } # each channel depth = 8
56
+ its(:color) { should == COLOR_RGB }
57
+ end
58
+
59
+ describe "new( :bpp => 32, :color => false )" do
60
+ subject(:img){ _new_img(32,false) }
61
+ it("should export"){ img.export.should start_with(Image::PNG_HDR) }
62
+ it("should to_s") { img.to_s.strip.split("\n").size.should == 8 }
63
+
64
+ subject{ img.hdr }
65
+ its(:depth) { should == 8 } # each channel depth = 8
66
+ its(:color) { should == COLOR_RGBA }
67
+ end
68
+
69
+ describe "new( :bpp => 32, :color => true )" do
70
+ subject(:img){ _new_img(32,true) }
71
+ it("should export"){ img.export.should start_with(Image::PNG_HDR) }
72
+ it("should to_s") { img.to_s.strip.split("\n").size.should == 8 }
73
+
74
+ subject{ img.hdr }
75
+ its(:depth) { should == 8 } # each channel depth = 8
76
+ its(:color) { should == COLOR_RGBA }
77
+ end
78
+ end
@@ -0,0 +1,93 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ CROP_WIDTH = 10
4
+ CROP_HEIGHT = 10
5
+ CROP_SAMPLE = File.join(SAMPLES_DIR, "captcha_4bpp.png")
6
+
7
+ QR_SQUARE = <<EOF
8
+ #######
9
+ #.....#
10
+ #.###.#
11
+ #.###.#
12
+ #.###.#
13
+ #.....#
14
+ #######
15
+ EOF
16
+
17
+ include ZPNG
18
+
19
+ describe Image do
20
+ describe "crop" do
21
+ it "crops and keeps original image unchanged" do
22
+ src1 = Image.load(CROP_SAMPLE)
23
+ src2 = Image.load(CROP_SAMPLE)
24
+ dest = src1.crop :width => CROP_WIDTH, :height => CROP_HEIGHT
25
+
26
+ dest.width.should == CROP_WIDTH
27
+ dest.height.should == CROP_HEIGHT
28
+
29
+ dest.width.should_not == src1.width
30
+ dest.height.should_not == src1.height
31
+
32
+ src1.export.should == src2.export
33
+ src1.export.should_not == dest.export
34
+ src2.export.should_not == dest.export
35
+ end
36
+ end
37
+
38
+ describe "crop! result" do
39
+ let!(:img){
40
+ Image.load(CROP_SAMPLE).crop! :width => CROP_WIDTH, :height => CROP_HEIGHT
41
+ }
42
+ it "has #{CROP_HEIGHT} scanlines" do
43
+ img.scanlines.size.should == CROP_HEIGHT
44
+ end
45
+
46
+ CROP_HEIGHT.times do |i|
47
+ it "calculates proper #size" do
48
+ scanline_size = (img.hdr.bpp*img.width/8).ceil + 1
49
+ img.scanlines[i].size.should == scanline_size
50
+ end
51
+ it "exports proper count of bytes" do
52
+ scanline_size = (img.hdr.bpp*img.width/8).ceil + 1
53
+ img.scanlines[i].export.size.should == scanline_size
54
+ end
55
+ end
56
+
57
+ describe "reimported" do
58
+ let!(:img2){ Image.new(img.export) }
59
+
60
+ it "has #{CROP_HEIGHT} scanlines" do
61
+ img2.scanlines.size.should == CROP_HEIGHT
62
+ end
63
+ end
64
+ end
65
+
66
+ SAMPLES.find_all{ |fname| fname['qr_'] }.each do |fname|
67
+ describe fname do
68
+ let!(:img){ Image.load fname }
69
+
70
+ it "should extract left square" do
71
+ img.crop! :x => 1, :y => 1, :width => 7, :height => 7
72
+ img.to_s(:white => '.', :black => '#').strip.should == QR_SQUARE.strip
73
+ end
74
+
75
+ it "should extract right square" do
76
+ img.crop! :x => 27, :y => 1, :width => 7, :height => 7
77
+ img.to_s(:white => '.', :black => '#').strip.should == QR_SQUARE.strip
78
+ end
79
+
80
+ it "should extract bottom square" do
81
+ img.crop! :x => 1, :y => 27, :width => 7, :height => 7
82
+ img.to_s(:white => '.', :black => '#').strip.should == QR_SQUARE.strip
83
+ end
84
+
85
+ it "keeps whole original image if crop is larger than image" do
86
+ img2 = img.crop :x => 0, :y => 0, :width => 7000, :height => 7000
87
+ img2.width.should == img.width
88
+ img2.height.should == img.height
89
+ img2.to_s.should == img.to_s
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ NEW_IMG_WIDTH = 20
4
+ NEW_IMG_HEIGHT = 10
5
+
6
+ describe ZPNG::Image do
7
+ describe "new" do
8
+ let!(:img){ ZPNG::Image.new :width => NEW_IMG_WIDTH, :height => NEW_IMG_HEIGHT }
9
+
10
+ it "returns ZPNG::Image" do
11
+ img.should be_instance_of(ZPNG::Image)
12
+ end
13
+ it "creates new image of specified size" do
14
+ img.width.should == NEW_IMG_WIDTH
15
+ img.height.should == NEW_IMG_HEIGHT
16
+ end
17
+
18
+ describe "exported image" do
19
+ let!(:eimg){ img.export }
20
+ it "has PNG header" do
21
+ eimg.should start_with(ZPNG::Image::PNG_HDR)
22
+ end
23
+
24
+ describe "parsed again" do
25
+ let!(:img2){ ZPNG::Image.new(eimg) }
26
+
27
+ it "is a ZPNG::Image" do
28
+ img2.should be_instance_of(ZPNG::Image)
29
+ end
30
+
31
+ it "should be of specified size" do
32
+ img2.width.should == NEW_IMG_WIDTH
33
+ img2.height.should == NEW_IMG_HEIGHT
34
+ end
35
+
36
+ it "should have bpp = 32" do
37
+ img2.hdr.bpp.should == 32
38
+ end
39
+
40
+ it "should have 3 chunks: IHDR, IDAT, IEND" do
41
+ img2.chunks.map(&:type).should == %w'IHDR IDAT IEND'
42
+ end
43
+
44
+ it "should have all pixels transparent" do
45
+ NEW_IMG_HEIGHT.times do |y|
46
+ NEW_IMG_WIDTH.times do |x|
47
+ img2[x,y].should be_transparent
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
@@ -38,18 +38,11 @@ ASCII_MODIFIED_QR = <<EOF
38
38
  ...................................
39
39
  EOF
40
40
 
41
- samples =
42
- if ENV['SAMPLES']
43
- ENV['SAMPLES'].split(' ')
44
- else
45
- Dir[File.join(SAMPLES_DIR,'qr_*.png')]
46
- end
47
-
48
41
  describe "ZPNG modify" do
49
42
  it "should have QR examples" do
50
- samples.should_not be_empty
43
+ SAMPLES.should_not be_empty
51
44
  end
52
- samples.each do |fname|
45
+ SAMPLES.each do |fname|
53
46
  describe fname.sub(File.dirname(SAMPLES_DIR)+'/','') do
54
47
  img = ZPNG::Image.new(fname)
55
48
  it "modifies img - color=#{img.hdr.color}, depth=#{img.hdr.depth}, bpp=#{img.hdr.bpp}" do
@@ -0,0 +1,47 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+ include ZPNG
3
+
4
+ describe Image do
5
+ def _new_img bpp, color
6
+ Image.new(:width => 16, :height => 1, :bpp => bpp, :color => color)
7
+ end
8
+
9
+ # before :all do
10
+ # $html = "<style>img {width:64px}</style>\n<div style='background-color:#ccc'>\n"
11
+ # end
12
+
13
+ [1,2,4,8,16,24,32].each do |bpp|
14
+ [true, false].each do |color|
15
+ next if bpp == 16 && color
16
+ describe "new( :bpp => #{bpp}, :color => #{color} )" do
17
+ 16.times do |x|
18
+ it "should set pixel at pos #{x}" do
19
+ bg = Color::BLACK
20
+ fg = Color::WHITE
21
+
22
+ img = _new_img bpp, color
23
+ if img.palette
24
+ img.palette << bg if img.palette
25
+ else
26
+ img.width.times{ |i| img[i,0] = bg }
27
+ end
28
+ img[x,0] = fg
29
+
30
+ s = '#'*16
31
+ s[x] = ' '
32
+ img.to_s.should == s
33
+
34
+ # fname = "out-#{x}-#{bpp}-#{color}.png"
35
+ # img.save fname
36
+ # $html << "<img src='#{fname}'><br/>\n"
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ # after :all do
44
+ # $html << "</div>"
45
+ # File.open("index.html","w"){ |f| f<<$html }
46
+ # end
47
+ end
@@ -8,7 +8,6 @@ require 'zpng'
8
8
  Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
9
9
 
10
10
  RSpec.configure do |config|
11
-
12
11
  end
13
12
 
14
13
  SAMPLES_DIR = File.join(
@@ -16,3 +15,10 @@ SAMPLES_DIR = File.join(
16
15
  File.dirname(
17
16
  File.expand_path(__FILE__))),
18
17
  "samples")
18
+
19
+ SAMPLES =
20
+ if ENV['SAMPLES']
21
+ ENV['SAMPLES'].split(' ')
22
+ else
23
+ Dir[File.join(SAMPLES_DIR,'qr_*.png')]
24
+ end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "zpng"
8
- s.version = "0.0.2"
8
+ s.version = "0.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Andrey \"Zed\" Zaikin"]
12
- s.date = "2012-03-18"
12
+ s.date = "2012-12-10"
13
13
  s.email = "zed.0xff@gmail.com"
14
14
  s.executables = ["zpng"]
15
15
  s.extra_rdoc_files = [
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  "lib/zpng/color.rb",
36
36
  "lib/zpng/image.rb",
37
37
  "lib/zpng/scan_line.rb",
38
+ "samples/captcha_4bpp.png",
38
39
  "samples/modify.rb",
39
40
  "samples/qr_aux_chunks.png",
40
41
  "samples/qr_bw.png",
@@ -45,7 +46,11 @@ Gem::Specification.new do |s|
45
46
  "samples/qr_rgb.png",
46
47
  "samples/qr_rgba.png",
47
48
  "spec/ascii_spec.rb",
49
+ "spec/create_image_spec.rb",
50
+ "spec/crop_spec.rb",
51
+ "spec/image_spec.rb",
48
52
  "spec/modify_spec.rb",
53
+ "spec/running_pixel_spec.rb",
49
54
  "spec/spec_helper.rb",
50
55
  "spec/zpng_spec.rb",
51
56
  "zpng.gemspec"
@@ -53,7 +58,7 @@ Gem::Specification.new do |s|
53
58
  s.homepage = "http://github.com/zed-0xff/zpng"
54
59
  s.licenses = ["MIT"]
55
60
  s.require_paths = ["lib"]
56
- s.rubygems_version = "1.8.10"
61
+ s.rubygems_version = "1.8.24"
57
62
  s.summary = "pure ruby PNG file manipulation & validation"
58
63
 
59
64
  if s.respond_to? :specification_version then
@@ -62,25 +67,22 @@ Gem::Specification.new do |s|
62
67
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
63
68
  s.add_runtime_dependency(%q<colorize>, [">= 0"])
64
69
  s.add_runtime_dependency(%q<hexdump>, [">= 0"])
65
- s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
66
- s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
67
- s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
68
- s.add_development_dependency(%q<rcov>, [">= 0"])
70
+ s.add_development_dependency(%q<rspec>, [">= 2.8.0"])
71
+ s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
72
+ s.add_development_dependency(%q<jeweler>, ["~> 1.8.4"])
69
73
  else
70
74
  s.add_dependency(%q<colorize>, [">= 0"])
71
75
  s.add_dependency(%q<hexdump>, [">= 0"])
72
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
73
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
74
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
75
- s.add_dependency(%q<rcov>, [">= 0"])
76
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
77
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
78
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
76
79
  end
77
80
  else
78
81
  s.add_dependency(%q<colorize>, [">= 0"])
79
82
  s.add_dependency(%q<hexdump>, [">= 0"])
80
- s.add_dependency(%q<rspec>, ["~> 2.8.0"])
81
- s.add_dependency(%q<bundler>, ["~> 1.0.0"])
82
- s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
83
- s.add_dependency(%q<rcov>, [">= 0"])
83
+ s.add_dependency(%q<rspec>, [">= 2.8.0"])
84
+ s.add_dependency(%q<bundler>, [">= 1.0.0"])
85
+ s.add_dependency(%q<jeweler>, ["~> 1.8.4"])
84
86
  end
85
87
  end
86
88
 
metadata CHANGED
@@ -1,82 +1,96 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: zpng
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
4
  prerelease:
5
+ version: 0.1.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Andrey "Zed" Zaikin
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-03-18 00:00:00.000000000 Z
12
+ date: 2012-12-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
+ prerelease: false
16
+ type: :runtime
15
17
  name: colorize
16
- requirement: &70231152732120 !ruby/object:Gem::Requirement
18
+ requirement: !ruby/object:Gem::Requirement
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
17
23
  none: false
24
+ version_requirements: !ruby/object:Gem::Requirement
18
25
  requirements:
19
26
  - - ! '>='
20
27
  - !ruby/object:Gem::Version
21
28
  version: '0'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: *70231152732120
29
+ none: false
25
30
  - !ruby/object:Gem::Dependency
31
+ prerelease: false
32
+ type: :runtime
26
33
  name: hexdump
27
- requirement: &70231152731280 !ruby/object:Gem::Requirement
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
28
39
  none: false
40
+ version_requirements: !ruby/object:Gem::Requirement
29
41
  requirements:
30
42
  - - ! '>='
31
43
  - !ruby/object:Gem::Version
32
44
  version: '0'
33
- type: :runtime
34
- prerelease: false
35
- version_requirements: *70231152731280
45
+ none: false
36
46
  - !ruby/object:Gem::Dependency
47
+ prerelease: false
48
+ type: :development
37
49
  name: rspec
38
- requirement: &70231152746560 !ruby/object:Gem::Requirement
50
+ requirement: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 2.8.0
39
55
  none: false
56
+ version_requirements: !ruby/object:Gem::Requirement
40
57
  requirements:
41
- - - ~>
58
+ - - ! '>='
42
59
  - !ruby/object:Gem::Version
43
60
  version: 2.8.0
44
- type: :development
45
- prerelease: false
46
- version_requirements: *70231152746560
61
+ none: false
47
62
  - !ruby/object:Gem::Dependency
63
+ prerelease: false
64
+ type: :development
48
65
  name: bundler
49
- requirement: &70231152745500 !ruby/object:Gem::Requirement
66
+ requirement: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ! '>='
69
+ - !ruby/object:Gem::Version
70
+ version: 1.0.0
50
71
  none: false
72
+ version_requirements: !ruby/object:Gem::Requirement
51
73
  requirements:
52
- - - ~>
74
+ - - ! '>='
53
75
  - !ruby/object:Gem::Version
54
76
  version: 1.0.0
55
- type: :development
56
- prerelease: false
57
- version_requirements: *70231152745500
77
+ none: false
58
78
  - !ruby/object:Gem::Dependency
79
+ prerelease: false
80
+ type: :development
59
81
  name: jeweler
60
- requirement: &70231152744400 !ruby/object:Gem::Requirement
61
- none: false
82
+ requirement: !ruby/object:Gem::Requirement
62
83
  requirements:
63
84
  - - ~>
64
85
  - !ruby/object:Gem::Version
65
- version: 1.6.4
66
- type: :development
67
- prerelease: false
68
- version_requirements: *70231152744400
69
- - !ruby/object:Gem::Dependency
70
- name: rcov
71
- requirement: &70231152742060 !ruby/object:Gem::Requirement
86
+ version: 1.8.4
72
87
  none: false
88
+ version_requirements: !ruby/object:Gem::Requirement
73
89
  requirements:
74
- - - ! '>='
90
+ - - ~>
75
91
  - !ruby/object:Gem::Version
76
- version: '0'
77
- type: :development
78
- prerelease: false
79
- version_requirements: *70231152742060
92
+ version: 1.8.4
93
+ none: false
80
94
  description:
81
95
  email: zed.0xff@gmail.com
82
96
  executables:
@@ -104,6 +118,7 @@ files:
104
118
  - lib/zpng/color.rb
105
119
  - lib/zpng/image.rb
106
120
  - lib/zpng/scan_line.rb
121
+ - samples/captcha_4bpp.png
107
122
  - samples/modify.rb
108
123
  - samples/qr_aux_chunks.png
109
124
  - samples/qr_bw.png
@@ -114,7 +129,11 @@ files:
114
129
  - samples/qr_rgb.png
115
130
  - samples/qr_rgba.png
116
131
  - spec/ascii_spec.rb
132
+ - spec/create_image_spec.rb
133
+ - spec/crop_spec.rb
134
+ - spec/image_spec.rb
117
135
  - spec/modify_spec.rb
136
+ - spec/running_pixel_spec.rb
118
137
  - spec/spec_helper.rb
119
138
  - spec/zpng_spec.rb
120
139
  - zpng.gemspec
@@ -126,23 +145,23 @@ rdoc_options: []
126
145
  require_paths:
127
146
  - lib
128
147
  required_ruby_version: !ruby/object:Gem::Requirement
129
- none: false
130
148
  requirements:
131
149
  - - ! '>='
132
150
  - !ruby/object:Gem::Version
133
151
  version: '0'
134
152
  segments:
135
153
  - 0
136
- hash: -893836760864101821
137
- required_rubygems_version: !ruby/object:Gem::Requirement
154
+ hash: -488684070557161038
138
155
  none: false
156
+ required_rubygems_version: !ruby/object:Gem::Requirement
139
157
  requirements:
140
158
  - - ! '>='
141
159
  - !ruby/object:Gem::Version
142
160
  version: '0'
161
+ none: false
143
162
  requirements: []
144
163
  rubyforge_project:
145
- rubygems_version: 1.8.10
164
+ rubygems_version: 1.8.24
146
165
  signing_key:
147
166
  specification_version: 3
148
167
  summary: pure ruby PNG file manipulation & validation