-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathImage.cpp
More file actions
65 lines (53 loc) · 1.68 KB
/
Image.cpp
File metadata and controls
65 lines (53 loc) · 1.68 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
#include "Image.h"
Image::Image(): width(0), height(0){}
Image::Image(int width, int height, vector <Pixel> pixels): width(width), height(height), pixels(pixels){}
Image::Image(int width, int height)
{
throw std::exception();//todo: sdelat
}
Image::Image(const Image& img): width(img.width), height(img.height), pixels(img.pixels){}
Image::Image(string filename)
{
const char* filein = filename.c_str();
vector <uchar> image;
unsigned width, height, w=0, h=0;
lodepng::decode(image, width, height, filein);
for(unsigned long long i=0; i<width*height*4; i+=4, w++)
{
Color color(image[i], image[i+1], image[i+2], image[i+3]);
Point point(w/width, w%width+1);
Pixel pixel(color, point);
pixels.push_back(pixel);
}
}
void Image::setPixel(Pixel pixel, unsigned x){
pixels[x]=pixel;
}
void Image::setPixel(Color c, unsigned x, unsigned y){
pixels[y*width+x].setColor(c);
}
void Image::setPixel(Pixel pixel){
pixels[pixel.getY()*width+pixel.getX()]=pixel;
}
Pixel Image::getPixel(unsigned x){
return pixels[x];
}
Pixel Image::getPixel(unsigned x, unsigned y){
return pixels[y*width+x];
}
vector<Pixel> Image::getPixels(){
return pixels;
}
void Image::outputImage(string filename)
{
vector <uchar> image;
unsigned width = pixels[pixels.size()-1].getY(), height=pixels[pixels.size()-1].getX();
for(unsigned long long i=0; i<pixels.size(); i++)
{
image.push_back(pixels[i].getColor().R);
image.push_back(pixels[i].getColor().G);
image.push_back(pixels[i].getColor().B);
image.push_back(pixels[i].getColor().A);
}
lodepng::encode(filename.c_str(), image, width, height);
}