Class: ZPNG::Image
- Inherits:
-
Object
- Object
- ZPNG::Image
- Defined in:
- lib/zpng/image.rb
Constant Summary collapse
- PNG_HDR =
"\x89PNG\x0d\x0a\x1a\x0a"
Instance Attribute Summary collapse
-
#chunks ⇒ Object
Returns the value of attribute chunks.
-
#data ⇒ Object
Returns the value of attribute data.
-
#header ⇒ Object
(also: #hdr)
Returns the value of attribute header.
-
#imagedata ⇒ Object
Returns the value of attribute imagedata.
-
#palette ⇒ Object
Returns the value of attribute palette.
-
#scanlines ⇒ Object
Returns the value of attribute scanlines.
Instance Method Summary collapse
- #[](x, y) ⇒ Object
- #[]=(x, y, newpixel) ⇒ Object
- #_decode_all_scanlines ⇒ Object
- #dump ⇒ Object
- #each_block(bw, bh, &block) ⇒ Object
- #export ⇒ Object
- #extract_block(x, y = nil, w = nil, h = nil) ⇒ Object
- #height ⇒ Object
-
#initialize(x = nil) ⇒ Image
constructor
A new instance of Image.
- #to_s(h = {}) ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(x = nil) ⇒ Image
Returns a new instance of Image.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/zpng/image.rb', line 8 def initialize x = nil if x if x[PNG_HDR] # raw image data @data = x else # filename @data = File.binread(x) end end d = data[0,PNG_HDR.size] if d != PNG_HDR puts "[!] first #{PNG_HDR.size} bytes must be #{PNG_HDR.inspect}, but got #{d.inspect}".red end io = StringIO.new(data) io.seek PNG_HDR.size @chunks = [] while !io.eof? chunk = Chunk.from_stream(io) @chunks << chunk case chunk when Chunk::IHDR @header = chunk when Chunk::PLTE @palette = chunk when Chunk::IEND break end end unless io.eof? offset = io.tell extradata = io.read puts "[?] #{extradata.size} bytes of extra data after image end (IEND), offset = 0x#{offset.to_s(16)}".red end end |
Instance Attribute Details
#chunks ⇒ Object
Returns the value of attribute chunks.
3 4 5 |
# File 'lib/zpng/image.rb', line 3 def chunks @chunks end |
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/zpng/image.rb', line 3 def data @data end |
#header ⇒ Object Also known as: hdr
Returns the value of attribute header.
3 4 5 |
# File 'lib/zpng/image.rb', line 3 def header @header end |
#imagedata ⇒ Object
Returns the value of attribute imagedata.
3 4 5 |
# File 'lib/zpng/image.rb', line 3 def imagedata @imagedata end |
#palette ⇒ Object
Returns the value of attribute palette.
3 4 5 |
# File 'lib/zpng/image.rb', line 3 def palette @palette end |
#scanlines ⇒ Object
Returns the value of attribute scanlines.
3 4 5 |
# File 'lib/zpng/image.rb', line 3 def scanlines @scanlines end |
Instance Method Details
#[](x, y) ⇒ Object
69 70 71 |
# File 'lib/zpng/image.rb', line 69 def [] x, y scanlines[y][x] end |
#[]=(x, y, newpixel) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/zpng/image.rb', line 73 def []= x, y, newpixel # we must decode all scanlines before doing any modifications # or scanlines decoded AFTER modification of UPPER ones will be decoded wrong _decode_all_scanlines unless @_all_scanlines_decoded scanlines[y][x] = newpixel end |
#_decode_all_scanlines ⇒ Object
80 81 82 83 |
# File 'lib/zpng/image.rb', line 80 def _decode_all_scanlines scanlines.each(&:decode!) @_all_scanlines_decoded = true end |
#dump ⇒ Object
46 47 48 49 50 |
# File 'lib/zpng/image.rb', line 46 def dump @chunks.each do |chunk| puts "[.] #{chunk.inspect} #{chunk.crc_ok? ? 'CRC OK'.green : 'CRC ERROR'.red}" end end |
#each_block(bw, bh, &block) ⇒ Object
108 109 110 111 112 113 114 115 |
# File 'lib/zpng/image.rb', line 108 def each_block bw,bh, &block 0.upto(height/bh-1) do |by| 0.upto(width/bw-1) do |bx| b = extract_block(bx*bw, by*bh, bw, bh) yield b end end end |
#export ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/zpng/image.rb', line 117 def export imagedata # fill @imagedata, if not already filled # delete redundant IDAT chunks first_idat = @chunks.find{ |c| c.type == 'IDAT' } @chunks.delete_if{ |c| c.type == 'IDAT' && c != first_idat } # fill first_idat @data with compressed imagedata first_idat.data = Zlib::Deflate.deflate(scanlines.map(&:export).join, 9) PNG_HDR + @chunks.map(&:export).join end |
#extract_block(x, y = nil, w = nil, h = nil) ⇒ Object
100 101 102 103 104 105 106 |
# File 'lib/zpng/image.rb', line 100 def extract_block x,y=nil,w=nil,h=nil if x.is_a?(Hash) Block.new(self,x[:x], x[:y], x[:width], x[:height]) else Block.new(self,x,y,w,h) end end |
#height ⇒ Object
56 57 58 |
# File 'lib/zpng/image.rb', line 56 def height @header && @header.height end |
#to_s(h = {}) ⇒ Object
96 97 98 |
# File 'lib/zpng/image.rb', line 96 def to_s h={} scanlines.map{ |l| l.to_s(h) }.join("\n") end |
#width ⇒ Object
52 53 54 |
# File 'lib/zpng/image.rb', line 52 def width @header && @header.width end |