-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimg.c
42 lines (42 loc) · 1.16 KB
/
img.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
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "img.h"
static unsigned char buf[HEIGHT][WIDTH][3];
static int filecnt = 0;
static char fname[100];
void img_clear(void) {
int i, j;
for(j = 0; j < HEIGHT; ++j){
for(i = 0; i < WIDTH; ++i) {
buf[j][i][0] = buf[j][i][1] = buf[j][i][2] = 255;
}
}
}
void img_write(void) {
sprintf(fname, "img%04d.ppm", ++filecnt);
FILE *f = fopen(fname, "wb");
if(f == NULL) {
fprintf(stderr, "can't open %s\n", fname);
exit(1);
}
fprintf(f, "P6\n%d %d\n255\n", WIDTH, HEIGHT);
fwrite(buf, sizeof(buf), 1, f);
fclose(f);
}
void img_putpixel(struct color c, int x, int y) {
if(x < 0 || x >= WIDTH || y < 0 || y >= HEIGHT) {
return;
}
buf[HEIGHT-y-1][x][0] = c.r;
buf[HEIGHT-y-1][x][1] = c.g;
buf[HEIGHT-y-1][x][2] = c.b;
}
void img_fillrect(struct color c, double x, double y, double w, double h) {
int imin = (int)(x - w/2), imax = (int)(x + w/2);
int jmin = (int)(y - h/2), jmax = (int)(y + h/2);
int i, j;
for(j = jmin; j <= jmax; ++j) {
for(i = imin; i <= imax; ++i) { img_putpixel(c, i, j); }
}
}