-
Notifications
You must be signed in to change notification settings - Fork 123
/
makegraphic.c
283 lines (234 loc) · 6.78 KB
/
makegraphic.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
/*
* OID image generator by Matthias Bilger <[email protected]>
*
* This is an alternative to `tttool oid-code`
*/
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <getopt.h>
/* A coloured pixel. */
typedef struct {
uint8_t set;
} pixel_t;
/* A picture. */
typedef struct {
pixel_t *pixels;
size_t width;
size_t height;
} bitmap_t;
int space = 10;
int width = 2;
static int verbose_flag = 0;
static int ting_flag = 0;
/* Given "bitmap", this returns the pixel of bitmap at the point
("x", "y"). */
static pixel_t * pixel_at (bitmap_t * bitmap, int x, int y)
{
return bitmap->pixels + bitmap->width * y + x;
}
/* Write "bitmap" to a PNG file specified by "path"; returns 0 on
success, non-zero on error. */
static int save_png_to_file (bitmap_t *bitmap, const char *path)
{
FILE * fp;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte ** row_pointers = NULL;
/* "status" contains the return value of this function. At first
it is set to a value which means 'failure'. When the routine
has finished its work, it is set to a value which means
'success'. */
int status = -1;
/* The following number is set by trial and error only. I cannot
see where it it is documented in the libpng manual.
*/
int pixel_size = 3;
int depth = 8;
fp = fopen (path, "wb");
if (! fp) {
goto fopen_failed;
}
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
goto png_create_write_struct_failed;
}
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) {
goto png_create_info_struct_failed;
}
/* Set up error handling. */
if (setjmp (png_jmpbuf (png_ptr))) {
goto png_failure;
}
/* Set image attributes. */
png_set_IHDR (png_ptr,
info_ptr,
bitmap->width,
bitmap->height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
/* Set the resolution */
png_set_pHYs(png_ptr, info_ptr, 47244, 47244,
PNG_RESOLUTION_METER) ;
/* Initialize rows of PNG. */
row_pointers = png_malloc (png_ptr, bitmap->height * sizeof (png_byte *));
for (y = 0; y < bitmap->height; ++y) {
png_byte *row =
png_malloc (png_ptr, sizeof (uint8_t) * bitmap->width * pixel_size);
row_pointers[y] = row;
for (x = 0; x < bitmap->width; ++x) {
pixel_t * pixel = pixel_at (bitmap, x, y);
*row++ = (pixel->set)?0:255;
*row++ = (pixel->set)?0:255;
*row++ = (pixel->set)?0:255;
}
}
/* Write the image data to "fp". */
png_init_io (png_ptr, fp);
png_set_rows (png_ptr, info_ptr, row_pointers);
png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
/* The routine has successfully written the file, so we set
"status" to a value which indicates success. */
status = 0;
for (y = 0; y < bitmap->height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
png_create_write_struct_failed:
fclose (fp);
fopen_failed:
return status;
}
typedef struct {
uint8_t value[9];
} field;
static int calculateChecksum(int dec) {
int checksum = 0;
if(ting_flag){
checksum = (((dec >> 2) ^ (dec >> 8) ^ (dec >> 12) ^ (dec >> 14)) & 0x01) << 1;
checksum |= (((dec) ^ (dec >> 4) ^ (dec >> 6) ^ (dec >> 10)) & 0x01);
}else{
checksum = (((dec >> 2) ^ (dec >> 8) ^ (dec >> 12) ^ (dec >> 14)) & 0x01) << 1;
checksum |= (((dec) ^ (dec >> 4) ^ (dec >> 6) ^ (dec >> 10)) & 0x01);
checksum ^= 0x02; //needs to be tested
}
return checksum;
}
field create_field(int value){
field tmp = {.value = 0};
int counter = 0;
tmp.value[8] = calculateChecksum(value);
while (value){
tmp.value[counter] = value%4;
value = value/4;
counter++;
}
return tmp;
}
/* Sets a single pixel to the absolute x and y coordinate */
void set_pixel(bitmap_t* image, int x, int y){
pixel_t* pixel = pixel_at (image, x, y);
if(verbose_flag){
printf("Set pixel at %i, %i\n", x,y);
}
pixel->set = 1;
}
/* Sets a set of 4 pixel, x and y are absolute */
void set_pixels(bitmap_t* image, int x, int y){
set_pixel(image, x, y);
set_pixel(image, x+1, y);
set_pixel(image, x, y+1);
set_pixel(image, x+1, y+1);
}
void makeAbsoluteCoordinates(int* x, int* y){
*x*=(width + space);
*y*=(width + space);
}
void set_frame(bitmap_t* image, int x, int y){
makeAbsoluteCoordinates(&x, &y);
set_pixels(image, x, y);
}
void set_frame_marker(bitmap_t* image, int x, int y){
makeAbsoluteCoordinates(&x, &y);
set_pixels(image, x+1, y);
}
void set_point(bitmap_t* image, int value, int x, int y){
makeAbsoluteCoordinates(&x, &y);
x += ((value == 0 || value == 3)?1:-1)*2;
y += ((value == 0 || value == 1)?1:-1)*2;
set_pixels(image, x, y);
}
int main (int argc, char* argv[])
{
bitmap_t image;
int x;
int y;
field myfield;
char *filename;
int c;
image.width = 1000;
image.height = 1000;
int option_index = 0;
while (1)
{
static struct option long_options[] = {
{"sizex",required_argument,0,'x'},
{"sizey",required_argument,0,'y'},
{"file",required_argument,0,'f'},
{0,0,0,0}};
/* getopt_long stores the option index here. */
c = getopt_long (argc, argv, "x:y:f:",
long_options, &option_index);
/* Detect the end of the options. */
if (c == -1)
break;
switch (c)
{
case 'x':
image.width = atoi(optarg);
break;
case 'y':
image.height = atoi(optarg);
break;
case 'f':
filename = optarg;
break;
case '?':
/* getopt_long already printed an error message. */
break;
default:
abort ();
}
}
myfield = create_field(atoi(argv[option_index]));
printf("Checksum is: %i\n", myfield.value[8]);
/* Create an image. */
image.pixels = calloc (sizeof (pixel_t), image.width * image.height);
for (y = 0; y < image.height/(space + width); y++) {
for (x = 0; x < image.width/(space + width); x++) {
int fieldx = x & 3;
int fieldy = y & 3;
if(fieldx == 0 && fieldy == 2){
set_frame_marker(&image, x,y);
}else
if(fieldx == 0 || fieldy == 0){
set_frame(&image, x,y);
}
else{
set_point(&image, myfield.value[8-(fieldx-1)-((fieldy-1)*3)], x,y);
}
}
}
/* Write the image to a file 'fruit.png'. */
save_png_to_file (&image, filename);
return 0;
}