Class: KoreFileFormat::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/kore_fileformat/compressor.rb

Instance Method Summary collapse

Constructor Details

#initialize(level = :balanced) ⇒ Compressor

Returns a new instance of Compressor.



5
6
7
# File 'lib/kore_fileformat/compressor.rb', line 5

def initialize(level = :balanced)
  @level = parse_level(level)
end

Instance Method Details

#compress(data) ⇒ Object

Raises:

  • (ArgumentError)


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/kore_fileformat/compressor.rb', line 9

def compress(data)
  raise ArgumentError, "data cannot be nil" if data.nil?

  data_bytes = data.is_a?(String) ? data.bytes : data
  input = FFI::MemoryPointer.new(:uchar, data_bytes.length)
  input.put_array_of_uchar(0, data_bytes)

  output_size = (data_bytes.length * 1.5).to_i + 1024
  output = FFI::MemoryPointer.new(:uchar, output_size)
  compressed_size = FFI::MemoryPointer.new(:int)

  result = Native.compress_data(
    input, data_bytes.length,
    output, output_size,
    compressed_size,
    @level
  )

  raise CompressionError, "Compression failed with code: #{result}" if result != 0

  output.get_array_of_uchar(0, compressed_size.read_int).pack("c*")
end