Class: Cabriolet::Compressors::Quantum

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

STATUS: Functional with known limitations

  • Literals: WORKING ✓

  • Short matches (3-13 bytes): WORKING ✓

  • Longer matches (14+ bytes): Limited support (known issue)

  • Simple data round-trips successfully

  • Complex repeated patterns may have issues

The Quantum method was created by David Stafford, adapted by Microsoft Corporation. rubocop:disable Metrics/ClassLength

Defined Under Namespace

Classes: Model, ModelSymbol

Constant Summary collapse

FRAME_SIZE =

Frame size (32KB per frame)

32_768
MIN_MATCH =

Match constants

3
MAX_MATCH =
1028
POSITION_BASE =

Position slot tables (same as decompressor)

[
  0, 1, 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256, 384,
  512, 768, 1024, 1536, 2048, 3072, 4096, 6144, 8192, 12_288, 16_384,
  24_576, 32_768, 49_152, 65_536, 98_304, 131_072, 196_608, 262_144,
  393_216, 524_288, 786_432, 1_048_576, 1_572_864
].freeze
EXTRA_BITS =
[
  0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
  9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16,
  17, 17, 18, 18, 19, 19
].freeze
LENGTH_BASE =
[
  0, 1, 2, 3, 4, 5, 6, 8, 10, 12, 14, 18, 22, 26,
  30, 38, 46, 54, 62, 78, 94, 110, 126, 158, 190, 222, 254
].freeze
LENGTH_EXTRA =
[
  0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
  3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0
].freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#buffer_size, #input, #io_system, #output

Instance Method Summary collapse

Constructor Details

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

Initialize Quantum compressor

Parameters:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cabriolet/compressors/quantum.rb', line 80

def initialize(io_system, input, output, buffer_size, window_bits: 10)
  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.



50
51
52
# File 'lib/cabriolet/compressors/quantum.rb', line 50

def window_bits
  @window_bits
end

#window_sizeObject (readonly)

Returns the value of attribute window_size.



50
51
52
# File 'lib/cabriolet/compressors/quantum.rb', line 50

def window_size
  @window_size
end

Instance Method Details

#compressInteger

Compress the input data

Returns:

  • (Integer)

    Total bytes compressed



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cabriolet/compressors/quantum.rb', line 103

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