-
Notifications
You must be signed in to change notification settings - Fork 1
/
entropypic.c
181 lines (156 loc) · 3.29 KB
/
entropypic.c
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
// entropypic.c
// Glen Wiley <glen.wiley@gmail.com>
//
// generate a BMP from an entropy stream to help catch non-random
// patterns in the entropy
#include "bmpfile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define READBUFSIZE 128
#define MODE_BITS 'b'
#define MODE_BYTES 'B'
//---------------------------------------- usage
void
usage(void)
{
printf("USAGE: entropypic [-B] -s <x,y> [-f <file>] -o <file>\n"
"\n"
"-B input file is bytes\n"
"-s <x,y> set image size in pixels\n"
"-f <file> input file, default is stdin\n"
"-o <file> output file\n"
"\n"
"Input file should be a stream of 1s and 0s, a 1 will show as black\n"
"on the output image, if the input file is s stream of bytes (-B) then\n"
"each byte is converted to bits.\n"
"\n"
"The file is read until EOF or until the bitmap is filled.\n"
"\n"
"Glen Wiley <glen.wiley@gmail.com>\n"
);
} // usage
//---------------------------------------- createbmp
// generate the bmp
void
createbmp(FILE *fh_in, char *fn_out, int xsize, int ysize, char mode)
{
int bmpfull = 0;
int nbytes;
int bnum;
int i;
int x = 0;
int y = 0;
int bitval;
char buf[READBUFSIZE+1];
bmpfile_t *bmp;
rgb_pixel_t pixel = {128, 64, 0, 0};
bmp = bmp_create(xsize, ysize, 1);
while((nbytes = fread(buf, 1, READBUFSIZE, fh_in)) > 0 && bmpfull == 0)
{
for(bnum=0; bnum < nbytes; bnum++)
{
// if we are in bit mode then ignore anything other than 0 and 1
if(mode == MODE_BITS && (buf[bnum] != '0' && buf[bnum] != '1'))
continue;
for(i=0; i<(mode==MODE_BITS ? 1 : 8); i++)
{
if(mode == MODE_BITS)
{
if(buf[bnum] == '1')
bitval == 1;
}
else
{
bitval = buf[bnum] & 1;
buf[bnum] = buf[bnum] >> 1;
}
if(bitval == 1)
bmp_set_pixel(bmp, x, y, pixel);
x++;
if(x == xsize)
{
x = 0;
y++;
}
if(y == ysize)
{
bmpfull = 1;
break;
}
} // for i
} // for bnum
} // while fread
bmp_save(bmp, fn_out);
bmp_destroy(bmp);
return;
} // createbmp
//---------------------------------------- main
int
main(int argc, char *argv[])
{
int opt;
int retval = 0;
int x = 0;
int y = 0;
FILE *fh_in = NULL;
char *fn_in = NULL;
char *fn_out = NULL;
char *tok;
char mode = MODE_BITS;
if(argc == 1)
{
usage();
exit(1);
}
while((opt=getopt(argc, argv, "Bf:o:s:")) != -1)
{
switch(opt)
{
case 'B':
mode = MODE_BYTES;
break;
case 'f':
fn_in = optarg;
break;
case 'o':
fn_out = optarg;
break;
case 's':
tok = strtok(optarg, ",xX");
if(tok != NULL && *tok != '\0')
{
x = atoi(tok);
if((tok = strtok(NULL, ",xX")) != NULL && *tok != '\0')
y = atoi(tok);
}
break;
default:
usage();
exit(1);
}
} // while getopt
if(x == 0 || y == 0)
printf("ERROR: no size specified\n");
if(fn_in != NULL)
{
fh_in = fopen(fn_in, "r");
if(fh_in == NULL)
{
printf("ERROR: failed to open input file %s\n", fn_in);
exit(1);
}
}
else
fh_in = stdin;
if(fn_out == NULL)
{
printf("ERROR: no output file specified, exiting.\n");
exit(1);
}
createbmp(fh_in, fn_out, x, y, mode);
fclose(fh_in);
return retval;
} // main
// entropypic.c