summaryrefslogtreecommitdiffstats
path: root/server/glz_encode_match_tmpl.c
blob: 1fd251eed9113ab6ab73f1ee1bb59bfd28d210b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
   Copyright (C) 2009 Red Hat, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define SHORT_PIX_IMAGE_DIST_LEVEL_1 64 //(1 << 6)
#define SHORT_PIX_IMAGE_DIST_LEVEL_2 16384 // (1 << 14)
#define SHORT_PIX_IMAGE_DIST_LEVEL_3 4194304 // (1 << 22)
#define FAR_PIX_IMAGE_DIST_LEVEL_1 256 // (1 << 8)
#define FAR_PIX_IMAGE_DIST_LEVEL_2 65536 // (1 << 16)
#define FAR_PIX_IMAGE_DIST_LEVEL_3 16777216 // (1 << 24)

/* if image_distance = 0, pixel_distance is the distance between the matching pixels.
  Otherwise, it is the offset from the beginning of the referred image */
#if defined(GLZ_ENCODE_MATCH) /* actually performing the encoding */
static inline void encode_match(Encoder *encoder, uint32_t image_distance,
                                size_t pixel_distance, size_t len)
#elif defined(GLZ_ENCODE_SIZE) /* compute the size of the encoding except for the match length*/
static inline int get_encode_ref_size(uint32_t image_distance, size_t pixel_distance)
#endif
{
#if defined(GLZ_ENCODE_SIZE)
    int encode_size;
#endif

#if defined(GLZ_ENCODE_MATCH)
    /* encoding the match length + Long/Short dist bit +  12 LSB pixels of pixel_distance*/
    if (len < 7) {
        if (pixel_distance < MAX_PIXEL_SHORT_DISTANCE) {
            encode(encoder, (uint8_t)((len << 5) + (pixel_distance & 0x0f)));
        } else {
            encode(encoder, (uint8_t)((len << 5) + 16 + (pixel_distance & 0x0f)));
        }
        encode(encoder, (uint8_t)((pixel_distance >> 4) & 255));
    } else {
        if (pixel_distance < MAX_PIXEL_SHORT_DISTANCE) {
            encode(encoder, (uint8_t)((7 << 5) + (pixel_distance & 0x0f)));
        } else {
            encode(encoder, (uint8_t)((7 << 5) + 16 + (pixel_distance & 0x0f)));
        }
        for (len -= 7; len >= 255; len -= 255) {
            encode(encoder, 255);
        }
        encode(encoder, (uint8_t)len);
        encode(encoder, (uint8_t)((pixel_distance >> 4) & 255));
    }
#endif


    /* encoding the rest of the pixel distance and the image_dist and its 2 control bits */

    /* The first 2 MSB bits indicate how many more bytes should be read for image dist */
    if (pixel_distance < MAX_PIXEL_SHORT_DISTANCE) {
        if (image_distance < SHORT_PIX_IMAGE_DIST_LEVEL_1) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder, (uint8_t)(image_distance & 0x3f));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 3;
#endif
        } else if (image_distance < SHORT_PIX_IMAGE_DIST_LEVEL_2) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder, (uint8_t)((1 << 6) + (image_distance & 0x3f)));
            encode(encoder, (uint8_t)((image_distance >> 6) & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 4;
#endif
        } else if (image_distance < SHORT_PIX_IMAGE_DIST_LEVEL_3) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder, (uint8_t)((1 << 7) + (image_distance & 0x3f)));
            encode(encoder, (uint8_t)((image_distance >> 6) & 255));
            encode(encoder, (uint8_t)((image_distance >> 14) & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 5;
#endif
        } else {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder, (uint8_t)((1 << 7) + (1 << 6) + (image_distance & 0x3f)));
            encode(encoder, (uint8_t)((image_distance >> 6) & 255));
            encode(encoder, (uint8_t)((image_distance >> 14) & 255));
            encode(encoder, (uint8_t)((image_distance >> 22) & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 6;
#endif
        }
    } else {
        /* the third MSB bit indicates if the pixel_distance is medium/long*/
        uint8_t long_dist_control = (pixel_distance < MAX_PIXEL_MEDIUM_DISTANCE) ? 0 : 32;
        if (image_distance == 0) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder, (uint8_t)(long_dist_control + ((pixel_distance >> 12) & 31)));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 3;
#endif
        } else if (image_distance < FAR_PIX_IMAGE_DIST_LEVEL_1) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder,
                   (uint8_t)(long_dist_control + (1 << 6) + ((pixel_distance >> 12) & 31)));
            encode(encoder, (uint8_t)(image_distance & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 4;
#endif
        } else if (image_distance < FAR_PIX_IMAGE_DIST_LEVEL_2) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder,
                   (uint8_t)(long_dist_control + (1 << 7) + ((pixel_distance >> 12) & 31)));
            encode(encoder, (uint8_t)(image_distance & 255));
            encode(encoder, (uint8_t)((image_distance >> 8) & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 5;
#endif
        } else {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder,
                   (uint8_t)(long_dist_control + (1 << 7) + (1 << 6) +
                                                                    ((pixel_distance >> 12) & 31)));
            encode(encoder, (uint8_t)(image_distance & 255));
            encode(encoder, (uint8_t)((image_distance >> 8) & 255));
            encode(encoder, (uint8_t)((image_distance >> 16) & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size = 6;
#endif
        }

        if (long_dist_control) {
#if defined(GLZ_ENCODE_MATCH)
            encode(encoder, (uint8_t)((pixel_distance >> 17) & 255));
#elif defined(GLZ_ENCODE_SIZE)
            encode_size++;
#endif
        }
    }

#if defined(GLZ_ENCODE_SIZE)
    return encode_size;
#endif
}

#undef GLZ_ENCODE_SIZE
#undef GLZ_ENCODE_MATCH