text stringlengths 0 234 |
|---|
if I /= Length then |
Sum := Sum + Counts (I); |
end if; |
end loop; |
end Tweak_For_Better_Rle; |
subtype Stats_Lit_Len_Type is Stats_Type (Alphabet_Lit_Len); |
subtype Stats_Dis_Type is Stats_Type (Alphabet_Dis); |
-- Phase (B) : we turn statistics into Huffman bit lengths |
function Build_Descriptors |
(Stats_Lit_Len : Stats_Lit_Len_Type; |
Stats_Dis : Stats_Dis_Type) return Deflate_Huff_Descriptors |
is |
Bl_For_Lit_Len : Bit_Length_Array_Lit_Len; |
Bl_For_Dis : Bit_Length_Array_Dis; |
procedure Llhcl_Lit_Len is new Length_Limited_Huffman_Code_Lengths |
(Alphabet_Lit_Len, |
Count_Type, |
Stats_Lit_Len_Type, |
Bit_Length_Array_Lit_Len, |
15); |
procedure Llhcl_Dis is new Length_Limited_Huffman_Code_Lengths |
(Alphabet_Dis, |
Count_Type, |
Stats_Dis_Type, |
Bit_Length_Array_Dis, |
15); |
Stats_Dis_Copy : Stats_Dis_Type := Stats_Dis; |
Used : Natural := 0; |
begin |
-- See "PatchDistanceCodesForBuggyDecoders" in Zopfli's deflate.c |
-- NB: here, we patch the occurrences and not the bit lengths, to avoid invalid codes. |
-- The decoding bug concerns Zlib v.<= 1.2.1, UnZip v.<= 6.0, WinZip v.10.0. |
for I in Stats_Dis_Copy'Range loop |
if Stats_Dis_Copy (I) /= 0 then |
Used := Used + 1; |
end if; |
end loop; |
if Used < 2 then |
if Used = 0 then -- No distance code used at all (data must be almost random) |
Stats_Dis_Copy (0) := 1; |
Stats_Dis_Copy (1) := 1; |
elsif Stats_Dis_Copy (0) = 0 then |
Stats_Dis_Copy (0) := 1; -- now code 0 and some other code have non-zero counts |
else |
Stats_Dis_Copy (1) := 1; -- now codes 0 and 1 have non-zero counts |
end if; |
end if; |
Llhcl_Lit_Len (Stats_Lit_Len, Bl_For_Lit_Len); -- Call the magic algorithm for setting |
Llhcl_Dis (Stats_Dis_Copy, Bl_For_Dis); -- up Huffman lengths of both trees |
return Build_Descriptors (Bl_For_Lit_Len, Bl_For_Dis); |
end Build_Descriptors; |
-- Here is one original part in the Taillaule algorithm: use of basic |
-- topology (L1, L2 distances) to check similarities between Huffman code sets. |
-- Bit length vector. Convention: 16 is unused bit length (close to the bit length for the |
-- rarest symbols, 15, and far from the bit length for the most frequent symbols, 1). |
-- Deflate uses 0 for unused. |
subtype Bl_Code is Integer_M32 range 1 .. 16; |
type Bl_Vector is array (1 .. 288 + 32) of Bl_Code; |
function Convert (H : Deflate_Huff_Descriptors) return Bl_Vector is |
Bv : Bl_Vector; |
J : Positive := 1; |
begin |
for I in H.Lit_Len'Range loop |
if H.Lit_Len (I).Bit_Length = 0 then |
Bv (J) := 16; |
else |
Bv (J) := Integer_M32 (H.Lit_Len (I).Bit_Length); |
end if; |
J := J + 1; |
end loop; |
for I in H.Dis'Range loop |
if H.Dis (I).Bit_Length = 0 then |
Bv (J) := 16; |
else |
Bv (J) := Integer_M32 (H.Dis (I).Bit_Length); |
end if; |
J := J + 1; |
end loop; |
return Bv; |
end Convert; |
-- L1 or Manhattan distance |
function L1_Distance (B1, B2 : Bl_Vector) return Natural_M32 is |
S : Natural_M32 := 0; |
begin |
for I in B1'Range loop |
S := S + abs (B1 (I) - B2 (I)); |
end loop; |
return S; |
end L1_Distance; |
-- L1, tweaked |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.