-
Notifications
You must be signed in to change notification settings - Fork 0
/
Image.cpp
127 lines (116 loc) · 4.59 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
#include <cassert>
#include <sstream>
#include "Image.hpp"
using std::cout, std::endl, std::string;
// REQUIRES: img points to an Image
// 0 < width && width <= MAX_MATRIX_WIDTH
// 0 < height && 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(0 < width && width <= MAX_MATRIX_WIDTH);
assert(0 < height && height <= MAX_MATRIX_HEIGHT);
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) {
int height;
int width;
string p3;
int junk;
is >> p3 >> width >> height >> junk;
img->height = height;
img->width = width;
Matrix_init(&img->red_channel, Image_width(img), Image_height(img));
Matrix_init(&img->green_channel, Image_width(img), Image_height(img));
Matrix_init(&img->blue_channel, Image_width(img), Image_height(img));
for (int r = 0; r < height; ++r){
for (int c = 0; c < width; ++c){
is >> *Matrix_at(&img->red_channel, r, c);
is >> *Matrix_at(&img->green_channel, r, c);
is >> *Matrix_at(&img->blue_channel, r, c);
}
}
}
// 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) {
os << "P3\n" << Image_width(img) << " " << Image_height(img) << "\n255\n";
for (int r = 0; r < Image_height(img); ++r){
for (int c = 0; c < Image_width(img); ++c){
os << *Matrix_at(&img->red_channel, r, c) << " ";
os << *Matrix_at(&img->green_channel, r, c) << " ";
os << *Matrix_at(&img->blue_channel, r, c) << " ";
}
os << "\n";
}
}
// REQUIRES: img points to a valid Image
// EFFECTS: Returns the width of the Image.
int Image_width(const Image* img) {
return img->width;
}
// REQUIRES: img points to a valid Image
// EFFECTS: Returns the height of the Image.
int Image_height(const Image* img) {
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(0 <= row && row < Image_height(img));
assert(0 <= column && column < Image_width(img));
Pixel p;
p.r = *Matrix_at(&img->red_channel, row, column);
p.g = *Matrix_at(&img->green_channel, row, column);
p.b = *Matrix_at(&img->blue_channel, row, column);
return p;
}
// REQUIRES: img points to a valid Image
// 0 <= row && row < Image_height(img)
// 0 <= column && column < Image_width(img)
// MODIFIES: *img
// EFFECTS: Sets the 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(0 <= row && row < Image_height(img));
assert(0 <= column && column < Image_width(img));
*Matrix_at(&img->red_channel, row, column) = color.r;
*Matrix_at(&img->green_channel, row, column) = color.g;
*Matrix_at(&img->blue_channel, row, column) = 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) {
for (int r = 0; r < Image_height(img); ++r){
for (int c = 0; c < Image_width(img); ++c){
Image_set_pixel(img, r, c, color);
}
}
}