-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.c
166 lines (146 loc) · 3.24 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
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define KIMG_VERSION 0
struct {
unsigned char *infile;
FILE *outfile;
bool bare;
uint16_t width, height;
} context;
const char usage[] =
"Usage: %s [options] input output\n"
"\n"
"Options:\n"
"\n"
"-m, --monochrome\n"
"\tConverts the image to a monochrome KIMG file. This is on by default for now; color support is not yet implemented.\n"
"\n"
"For more information, see the man page.\n"
;
void
init_context(void)
{
context.bare = false;
}
void
load_image(const char *const path)
{
int h, w;
context.infile = stbi_load(path, &w, &h, NULL, 1);
if(context.infile == NULL){
fprintf(stderr, "Error parsing image: %s\n", path);
exit(1);
}
context.height = (uint16_t)h;
context.width = (uint16_t)w;
}
void
load_file(const char *const path)
{
if(context.infile == NULL){
load_image(path);
}else if(context.outfile == NULL){
context.outfile = fopen(path, "wb");
if(context.outfile == NULL){
fprintf(stderr, "Error opening file: %s\n", path);
exit(1);
}
}else{
fprintf(stderr, "Extra file specified: %s\n", path);
exit(1);
}
}
#define arg(short, long) strcmp(short, argv[i]) == 0 || strcmp(long, argv[i]) == 0
void
parse_arguments(const int argc, const char *const *const argv)
{
if(argc == 1){
printf(usage, argv[0]);
exit(1);
}
for (int i = 1; i < argc; i++){
if(arg("-h", "--help")){
printf(usage, argv[0]);
exit(0);
}else if(arg("-b", "--bare")){
context.bare = true;
}else if(argv[i][0] == '-'){
fprintf(stderr, "Unrecognized argument: %s\n", argv[i]);
exit(1);
}else{
load_file(argv[i]);
}
}
}
void
write_header(void)
{
if(!context.bare){
fwrite("KIMG", 4, 1, context.outfile); // 0x00: Magic Header
fprintf(context.outfile, "%c", KIMG_VERSION); // 0x04: Format Version
fprintf(context.outfile, "%c", 0); // 0x05: Format Flags (TODO!)
fwrite(&context.height, 2, 1, context.outfile); // 0x06: Image Height
fwrite(&context.width, 2, 1, context.outfile); // 0x08: Image Width // 0x09: Palette/Image Data...
}
}
uint16_t
process_data(uint8_t *buffer)
{
uint8_t byte, mask;
uint16_t index = 0;
uint16_t x, y;
for(y = 0; y < context.height; y++){
byte = 0;
mask = 0x80;
for(x = 0; x < context.width; x++){
if(context.infile[y * context.width + x] < 127)
byte |= mask;
mask >>= 1;
if(mask == 0){
buffer[index] = byte;
index += 1;
mask = 0x80;
byte = 0;
}
}
if(mask != 0x80){
buffer[index] = byte;
index += 1;
}
}
return index;
}
void
write_data(void)
{
uint16_t size;
uint8_t *buffer;
size = context.width * context.height / 8;
if((context.width * context.height) % 8 != 0)
size += 1;
buffer = malloc(size);
if(buffer == NULL){
fwrite("OOM\n", 4, 1, stderr);
exit(1);
}
size = process_data(buffer);
fwrite(buffer, size, 1, context.outfile);
free(buffer);
}
int
main(const int argc, const char *const *const argv)
{
init_context();
parse_arguments(argc, argv);
write_header();
write_data();
stbi_image_free(context.infile);
fflush(context.outfile);
fclose(context.outfile);
return 0;
}