-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.c
More file actions
188 lines (156 loc) · 4.99 KB
/
lib.c
File metadata and controls
188 lines (156 loc) · 4.99 KB
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
#include "lib.h"
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
static int set_header(FILE *file, int width, int height) {
int row_bytes = width * 3;
int padding = (4 - (row_bytes % 4)) % 4;
int row_size_padded = row_bytes + padding;
int pixel_data_size = row_size_padded * height;
int file_size = 14 + 40 + pixel_data_size;
if (fwrite("BM", 2, 1, file) != 1) return -1;
if (fwrite(&file_size, 4, 1, file) != 1) return -1;
int reserved = 0;
if (fwrite(&reserved, 4, 1, file) != 1) return -1;
int offset = 14 + 40;
if (fwrite(&offset, 4, 1, file) != 1) return -1;
int header_size = 40;
if (fwrite(&header_size, 4, 1, file) != 1) return -1;
if (fwrite(&width, 4, 1, file) != 1) return -1;
if (fwrite(&height, 4, 1, file) != 1) return -1;
short planes = 1;
if (fwrite(&planes, 2, 1, file) != 1) return -1;
short bits_per_pixel = 24;
if (fwrite(&bits_per_pixel, 2, 1, file) != 1) return -1;
int compression = 0;
if (fwrite(&compression, 4, 1, file) != 1) return -1;
if (fwrite(&pixel_data_size, 4, 1, file) != 1) return -1;
int ppm = 2835;
if (fwrite(&ppm, 4, 1, file) != 1) return -1;
if (fwrite(&ppm, 4, 1, file) != 1) return -1;
int colors_used = 0;
if (fwrite(&colors_used, 4, 1, file) != 1) return -1;
if (fwrite(&colors_used, 4, 1, file) != 1) return -1;
return 0;
}
struct Bitmap init_bitmap(int width, int height) {
struct Bitmap bitmap = { .width = 0, .height = 0, .pixels = NULL };
if (width <= 0 || height <= 0) {
return bitmap;
}
size_t pixel_count = (size_t) width * (size_t) height;
if (pixel_count > SIZE_MAX / sizeof(color_t)) {
return bitmap;
}
color_t *pixels = malloc(pixel_count * sizeof(color_t));
if (!pixels) {
return bitmap;
}
bitmap.width = width;
bitmap.height = height;
bitmap.pixels = pixels;
return bitmap;
}
void set_pixel(struct Bitmap bitmap, int x, int y, color_t color) {
if (!bitmap.pixels) {
return;
}
if (x < 0 || x >= bitmap.width || y < 0 || y >= bitmap.height) {
return;
}
bitmap.pixels[y * bitmap.width + x] = color;
}
void set_line(struct Bitmap bitmap, int x1, int y1, int x2, int y2, color_t color){
int dx = abs(x2 - x1);
int dy = -abs(y2 - y1);
int sx = (x1 < x2) ? 1 : -1;
int sy = (y1 < y2) ? 1 : -1;
int err = dx + dy;
while (1) {
set_pixel(bitmap, x1, y1, color);
if (x1 == x2 && y1 == y2) break;
int e2 = 2 * err;
if (e2 >= dy) {
err += dy;
x1 += sx;
}
if (e2 <= dx) {
err += dx;
y1 += sy;
}
}
}
void set_circle(struct Bitmap bitmap, int xc, int yc, int r, color_t color){
int x = 0;
int y = r;
int d = 3 - 2 * r;
while (y >= x) {
set_pixel(bitmap, xc + x, yc + y, color);
set_pixel(bitmap, xc - x, yc + y, color);
set_pixel(bitmap, xc + x, yc - y, color);
set_pixel(bitmap, xc - x, yc - y, color);
set_pixel(bitmap, xc + y, yc + x, color);
set_pixel(bitmap, xc - y, yc + x, color);
set_pixel(bitmap, xc + y, yc - x, color);
set_pixel(bitmap, xc - y, yc - x, color);
x++;
if (d > 0) {
y--;
d = d + 4 * (x - y) + 10;
} else {
d = d + 4 * x + 6;
}
}
}
void set_rec(struct Bitmap bitmap, int x1, int y1, int x2, int y2, color_t color){
if (x1 > x2) { int tmp = x1; x1 = x2; x2 = tmp; }
if (y1 > y2) { int tmp = y1; y1 = y2; y2 = tmp; }
for (int y = y1; y <= y2; y++) {
for (int x = x1; x <= x2; x++) {
set_pixel(bitmap, x, y, color);
}
}
}
int write_bitmap(struct Bitmap bitmap, const char *filename) {
if (!bitmap.pixels || !filename || bitmap.width <= 0 || bitmap.height <= 0) {
return -1;
}
FILE *file = fopen(filename, "wb");
if (!file) {
return -1;
}
int width = bitmap.width;
int height = bitmap.height;
int row_bytes = width * 3;
int padding = (4 - (row_bytes % 4)) % 4;
if (set_header(file, width, height) != 0) {
fclose(file);
return -1;
}
unsigned char pad[3] = {0};
for (int y = height - 1; y >= 0; --y) {
for (int x = 0; x < width; ++x) {
color_t color = bitmap.pixels[y * width + x];
unsigned char red = (color & 0xFF0000) >> 16;
unsigned char green = (color & 0x00FF00) >> 8;
unsigned char blue = color & 0x0000FF;
if (fwrite(&blue, 1, 1, file) != 1 ||
fwrite(&green, 1, 1, file) != 1 ||
fwrite(&red, 1, 1, file) != 1) {
fclose(file);
return -1;
}
}
if (fwrite(pad, 1, padding, file) != (size_t) padding) {
fclose(file);
return -1;
}
}
if (fclose(file) != 0) {
return -1;
}
return 0;
}
void free_bitmap(struct Bitmap bitmap) {
free(bitmap.pixels);
}