-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
294 lines (262 loc) · 6.47 KB
/
main.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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
//
// MIT License 2017-2024 Dmitri Smirnov <https://www.whoop.ee>
//
// bmp2bytes
//
// Version 2.
//
// Converts 1-bit monochrome bitmap image (BMP format) to string
// and outputs it to `stdout`. Where data is require as text input.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdint.h>
#include "bitmap.h"
uint8_t check_error(char*);
FILE* read_file(char*, char*);
BMPImage* parse_bmp(char*, uint8_t, uint8_t, char*);
void parse_bmp_header(BMPImage*, FILE*, char*);
void parse_bmp_data(BMPImage*, FILE*, char*);
void print_meta(BMPImage*);
int32_t adjust_width(int32_t);
void check_allocation(void*, char*);
long calc_line_size(BMPImage*);
void usage();
int main(int argc, char** argv) {
int i, rev_i, opt;
uint32_t w, h;
BMPImage* image;
char* filename = NULL;
uint8_t reverse = 0;
uint8_t invert = 0;
uint8_t meta = 0;
uint8_t newline = 0;
char* error = NULL;
char* format = "0x%02X";
char* delim = ",";
while((opt = getopt(argc, argv, "mrihnf:d:")) != -1) {
switch(opt) {
case 'n':
newline = 1;
break;
case 'm':
meta = 1;
break;
case 'i':
invert = 1;
break;
case 'r':
reverse = 1;
break;
case 'f':
format = optarg;
break;
case 'd':
delim = optarg;
break;
case 'h':
usage();
return EXIT_SUCCESS;
default:
break;
}
}
// optind is for the extra arguments
// which are not parsed
for(; optind < argc; optind++){
filename = argv[optind];
// Only one unparsed argument is required, it is a file name
if (filename != NULL) {
break;
}
}
if (access(filename, F_OK) != 0) {
error = "file missing";
}
if (check_error(error)) {
return EXIT_FAILURE;
}
image = parse_bmp(filename, reverse, invert, error);
check_error(error);
w = adjust_width(image->header.width);
h = image->header.height;
if (meta == 1) {
print_meta(image);
return EXIT_SUCCESS;
}
for (i = h *( w / 8) - 1; i >= 0 ; i--) {
printf(format, image->data[i]);
// Do not print last comma?
if (i != 0) {
printf(delim);
}
}
if (newline == 1) {
printf("\n");
}
free(image);
return EXIT_SUCCESS;
}
// In 1-bit image rows have 4 bytes border.
// 2-bit images and more have color in every byte.
long calc_line_size(BMPImage* image) {
long len = (image->header.width / 32) * 4;
if (image->header.width % 32) {
len += 4;
}
return len;
}
uint8_t check_error(char* error) {
if (error != NULL) {
fprintf(stderr, "%s\n", error);
return 1;
}
return 0;
}
void check_allocation(void* cs, char* error) {
if (cs == NULL) {
error = "failed allocate memory";
}
}
FILE* read_file(char *filename, char* error) {
FILE* fp = fopen(filename, "rb");
if (fp == NULL) {
sprintf(error, "%s cannot be opened.", filename);
return NULL;
}
return fp;
}
// Minimum width size is 8.
int32_t adjust_width(int32_t w) {
if (w < 8) {
return 8;
}
return w;
}
void parse_bmp_header(BMPImage* image, FILE* fp, char* error) {
int status = fread(&image->header, sizeof(BMPHeader), 1, fp);
if (status != 0) {
error = "cannot read file";
return;
}
if (image->header.type != BITMAP_IDENTIFIER) {
error = "file is not bitmap image";
return;
}
if (image->header.bits != 1) {
error = "only 1-bit monochrome bitmap files are supported.";
return;
}
}
void parse_bmp_data(BMPImage* image, FILE* fp, char* error) {
// Move file point to the begging of bitmap data
int status = fseek(fp, image->header.offset, SEEK_SET);
if (status != 0) {
error = "failed to set file offset";
}
// Read in the bitmap image data
status = fread(image->data, image->header.size, 1, fp);
if (status != 0) {
error = "failed to read image data";
}
}
BMPImage* parse_bmp(
char* filename,
uint8_t reverse,
uint8_t invert,
char* error
) {
int32_t j, i, rev_i, pos, dw;
int32_t ipos = 0;
BMPImage* image = malloc(sizeof(BMPImage));
check_allocation(image, error);
FILE* fp = read_file(filename, error);
if (check_error(error)) {
fclose(fp);
free(image);
return NULL;
}
parse_bmp_header(image, fp, error);
if (check_error(error)) {
fclose(fp);
free(image);
return NULL;
}
dw = adjust_width(image->header.width);
// In 1-bit image rows have 4 bytes border.
// 2-bit images and more have color in every byte.
long line_size = calc_line_size(image);
long martix_size = line_size * image->header.height;
// Allocate enough memory for the bitmap image data.
image->data = malloc(martix_size);
uint8_t* temp_data = malloc(martix_size);
check_allocation(image->data, error);
if (check_error(error)) {
fclose(fp);
free(image);
return NULL;
}
parse_bmp_data(image, fp, error);
if (check_error(error)) {
fclose(fp);
free(image);
return NULL;
}
ipos = 0;
for (
i = 0, rev_i = image->header.height - 1;
i < image->header.height;
i++, rev_i--
) {
for (j = dw/8 - 1; j >= 0; j--) {
// Revere byte order
if (reverse == 1) {
pos = rev_i*line_size + j;
} else {
pos = i*line_size + j;
}
// Invert image
if (invert == 1) {
temp_data[ipos] = image->data[pos];
} else {
temp_data[ipos] = 0xFF - image->data[pos];
}
ipos++;
}
}
image->data = temp_data;
free(temp_data);
fclose(fp);
return image;
}
void print_meta(BMPImage* image) {
printf("type %d\n", image->header.type);
printf("size %d\n", image->header.size);
printf("reserved1 %d\n", image->header.reserved1);
printf("reserved2 %d\n", image->header.reserved2);
printf("offset %d\n", image->header.offset);
printf("dibheadersize %d\n", image->header.dibheadersize);
printf("width %d\n", image->header.width);
printf("height %d\n", image->header.height);
printf("planes %d\n", image->header.planes);
printf("bits %d\n", image->header.bits);
printf("compression %d\n", image->header.compression);
printf("imagesize %d\n", image->header.imagesize);
printf("xres %d\n", image->header.xres);
printf("yres %d\n", image->header.yres);
printf("ncolours %d\n", image->header.ncolors);
printf("importantcolours %d\n", image->header.importantcolors);
}
void usage() {
printf("bmp2bytes usage:\n");
printf("bmp2tpues [-mrihfd] <file.bmp>\n");
printf(" -h: prints usage;\n");
printf(" -r: reverse rows;\n");
printf(" -i: invert image bits;\n");
printf(" -m: prints meta data of the image and exit;\n");
printf(" -f <string>: format of the output, any number qualifier ");
printf("for `printf` function is accepted.\n");
printf(" Examples: %%d, %%X, %%02X, etc. Default: 0x%%02X\n");
printf(" -d: delimiter of the output. Default: \",\" (comma).\n");
printf(" -n: print new line at the end of output\n");
}