Class: PureJPEG::Huffman::Encoder
- Inherits:
-
Object
- Object
- PureJPEG::Huffman::Encoder
- Defined in:
- lib/pure_jpeg/huffman/encoder.rb
Class Method Summary collapse
-
.category(value) ⇒ Object
Return the Huffman category (bit length) for a value.
- .each_ac_item(zigzag) {|0x00, 0| ... } ⇒ Object
- .each_ac_symbol(zigzag) ⇒ Object
-
.value_bits(value, cat) ⇒ Object
Return the extra bits to encode for a value with the given category.
Instance Method Summary collapse
-
#encode_block(zigzag, prev_dc, writer) ⇒ Object
Encode a single 8x8 block (in zigzag order, quantized).
-
#initialize(dc_table, ac_table) ⇒ Encoder
constructor
A new instance of Encoder.
Constructor Details
#initialize(dc_table, ac_table) ⇒ Encoder
Returns a new instance of Encoder.
61 62 63 64 |
# File 'lib/pure_jpeg/huffman/encoder.rb', line 61 def initialize(dc_table, ac_table) @dc_table = dc_table @ac_table = ac_table end |
Class Method Details
.category(value) ⇒ Object
Return the Huffman category (bit length) for a value. Avoids Array allocation compared to the combined category_and_bits.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/pure_jpeg/huffman/encoder.rb', line 8 def self.category(value) return 0 if value == 0 v = value.abs cat = 0 while v > 0 cat += 1 v >>= 1 end cat end |
.each_ac_item(zigzag) {|0x00, 0| ... } ⇒ Object
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 |
# File 'lib/pure_jpeg/huffman/encoder.rb', line 24 def self.each_ac_item(zigzag) last_nonzero = 63 last_nonzero -= 1 while last_nonzero > 0 && zigzag[last_nonzero] == 0 if last_nonzero == 0 yield 0x00, 0 return end i = 1 while i <= last_nonzero run = 0 while i <= last_nonzero && zigzag[i] == 0 run += 1 i += 1 end while run >= 16 yield 0xF0, 0 run -= 16 end value = zigzag[i] cat = category(value) yield (run << 4) | cat, value i += 1 end yield 0x00, 0 if last_nonzero < 63 end |
.each_ac_symbol(zigzag) ⇒ Object
55 56 57 58 59 |
# File 'lib/pure_jpeg/huffman/encoder.rb', line 55 def self.each_ac_symbol(zigzag) each_ac_item(zigzag) do |symbol, _value| yield symbol end end |
.value_bits(value, cat) ⇒ Object
Return the extra bits to encode for a value with the given category.
20 21 22 |
# File 'lib/pure_jpeg/huffman/encoder.rb', line 20 def self.value_bits(value, cat) value > 0 ? value : value + (1 << cat) - 1 end |
Instance Method Details
#encode_block(zigzag, prev_dc, writer) ⇒ Object
Encode a single 8x8 block (in zigzag order, quantized). ‘prev_dc` is the DC value of the previous block (for DPCM). Writes encoded bits to `writer` (a BitWriter). Returns the current block’s DC value.
70 71 72 73 74 75 76 |
# File 'lib/pure_jpeg/huffman/encoder.rb', line 70 def encode_block(zigzag, prev_dc, writer) dc = zigzag[0] diff = dc - prev_dc encode_dc(diff, writer) encode_ac(zigzag, writer) dc end |