File: bitstream.c

package info (click to toggle)
graphicsmagick 1.3.20-3+deb8u1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 38,472 kB
  • ctags: 14,330
  • sloc: ansic: 220,724; sh: 24,578; cpp: 18,199; perl: 3,365; makefile: 2,359; tcl: 2,208; python: 1,280
file content (201 lines) | stat: -rw-r--r-- 4,483 bytes parent folder | download | duplicates (5)
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
/*
  Copyright (C) 2003, 2005, 2009 GraphicsMagick Group
 
  This program is covered by multiple licenses, which are described in
  Copyright.txt. You should have received a copy of Copyright.txt with this
  package; otherwise see http://www.graphicsmagick.org/www/Copyright.html.

  Bitstream benchmarks and tests.  
  Written by Bob Friesenhahn, September 2003
 
*/

#include <magick/studio.h>
#include <magick/bit_stream.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main ( int argc, char *argv[])
{
  const char
    *mode;

  register unsigned int
    rep,
    x;

  register unsigned char
    *bytes;

  unsigned int
    result=0;

  unsigned int
    quantum;

  FILE
    *file;

  if (argc < 2)
    {
      (void) printf("usage: %s read/write/test args\n", argv[0]);
      exit(1);
    }

  mode = argv[1];

  file=fopen("/dev/null","wb+");
  if (!file)
    {
      (void) printf("Failed to open file\n");
      exit(1);
    }

  if (strcmp(mode,"read") == 0)
    {
      BitStreamReadHandle
        read_stream;

      unsigned int
        bits;

      unsigned int
        reps;

      if (argc != 4)
        {
          (void) printf("usage: %s read repetitions bits\n", argv[0]);
          exit(1);
        }

      reps = atoi(argv[2]);
      bits = atoi(argv[3]);

      bytes=(unsigned char *)malloc(reps*2);
      if (!bytes)
        {
          (void) printf("Failed to allocate %lu bytes\n", (unsigned long) reps*2);
          exit(1);
        }
      for (rep=0; rep < reps*2; rep++)
        bytes[rep]=(unsigned char) rep;

      for (rep=reps; rep > 0; rep--)
        {
          MagickBitStreamInitializeRead(&read_stream,bytes);
          for (x=reps; x > 0; x--)
            {
              quantum=MagickBitStreamMSBRead(&read_stream,bits);
              result += quantum;
            }
        }

      free(bytes);
    }
  else if (strcmp(mode,"write") == 0)
    {
      BitStreamWriteHandle
        write_stream;

      unsigned int
        bits;

      unsigned int
        reps;

      if (argc != 4)
        {
          (void) printf("usage: %s write repetitions bits\n", argv[0]);
          exit(1);
        }

      reps = atoi(argv[2]);
      bits = atoi(argv[3]);

      bytes=(unsigned char *)malloc(reps*2);
      if (!bytes)
        {
          (void) printf("Failed to allocate %lu bytes\n", (unsigned long) reps*2);
          exit(1);
        }
      (void) memset(bytes,0,reps*2);

      for (rep=reps; rep > 0; rep--)
        {
          MagickBitStreamInitializeWrite(&write_stream,bytes);
          for (x=reps; x > 0; x--)
            {
              MagickBitStreamMSBWrite(&write_stream,bits,x);
              result += bytes[x];
            }
        }
    }
  else if (strcmp(mode,"test") == 0)
    {
      BitStreamWriteHandle
        write_stream;

      BitStreamReadHandle
        read_stream;

      size_t
        allocated_bytes;

      unsigned int
        bits,
        max_bits,
        max_quantum,
        read_quantum,
        write_quantum;

      if (argc != 3)
        {
          (void) printf("usage: %s test max_bits\n", argv[0]);
          exit(1);
        }

      max_bits = atoi(argv[2]);

      for (bits=1 ; bits <= max_bits; bits++)
        {
          max_quantum=0x01 << bits;

          allocated_bytes=max_quantum;
          if (bits > 8)
            allocated_bytes *= 2;
          if (bits > 16)
            allocated_bytes *= 2;
          
          bytes=(unsigned char *)malloc(allocated_bytes);
          if (!bytes)
            {
              (void) printf("Failed to allocate %lu bytes\n",(unsigned long) allocated_bytes);
              exit(1);
            }
          (void) memset(bytes,0xff,allocated_bytes);
          
          MagickBitStreamInitializeWrite(&write_stream,bytes);
          MagickBitStreamInitializeRead(&read_stream,bytes);
          
          for (write_quantum=0; write_quantum < max_quantum; write_quantum++)
            {
              MagickBitStreamMSBWrite(&write_stream,bits,write_quantum);
              read_quantum=MagickBitStreamMSBRead(&read_stream,bits);
              if (read_quantum != write_quantum)
                {
                  (void) printf("mismatch: bits=%u write_quantum=%u read_quantum=%u\n",
                         bits,write_quantum,read_quantum);
                }
            }
          
          free(bytes);
        }
    }

  (void) fprintf(file,"result=%u\n",result);
  (void) fclose(file);

  return 0;
}