opencv
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_OK14# if defined(UNALIGNED64_OK) && defined(HAVE_BUILTIN_CTZLL)15# define compare256_rle compare256_rle_unaligned_6416# elif defined(HAVE_BUILTIN_CTZ)17# define compare256_rle compare256_rle_unaligned_3218# else19# define compare256_rle compare256_rle_unaligned_1620# endif21#else22# define compare256_rle compare256_rle_c23#endif24
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*/
30Z_INTERNAL block_state deflate_rle(deflate_state *s, int flush) {31int bflush = 0; /* set if current block must be flushed */32unsigned char *scan; /* scan goes up to strend for length of run */33uint32_t match_len = 0;34
35for (;;) {36/* Make sure that we always have enough lookahead, except37* 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*/
40if (s->lookahead <= STD_MAX_MATCH) {41PREFIX(fill_window)(s);42if (s->lookahead <= STD_MAX_MATCH && flush == Z_NO_FLUSH)43return need_more;44if (s->lookahead == 0)45break; /* flush the current block */46}47
48/* See how many times the previous byte repeats */49if (s->lookahead >= STD_MIN_MATCH && s->strstart > 0) {50scan = s->window + s->strstart - 1;51if (scan[0] == scan[1] && scan[1] == scan[2]) {52match_len = compare256_rle(scan, scan+3)+2;53match_len = MIN(match_len, s->lookahead);54match_len = MIN(match_len, STD_MAX_MATCH);55}56Assert(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 */60if (match_len >= STD_MIN_MATCH) {61check_match(s, s->strstart, s->strstart - 1, match_len);62
63bflush = zng_tr_tally_dist(s, 1, match_len - STD_MIN_MATCH);64
65s->lookahead -= match_len;66s->strstart += match_len;67match_len = 0;68} else {69/* No match, output a literal byte */70bflush = zng_tr_tally_lit(s, s->window[s->strstart]);71s->lookahead--;72s->strstart++;73}74if (bflush)75FLUSH_BLOCK(s, 0);76}77s->insert = 0;78if (flush == Z_FINISH) {79FLUSH_BLOCK(s, 1);80return finish_done;81}82if (s->sym_next)83FLUSH_BLOCK(s, 0);84return block_done;85}
86