Class: PNG::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/spittle/png/image.rb

Constant Summary collapse

RGB =

color types

2
RGBA =
3

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ihdr, idat, name, options = {}) ⇒ Image

Returns a new instance of Image.



38
39
40
41
42
43
# File 'lib/spittle/png/image.rb', line 38

def initialize( ihdr, idat, name, options = {} )
  @ihdr = ihdr
  @idat = idat
  @name = name
  @rows = options[:rows]
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



45
46
47
# File 'lib/spittle/png/image.rb', line 45

def name
  @name
end

Class Method Details

.color_type_of(pixel_width) ⇒ Object

TODO - Nieve We should only store RBGA



29
30
31
32
33
34
35
36
# File 'lib/spittle/png/image.rb', line 29

def self.color_type_of(pixel_width)
  case pixel_width
  when 3
    RGB
  when 4
    RGBA
  end
end

.default_filter_typeObject



4
5
6
# File 'lib/spittle/png/image.rb', line 4

def self.default_filter_type
  4 # paeth
end

.image_data(file_name) ⇒ Object

TODO - rename this ‘image_data’



18
19
20
21
# File 'lib/spittle/png/image.rb', line 18

def self.image_data( file_name )
  png = open(file_name)
  png.to_image
end

.open(file_name) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/spittle/png/image.rb', line 8

def self.open( file_name )
  name = File.basename( file_name, ".png" )

  File.open(file_name, "r") do |f|
    ihdr, idat = Parser.go!( f )
    Image.new( ihdr, idat, name )
  end
end

.write(file_name, data, options = {}) ⇒ Object



23
24
25
26
# File 'lib/spittle/png/image.rb', line 23

def self.write( file_name, data, options = {} )
  ihdr = PNG::IHDR.new(data.width, data.height, 8, color_type_of(data.pixel_width))
  Image.new(ihdr, nil, file_name, :rows => data).write( file_name, options)
end

Instance Method Details

#color_typeObject



49
# File 'lib/spittle/png/image.rb', line 49

def color_type; @ihdr.color_type end

#depthObject



48
# File 'lib/spittle/png/image.rb', line 48

def depth; @ihdr.depth end

#filter_encoded_rows(filter_type) ⇒ Object



82
83
84
85
86
87
88
89
# File 'lib/spittle/png/image.rb', line 82

def filter_encoded_rows(filter_type)
  out = Array.new(height)
  rows.each_with_index do |row, scanline|
    last_row = rows.last_scanline(scanline)
    out[scanline] = encode_row( row, last_row, filter_type, pixel_width)
  end
  out
end

#heightObject



47
# File 'lib/spittle/png/image.rb', line 47

def height; @ihdr.height end

#inspectObject



109
110
111
# File 'lib/spittle/png/image.rb', line 109

def inspect
  "#{@name} (#{height} x #{width}) [color type: #{color_type}, depth: #{depth}]"
end

#pixel_widthObject

check for RGB or RGBA



68
69
70
# File 'lib/spittle/png/image.rb', line 68

def pixel_width
  ( color_type == RGB ? 3 : 4)
end

#rowsObject



78
79
80
# File 'lib/spittle/png/image.rb', line 78

def rows
  @rows ||= to_image
end

#scanline_widthObject



73
74
75
76
# File 'lib/spittle/png/image.rb', line 73

def scanline_width
  # + 1 adds filter byte
  (width * pixel_width) + 1
end

#to_imageObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/spittle/png/image.rb', line 91

def to_image
  uncompressed = @idat.uncompressed

  #scanline_width - 1 because we're stripping the filter bit
  n_out = Spittle::ImageData.new(:scanline_width => scanline_width - 1,
                                 :pixel_width => pixel_width,
                                 :name => self.name,
                                 :data => Array.new(height))
  offset = 0
  height.times do |scanline|
    end_row = scanline_width + offset
    row = uncompressed.slice(offset, scanline_width)
    n_out[scanline] = decode(scanline, row, n_out, pixel_width)
    offset = end_row
  end
  n_out
end

#to_sObject



59
60
61
# File 'lib/spittle/png/image.rb', line 59

def to_s
  inspect
end

#uncompressedObject



50
# File 'lib/spittle/png/image.rb', line 50

def uncompressed; @idat.uncompressed end

#widthObject



46
# File 'lib/spittle/png/image.rb', line 46

def width; @ihdr.width end

#write(file_name, options = {}) ⇒ Object



52
53
54
55
56
57
# File 'lib/spittle/png/image.rb', line 52

def write(file_name, options={})
  filter_type = options[:filter_type] || Image.default_filter_type
  File.open(file_name, 'w') do |f|
    f.write(generate_png( filter_type ))
  end
end