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
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
inline int
OK(int x) {
return x == 0;
}
////RGBImage////
typedef uint8_t byte;
typedef uint16_t byte2;
typedef uint32_t byte4;
typedef struct _RGB {
byte r;
byte g;
byte b;
} RGB;
void
ARGB_set(RGB* rgb, byte4 color) {
rgb->r = (0xFF0000 & color) >> 16;
rgb->g = (0x00FF00 & color) >> 8;
rgb->b = (0x0000FF & color);
}
typedef struct _RGBImage {
int width;
int height;
RGB* img;
} RGBImage;
RGBImage
RGBImage_new(int width, int height) {
return (RGBImage){
.width = width,
.height = height,
.img = malloc(sizeof(RGB) * width * height)
};
}
void
RGBImage_free(RGBImage img) {
free(img.img);
}
void
RGBImage_delete(RGBImage* img) {
RGBImage_free(*img);
img->width = img->height = 0;
img->img = NULL;
}
RGB*
RGBImage_at(RGBImage img, int row, int col) {
return img.img + (row % img.height) * img.width + col % img.width;
}
int
ppm6_write(FILE* f, RGBImage img) {
fprintf(f, "P6\n%d %d\n255\n", img.width, img.height);
fwrite(img.img, sizeof(*img.img), img.width * img.height, f);
return fflush(f);
}
////main////
int
main() {
RGBImage frame = RGBImage_new(512, 512);
for (int frameIndex = 0; frameIndex < 120; ++frameIndex) {
for (int r = 0; r < frame.width; ++r)
for (int c = 0; c < frame.height; ++c) {
RGB* pixel = RGBImage_at(frame, r, c);
if (((r + frameIndex) / 32 + (c + frameIndex) / 32) % 2 == 0) {
ARGB_set(pixel, 0x000000);
}
else {
ARGB_set(pixel, 0xFF00FF);
}
}
ppm6_write(stdout, frame);
}
RGBImage_delete(&frame);
return 0;
}
|