Skip to content

Commit 8f8fc6e

Browse files
committed
phase 2 interlacing working
1 parent 00c80e8 commit 8f8fc6e

File tree

7 files changed

+166
-25
lines changed

7 files changed

+166
-25
lines changed

lib/spittle.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@
1616
require 'spittle/processor'
1717
require 'spittle/mtime_tracker'
1818
require 'spittle/image_data'
19+
require 'spittle/deinterlacer'

lib/spittle/deinterlacer.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class DeInterlacer
2+
def initialize(height, width, pixel_width, raw, decoder)
3+
@height, @width, @decoder = height, width, decoder
4+
@pixel_width = pixel_width
5+
@raw = raw.dup
6+
@out = Spittle::ImageData.new :scanline_width => width, :pixel_width => pixel_width
7+
end
8+
9+
def process
10+
pass1
11+
pass2
12+
@out
13+
end
14+
15+
def pass1_height
16+
@height / 8
17+
end
18+
19+
def pass1_width
20+
end
21+
22+
def pass1
23+
sub_width = ((@width / 8) * @pixel_width) + 1
24+
sub_height = pass1_height
25+
scanline_width = @width / 8
26+
27+
sub_image = Spittle::ImageData.new(:scanline_width => scanline_width,
28+
:pixel_width => @pixel_width,
29+
:data => [])
30+
sub_height.times do |line_idx|
31+
row = @raw.slice!(0, sub_width)
32+
l = @decoder.decode(line_idx, row, sub_image, @pixel_width)
33+
sub_image << l
34+
end
35+
sub_image
36+
37+
#FUck!
38+
idxes = [0, (1..scanline_width - 1).to_a.map{ |i| (i * 8)}.map{|i| i - 1}].flatten
39+
rows = [0, (1..sub_height - 1).to_a.map{ |i| i * 8 }.map{|i| i - 1}].flatten
40+
41+
input = sub_image.to_a
42+
rows.each do |row|
43+
idxes.each do |idx|
44+
pixel = input.take(@pixel_width)
45+
@out.set_pixel(row, idx, pixel)
46+
end
47+
end
48+
end
49+
50+
def pass2
51+
sub_width = ((@width / 8) * @pixel_width) + 1
52+
sub_height = pass1_height
53+
scanline_width = @width / 8
54+
55+
sub_image = Spittle::ImageData.new(:scanline_width => scanline_width,
56+
:pixel_width => @pixel_width,
57+
:data => [])
58+
offset = 0
59+
sub_height.times do |line_idx|
60+
row = @raw.slice!(0, sub_width)
61+
sub_image << @decoder.decode(line_idx, row, sub_image, @pixel_width)
62+
end
63+
sub_image
64+
65+
#FUck!
66+
idxes = (0..scanline_width - 1).to_a.map{ |i| (i * 8) + 4}.map{|i| i - 1}
67+
rows = [0, (1..sub_height - 1).to_a.map{ |i| i * 8 }.map{|i| i - 1}].flatten
68+
69+
input = sub_image.to_a
70+
rows.each do |row|
71+
idxes.each do |idx|
72+
pixel = input.take(@pixel_width)
73+
@out.set_pixel(row, idx, pixel)
74+
end
75+
end
76+
end
77+
end

lib/spittle/image_data.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,26 @@ def scanline_width; @properties[:scanline_width]; end
1313
def width; scanline_width / pixel_width; end
1414
def pixel_width; @properties[:pixel_width]; end
1515

16+
def to_a
17+
@data.flatten
18+
end
19+
20+
def set_pixel(row, idx, pixel)
21+
return unless pixel
22+
@data[row] = [] unless @data[row]
23+
row = @data[row]
24+
offset = idx * pixel_width
25+
26+
pixel.each_with_index do |v, pidx|
27+
row[offset + pidx] = v
28+
end
29+
end
30+
31+
def fetch_pixel(idx, row = 0)
32+
offset = idx * pixel_width
33+
@data[row].slice(offset, pixel_width)
34+
end
35+
1636
# need better checks, because currently compatible is
1737
# similar color type, or depth.. maybe it doesn't matter...
1838
def compatible?(image)
@@ -73,6 +93,10 @@ def last
7393
@data.last
7494
end
7595

96+
def rows
97+
size
98+
end
99+
76100
def <<(row)
77101
@data << row
78102
self

lib/spittle/png/image.rb

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -99,20 +99,23 @@ def to_image
9999
def inspect
100100
"#{@name} (#{height} x #{width}) [color type: #{color_type}, depth: #{depth}]"
101101
end
102-
private
102+
103103
# spike spikey spike
104104
def decode_interlaced_image( uncompressed )
105-
subimage1 = Spittle::ImageData.new(:scanline_width => sub_image_1_width - 1,
106-
:pixel_width => pixel_width,
107-
:data => Array.new(sub_image_1_height))
108-
offset = 0
109-
sub_image_1_height.times do |scanline|
110-
end_row = sub_image_1_width + offset
111-
row = uncompressed.slice(offset, sub_image_1_width)
112-
subimage1[scanline] = decode(scanline, row, subimage1, pixel_width)
113-
offset = end_row
114-
end
115-
subimage1
105+
#subimage1 = Spittle::ImageData.new(:scanline_width => sub_image_1_width - 1,
106+
#:pixel_width => pixel_width,
107+
#:data => Array.new(sub_image_1_height))
108+
#offset = 0
109+
#sub_image_1_height.times do |scanline|
110+
#end_row = sub_image_1_width + offset
111+
#row = uncompressed.slice(offset, sub_image_1_width)
112+
#subimage1[scanline] = decode(scanline, row, subimage1, pixel_width)
113+
#offset = end_row
114+
#end
115+
#subimage1
116+
117+
deinterlacer = DeInterlacer.new(height, width, pixel_width, uncompressed, self)
118+
deinterlacer.process
116119
end
117120

118121
def sub_image_1_width

spec/integration/interlace_spec.rb

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,53 @@
11
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
22

3+
4+
#Interlaced Image color breakdown
5+
#
6+
# pass 1: (1,1,1)
7+
# pass 2: (42,42,42)
8+
# pass 3: (103, 103, 0)
9+
# pass 4: (104, 0, 0)
10+
# pass 5: (0, 105, 0)
11+
# pass 6: (0, 0, 106)
12+
# pass 6: (207, 207)
13+
314
describe "reading interlaced images" do
4-
before :all do
5-
@image_dir = File.join( File.dirname( __FILE__ ), "interlaced_images" )
6-
end
7-
8-
before :each do
9-
@image = PNG::Image.open( @image_dir + "/interlaced-16x16-first-image.png" )
15+
before do
16+
begin
17+
@image_dir = File.join( File.dirname( __FILE__ ), "interlaced_images" )
18+
@image = PNG::Image.open( @image_dir + "/interlaced-16x16.png" )
19+
@result = @image.to_image
20+
rescue Exception => e
21+
puts e.backtrace.join("\n")
22+
raise e
23+
end
1024
end
11-
25+
1226
describe "sanity checks" do
1327
it "knows the images width" do
1428
@image.width.should == 16
1529
end
16-
30+
1731
it "knows the images height" do
1832
@image.height.should == 16
1933
end
2034
end
21-
22-
it "can read the first sub-image from an interlaced image" do
23-
# should be the only colored pixels in the image.. with four different colors
24-
@image.to_image.should == [[255, 127, 63, 63, 127, 255], [33, 66, 99, 192, 255, 127]]
35+
36+
describe "pass extraction" do
37+
it "can read the first sub-image from an interlaced image" do
38+
@result.fetch_pixel(0).should == [1,1,1]
39+
@result.fetch_pixel(7).should == [1,1,1]
40+
41+
@result.fetch_pixel(0, 7).should == [1,1,1]
42+
@result.fetch_pixel(7, 7).should == [1,1,1]
43+
end
44+
45+
it "can read the second sub-image" do
46+
@result.fetch_pixel(3).should == [42,42,42]
47+
@result.fetch_pixel(11).should == [42,42,42]
48+
49+
@result.fetch_pixel(3, 7).should == [42,42,42]
50+
@result.fetch_pixel(11, 7).should == [42,42,42]
51+
end
2552
end
26-
end
53+
end
Loading

spec/lib/image_data_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,13 @@
4646
it "will return the last scanline given a current index" do
4747
@id.last_scanline(1).should == [1,2,3]
4848
end
49+
50+
describe "fetch_pixel" do
51+
it "can fetch a given pixel" do
52+
@id.fetch_pixel(0).should == [1,2,3]
53+
end
54+
it "can fetch across all rows" do
55+
@id.fetch_pixel(0, 1).should == [4,5,6]
56+
end
57+
end
4958
end

0 commit comments

Comments
 (0)