File: merge_split.c

package info (click to toggle)
libepsilon 0.9.2%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,192 kB
  • sloc: ansic: 11,329; sh: 9,321; perl: 936; xml: 86; makefile: 85
file content (197 lines) | stat: -rw-r--r-- 5,993 bytes parent folder | download | duplicates (6)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * $Id: merge_split.c,v 1.15 2010/02/05 23:50:22 simakov Exp $
 *
 * EPSILON - wavelet image compression library.
 * Copyright (C) 2006,2007,2010 Alexander Simakov, <xander@entropyware.info>
 *
 * This file is part of EPSILON
 *
 * EPSILON 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 3 of the License, or
 * (at your option) any later version.
 *
 * EPSILON 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 EPSILON.  If not, see <http://www.gnu.org/licenses/>.
 *
 * http://epsilon-project.sourceforge.net
 */

#include <common.h>
#include <merge_split.h>

void merge_channels(unsigned char *channel_A, unsigned char *channel_B,
                    unsigned char *channel_AB, int len_A, int len_B)
{
    int pkg_A, pkg_B; /* Real number of bytes in the package */
    int div_A, div_B; /* Base number of bytes in the package */
    int rem_A, rem_B; /* Instant reminder */
    int cur_A, cur_B; /* Current savings */
    int tr0_A, tr0_B; /* Lower threshold */
    int tr1_A, tr1_B; /* Upper threshold */
    int i, pkg, min;

    /* Sanity checks. I love them! */
    assert((len_A > 0) && (len_B > 0));

    /* Total number of packages */
    min = MIN(len_A, len_B);

    /* 1-st channel setup */
    div_A = len_A / min;
    cur_A = len_A;
    rem_A = 0;
    tr0_A = div_A * min;
    tr1_A = tr0_A + min;

    /* 2-nd channel setup */
    div_B = len_B / min;
    cur_B = len_B;
    rem_B = 0;
    tr0_B = div_B * min;
    tr1_B = tr0_B + min;

    /* Loop for all packages */
    for (pkg = 0; pkg < min; pkg++) {
        /* Update current savings */
        cur_A = len_A + rem_A;

        if (cur_A >= tr1_A) {
            /* Choose maximal package & update reminder */
            pkg_A = div_A + 1;
            rem_A = cur_A - tr1_A;
        } else {
            /* Choose minimal package & update reminder */
            pkg_A = div_A;
            rem_A = cur_A - tr0_A;
        }

        /* Update current savings */
        cur_B = len_B + rem_B;

        if (cur_B >= tr1_B) {
            /* Choose maximal package & update reminder */
            pkg_B = div_B + 1;
            rem_B = cur_B - tr1_B;
        } else {
            /* Choose minimal package & update reminder */
            pkg_B = div_B;
            rem_B = cur_B - tr0_B;
        }

        /* Put bytes from the 1-st channel */
        for (i = 0; i < pkg_A; i++) {
            *channel_AB++ = *channel_A++;
        }

        /* Put bytes from the 2-nd channel */
        for (i = 0; i < pkg_B; i++) {
            *channel_AB++ = *channel_B++;
        }
    }
}

void split_channels(unsigned char *channel_AB,
                    unsigned char *channel_A, unsigned char *channel_B,
                    int len_AB, int len_A, int len_B,
                    int *real_len_A, int *real_len_B)
{
    int pkg_A, pkg_B; /* Real number of bytes in the package */
    int div_A, div_B; /* Base number of bytes in the package */
    int rem_A, rem_B; /* Instant reminder */
    int cur_A, cur_B; /* Current savings */
    int tr0_A, tr0_B; /* Lower threshold */
    int tr1_A, tr1_B; /* Upper threshold */
    int i, pkg, min;

    unsigned char *end_AB;
    unsigned char *end_A;
    unsigned char *end_B;

    /* Sanity checks */
    assert((len_A > 0) && (len_B > 0) && (len_AB > 0));
    assert(real_len_A && real_len_B);
    assert(len_A + len_B >= len_AB);

    /* Set end-of-buffer pointers */
    end_AB = channel_AB + len_AB;
    end_A = channel_A + len_A;
    end_B = channel_B + len_B;

    /* Real amount of saved bytes */
    *real_len_A = *real_len_B = 0;

    /* Original number of packages (real stream may be truncated) */
    min = MIN(len_A, len_B);

    /* 1-st channel setup */
    div_A = len_A / min;
    cur_A = len_A;
    rem_A = 0;
    tr0_A = div_A * min;
    tr1_A = tr0_A + min;

    /* 2-nd channel setup */
    div_B = len_B / min;
    cur_B = len_B;
    rem_B = 0;
    tr0_B = div_B * min;
    tr1_B = tr0_B + min;

    /* Loop for all packages */
    for (pkg = 0; pkg < min; pkg++) {
        /* Update current savings */
        cur_A = len_A + rem_A;

        if (cur_A >= tr1_A) {
            /* Choose maximal package & update reminder */
            pkg_A = div_A + 1;
            rem_A = cur_A - tr1_A;
        } else {
            /* Choose minimal package & update reminder */
            pkg_A = div_A;
            rem_A = cur_A - tr0_A;
        }

        /* Update current savings */
        cur_B = len_B + rem_B;

        if (cur_B >= tr1_B) {
            /* Choose maximal package & update reminder */
            pkg_B = div_B + 1;
            rem_B = cur_B - tr1_B;
        } else {
            /* Choose minimal package & update reminder */
            pkg_B = div_B;
            rem_B = cur_B - tr0_B;
        }

        /* Extract pkg_A bytes from the stream into the channel_A */
        for (i = 0; (i < pkg_A) && (channel_A < end_A) && (channel_AB < end_AB); i++) {
            *channel_A++ = *channel_AB++;
            (*real_len_A)++;
        }

        /* Extract pkg_B bytes from the stream into the channel_B */
        for (i = 0; (i < pkg_B) && (channel_B < end_B) && (channel_AB < end_AB); i++) {
            *channel_B++ = *channel_AB++; 
            (*real_len_B)++;
        }

        /* All avaiable bytes are processed */
        if (channel_AB >= end_AB) {
            assert(*real_len_A + *real_len_B == len_AB);

            if ((pkg == min - 1) && (channel_A == end_A) && (channel_B == end_B)) {
                assert((*real_len_A == len_A) && (*real_len_B == len_B));
            }

            return;
        }
    }
}