Class: PNG::Image

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

Constant Summary collapse

RGB =

color types

2
RGBA =
6

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.



48
49
50
51
52
53
# File 'lib/spittle/png/image.rb', line 48

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.



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

def name
  @name
end

Class Method Details

.default_filter_typeObject



32
33
34
# File 'lib/spittle/png/image.rb', line 32

def default_filter_type
  4 # paeth
end

.image_data(file_name, options = {}) ⇒ Object



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

def image_data( file_name, options={} )
  image_options = {:rgba => true}.merge( options )

  png = open(file_name)

  return png.to_image unless image_options[:rgba]
  png.to_image.to_rgba
end

.open(file_name) ⇒ Object



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

def 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



26
27
28
29
30
# File 'lib/spittle/png/image.rb', line 26

def 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



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

def color_type; @ihdr.color_type end

#depthObject



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

def depth; @ihdr.depth end

#filter_encoded_rows(filter_type) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/spittle/png/image.rb', line 74

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



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

def height; @ihdr.height end

#inspectObject



105
106
107
# File 'lib/spittle/png/image.rb', line 105

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

#pixel_widthObject

check for RGB or RGBA



70
71
72
# File 'lib/spittle/png/image.rb', line 70

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

#to_imageObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/spittle/png/image.rb', line 83

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



101
102
103
# File 'lib/spittle/png/image.rb', line 101

def to_s
  inspect
end

#uncompressedObject



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

def uncompressed; @idat.uncompressed end

#widthObject



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

def width; @ihdr.width end

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



62
63
64
65
66
67
# File 'lib/spittle/png/image.rb', line 62

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