writeBlockHuff encodes a block of bytes as either Huffman encoded literals or uncompressed bytes if the results only gains very little from compression.
(eof bool, input []byte, sync bool)
| 985 | // Huffman encoded literals or uncompressed bytes if the |
| 986 | // results only gains very little from compression. |
| 987 | func (w *huffmanBitWriter) writeBlockHuff(eof bool, input []byte, sync bool) { |
| 988 | if w.err != nil { |
| 989 | return |
| 990 | } |
| 991 | |
| 992 | // Clear histogram |
| 993 | for i := range w.literalFreq[:] { |
| 994 | w.literalFreq[i] = 0 |
| 995 | } |
| 996 | if !w.lastHuffMan { |
| 997 | for i := range w.offsetFreq[:] { |
| 998 | w.offsetFreq[i] = 0 |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | const numLiterals = endBlockMarker + 1 |
| 1003 | const numOffsets = 1 |
| 1004 | |
| 1005 | // Add everything as literals |
| 1006 | // We have to estimate the header size. |
| 1007 | // Assume header is around 70 bytes: |
| 1008 | // https://stackoverflow.com/a/25454430 |
| 1009 | const guessHeaderSizeBits = 70 * 8 |
| 1010 | histogram(input, w.literalFreq[:numLiterals]) |
| 1011 | ssize, storable := w.storedSize(input) |
| 1012 | if storable && len(input) > 1024 { |
| 1013 | // Quick check for incompressible content. |
| 1014 | abs := float64(0) |
| 1015 | avg := float64(len(input)) / 256 |
| 1016 | max := float64(len(input) * 2) |
| 1017 | for _, v := range w.literalFreq[:256] { |
| 1018 | diff := float64(v) - avg |
| 1019 | abs += diff * diff |
| 1020 | if abs > max { |
| 1021 | break |
| 1022 | } |
| 1023 | } |
| 1024 | if abs < max { |
| 1025 | if debugDeflate { |
| 1026 | fmt.Println("stored", abs, "<", max) |
| 1027 | } |
| 1028 | // No chance we can compress this... |
| 1029 | w.writeStoredHeader(len(input), eof) |
| 1030 | w.writeBytes(input) |
| 1031 | return |
| 1032 | } |
| 1033 | } |
| 1034 | w.literalFreq[endBlockMarker] = 1 |
| 1035 | w.tmpLitEncoding.generate(w.literalFreq[:numLiterals], 15) |
| 1036 | estBits := w.tmpLitEncoding.canReuseBits(w.literalFreq[:numLiterals]) |
| 1037 | if estBits < math.MaxInt32 { |
| 1038 | estBits += w.lastHeader |
| 1039 | if w.lastHeader == 0 { |
| 1040 | estBits += guessHeaderSizeBits |
| 1041 | } |
| 1042 | estBits += estBits >> w.logNewTablePenalty |
| 1043 | } |
| 1044 |