Class: Cabriolet::Compressors::Quantum

Inherits:
Base
  • Object
show all
Includes:
QuantumShared
Defined in:
lib/cabriolet/compressors/quantum.rb

Overview

Quantum compresses data using arithmetic coding and LZ77-based matching Based on the Quantum decompressor and libmspack qtmd.c implementation

The Quantum method was created by David Stafford, adapted by Microsoft Corporation.

NOTE: This compressor is a work-in-progress. The arithmetic coding implementation needs refinement to match the decoder exactly. For now, this implementation focuses on correct structure. rubocop:disable Metrics/ClassLength

Constant Summary

Constants included from QuantumShared

QuantumShared::EXTRA_BITS, QuantumShared::FRAME_SIZE, QuantumShared::LENGTH_BASE, QuantumShared::LENGTH_EXTRA, QuantumShared::MAX_MATCH, QuantumShared::MIN_MATCH, QuantumShared::POSITION_BASE

Instance Attribute Summary collapse

Attributes inherited from Base

#buffer_size, #input, #io_system, #output

Instance Method Summary collapse

Methods included from QuantumShared

find_length_slot, find_position_slot

Constructor Details

#initialize(io_system, input, output, buffer_size, window_bits: 10, **_kwargs) ⇒ Quantum

Initialize Quantum compressor

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cabriolet/compressors/quantum.rb', line 29

def initialize(io_system, input, output, buffer_size, window_bits: 10,
**_kwargs)
  super(io_system, input, output, buffer_size)

  # Validate window_bits
  unless (10..21).cover?(window_bits)
    raise ArgumentError,
          "Quantum window_bits must be 10-21, got #{window_bits}"
  end

  @window_bits = window_bits
  @window_size = 1 << window_bits

  # Initialize bitstream for MSB-first writing
  @bitstream = Binary::BitstreamWriter.new(io_system, output,
                                           buffer_size, msb_first: true)

  # Initialize models
  initialize_models
end

Instance Attribute Details

#window_bitsObject (readonly)

Returns the value of attribute window_bits.



20
21
22
# File 'lib/cabriolet/compressors/quantum.rb', line 20

def window_bits
  @window_bits
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



20
21
22
# File 'lib/cabriolet/compressors/quantum.rb', line 20

def window_size
  @window_size
end

Instance Method Details

#compressInteger

Compress the input data

Returns:

  • (Integer)

    Total bytes compressed



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/cabriolet/compressors/quantum.rb', line 53

def compress
  total_bytes = 0

  loop do
    # Read frame data
    frame_data = io_system.read(input, FRAME_SIZE)
    break if frame_data.empty?

    total_bytes += frame_data.bytesize

    # Compress frame
    compress_frame(frame_data)

    # Write trailer (0xFF marker)
    @bitstream.flush_msb
    @bitstream.write_byte(0xFF)

    # Reset models for next frame
    initialize_models

    break if frame_data.bytesize < FRAME_SIZE
  end

  total_bytes
end