-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImage.cpp
154 lines (129 loc) · 5.01 KB
/
Image.cpp
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
#include <cassert>
#include "Image.h"
#include "Matrix.h"
// REQUIRES: img points to an Image
// 0 < width <= MAX_MATRIX_WIDTH
// 0 < height <= MAX_MATRIX_HEIGHT
// MODIFIES: *img
// EFFECTS: Initializes the Image with the given width and height.
// NOTE: Do NOT use new or delete here.
void Image_init(Image* img, int width, int height) {
//assert(false);
(*img).width = width;
(*img).height = height;
Matrix_init(&(*img).red_channel, width, height);
Matrix_init(&(*img).green_channel, width, height);
Matrix_init(&(*img).blue_channel, width, height);
}
// REQUIRES: img points to an Image
// is contains an image in PPM format without comments
// (any kind of whitespace is ok)
// MODIFIES: *img
// EFFECTS: Initializes the Image by reading in an image in PPM format
// from the given input stream.
// NOTE: See the project spec for a discussion of PPM format.
// NOTE: Do NOT use new or delete here.
void Image_init(Image* img, std::istream& is)
{
//assert(false);
std::string PPM;
int width, height, intensity;
is >> PPM >> width >> height >> intensity;
Image_init(img, width, height);
int red, green, blue;
int row = 0;
int col = 0;
while(is >> red >> green >> blue)
{
*Matrix_at(&(*img).red_channel, row, col) = red;
*Matrix_at(&(*img).green_channel, row, col) = green;
*Matrix_at(&(*img).blue_channel, row, col) = blue;
++col;
if(col == width)
{
++row;
col = 0;
}
}
}
// REQUIRES: img points to a valid Image
// EFFECTS: Writes the image to the given output stream in PPM format.
// You must use the kind of whitespace specified here.
// First, prints out the header for the image like this:
// P3 [newline]
// WIDTH [space] HEIGHT [newline]
// 255 [newline]
// Next, prints out the rows of the image, each followed by a
// newline. Each pixel in a row is printed as three ints
// for its red, green, and blue components, in that order. Each
// int is followed by a space. This means that there will be an
// "extra" space at the end of each line. See the project spec
// for an example.
void Image_print(const Image* img, std::ostream& os) {
//assert(false); // TODO Replace with your implementation!
os << "P3" << "\n";
os << (*img).width << " " << (*img).height << "\n";
os << 255 << "\n";
for(int row = 0; row < Image_height(img); ++row)
{
for(int col = 0; col < Image_width(img); ++col)
{
os << *Matrix_at(&(*img).red_channel, row, col) << " ";
os << *Matrix_at(&(*img).green_channel, row, col) << " ";
os << *Matrix_at(&(*img).blue_channel, row, col) << " ";
}
os << "\n";
}
}
// REQUIRES: img points to a valid Image
// EFFECTS: Returns the width of the Image.
int Image_width(const Image* img) {
//assert(false); // TODO Replace with your implementation!
return (*img).width;
}
// REQUIRES: img points to a valid Image
// EFFECTS: Returns the height of the Image.
int Image_height(const Image* img) {
//assert(false); // TODO Replace with your implementation!
return (*img).height;
}
// REQUIRES: img points to a valid Image
// 0 <= row && row < Image_height(img)
// 0 <= column && column < Image_width(img)
// EFFECTS: Returns the pixel in the Image at the given row and column.
Pixel Image_get_pixel(const Image* img, int row, int column) {
//assert(false); // TODO Replace with your implementation!
Pixel color;
color.r = *Matrix_at(&(*img).red_channel, row, column);
color.g = *Matrix_at(&(*img).green_channel, row, column);
color.b = *Matrix_at(&(*img).blue_channel, row, column);
return color;
// int index = (row * (*img).width) + column;
// int *prt = (*img).red_channel;
// int *address = (prt + index);
// return *address;
}
// REQUIRES: img points to a valid Image
// 0 <= row && row < Image_height(img)
// 0 <= column && column < Image_width(img)
// MODIFIES: *img
// EFFECTS: Sets th e pixel in the Image at the given row and column
// to the given color.
void Image_set_pixel(Image* img, int row, int column, Pixel color) {
//assert(false); // TODO Replace with your implementation!
int *Red = Matrix_at(&(*img).red_channel, row, column);
int *Green = Matrix_at(&(*img).green_channel, row, column);
int *Blue = Matrix_at(&(*img).blue_channel, row, column);
*Red = color.r;
*Green = color.g;
*Blue = color.b;
}
// REQUIRES: img points to a valid Image
// MODIFIES: *img
// EFFECTS: Sets each pixel in the image to the given color.
void Image_fill(Image* img, Pixel color) {
//assert(false); // TODO Replace with your implementation!
Matrix_fill(&(*img).red_channel, color.r);
Matrix_fill(&(*img).green_channel, color.g);
Matrix_fill(&(*img).blue_channel, color.b);
}