Class: SwfRuby::Swf::BitsLossless2

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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_sizeObject

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

#formatObject

Returns the value of attribute format.



6
7
8
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6

def format
  @format
end

#heightObject

Returns the value of attribute height.



6
7
8
# File 'lib/swf_ruby/swf/bits_lossless2.rb', line 6

def height
  @height
end

#widthObject

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_dataObject

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