opencv

Форк
0
/
deflate_rle.c 
85 строк · 3.0 Кб
1
/* deflate_rle.c -- compress data using RLE strategy of deflation algorithm
2
 *
3
 * Copyright (C) 1995-2013 Jean-loup Gailly and Mark Adler
4
 * For conditions of distribution and use, see copyright notice in zlib.h
5
 */
6

7
#include "zbuild.h"
8
#include "compare256_rle.h"
9
#include "deflate.h"
10
#include "deflate_p.h"
11
#include "functable.h"
12

13
#ifdef UNALIGNED_OK
14
#  if defined(UNALIGNED64_OK) && defined(HAVE_BUILTIN_CTZLL)
15
#    define compare256_rle compare256_rle_unaligned_64
16
#  elif defined(HAVE_BUILTIN_CTZ)
17
#    define compare256_rle compare256_rle_unaligned_32
18
#  else
19
#    define compare256_rle compare256_rle_unaligned_16
20
#  endif
21
#else
22
#  define compare256_rle compare256_rle_c
23
#endif
24

25
/* ===========================================================================
26
 * For Z_RLE, simply look for runs of bytes, generate matches only of distance
27
 * one.  Do not maintain a hash table.  (It will be regenerated if this run of
28
 * deflate switches away from Z_RLE.)
29
 */
30
Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {
31
    int bflush = 0;                 /* set if current block must be flushed */
32
    unsigned char *scan;            /* scan goes up to strend for length of run */
33
    uint32_t match_len = 0;
34

35
    for (;;) {
36
        /* Make sure that we always have enough lookahead, except
37
         * at the end of the input file. We need STD_MAX_MATCH bytes
38
         * for the longest run, plus one for the unrolled loop.
39
         */
40
        if (s->lookahead <= STD_MAX_MATCH) {
41
            PREFIX(fill_window)(s);
42
            if (s->lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH)
43
                return need_more;
44
            if (s->lookahead == 0)
45
                break; /* flush the current block */
46
        }
47

48
        /* See how many times the previous byte repeats */
49
        if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {
50
            scan = s->window + s->strstart - 1;
51
            if (scan[0] == scan[1] && scan[1] == scan[2]) {
52
                match_len = compare256_rle(scan, scan+3)+2;
53
                match_len = MIN(match_len, s->lookahead);
54
                match_len = MIN(match_len, STD_MAX_MATCH);
55
            }
56
            Assert(scan+match_len <= s->window + s->window_size - 1, "wild scan");
57
        }
58

59
        /* Emit match if have run of STD_MIN_MATCH or longer, else emit literal */
60
        if (match_len >= STD_MIN_MATCH) {
61
            check_match(s, s->strstart, s->strstart - 1, match_len);
62

63
            bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);
64

65
            s->lookahead -= match_len;
66
            s->strstart += match_len;
67
            match_len = 0;
68
        } else {
69
            /* No match, output a literal byte */
70
            bflush = zng_tr_tally_lit(s, s->window[s->strstart]);
71
            s->lookahead--;
72
            s->strstart++;
73
        }
74
        if (bflush)
75
            FLUSH_BLOCK(s, 0);
76
    }
77
    s->insert = 0;
78
    if (flush == Z_FINISH) {
79
        FLUSH_BLOCK(s, 1);
80
        return finish_done;
81
    }
82
    if (s->sym_next)
83
        FLUSH_BLOCK(s, 0);
84
    return block_done;
85
}
86

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.