Class: SwfRuby::Swf::BitsLossless2
- Inherits:
-
Object
- Object
- SwfRuby::Swf::BitsLossless2
- Defined in:
- lib/swf_ruby/swf/bits_lossless2.rb
Constant Summary collapse
- ShiftDepth =
Magick::QuantumDepth - 8
- MaxRGB =
2 ** Magick::QuantumDepth - 1
Instance Attribute Summary collapse
-
#color_table_size ⇒ Object
Returns the value of attribute color_table_size.
-
#format ⇒ Object
Returns the value of attribute format.
-
#height ⇒ Object
Returns the value of attribute height.
-
#width ⇒ Object
Returns the value of attribute width.
-
#zlib_bitmap_data ⇒ Object
Returns the value of attribute zlib_bitmap_data.
Instance Method Summary collapse
-
#initialize(image_bytearray) ⇒ BitsLossless2
constructor
A new instance of BitsLossless2.
Constructor Details
#initialize(image_bytearray) ⇒ BitsLossless2
Returns a new instance of BitsLossless2.
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 11 def initialize(image_bytearray) image = Magick::Image.from_blob(image_bytearray).first data = "" data.force_encoding("ASCII-8BIT") if data.respond_to? :force_encoding @format = nil colormap = [] # creating colormap to check number of colors image.get_pixels(0, 0, image.columns, image.rows).each_with_index do |pixel,i| break if colormap.length > 255 idx = colormap.index(pixel) if idx data << [idx].pack("C") else colormap << pixel data << [colormap.length-1].pack("C") end if (i+1) % image.columns == 0 # padding data += [0].pack("C") * (4-image.columns&3) end end # checking image format by size of colormap if colormap.length > 255 # format=5 # reset and re-build data_stream without colopmap data = "" image.get_pixels(0, 0, image.columns, image.rows).each_with_index do |pixel,i| opacity = (MaxRGB-pixel.opacity) >> ShiftDepth data += [opacity].pack("C") data += [pixel.red >> ShiftDepth].pack("C") data += [pixel.green >> ShiftDepth].pack("C") data += [pixel.blue >> ShiftDepth].pack("C") end @format = 5 else # format=3 # added colormap before data_stream data = colormap.inject("") { |r,c| opacity = (MaxRGB-c.opacity) >> ShiftDepth if opacity == 0 r += [0].pack("C") + [0].pack("C") + [0].pack("C") + [opacity].pack("C") else r += [c.red >> ShiftDepth].pack("C") + [c.green >> ShiftDepth].pack("C") + [c.blue >> ShiftDepth].pack("C") + [opacity].pack("C") end } + data @format = 3 @color_table_size = colormap.length-1 end @width = image.columns @height = image.rows @zlib_bitmap_data = Zlib::Deflate.deflate(data) end |
Instance Attribute Details
#color_table_size ⇒ Object
Returns the value of attribute color_table_size.
6 7 8 |
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6 def color_table_size @color_table_size end |
#format ⇒ Object
Returns the value of attribute format.
6 7 8 |
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6 def format @format end |
#height ⇒ Object
Returns the value of attribute height.
6 7 8 |
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6 def height @height end |
#width ⇒ Object
Returns the value of attribute width.
6 7 8 |
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6 def width @width end |
#zlib_bitmap_data ⇒ Object
Returns the value of attribute zlib_bitmap_data.
6 7 8 |
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6 def zlib_bitmap_data @zlib_bitmap_data end |