summaryrefslogtreecommitdiff
path: root/src/compression/mtf.c
blob: bfb0e19f5ba514be89cd02e725701c532524b87f (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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/* This code is part of the tng compression routines.
 *
 * Written by Daniel Spangberg and Magnus Lundborg
 * Copyright (c) 2010, 2013-2014 The GROMACS development team.
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the Revised BSD License.
 */


#include <stdlib.h>
#include <string.h>
#include "../../include/compression/warnmalloc.h"
#include "../../include/compression/mtf.h"

/* "Partial" MTF. Byte based. */
/* Move to front coding.
   Acceptable inputs are max 8 bits (0-0xFF) */
static void comp_conv_to_mtf_byte(unsigned char *vals, int nvals,
                                  unsigned char *valsmtf)
{
  int i;
  /* Indices into a linked list */
  int list[256];
  int dict[256];
  /* Head of the linked list */
  int head;
  for (i=0; i<256; i++)
    dict[i]=i;
  for (i=0; i<255; i++)
    list[i]=i+1;
  list[255]=-1; /* end. */
  head=0;
  for (i=0; i<nvals; i++)
    {
      int v=(int)vals[i];
      /* Find how early in the dict the value is */
      int ptr=head;
      int oldptr=-1;
      int r=0;
      while (dict[ptr]!=v)
        {
          oldptr=ptr;
          ptr=list[ptr];
          r++;
        }
      valsmtf[i]=(unsigned char)r;
      /* Move it to front in list */
      /* Is it the head? Then it is already at the front. */
      if (oldptr!=-1)
        {
          /* Remove it from inside the list */
          list[oldptr]=list[ptr];
          /* Move it to the front. */
          list[ptr]=head;
          head=ptr;
        }
    }
}

void Ptngc_comp_conv_to_mtf_partial(unsigned int *vals, int nvals,
                              unsigned int *valsmtf)
{
  unsigned char *tmp=warnmalloc(nvals*2);
  int i, j;

  memset(valsmtf, 0U, sizeof(unsigned int) * nvals);

  for (j=0; j<3; j++)
    {
      for (i=0; i<nvals; i++)
        tmp[i]=(unsigned char)((vals[i]>>(8*j))&0xFF);
      comp_conv_to_mtf_byte(tmp,nvals,tmp+nvals);
      for (i=0; i<nvals; i++)
        valsmtf[i]|=(((unsigned int)(tmp[nvals+i]))<<(8*j));
    }
  free(tmp);
}

void Ptngc_comp_conv_to_mtf_partial3(unsigned int *vals, int nvals,
                               unsigned char *valsmtf)
{
  unsigned char *tmp=warnmalloc(nvals);
  int i, j;
  for (j=0; j<3; j++)
    {
      for (i=0; i<nvals; i++)
        tmp[i]=(unsigned char)((vals[i]>>(8*j))&0xFF);
      comp_conv_to_mtf_byte(tmp,nvals,valsmtf+j*nvals);
    }
  free(tmp);
}

/* Move to front decoding */
static void comp_conv_from_mtf_byte(unsigned char *valsmtf, int nvals,
                             unsigned char *vals)
{
  int i;
  /* Indices into a linked list */
  int list[256];
  int dict[256];
  /* Head of the linked list */
  int head;
  for (i=0; i<256; i++)
    dict[i]=i;
  for (i=0; i<255; i++)
    list[i]=i+1;
  list[255]=-1; /* end. */
  head=0;
  for (i=0; i<nvals; i++)
    {
      int r=(int)valsmtf[i];
      /* Find value at position r in the list */
      int ptr=head;
      int oldptr=-1;
      int cnt=0;
      while (cnt<r)
        {
          oldptr=ptr;
          ptr=list[ptr];
          cnt++;
        }
      vals[i]=(unsigned int)dict[ptr];
      /* Move it to front in list */
      /* Is it the head? Then it is already at the front. */
      if (oldptr!=-1)
        {
          /* Remove it from inside the list */
          list[oldptr]=list[ptr];
          /* Move it to the front. */
          list[ptr]=head;
          head=ptr;
        }
    }
}

void Ptngc_comp_conv_from_mtf_partial(unsigned int *valsmtf, int nvals,
                                unsigned int *vals)
{
  unsigned char *tmp=warnmalloc(nvals*2);
  int i, j;

  memset(vals, 0U, sizeof(unsigned int) * nvals);

  for (j=0; j<3; j++)
    {
      for (i=0; i<nvals; i++)
        tmp[i]=(unsigned char)((valsmtf[i]>>(8*j))&0xFF);
      comp_conv_from_mtf_byte(tmp,nvals,tmp+nvals);
      for (i=0; i<nvals; i++)
        vals[i]|=(((unsigned int)(tmp[nvals+i]))<<(8*j));
    }
  free(tmp);
}

void Ptngc_comp_conv_from_mtf_partial3(unsigned char *valsmtf, int nvals,
                                 unsigned int *vals)
{
  unsigned char *tmp=warnmalloc(nvals);
  int i, j;

  memset(vals, 0U, sizeof(unsigned int) * nvals);

  for (j=0; j<3; j++)
    {
      comp_conv_from_mtf_byte(valsmtf+j*nvals,nvals,tmp);
      for (i=0; i<nvals; i++)
        vals[i]|=(((unsigned int)(tmp[i]))<<(8*j));
    }
  free(tmp);
}

/* Move to front coding.
   Acceptable inputs are max 24 bits (0-0xFFFFFF) */
void Ptngc_comp_conv_to_mtf(unsigned int *vals, int nvals,
                      unsigned int *dict, int ndict,
                      unsigned int *valsmtf)
{
  int i;
  /* Indices into a linked list */
  int *list=warnmalloc(ndict*sizeof *list);
  /* Head of the linked list */
  int head;
  for (i=0; i<ndict-1; i++)
    list[i]=i+1;
  list[ndict-1]=-1; /* end. */
  head=0;
  for (i=0; i<nvals; i++)
    {
      int v=vals[i];
      /* Find how early in the dict the value is */
      int ptr=head;
      int oldptr=-1;
      int r=0;
      while (dict[ptr]!=v)
        {
          oldptr=ptr;
          ptr=list[ptr];
          r++;
        }
      valsmtf[i]=r;
      /* Move it to front in list */
      /* Is it the head? Then it is already at the front. */
      if (oldptr!=-1)
        {
          /* Remove it from inside the list */
          list[oldptr]=list[ptr];
          /* Move it to the front. */
          list[ptr]=head;
          head=ptr;
        }
    }
  free(list);
}

/* Move to front decoding */
void Ptngc_comp_conv_from_mtf(unsigned int *valsmtf, int nvals,
                        unsigned int *dict, int ndict,
                        unsigned int *vals)
{
  int i;
  /* Indices into a linked list */
  int *list=warnmalloc(ndict*sizeof *list);
  /* Head of the linked list */
  int head;
  for (i=0; i<ndict-1; i++)
    list[i]=i+1;
  list[ndict-1]=-1; /* end. */
  head=0;
  for (i=0; i<nvals; i++)
    {
      int r=valsmtf[i];
      /* Find value at position r in the list */
      int ptr=head;
      int oldptr=-1;
      int cnt=0;
      while (cnt<r)
        {
          oldptr=ptr;
          ptr=list[ptr];
          cnt++;
        }
      vals[i]=dict[ptr];
      /* Move it to front in list */
      /* Is it the head? Then it is already at the front. */
      if (oldptr!=-1)
        {
          /* Remove it from inside the list */
          list[oldptr]=list[ptr];
          /* Move it to the front. */
          list[ptr]=head;
          head=ptr;
        }
    }
  free(list);
}

contact: Jan Huwald // Impressum