Class: PNG::Image
- Inherits:
-
Object
- Object
- PNG::Image
- Defined in:
- lib/spittle/png/image.rb
Constant Summary collapse
- RGB =
color types
2
- RGBA =
3
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.color_type_of(pixel_width) ⇒ Object
TODO - Nieve We should only store RBGA.
- .default_filter_type ⇒ Object
-
.image_data(file_name) ⇒ Object
TODO - rename this ‘image_data’.
- .open(file_name) ⇒ Object
- .write(file_name, data, options = {}) ⇒ Object
Instance Method Summary collapse
- #color_type ⇒ Object
- #depth ⇒ Object
- #filter_encoded_rows(filter_type) ⇒ Object
- #height ⇒ Object
-
#initialize(ihdr, idat, name, options = {}) ⇒ Image
constructor
A new instance of Image.
- #inspect ⇒ Object
-
#pixel_width ⇒ Object
check for RGB or RGBA.
- #rows ⇒ Object
- #scanline_width ⇒ Object
- #to_image ⇒ Object
- #to_s ⇒ Object
- #uncompressed ⇒ Object
- #width ⇒ Object
- #write(file_name, options = {}) ⇒ Object
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, = {} ) @ihdr = ihdr @idat = idat @name = name @rows = [:rows] end |
Instance Attribute Details
#name ⇒ Object (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_type ⇒ Object
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 |
Instance Method Details
#color_type ⇒ Object
49 |
# File 'lib/spittle/png/image.rb', line 49 def color_type; @ihdr.color_type end |
#depth ⇒ Object
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 |
#height ⇒ Object
47 |
# File 'lib/spittle/png/image.rb', line 47 def height; @ihdr.height end |
#inspect ⇒ Object
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_width ⇒ Object
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 |
#rows ⇒ Object
78 79 80 |
# File 'lib/spittle/png/image.rb', line 78 def rows @rows ||= to_image end |
#scanline_width ⇒ Object
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_image ⇒ Object
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_s ⇒ Object
59 60 61 |
# File 'lib/spittle/png/image.rb', line 59 def to_s inspect end |
#uncompressed ⇒ Object
50 |
# File 'lib/spittle/png/image.rb', line 50 def uncompressed; @idat.uncompressed end |
#width ⇒ Object
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, ={}) filter_type = [:filter_type] || Image.default_filter_type File.open(file_name, 'w') do |f| f.write(generate_png( filter_type )) end end |