-
Notifications
You must be signed in to change notification settings - Fork 0
/
display.c
143 lines (113 loc) · 3.07 KB
/
display.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
/*====================== display.c ========================
Contains functions for basic manipulation of a screen
represented as a 2 dimensional array of colors.
A color is an ordered triple of ints, with each value standing
for red, green and blue respectively
==================================================*/
#include <stdio.h>
#include <stdlib.h>
#include "ml6.h"
#include "display.h"
/*======== void plot() ==========
Inputs: screen s
color c
int x
int y
Returns:
Sets the color at pixel x, y to the color represented by c
Note that s[0][0] will be the upper left hand corner
of the screen.
If you wish to change this behavior, you can change the indicies
of s that get set. For example, using s[x][YRES-1-y] will have
pixel 0, 0 located at the lower left corner of the screen
02/12/10 09:09:00
jdyrlandweaver
====================*/
void plot( screen s, color c, int x, int y) {
int newy = YRES - 1 - y;
if ( x >= 0 && x < XRES && newy >=0 && newy < YRES )
s[x][newy] = c;
}
/*======== void clear_screen() ==========
Inputs: screen s
Returns:
Sets every color in screen s to black
02/12/10 09:13:40
jdyrlandweaver
====================*/
void clear_screen( screen s ) {
int x, y;
color c;
c.red = 0;
c.green = 0;
c.blue = 0;
for ( y=0; y < YRES; y++ )
for ( x=0; x < XRES; x++)
s[x][y] = c;
}
/*======== void save_ppm() ==========
Inputs: screen s
char *file
Returns:
Saves screen s as a valid ppm file using the
settings in ml6.h
02/12/10 09:14:07
jdyrlandweaver
====================*/
void save_ppm( screen s, char *file) {
int x, y;
FILE *f;
f = fopen(file, "w");
fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
for ( y=0; y < YRES; y++ ) {
for ( x=0; x < XRES; x++)
fprintf(f, "%d %d %d ", s[x][y].red, s[x][y].green, s[x][y].blue);
fprintf(f, "\n");
}
fclose(f);
}
/*======== void save_extension() ==========
Inputs: screen s
char *file
Returns:
Saves the screen stored in s to the filename represented
by file.
If the extension for file is an image format supported
by the "convert" command, the image will be saved in
that format.
02/12/10 09:14:46
jdyrlandweaver
====================*/
void save_extension( screen s, char *file) {
int x, y;
FILE *f;
char line[256];
sprintf(line, "convert - %s", file);
f = popen(line, "w");
fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
for ( y=0; y < YRES; y++ ) {
for ( x=0; x < XRES; x++)
fprintf(f, "%d %d %d ", s[x][y].red, s[x][y].green, s[x][y].blue);
fprintf(f, "\n");
}
pclose(f);
}
/*======== void display() ==========
Inputs: screen s
Returns:
Will display the screen s on your monitor
02/12/10 09:16:30
jdyrlandweaver
====================*/
void display( screen s) {
int x, y;
FILE *f;
f = popen("display", "w");
fprintf(f, "P3\n%d %d\n%d\n", XRES, YRES, MAX_COLOR);
for ( y=0; y < YRES; y++ ) {
for ( x=0; x < XRES; x++)
fprintf(f, "%d %d %d ", s[x][y].red, s[x][y].green, s[x][y].blue);
fprintf(f, "\n");
}
pclose(f);
}