Class: Omnizip::Filters::Delta
- Inherits:
-
Omnizip::Filter
- Object
- Omnizip::Filter
- Omnizip::Filters::Delta
- Defined in:
- lib/omnizip/filters/delta.rb
Overview
Delta filter for multimedia and database preprocessing.
This filter computes byte-wise differences between adjacent bytes at a specified distance. It is particularly effective for multimedia files (WAV, BMP) and database dumps where adjacent bytes often have small differences.
The filter uses wrap-around arithmetic (modulo 256) and is fully reversible.
Filter IDs by format:
- XZ: 0x03
- 7-Zip: 0x03
Constant Summary collapse
- DEFAULT_DISTANCE =
Default distance for delta calculation (audio/single channel)
1- MAX_DISTANCE =
Maximum allowed distance value
256- BYTE_MODULO =
Byte modulo for wrap-around arithmetic
256- XZ_FILTER_ID =
Filter ID for XZ format
0x03- SEVEN_ZIP_FILTER_ID =
Filter ID for 7-Zip format
0x03
Instance Attribute Summary collapse
-
#distance ⇒ Object
readonly
Returns the value of attribute distance.
Attributes inherited from Omnizip::Filter
Class Method Summary collapse
-
.metadata ⇒ Hash
Get metadata about this filter.
Instance Method Summary collapse
-
#decode(data, _position = 0) ⇒ String
Decode (postprocess) data by restoring from differences.
-
#encode(data, _position = 0) ⇒ String
Encode (preprocess) data by computing forward differences.
-
#id_for_format(format) ⇒ Integer
Get filter ID for specific format.
-
#initialize(distance_arg = DEFAULT_DISTANCE, distance: DEFAULT_DISTANCE) ⇒ Delta
constructor
Initialize Delta filter with specified distance.
Constructor Details
#initialize(distance_arg = DEFAULT_DISTANCE, distance: DEFAULT_DISTANCE) ⇒ Delta
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/omnizip/filters/delta.rb', line 64 def initialize(distance_arg = DEFAULT_DISTANCE, distance: DEFAULT_DISTANCE) # Support both positional and keyword argument styles # If called with Delta.new(3), distance_arg=3, distance=DEFAULT (keyword not used) # If called with Delta.new(distance: 3), distance_arg=DEFAULT, distance=3 dist = if distance == DEFAULT_DISTANCE distance_arg else distance end validate_distance(dist) @distance = dist super(architecture: :delta, name: "Delta") end |
Instance Attribute Details
#distance ⇒ Object (readonly)
Returns the value of attribute distance.
50 51 52 |
# File 'lib/omnizip/filters/delta.rb', line 50 def distance @distance end |
Class Method Details
.metadata ⇒ Hash
Get metadata about this filter.
161 162 163 164 165 166 167 168 |
# File 'lib/omnizip/filters/delta.rb', line 161 def { name: "Delta", description: "Byte-wise difference filter for multimedia " \ "and database preprocessing", typical_usage: "WAV audio, BMP images, database dumps", } end |
Instance Method Details
#decode(data, _position = 0) ⇒ String
Decode (postprocess) data by restoring from differences.
For each byte at position i >= distance:
old[i] = (new[i] + old[i - distance]) mod 256
Bytes before the distance remain unchanged (already original).
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/omnizip/filters/delta.rb', line 138 def decode(data, _position = 0) return data.dup if data.empty? source = data.b result = data.b size = data.bytesize # Process bytes starting from distance distance.upto(size - 1) do |i| diff = source.getbyte(i) previous = result.getbyte(i - distance) # Restore original value with wrap-around original = (diff + previous) & 0xFF result.setbyte(i, original) end result end |
#encode(data, _position = 0) ⇒ String
Encode (preprocess) data by computing forward differences.
For each byte at position i >= distance:
new[i] = (old[i] - old[i - distance]) mod 256
Bytes before the distance remain unchanged (no previous value).
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/omnizip/filters/delta.rb', line 108 def encode(data, _position = 0) return data.dup if data.empty? source = data.b result = data.b size = data.bytesize # Process bytes starting from distance distance.upto(size - 1) do |i| current = source.getbyte(i) previous = source.getbyte(i - distance) # Compute difference with wrap-around diff = (current - previous) & 0xFF result.setbyte(i, diff) end result end |
#id_for_format(format) ⇒ Integer
Get filter ID for specific format
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/omnizip/filters/delta.rb', line 85 def id_for_format(format) case format when :seven_zip SEVEN_ZIP_FILTER_ID when :xz XZ_FILTER_ID else raise ArgumentError, "Unknown format: #{format}. Supported: :seven_zip, :xz" end end |