-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.cpp
More file actions
76 lines (65 loc) · 1.8 KB
/
Image.cpp
File metadata and controls
76 lines (65 loc) · 1.8 KB
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
#include "Image.h"
#include <algorithm>
#include <climits>
// This class has mostly consist of helper functions, you could use them when dealing image pixels.
/*
* BEWARE!
* DO NOT MODIFY THIS FILE
* IT WILL BE REPLACED DURING CHECKS
*/
Image::Image(int width, int height, const Color& color)
{
_width = width;
_height = height;
_pixels = new Color[(int)width*(int)height];
_buffer = new float[(int)width*(int)height];
for(int i=0; i<height; i++)
{
for (int j=0; j<width; j++)
{
_buffer[i*width+j] = 9999999.0f;
_pixels[i*width+j] = color;
}
}
}
Image::Image(const Image& rhs) : _width(rhs._width), _height(rhs._height)
{
_pixels = new Color[_width * _height];
std::copy(rhs._pixels, rhs._pixels + _width * _height, _pixels);
}
Image::Image(Image&& rhs) : _width(rhs._width), _height(rhs._height), _pixels(rhs._pixels)
{
rhs._pixels = nullptr;
}
Image::~Image()
{
delete[] _pixels;
}
Color &Image::Pixel(int x, int y)
{
return _pixels[x * _width + y];
}
const Color &Image::Pixel(int x, int y) const
{
return _pixels[x * _width + y];
}
Image& Image::operator=(Image rhs)
{
using std::swap;
swap(_width, rhs._width);
swap(_height, rhs._height);
swap(_pixels, rhs._pixels);
return *this;
}
std::ostream &operator<<(std::ostream &os, const Image &image) {
os << "P3" << std::endl << image._width << " " << image._height << std::endl << 255 << std::endl ;
for (int i = 0; i < image._height; ++i) {
for (int j = 0; j < image._width; ++j) {
os << static_cast<int>(image.Pixel(i, j).R()) << " ";
os << static_cast<int>(image.Pixel(i, j).G()) << " ";
os << static_cast<int>(image.Pixel(i, j).B()) << " ";
}
os << std::endl;
}
return os;
}