text
stringlengths
0
234
Tweak : constant array (Bl_Code) of Positive_M32 :=
-- For the origin of the tweak function, see "za_work.xls", sheet "Deflate".
-- function f3 = 0.20 f1 [logarithmic] + 0.80 * identity
-- NB: all values are multiplied by 100 for accuracy.
(100, 255, 379, 490, 594, 694, 791, 885, 978, 1069, 1159, 1249, 1338, 1426, 1513, 1600);
-- Neutral is:
-- (100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200, 1300, 1400, 1500, 1600)
function L1_Tweaked (B1, B2 : Bl_Vector) return Natural_M32 is
S : Natural_M32 := 0;
begin
for I in B1'Range loop
S := S + abs (Tweak (B1 (I)) - Tweak (B2 (I)));
end loop;
return S;
end L1_Tweaked;
-- L2 or Euclidean distance
function L2_Distance_Square (B1, B2 : Bl_Vector) return Natural_M32 is
S : Natural_M32 := 0;
begin
for I in B1'Range loop
S := S + (B1 (I) - B2 (I))**2;
end loop;
return S;
end L2_Distance_Square;
-- L2, tweaked
function L2_Tweaked_Square (B1, B2 : Bl_Vector) return Natural_M32 is
S : Natural_M32 := 0;
begin
for I in B1'Range loop
S := S + (Tweak (B1 (I)) - Tweak (B2 (I)))**2;
end loop;
return S;
end L2_Tweaked_Square;
type Distance_Type is (L1, L1_Tweaked, L2, L2_Tweaked);
function Similar
(H1, H2 : Deflate_Huff_Descriptors;
Dist_Kind : Distance_Type;
Threshold : Natural) return Boolean
is
Dist : Natural_M32;
Thres : Natural_M32 := Natural_M32 (Threshold);
begin
case Dist_Kind is
when L1 =>
Dist := L1_Distance (Convert (H1), Convert (H2));
when L1_Tweaked =>
Thres := Thres * Tweak (1);
Dist := L1_Tweaked (Convert (H1), Convert (H2));
when L2 =>
Thres := Thres * Thres;
Dist := L2_Distance_Square (Convert (H1), Convert (H2));
when L2_Tweaked =>
Thres := (Thres * Thres) * (Tweak (1) * Tweak (1));
Dist := L2_Tweaked_Square (Convert (H1), Convert (H2));
end case;
return Dist < Thres;
end Similar;
-- Another original part in the Taillaule algorithm: the possibility of recycling
-- Huffman codes. It is possible only if previous block was not stored and if
-- the new block's used alphabets are included in the old block's used alphabets.
function Recyclable (H_Old, H_New : Deflate_Huff_Descriptors) return Boolean is
begin
for I in H_Old.Lit_Len'Range loop
if H_Old.Lit_Len (I).Bit_Length = 0 and H_New.Lit_Len (I).Bit_Length > 0 then
return False; -- Code used in new, but not in old
end if;
end loop;
for I in H_Old.Dis'Range loop
if H_Old.Dis (I).Bit_Length = 0 and H_New.Dis (I).Bit_Length > 0 then
return False; -- Code used in new, but not in old
end if;
end loop;
return True;
end Recyclable;
-- Phase (C): the Prepare_Huffman_codes procedure finds the Huffman code for each
-- value, given the bit length imposed as input.
procedure Prepare_Huffman_Codes (Hd : in out Huff_Descriptor) is
Max_Huffman_Bits : constant := 15;
Bl_Count, Next_Code : array (0 .. Max_Huffman_Bits) of Natural := (others => 0);
Code : Natural := 0;
Bl : Natural;
begin
-- Algorithm from RFC 1951, section 3.2.2.
-- Step 1)
for I in Hd'Range loop
Bl := Hd (I).Bit_Length;
Bl_Count (Bl) := Bl_Count (Bl) + 1; -- One more code to be defined with bit length bl
end loop;
-- Step 2)
for Bits in 1 .. Max_Huffman_Bits loop
Code := (Code + Bl_Count (Bits - 1)) * 2;
Next_Code (Bits) := Code; -- This will be the first code for bit length "bits"
end loop;