OpenCores
URL https://opencores.org/ocsvn/lzrw1-compressor-core/lzrw1-compressor-core/trunk

Subversion Repositories lzrw1-compressor-core

[/] [lzrw1-compressor-core/] [trunk/] [TestVectGen.java] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 habicht
/**************************************************************************************************************
2
*
3
*    L Z R W 1   E N C O D E R   C O R E
4
*
5
*  A high throughput loss less data compression core.
6
*
7
* Copyright 2012-2013   Lukas Schrittwieser (LS)
8
*
9
*    This program is free software: you can redistribute it and/or modify
10
*    it under the terms of the GNU General Public License as published by
11
*    the Free Software Foundation, either version 2 of the License, or
12
*    (at your option) any later version.
13
*
14
*    This program is distributed in the hope that it will be useful,
15
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
*    GNU General Public License for more details.
18
*
19
*    You should have received a copy of the GNU General Public License
20
*    along with this program; if not, write to the Free Software
21
*    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
22
*    Or see <http://www.gnu.org/licenses/>
23
*
24
***************************************************************************************************************
25
*
26
* Change Log:
27
*
28
* Version 1.0 - 2013/04/05 - LS
29
*   released
30
*
31
***************************************************************************************************************
32
*
33
* This program can be used to create random test vectors with a given probability of redundency and a
34
* given length.
35
*
36
***************************************************************************************************************/
37
 
38
import java.io.BufferedOutputStream;
39
import java.io.BufferedInputStream;
40
import java.io.BufferedReader;
41
import java.io.InputStreamReader;
42
import java.io.FileReader;
43
import java.io.FileOutputStream;
44
import java.io.FileInputStream;
45
import java.io.File;
46
import java.io.IOException;
47
import java.lang.Integer;
48
import java.util.Random;
49
import java.lang.Math;
50
 
51
class TestVectGen
52
{
53
        // define probabilities defining the generated vectors specifics
54
 
55
        // probability in percent that an object is a literal (otherwise it is repeated from the history buffer)
56
        final static int P_LITERAL      = 90;
57
 
58
        final static int MAX_COPY_LEN = 16;
59
 
60
        final static int HISTORY_LEN = 4096;    // buffer length in bytes 
61
 
62
        // create a binary test vector (file) to test compression programms
63
        public static void main (String args[])
64
        {
65
                if (args.length < 2)
66
                {
67
                        System.err.println("Usage: TestVectGen out-file min-length-in-bytes");
68
                        return;
69
                }
70
 
71
                int len = -1;
72
                int [] historyBuffer = new int[HISTORY_LEN];
73
                int histLength = 0;
74
                int histWrInd = 0;
75
                int ch;
76
                Random rand = new Random();
77
 
78
                try
79
                {
80
                        BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(args[0]));
81
                        len = Integer.parseInt(args[1]);
82
 
83
                        for (int i=0; i<len; i++)
84
                        {
85
                                // decide wether we want a literal or a replica
86
                                if (((Math.abs(rand.nextInt())%100) < P_LITERAL) || (i == 0))
87
                                {
88
                                        ch = Math.abs(rand.nextInt());  // create a random literal
89
                                        ch &= 0x7f;
90
                                        if (ch < 32) // make it a valid asci symbol
91
                                                ch += 32;
92
                                        ch &= 0x7F;
93
                                        if (ch == 127)
94
                                                ch = 32;
95
                                        outStream.write(ch);
96
                                        historyBuffer[histWrInd++] = ch;
97
                                        if (histWrInd >= HISTORY_LEN)
98
                                                histWrInd -= HISTORY_LEN;
99
                                        if (histLength < HISTORY_LEN)
100
                                                histLength++;
101
                                }
102
                                else
103
                                {
104
                                        // choose a random length for the copy operation
105
                                        int cLen        = (Math.abs(rand.nextInt()) % (MAX_COPY_LEN-3)) + 3; // -3 and +3 because we want values between 3 and MAX_COPY_LEN (both inclusive)
106
                                        int offset = Math.abs(rand.nextInt()) % (histLength);
107
                                        //System.out.printf("cpy:  i=%d  len=%d  off=%d\n",i,cLen,offset);
108
 
109
                                        for (int j=0; j<=cLen; j++)
110
                                        {
111
                                                // load the byte from the history buffer
112
                                                int ind = histWrInd-offset-1;
113
                                                while (ind < 0)
114
                                                        ind += HISTORY_LEN;
115
 
116
                                                ch = historyBuffer[ind];
117
                                                outStream.write(ch);
118
 
119
                                                // save this byte in the history buffer
120
                                                historyBuffer[histWrInd++] = ch;
121
                                                if (histWrInd >= HISTORY_LEN)
122
                                                        histWrInd = 0;
123
                                                if (histLength < HISTORY_LEN)
124
                                                histLength++;
125
                                        }
126
                                        // correct the loop variable (we created more than 1 byte)
127
                                        i += cLen - 1; // compensate for the i++ of for by -1
128
                                }
129
 
130
                        }
131
                        outStream.close();
132
                }
133
                catch (Exception e)
134
                {
135
                        System.err.println("exception: "+e);
136
                        e.printStackTrace();
137
                }
138
 
139
        }
140
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.